Skip to content
Navigation Menu
{{ message }}
forked from GoogleCloudPlatform/php-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspannerTest.php
More file actions
597 lines (534 loc) · 20.5 KB
/
Copy pathspannerTest.php
File metadata and controls
597 lines (534 loc) · 20.5 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
<?php
/**
* Copyright 2016 Google LLC
*
* Licensed 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 Google\Cloud\Samples\Spanner;
use Google\Cloud\Spanner\SpannerClient;
use Google\Cloud\Spanner\Instance;
use Google\Cloud\TestUtils\ExecuteCommandTrait;
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
use Google\Cloud\TestUtils\TestTrait;
use PHPUnit\Framework\TestCase;
class spannerTest extends TestCase
{
use TestTrait, EventuallyConsistentTestTrait, ExecuteCommandTrait {
ExecuteCommandTrait::runCommand as traitRunCommand;
}
private static $commandFile = __DIR__ . '/../spanner.php';
/** @var string instanceId */
protected static $instanceId;
/** @var string databaseId */
protected static $databaseId;
/** @var $instance Instance */
protected static $instance;
/** @var $lastUpdateData int */
protected static $lastUpdateDataTimestamp;
public static function setUpBeforeClass()
{
self::checkProjectEnvVars();
if (!extension_loaded('grpc')) {
self::markTestSkipped('Must enable grpc extension.');
}
$instanceId = self::requireEnv('GOOGLE_SPANNER_INSTANCE_ID');
$spanner = new SpannerClient([
'projectId' => self::$projectId,
]);
self::$databaseId = 'test-' . time() . rand();
self::$instanceId = $instanceId;
self::$instance = $spanner->instance(self::$instanceId);
}
public function testCreateDatabase()
{
$output = $this->runCommand('create-database');
$this->assertContains('Waiting for operation to complete...', $output);
$this->assertContains('Created database test-', $output);
}
/**
* @depends testCreateDatabase
*/
public function testInsertData()
{
$output = $this->runCommand('insert-data');
$this->assertEquals('Inserted data.' . PHP_EOL, $output);
}
/**
* @depends testInsertData
*/
public function testQueryData()
{
$output = $this->runCommand('query-data');
$this->assertContains('SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk', $output);
$this->assertContains('SingerId: 1, AlbumId: 2, AlbumTitle: Go, Go, Go', $output);
$this->assertContains('SingerId: 2, AlbumId: 1, AlbumTitle: Green', $output);
$this->assertContains('SingerId: 2, AlbumId: 2, AlbumTitle: Forever Hold Your Peace', $output);
$this->assertContains('SingerId: 2, AlbumId: 3, AlbumTitle: Terrified', $output);
}
/**
* @depends testInsertData
*/
public function testBatchQueryData()
{
$output = $this->runCommand('batch-query-data');
$this->assertContains('SingerId: 1, FirstName: Marc, LastName: Richards', $output);
$this->assertContains('SingerId: 2, FirstName: Catalina, LastName: Smith', $output);
$this->assertContains('SingerId: 3, FirstName: Alice, LastName: Trentor', $output);
$this->assertContains('SingerId: 4, FirstName: Lea, LastName: Martin', $output);
$this->assertContains('SingerId: 5, FirstName: David, LastName: Lomond', $output);
$this->assertContains('Total Partitions:', $output);
$this->assertContains('Total Records: 5', $output);
$this->assertContains('Average Records Per Partition:', $output);
}
/**
* @depends testInsertData
*/
public function testReadData()
{
$output = $this->runCommand('read-data');
$this->assertContains('SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk', $output);
$this->assertContains('SingerId: 1, AlbumId: 2, AlbumTitle: Go, Go, Go', $output);
$this->assertContains('SingerId: 2, AlbumId: 1, AlbumTitle: Green', $output);
$this->assertContains('SingerId: 2, AlbumId: 2, AlbumTitle: Forever Hold Your Peace', $output);
$this->assertContains('SingerId: 2, AlbumId: 3, AlbumTitle: Terrified', $output);
}
/**
* @depends testInsertData
*/
public function testAddColumn()
{
$output = $this->runCommand('add-column');
$this->assertContains('Waiting for operation to complete...', $output);
$this->assertContains('Added the MarketingBudget column.', $output);
}
/**
* @depends testAddColumn
*/
public function testUpdateData()
{
$output = $this->runCommand('update-data');
self::$lastUpdateDataTimestamp = time();
$this->assertEquals('Updated data.' . PHP_EOL, $output);
}
/**
* @depends testAddColumn
*/
public function testQueryDataWithNewColumn()
{
$output = $this->runCommand('query-data-with-new-column');
$this->assertContains('SingerId: 1, AlbumId: 1, MarketingBudget:', $output);
$this->assertContains('SingerId: 1, AlbumId: 2, MarketingBudget:', $output);
$this->assertContains('SingerId: 2, AlbumId: 1, MarketingBudget:', $output);
$this->assertContains('SingerId: 2, AlbumId: 2, MarketingBudget:', $output);
$this->assertContains('SingerId: 2, AlbumId: 3, MarketingBudget:', $output);
}
/**
* @depends testUpdateData
*/
public function testReadWriteTransaction()
{
$this->runCommand('update-data');
$output = $this->runCommand('read-write-transaction');
$this->assertContains('Setting first album\'s budget to 300000 and the second album\'s budget to 300000', $output);
$this->assertContains('Transaction complete.', $output);
}
/**
* @depends testAddColumn
*/
public function testCreateIndex()
{
$output = $this->runCommand('create-index');
$this->assertContains('Waiting for operation to complete...', $output);
$this->assertContains('Added the AlbumsByAlbumTitle index.', $output);
}
/**
* @depends testCreateIndex
*/
public function testQueryDataWithIndex()
{
$output = $this->runCommand('query-data-with-index');
$this->assertContains('AlbumId: 2, AlbumTitle: Forever Hold Your Peace', $output);
$this->assertContains('AlbumId: 2, AlbumTitle: Go, Go, Go', $output);
}
/**
* @depends testCreateIndex
*/
public function testReadDataWithIndex()
{
$output = $this->runCommand('read-data-with-index');
$this->assertContains('AlbumId: 1, AlbumTitle: Total Junk', $output);
$this->assertContains('AlbumId: 2, AlbumTitle: Go, Go, Go', $output);
$this->assertContains('AlbumId: 1, AlbumTitle: Green', $output);
$this->assertContains('AlbumId: 3, AlbumTitle: Terrified', $output);
$this->assertContains('AlbumId: 2, AlbumTitle: Forever Hold Your Peace', $output);
}
/**
* @depends testCreateIndex
*/
public function testCreateStoringIndex()
{
$output = $this->runCommand('create-storing-index');
$this->assertContains('Waiting for operation to complete...', $output);
$this->assertContains('Added the AlbumsByAlbumTitle2 index.', $output);
}
/**
* @depends testCreateStoringIndex
*/
public function testReadDataWithStoringIndex()
{
$output = $this->runCommand('read-data-with-storing-index');
$this->assertContains('AlbumId: 2, AlbumTitle: Forever Hold Your Peace, MarketingBudget:', $output);
$this->assertContains('AlbumId: 2, AlbumTitle: Go, Go, Go, MarketingBudget:', $output);
$this->assertContains('AlbumId: 1, AlbumTitle: Green, MarketingBudget:', $output);
$this->assertContains('AlbumId: 3, AlbumTitle: Terrified, MarketingBudget:', $output);
$this->assertContains('AlbumId: 1, AlbumTitle: Total Junk, MarketingBudget:', $output);
}
/**
* @depends testUpdateData
*/
public function testReadOnlyTransaction()
{
$output = $this->runCommand('read-only-transaction');
$this->assertContains('SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk', $output);
$this->assertContains('SingerId: 1, AlbumId: 2, AlbumTitle: Go, Go, Go', $output);
$this->assertContains('SingerId: 2, AlbumId: 1, AlbumTitle: Green', $output);
$this->assertContains('SingerId: 2, AlbumId: 2, AlbumTitle: Forever Hold Your Peace', $output);
$this->assertContains('SingerId: 2, AlbumId: 3, AlbumTitle: Terrified', $output);
}
/**
* @depends testUpdateData
*/
public function testReadStaleData()
{
// read-stale-data reads data that is exactly 15 seconds old. So, make sure 15 seconds
// have elapsed since testUpdateData().
$elapsed = time() - self::$lastUpdateDataTimestamp;
if ($elapsed < 16) {
sleep(16 - $elapsed);
}
$output = $this->runCommand('read-stale-data');
$this->assertContains('SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk', $output);
$this->assertContains('SingerId: 1, AlbumId: 2, AlbumTitle: Go, Go, Go', $output);
$this->assertContains('SingerId: 2, AlbumId: 1, AlbumTitle: Green', $output);
$this->assertContains('SingerId: 2, AlbumId: 2, AlbumTitle: Forever Hold Your Peace', $output);
$this->assertContains('SingerId: 2, AlbumId: 3, AlbumTitle: Terrified', $output);
}
/**
* @depends testReadStaleData
*/
public function testCreateTableTimestamp()
{
$output = $this->runCommand('create-table-timestamp');
$this->assertContains('Waiting for operation to complete...', $output);
$this->assertContains('Created Performances table in database test-', $output);
}
/**
* @depends testCreateTableTimestamp
*/
public function testInsertDataTimestamp()
{
$output = $this->runCommand('insert-data-timestamp');
$this->assertEquals('Inserted data.' . PHP_EOL, $output);
}
/**
* @depends testInsertDataTimestamp
*/
public function testAddTimestampColumn()
{
$output = $this->runCommand('add-timestamp-column');
$this->assertContains('Waiting for operation to complete...', $output);
$this->assertContains('Added LastUpdateTime as a commit timestamp column in Albums table', $output);
}
/**
* @depends testAddTimestampColumn
*/
public function testUpdateDataTimestamp()
{
$output = $this->runCommand('update-data-timestamp');
$this->assertEquals('Updated data.' . PHP_EOL, $output);
}
/**
* @depends testUpdateDataTimestamp
*/
public function testQueryDataTimestamp()
{
$output = $this->runCommand('query-data-timestamp');
$this->assertContains('SingerId: 1, AlbumId: 1, MarketingBudget: 1000000, LastUpdateTime: 20', $output);
$this->assertContains('SingerId: 2, AlbumId: 2, MarketingBudget: 750000, LastUpdateTime: 20', $output);
$this->assertContains('SingerId: 1, AlbumId: 2, MarketingBudget: NULL, LastUpdateTime: NULL', $output);
$this->assertContains('SingerId: 2, AlbumId: 1, MarketingBudget: NULL, LastUpdateTime: NULL', $output);
$this->assertContains('SingerId: 2, AlbumId: 3, MarketingBudget: NULL, LastUpdateTime: NULL', $output);
}
/**
* @depends testCreateDatabase
*/
public function testInsertStructData()
{
$output = $this->runCommand('insert-struct-data');
$this->assertEquals('Inserted data.' . PHP_EOL, $output);
}
/**
* @depends testInsertStructData
*/
public function testQueryDataWithStruct()
{
$output = $this->runCommand('query-data-with-struct');
$this->assertContains('SingerId: 6', $output);
}
/**
* @depends testInsertStructData
*/
public function testQueryDataWithArrayOfStruct()
{
$output = $this->runCommand('query-data-with-array-of-struct');
$this->assertContains('SingerId: 6', $output);
$this->assertContains('SingerId: 7', $output);
$this->assertContains('SingerId: 8', $output);
}
/**
* @depends testInsertStructData
*/
public function testQueryDataWithStructField()
{
$output = $this->runCommand('query-data-with-struct-field');
$this->assertContains('SingerId: 6', $output);
}
/**
* @depends testInsertStructData
*/
public function testQueryDataWithNestedStructField()
{
$output = $this->runCommand('query-data-with-nested-struct-field');
$this->assertContains('SingerId: 6 SongName: Imagination', $output);
$this->assertContains('SingerId: 9 SongName: Imagination', $output);
}
/**
* @depends testCreateDatabase
*/
public function testInsertDataWithDml()
{
$output = $this->runCommand('insert-data-with-dml');
$this->assertContains('Inserted 1 row(s)', $output);
}
/**
* @depends testAddColumn
*/
public function testUpdateDataWithDml()
{
$output = $this->runCommand('update-data-with-dml');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('Updated 1 row(s)', $output);
}
/**
* @depends testAddColumn
*/
public function testDeleteDataWithDml()
{
$output = $this->runCommand('delete-data-with-dml');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('Deleted 1 row(s)', $output);
}
/**
* @depends testInsertData
*/
public function testUpdateDataWithDmlTimestamp()
{
$output = $this->runCommand('update-data-with-dml-timestamp');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('Updated 2 row(s)', $output);
}
/**
* @depends testCreateDatabase
*/
public function testWriteReadWithDml()
{
$output = $this->runCommand('write-read-with-dml');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('Timothy Campbell', $output);
}
/**
* @depends testCreateDatabase
*/
public function testUpdateDataWithDmlStructs()
{
$output = $this->runCommand('update-data-with-dml-structs');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('Updated 1 row(s)', $output);
}
/**
* @depends testInsertData
*/
public function testWriteDataWithDML()
{
$output = $this->runCommand('write-data-with-dml');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('Inserted 4 row(s)', $output);
}
/**
* @depends testWriteDataWithDML
*/
public function testQueryDataWithParameter()
{
$output = $this->runCommand('query-data-with-parameter');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('SingerId: 12, FirstName: Melissa, LastName: Garcia', $output);
}
/**
* @depends testAddColumn
*/
public function testUpdateDataWithDmlTransaction()
{
$output = $this->runCommand('write-data-with-dml-transaction');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('Transaction complete', $output);
}
/**
* @depends testAddColumn
*/
public function testUpdateDataWithPartitionedDML()
{
$output = $this->runCommand('update-data-with-partitioned-dml');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('Updated 3 row(s)', $output);
}
/**
* @depends testAddColumn
*/
public function testDeleteDataWithPartitionedDML()
{
$output = $this->runCommand('deleted-data-with-partitioned-dml');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('Deleted 5 row(s)', $output);
}
/**
* @depends testAddColumn
*/
public function testUpdateDataWithBatchDML()
{
$output = $this->runCommand('update-data-with-batch-dml');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('Executed 2 SQL statements using Batch DML', $output);
}
/**
* @depends testCreateDatabase
*/
public function testCreateTableDatatypes()
{
$output = $this->runCommand('create-table-with-datatypes');
$this->assertContains('Waiting for operation to complete...', $output);
$this->assertContains('Created Venues table in database test-', $output);
}
/**
* @depends testCreateTableDatatypes
*/
public function testInsertDataWithDatatypes()
{
$output = $this->runCommand('insert-data-with-datatypes');
$this->assertEquals('Inserted data.' . PHP_EOL, $output);
}
/**
* @depends testInsertDataWithDatatypes
*/
public function testQueryDataWithArrayParameter()
{
$output = $this->runCommand('query-data-with-array-parameter');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('VenueId: 19, VenueName: Venue 19, AvailableDate: 2020-11-01', $output);
$this->assertContains('VenueId: 42, VenueName: Venue 42, AvailableDate: 2020-10-01', $output);
}
/**
* @depends testInsertDataWithDatatypes
*/
public function testQueryDataWithBoolParameter()
{
$output = $this->runCommand('query-data-with-bool-parameter');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('VenueId: 19, VenueName: Venue 19, OutdoorVenue: True', $output);
}
/**
* @depends testInsertDataWithDatatypes
*/
public function testQueryDataWithBytesParameter()
{
$output = $this->runCommand('query-data-with-bytes-parameter');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('VenueId: 4, VenueName: Venue 4', $output);
}
/**
* @depends testInsertDataWithDatatypes
*/
public function testQueryDataWithDateParameter()
{
$output = $this->runCommand('query-data-with-date-parameter');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('VenueId: 4, VenueName: Venue 4, LastContactDate: 2018-09-02', $output);
$this->assertContains('VenueId: 42, VenueName: Venue 42, LastContactDate: 2018-10-01', $output);
}
/**
* @depends testInsertDataWithDatatypes
*/
public function testQueryDataWithFloatParameter()
{
$output = $this->runCommand('query-data-with-float-parameter');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('VenueId: 4, VenueName: Venue 4, PopularityScore: 0.8', $output);
$this->assertContains('VenueId: 19, VenueName: Venue 19, PopularityScore: 0.9', $output);
}
/**
* @depends testInsertDataWithDatatypes
*/
public function testQueryDataWithIntParameter()
{
$output = $this->runCommand('query-data-with-int-parameter');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('VenueId: 19, VenueName: Venue 19, Capacity: 6300', $output);
$this->assertContains('VenueId: 42, VenueName: Venue 42, Capacity: 3000', $output);
}
/**
* @depends testInsertDataWithDatatypes
*/
public function testQueryDataWithStringParameter()
{
$output = $this->runCommand('query-data-with-string-parameter');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('VenueId: 42, VenueName: Venue 42', $output);
}
/**
* @depends testInsertDataWithDatatypes
*/
public function testQueryDataWithTimestampParameter()
{
$this->runEventuallyConsistentTest(function () {
$output = $this->runCommand('query-data-with-timestamp-parameter');
self::$lastUpdateDataTimestamp = time();
$this->assertContains('VenueId: 4, VenueName: Venue 4, LastUpdateTime:', $output);
$this->assertContains('VenueId: 19, VenueName: Venue 19, LastUpdateTime:', $output);
$this->assertContains('VenueId: 42, VenueName: Venue 42, LastUpdateTime:', $output);
});
}
private function runCommand($commandName)
{
return $this->traitRunCommand($commandName, [
'instance_id' => self::$instanceId,
'database_id' => self::$databaseId,
]);
}
public static function tearDownAfterClass()
{
if (self::$instance) {
// Clean up database
$database = self::$instance->database(self::$databaseId);
$database->drop();
}
}
}
You can’t perform that action at this time.
