Skip to content

Commit a078477

Browse files
author
Илья
committed
Add test
1 parent ddddf7a commit a078477

File tree

1 file changed

+222
-0
lines changed

1 file changed

+222
-0
lines changed

tests/KeepInCacheTest.php

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
7+
use YdbPlatform\Ydb\Logger\SimpleStdLogger;
8+
use YdbPlatform\Ydb\Session;
9+
use YdbPlatform\Ydb\Table;
10+
use YdbPlatform\Ydb\Types\Int64Type;
11+
use YdbPlatform\Ydb\Ydb;
12+
use YdbPlatform\Ydb\YdbQuery;
13+
14+
class KeepInCacheTest extends TestCase
15+
{
16+
/**
17+
* @var Ydb
18+
*/
19+
protected $ydb;
20+
/**
21+
* @var Table
22+
*/
23+
protected $table;
24+
protected $yqlPrepare = '
25+
declare $pk as Int64;
26+
select $pk;';
27+
protected $yql = 'select 1;';
28+
29+
public function __construct(?string $name = null, array $data = [], $dataName = '')
30+
{
31+
parent::__construct($name, $data, $dataName);
32+
$config = [
33+
34+
// Database path
35+
'database' => '/local',
36+
37+
// Database endpoint
38+
'endpoint' => 'localhost:2136',
39+
40+
// Auto discovery (dedicated server only)
41+
'discovery' => false,
42+
43+
// IAM config
44+
'iam_config' => [
45+
'insecure' => true,
46+
],
47+
'credentials' => new AnonymousAuthentication()
48+
];
49+
$this->ydb = new Ydb($config, new SimpleStdLogger(SimpleStdLogger::DEBUG));
50+
$this->table = $this->ydb->table();
51+
$this->sessionId = $this->table->createSession()->id();
52+
}
53+
54+
public function testPreparedQueryWithParamsWithoutConfig()
55+
{
56+
$session = new EditablePreparedSession($this->table, "");
57+
SessionWithEditable::setId($session, $this->sessionId);
58+
SessionWithEditable::setIdle($session);
59+
TableWithEditableSessions::addSession($this->table, $session);
60+
$prepared_query = $this->table->session()->prepare($this->yqlPrepare);
61+
$x = 2;
62+
$result = $prepared_query->execute([
63+
'pk' => $x,
64+
]);
65+
self::assertEquals(true, $result);
66+
TableWithEditableSessions::removeSession($this->table, $session);
67+
}
68+
69+
public function testPreparedQueryWithParamsWithConfig()
70+
{
71+
$session = new EditablePreparedSession($this->table, "");
72+
SessionWithEditable::setId($session, $this->sessionId);
73+
SessionWithEditable::setIdle($session);
74+
TableWithEditableSessions::addSession($this->table, $session);
75+
$session->keepInCache(false);
76+
$prepared_query = $this->table->session()->prepare($this->yqlPrepare);
77+
$x = 2;
78+
$result = $prepared_query->execute([
79+
'pk' => $x,
80+
]);
81+
self::assertEquals(false, $result);
82+
TableWithEditableSessions::removeSession($this->table, $session);
83+
}
84+
85+
public function testPreparedQueryWithoutParamsWithoutConfig()
86+
{
87+
$session = new EditablePreparedSession($this->table, "");
88+
SessionWithEditable::setId($session, $this->sessionId);
89+
SessionWithEditable::setIdle($session);
90+
TableWithEditableSessions::addSession($this->table, $session);
91+
$prepared_query = $this->table->session()->prepare($this->yql);
92+
$result = $prepared_query->execute();
93+
self::assertEquals(false, $result);
94+
TableWithEditableSessions::removeSession($this->table, $session);
95+
}
96+
97+
public function testPreparedQueryWithoutParamsWithConfig()
98+
{
99+
$session = new EditablePreparedSession($this->table, "");
100+
SessionWithEditable::setId($session, $this->sessionId);
101+
SessionWithEditable::setIdle($session);
102+
TableWithEditableSessions::addSession($this->table, $session);
103+
$session->keepInCache(true);
104+
$prepared_query = $this->table->session()->prepare($this->yql);
105+
$result = $prepared_query->execute();
106+
self::assertEquals(true, $result);
107+
TableWithEditableSessions::removeSession($this->table, $session);
108+
}
109+
110+
public function testNewQueryWithParamsWithoutConfig()
111+
{
112+
$session = new EditablePreparedSession($this->table, "");
113+
SessionWithEditable::setId($session, $this->sessionId);
114+
SessionWithEditable::setIdle($session);
115+
TableWithEditableSessions::addSession($this->table, $session);
116+
$query = $session->newQuery($this->yqlPrepare);
117+
$query->parameters([
118+
'$pk' => (new Int64Type(2))->toTypedValue(),
119+
]);
120+
$query->beginTx('stale');
121+
$result = $query->execute();
122+
self::assertEquals(true, $result);
123+
TableWithEditableSessions::removeSession($this->table, $session);
124+
}
125+
126+
public function testNewQueryWithoutParamsWithoutConfig()
127+
{
128+
$session = new EditablePreparedSession($this->table, "");
129+
SessionWithEditable::setId($session, $this->sessionId);
130+
SessionWithEditable::setIdle($session);
131+
TableWithEditableSessions::addSession($this->table, $session);
132+
$query = $session->newQuery($this->yql);
133+
$query->parameters();
134+
$query->beginTx('stale');
135+
$result = $query->execute();
136+
self::assertEquals(false, $result);
137+
TableWithEditableSessions::removeSession($this->table, $session);
138+
}
139+
140+
public function testNewQueryWithParamsWithConfig()
141+
{
142+
$session = new EditablePreparedSession($this->table, "");
143+
SessionWithEditable::setId($session, $this->sessionId);
144+
SessionWithEditable::setIdle($session);
145+
TableWithEditableSessions::addSession($this->table, $session);
146+
$query = $session->newQuery($this->yqlPrepare);
147+
$query->parameters([
148+
'$pk' => (new Int64Type(2))->toTypedValue(),
149+
]);
150+
$query->beginTx('stale');
151+
$query->keepInCache(false);
152+
$result = $query->execute();
153+
self::assertEquals(false, $result);
154+
TableWithEditableSessions::removeSession($this->table, $session);
155+
}
156+
157+
public function testNewQueryWithoutParamsWithConfig()
158+
{
159+
$session = new EditablePreparedSession($this->table, "");
160+
SessionWithEditable::setId($session, $this->sessionId);
161+
SessionWithEditable::setIdle($session);
162+
TableWithEditableSessions::addSession($this->table, $session);
163+
$query = $session->newQuery($this->yql);
164+
$query->parameters();
165+
$query->beginTx('stale');
166+
$query->keepInCache(true);
167+
$result = $query->execute();
168+
self::assertEquals(true, $result);
169+
TableWithEditableSessions::removeSession($this->table, $session);
170+
}
171+
}
172+
173+
class TableWithEditableSessions extends Table
174+
{
175+
public static function addSession(Table $table, Session $session)
176+
{
177+
$table::$session_pool->addSession($session);
178+
}
179+
public static function removeSession(Table $table, Session $session)
180+
{
181+
$table::$session_pool->dropSession($session->id());
182+
}
183+
}
184+
185+
class SessionWithEditable extends Session
186+
{
187+
public static function setId(Session $session, string $id)
188+
{
189+
$session->session_id = $id;
190+
}
191+
192+
public static function setIdle(Session $session)
193+
{
194+
$session->is_busy = false;
195+
}
196+
197+
public function executeQuery(YdbQuery $query)
198+
{
199+
return $query->getRequestData()['query_cache_policy']->getKeepInCache();
200+
}
201+
202+
}
203+
204+
class EditablePreparedSession extends Session
205+
{
206+
public static function setId(Session $session, string $id)
207+
{
208+
$session->session_id = $id;
209+
}
210+
211+
public static function setIdle(Session $session)
212+
{
213+
$session->is_busy = false;
214+
}
215+
216+
217+
public function executeQuery(YdbQuery $query)
218+
{
219+
return $query->getRequestData()['query_cache_policy']->getKeepInCache();
220+
}
221+
222+
}

0 commit comments

Comments
 (0)