33namespace YdbPlatform \Ydb \Test ;
44
55use PHPUnit \Framework \TestCase ;
6+ use Ydb \Table \OnlineModeSettings ;
7+ use Ydb \Table \SerializableModeSettings ;
8+ use Ydb \Table \SnapshotModeSettings ;
9+ use Ydb \Table \StaleModeSettings ;
610use YdbPlatform \Ydb \Auth \Implement \AnonymousAuthentication ;
711use YdbPlatform \Ydb \Logger \SimpleStdLogger ;
812use YdbPlatform \Ydb \Ydb ;
13+ use function YdbPlatform \Ydb \parseTxMode ;
14+
15+ require_once __DIR__ .'/../src/Session.php ' ;
916
1017class CheckTxSettingsTest extends TestCase
1118{
1219
13- /**
14- * @var Ydb
15- */
16- protected $ ydb ;
17- /**
18- * @var \YdbPlatform\Ydb\Table
19- */
20- protected $ table ;
21- /**
22- * @var \YdbPlatform\Ydb\Session|null
23- */
24- protected $ session ;
25-
26- public function __construct (?string $ name = null , array $ data = [], $ dataName = '' )
20+ public function testParseTxMode (?string $ name = null , array $ data = [], $ dataName = '' )
2721 {
2822 parent ::__construct ($ name , $ data , $ dataName );
2923 $ config = [
@@ -43,30 +37,41 @@ public function __construct(?string $name = null, array $data = [], $dataName =
4337 ],
4438 'credentials ' => new AnonymousAuthentication ()
4539 ];
46- $ this ->ydb = new Ydb ($ config , new SimpleStdLogger (SimpleStdLogger::DEBUG ));
47- $ this ->table = $ this ->ydb ->table ();
48- $ this ->session = $ this ->table ->session ();
49- }
40+ $ ydb = new Ydb ($ config , new SimpleStdLogger (SimpleStdLogger::DEBUG ));
41+ $ table = $ ydb ->table ();
42+ $ session = $ table ->createSession ();
5043
51- public function testSerializableTxConfig (){
52- $ this ->checkTx ('serializable ' , 'serializable_read_write ' );
53- }
54-
55- public function testSnapshotTxConfig (){
56- $ this ->checkTx ('snapshot ' , 'snapshot_read_only ' );
57- }
58- public function testStaleTxConfig (){
59- $ this ->checkTx ('stale ' , 'stale_read_only ' );
60- }
61- public function testOnlineTxConfig (){
62- $ this ->checkTx ('online ' , 'online_read_only ' );
63- }
64-
65- protected function checkTx (string $ mode , string $ value )
66- {
67- $ query = $ this ->session ->newQuery ("SELECT 1; " )
68- ->beginTx ($ mode );
69- self ::assertEquals ($ value , $ query ->getRequestData ()['tx_control ' ]->getBeginTx ()->getTxMode ());
70- $ query ->execute ();
44+ $ testsQuery = [
45+ ['mode ' => 'stale_read_only ' , 'result ' => ['stale_read_only ' => new StaleModeSettings ], 'interactive ' => false ],
46+ ['mode ' => 'stale ' , 'result ' => ['stale_read_only ' => new StaleModeSettings ], 'interactive ' => false ],
47+ ['mode ' => 'online_read_only ' , 'result ' => ['online_read_only ' => new OnlineModeSettings ([
48+ 'allow_inconsistent_reads ' => false ,
49+ ])], 'interactive ' => false ],
50+ ['mode ' => 'online ' , 'result ' => ['online_read_only ' => new OnlineModeSettings ([
51+ 'allow_inconsistent_reads ' => false ,
52+ ])], 'interactive ' => false ],
53+ ['mode ' => 'inconsistent_reads ' , 'result ' => ['online_read_only ' => new OnlineModeSettings ([
54+ 'allow_inconsistent_reads ' => true ,
55+ ])], 'interactive ' => false ],
56+ ['mode ' => 'online_inconsistent ' , 'result ' => ['online_read_only ' => new OnlineModeSettings ([
57+ 'allow_inconsistent_reads ' => true ,
58+ ])], 'interactive ' => false ],
59+ ['mode ' => 'online_inconsistent_reads ' , 'result ' => ['online_read_only ' => new OnlineModeSettings ([
60+ 'allow_inconsistent_reads ' => true ,
61+ ])], 'interactive ' => false ],
62+ ['mode ' => 'snapshot ' , 'result ' => ['snapshot_read_only ' => new SnapshotModeSettings ], 'interactive ' => true ],
63+ ['mode ' => 'snapshot_read_only ' , 'result ' => ['snapshot_read_only ' => new SnapshotModeSettings ], 'interactive ' => true ],
64+ ['mode ' => 'serializable ' , 'result ' => ['serializable_read_write ' => new SerializableModeSettings ], 'interactive ' => true ],
65+ ['mode ' => 'serializable_read_write ' , 'result ' => ['serializable_read_write ' => new SerializableModeSettings ], 'interactive ' => true ],
66+ ];
67+ foreach ($ testsQuery as $ i => $ test ){
68+ self ::assertEquals ($ test ["result " ], parseTxMode ($ test ["mode " ]));
69+ $ query = $ session ->newQuery ("SELECT 1; " )
70+ ->beginTx ($ test ['mode ' ]);
71+ $ query ->execute ();
72+ if ($ test ['interactive ' ]){
73+ $ table ->transaction (function (){}, $ test ['mode ' ]);
74+ }
75+ }
7176 }
7277}
0 commit comments