forked from FriendsOfDoctrine/dbal-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArraysTest.php
149 lines (127 loc) · 6.08 KB
/
ArraysTest.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
<?php
/*
* This file is part of the FODDBALClickHouse package -- Doctrine DBAL library
* for ClickHouse (a column-oriented DBMS for OLAP <https://clickhouse.yandex/>)
*
* (c) FriendsOfDoctrine <https://github.com/FriendsOfDoctrine/>.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOD\DBALClickHouse\Tests;
use FOD\DBALClickHouse\Connection;
use FOD\DBALClickHouse\Types\ArrayType;
use PHPUnit\Framework\TestCase;
/**
* ClickHouse DBAL test class. Testing work with array (insert, select)
*
* @author Nikolay Mitrofanov <[email protected]>
*/
class ArraysTest extends TestCase
{
/** @var Connection */
protected $connection;
public function setUp() : void
{
$this->connection = CreateConnectionTest::createConnection();
ArrayType::registerArrayTypes($this->connection->getDatabasePlatform());
}
public function tearDown() : void
{
$this->connection->exec('DROP TABLE test_array_table');
}
public function testArrayInt8()
{
$this->createTempTable('array(int8)');
$this->connection->insert('test_array_table', ['arr' => [1, 2, 3, 4, 5, 6, 7, 8]]);
$this->assertEquals(['arr' => [1, 2, 3, 4, 5, 6, 7, 8]], current($this->connection->fetchAll('SELECT arr FROM test_array_table')));
}
public function testArrayInt16()
{
$this->createTempTable('array(int16)');
$this->connection->insert('test_array_table', ['arr' => [100, 2000, 30000]]);
$this->assertEquals(['arr' => [100, 2000, 30000]], current($this->connection->fetchAll('SELECT arr FROM test_array_table')));
}
public function testArrayInt32()
{
$this->createTempTable('array(int32)');
$this->connection->insert('test_array_table', ['arr' => [1000000, 2000000000]]);
$this->assertEquals(['arr' => [1000000, 2000000000]], current($this->connection->fetchAll('SELECT arr FROM test_array_table')));
}
public function testArrayInt64()
{
$this->createTempTable('array(int64)');
$this->connection->insert('test_array_table', ['arr' => [200000000000, 3000000000000000000]]);
$this->assertEquals(['arr' => [200000000000, 3000000000000000000]], current($this->connection->fetchAll('SELECT arr FROM test_array_table')));
}
public function testArrayUInt8()
{
$this->createTempTable('array(uint8)');
$this->connection->insert('test_array_table', ['arr' => [1, 2, 3, 4, 5, 6, 7, 8]]);
$this->assertEquals(['arr' => [1, 2, 3, 4, 5, 6, 7, 8]], current($this->connection->fetchAll('SELECT arr FROM test_array_table')));
}
public function testArrayUInt16()
{
$this->createTempTable('array(uint16)');
$this->connection->insert('test_array_table', ['arr' => [100, 2000, 30000]]);
$this->assertEquals(['arr' => [100, 2000, 30000]], current($this->connection->fetchAll('SELECT arr FROM test_array_table')));
}
public function testArrayUInt32()
{
$this->createTempTable('array(uint32)');
$this->connection->insert('test_array_table', ['arr' => [1000000, 2000000000]]);
$this->assertEquals(['arr' => [1000000, 2000000000]], current($this->connection->fetchAll('SELECT arr FROM test_array_table')));
}
public function testArrayUInt64()
{
$this->createTempTable('array(uint64)');
$this->connection->insert('test_array_table', ['arr' => [200000000000, 3000000000000000000]]);
$this->assertEquals(['arr' => [200000000000, 3000000000000000000]], current($this->connection->fetchAll('SELECT arr FROM test_array_table')));
}
public function testArrayFloat32()
{
$this->createTempTable('array(float32)');
$this->connection->insert('test_array_table', ['arr' => [1.5, 10.5]]);
$this->assertEquals(['arr' => [1.5, 10.5]], current($this->connection->fetchAll('SELECT arr FROM test_array_table')));
}
public function testArrayFloat64()
{
$this->createTempTable('array(float64)');
$this->connection->insert('test_array_table', ['arr' => [100.512, 10000.5814]]);
$this->assertEquals(['arr' => [100.512, 10000.5814]], current($this->connection->fetchAll('SELECT arr FROM test_array_table')));
}
public function testArrayString()
{
$this->createTempTable('array(string)');
$this->connection->insert('test_array_table', ['arr' => ['foo', 'bar']]);
$this->assertEquals(['arr' => ['foo', 'bar']], current($this->connection->fetchAll('SELECT arr FROM test_array_table')));
}
public function testArrayDatetime()
{
$dateTimeArray = [(new \DateTime('2000-01-01'))->format('Y-m-d H:i:s'), (new \DateTime('2017-05-05'))->format('Y-m-d H:i:s')];
$this->createTempTable('array(datetime)');
$this->connection->insert('test_array_table', ['arr' => $dateTimeArray]);
$this->assertEquals(['arr' => $dateTimeArray], current($this->connection->fetchAll('SELECT arr FROM test_array_table')));
}
public function testArrayDate()
{
$datesArray = [(new \DateTime('2000-01-01'))->format('Y-m-d'), (new \DateTime('2017-05-05'))->format('Y-m-d')];
$this->createTempTable('array(date)');
$this->connection->insert('test_array_table', ['arr' => $datesArray]);
$this->assertEquals(['arr' => $datesArray], current($this->connection->fetchAll('SELECT arr FROM test_array_table')));
}
protected function createTempTable($arrayType)
{
$fromSchema = $this->connection->getSchemaManager()->createSchema();
$toSchema = clone $fromSchema;
if ($toSchema->hasTable('test_array_table') || $fromSchema->hasTable('test_array_table')) {
$this->connection->exec('DROP TABLE test_array_table');
}
$newTable = $toSchema->createTable('test_array_table');
$newTable->addColumn('arr', $arrayType);
$newTable->addOption('engine', 'Memory');
foreach ($fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform()) as $sql) {
$this->connection->exec($sql);
}
}
}