-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SelectTest.php
173 lines (148 loc) · 4.35 KB
/
SelectTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
namespace SimPod\ClickHouseClient\Tests\Client;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use SimPod\ClickHouseClient\Client\Http\RequestFactory;
use SimPod\ClickHouseClient\Client\PsrClickHouseClient;
use SimPod\ClickHouseClient\Exception\ServerError;
use SimPod\ClickHouseClient\Format\Json;
use SimPod\ClickHouseClient\Format\JsonCompact;
use SimPod\ClickHouseClient\Format\JsonEachRow;
use SimPod\ClickHouseClient\Format\Null_;
use SimPod\ClickHouseClient\Format\TabSeparated;
use SimPod\ClickHouseClient\Tests\TestCaseBase;
use SimPod\ClickHouseClient\Tests\WithClient;
#[CoversClass(RequestFactory::class)]
#[CoversClass(PsrClickHouseClient::class)]
#[CoversClass(ServerError::class)]
#[CoversClass(Json::class)]
#[CoversClass(\SimPod\ClickHouseClient\Output\Json::class)]
#[CoversClass(JsonEachRow::class)]
#[CoversClass(\SimPod\ClickHouseClient\Output\JsonEachRow::class)]
#[CoversClass(JsonCompact::class)]
#[CoversClass(\SimPod\ClickHouseClient\Output\JsonCompact::class)]
#[CoversClass(Null_::class)]
#[CoversClass(\SimPod\ClickHouseClient\Output\Null_::class)]
final class SelectTest extends TestCaseBase
{
use WithClient;
public function testSelectWithParams(): void
{
$client = self::$client;
$output = $client->selectWithParams('SELECT {p1:UInt8} AS data', ['p1' => 3], new TabSeparated());
self::assertSame("3\n", $output->contents);
}
#[DataProvider('providerJson')]
public function testJson(mixed $expectedData, string $sql): void
{
$client = self::$client;
$output = $client->select($sql, new Json());
self::assertSame($expectedData, $output->data);
}
/** @return iterable<int, array{mixed, string}> */
public static function providerJson(): iterable
{
yield [
[[1 => 1]],
<<<'CLICKHOUSE'
SELECT 1
CLICKHOUSE,
];
yield [
[
['number' => '0'],
['number' => '1'],
],
<<<'CLICKHOUSE'
SELECT number FROM system.numbers LIMIT 2
CLICKHOUSE,
];
yield [
[
["'ping'" => 'ping'],
],
<<<'CLICKHOUSE'
SELECT 'ping'
CLICKHOUSE,
];
}
#[DataProvider('providerJsonCompact')]
public function testJsonCompact(mixed $expectedData, string $sql): void
{
$client = self::$client;
$output = $client->select($sql, new JsonCompact());
self::assertSame($expectedData, $output->data);
}
/** @return iterable<int, array{mixed, string}> */
public static function providerJsonCompact(): iterable
{
yield [
[[1]],
<<<'CLICKHOUSE'
SELECT 1
CLICKHOUSE,
];
yield [
[
['0'],
['1'],
],
<<<'CLICKHOUSE'
SELECT number FROM system.numbers LIMIT 2
CLICKHOUSE,
];
yield [
[
['ping'],
],
<<<'CLICKHOUSE'
SELECT 'ping'
CLICKHOUSE,
];
}
#[DataProvider('providerJsonEachRow')]
public function testJsonEachRow(mixed $expectedData, string $sql): void
{
$output = self::$client->select($sql, new JsonEachRow());
self::assertSame($expectedData, $output->data);
}
/** @return iterable<int, array{mixed, string}> */
public static function providerJsonEachRow(): iterable
{
yield [
[[1 => 1]],
<<<'CLICKHOUSE'
SELECT 1
CLICKHOUSE,
];
yield [
[
['number' => '0'],
['number' => '1'],
],
<<<'CLICKHOUSE'
SELECT number FROM system.numbers LIMIT 2
CLICKHOUSE,
];
yield [
[
["'ping'" => 'ping'],
],
<<<'CLICKHOUSE'
SELECT 'ping'
CLICKHOUSE,
];
}
public function testNull(): void
{
self::$client->select('SELECT 1', new Null_());
self::assertTrue(true);
}
public function testSettingsArePassed(): void
{
self::expectException(ServerError::class);
$this->expectExceptionMessageMatches("~DB::Exception: Database `non-existent` (doesn't|does not) exist~");
self::$client->select('SELECT 1', new JsonCompact(), ['database' => 'non-existent']);
}
}