forked from apache/ignite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlQueryTest.php
More file actions
204 lines (183 loc) · 7.04 KB
/
Copy pathSqlQueryTest.php
File metadata and controls
204 lines (183 loc) · 7.04 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Apache\Ignite\Tests;
use Ds\Set;
use PHPUnit\Framework\TestCase;
use Apache\Ignite\Cache\CacheEntry;
use Apache\Ignite\Cache\QueryEntity;
use Apache\Ignite\Cache\QueryField;
use Apache\Ignite\Cache\CacheConfiguration;
use Apache\Ignite\Query\SqlQuery;
use Apache\Ignite\Query\SqlFieldsQuery;
use Apache\Ignite\Type\ObjectType;
use Apache\Ignite\Type\ComplexObjectType;
class SqlQueryTestClass
{
public $field1;
public $field2;
}
final class SqlQueryTestCase extends TestCase
{
const CACHE_NAME = '__php_test_sql_query';
const ELEMENTS_NUMBER = 10;
const TABLE_NAME = '__php_test_SqlQuery_table';
private static $cache;
private static $selectFromTable;
public static function setUpBeforeClass(): void
{
TestingHelper::init();
self::cleanUp();
self::$cache = (TestingHelper::$client->getOrCreateCache(
self::CACHE_NAME,
(new CacheConfiguration())->
setQueryEntities((new QueryEntity())->
setKeyTypeName('java.lang.Integer')->
setValueTypeName(self::TABLE_NAME)->
setFields(
new QueryField('field1', 'java.lang.Integer'),
new QueryField('field2', 'java.lang.String')))))->
setKeyType(ObjectType::INTEGER)->
setValueType((new ComplexObjectType())->
setIgniteTypeName(self::TABLE_NAME)->
setPhpClassName(SqlQueryTestClass::class)->
setFieldType('field1', ObjectType::INTEGER));
self::generateData();
$tableName = self::TABLE_NAME;
self::$selectFromTable = "SELECT * FROM {$tableName}";
}
public static function tearDownAfterClass(): void
{
self::cleanUp();
TestingHelper::cleanUp();
}
public function testGetAll(): void
{
$cache = self::$cache;
$cursor = $cache->query((new SqlQuery(self::TABLE_NAME, self::$selectFromTable)));
$set = new Set();
foreach ($cursor->getAll() as $cacheEntry) {
$this->checkCursorResult($cacheEntry);
$set->add($cacheEntry->getKey());
}
$this->assertEquals($set->count(), self::ELEMENTS_NUMBER);
}
public function testGetAllWithPageSize(): void
{
$cache = self::$cache;
$cursor = $cache->query((new SqlQuery(self::TABLE_NAME, self::$selectFromTable))->setPageSize(1));
$set = new Set();
foreach ($cursor->getAll() as $cacheEntry) {
$this->checkCursorResult($cacheEntry);
$set->add($cacheEntry->getKey());
}
$this->assertEquals($set->count(), self::ELEMENTS_NUMBER);
}
public function testIterateCursor(): void
{
$cache = self::$cache;
$cursor = $cache->query(new SqlQuery(self::TABLE_NAME, self::$selectFromTable));
$set = new Set();
foreach ($cursor as $cacheEntry) {
$this->checkCursorResult($cacheEntry);
$set->add($cacheEntry->getKey());
}
$this->assertEquals($set->count(), self::ELEMENTS_NUMBER);
}
public function testIterateCursorWithPageSize(): void
{
$cache = self::$cache;
$cursor = $cache->query((new SqlQuery(self::TABLE_NAME, self::$selectFromTable))->setPageSize(2));
$set = new Set();
foreach ($cursor as $cacheEntry) {
$this->checkCursorResult($cacheEntry);
$set->add($cacheEntry->getKey());
}
$this->assertEquals($set->count(), self::ELEMENTS_NUMBER);
}
public function testCloseCursor(): void
{
$cache = self::$cache;
$cursor = $cache->query((new SqlQuery(self::TABLE_NAME, self::$selectFromTable))->setPageSize(1));
$cursor->rewind();
$this->assertTrue($cursor->valid());
$this->checkCursorResult($cursor->current());
$cursor->next();
$cursor->close();
}
public function testCloseCursorAfterGetAll(): void
{
$cache = self::$cache;
$cursor = $cache->query(new SqlQuery(self::TABLE_NAME, self::$selectFromTable));
$cursor->getAll();
$cursor->close();
$this->assertTrue(true);
}
public function testSqlQuerySettings(): void
{
$cache = self::$cache;
$cursor = $cache->query((new SqlQuery(self::TABLE_NAME, self::$selectFromTable))->
setType(self::TABLE_NAME)->
setPageSize(2)->
setLocal(false)->
setSql('field1 > ? and field1 <= ?')->
setArgTypes(ObjectType::INTEGER, ObjectType::INTEGER)->
setArgs(3, 7)->
setDistributedJoins(true)->
setReplicatedOnly(false)->
setTimeout(10000));
$cursor->getAll();
$this->assertTrue(true);
}
public function testGetEmptyResults(): void
{
$tableName = self::TABLE_NAME;
$cache = self::$cache;
$cursor = $cache->query((new SqlQuery(self::TABLE_NAME, 'field1 > ?'))->setArgs(self::ELEMENTS_NUMBER));
$entries = $cursor->getAll();
$this->assertEquals(count($entries), 0);
$cursor->close();
$cursor = $cache->query((new SqlQuery(self::TABLE_NAME, 'field1 > ?'))->setArgs(self::ELEMENTS_NUMBER));
foreach ($cursor as $entry) {
$this->assertTrue(false);
}
$cursor->close();
}
private function checkCursorResult(CacheEntry $cacheEntry): void
{
$this->assertEquals($cacheEntry->getValue()->field2, self::generateValue($cacheEntry->getKey()));
$this->assertTrue($cacheEntry->getKey() >= 0 && $cacheEntry->getKey() < self::ELEMENTS_NUMBER);
}
private static function generateData(): void
{
$tableName = self::TABLE_NAME;
$cache = self::$cache;
$insertQuery = (new SqlFieldsQuery("INSERT INTO {$tableName} (_key, field1, field2) VALUES (?, ?, ?)"))->
setArgTypes(ObjectType::INTEGER, ObjectType::INTEGER);
for ($i = 0; $i < self::ELEMENTS_NUMBER; $i++) {
$cache->query($insertQuery->setArgs($i, $i, self::generateValue($i)))->getAll();
}
}
private static function generateValue(int $key): string
{
return 'value' . $key;
}
private static function cleanUp(): void
{
TestingHelper::destroyCache(self::CACHE_NAME);
}
}