Skip to content

Commit 8121741

Browse files
committed
fix(spanner): fix test database initialization and teardown on persistent databases, and apply code review fixes for tests
1 parent 4b60c45 commit 8121741

9 files changed

Lines changed: 35 additions & 33 deletions

File tree

Spanner/tests/System/BackupTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ public function testCreateBackupRequestFailed()
196196
} catch (FailedPreconditionException $e) {
197197
break;
198198
} catch (\Google\Cloud\Core\Exception\ServiceException $ex) {
199-
if ($i === 2 || !in_array($ex->getStatus(), ['UNAVAILABLE', 'DEADLINE_EXCEEDED'])) {
199+
$allowed = [14 /* UNAVAILABLE */, 4 /* DEADLINE_EXCEEDED */];
200+
if ($i === 2 || !in_array($ex->getCode(), $allowed)) {
200201
throw $ex;
201202
}
202203
sleep(2);
@@ -251,8 +252,8 @@ public function testCancelBackupOperation()
251252

252253
try {
253254
$op->cancel();
254-
} catch (\Google\ApiCore\ApiException $e) {
255-
if ($e->getStatus() !== 'DEADLINE_EXCEEDED') {
255+
} catch (\Google\Cloud\Core\Exception\ServiceException $e) {
256+
if ($e->getCode() !== 4 /* DEADLINE_EXCEEDED */) {
256257
throw $e;
257258
}
258259
}
@@ -724,8 +725,8 @@ private function pollWithExtendedTimeout($op)
724725
'maxPollingDurationSeconds' => $timeout - time()
725726
]);
726727
break;
727-
} catch (\Google\ApiCore\ApiException $e) {
728-
if ($e->getStatus() !== 'DEADLINE_EXCEEDED') {
728+
} catch (\Google\Cloud\Core\Exception\ServiceException $e) {
729+
if ($e->getCode() !== 4 /* DEADLINE_EXCEEDED */) {
729730
throw $e;
730731
}
731732
}

Spanner/tests/System/BatchTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public static function setUpTestFixtures(): void
4747
if (self::$isSetup) {
4848
return;
4949
}
50+
self::$database->delete(self::TABLE_NAME, new KeySet(['all' => true]));
5051
self::seedTable();
5152
self::$isSetup = true;
5253
}
@@ -99,7 +100,8 @@ public function testBatch()
99100
$partitions = $snapshot->partitionQuery($query, ['parameters' => $parameters]);
100101
break;
101102
} catch (\Google\Cloud\Core\Exception\ServiceException $ex) {
102-
if ($i === 2 || !in_array($ex->getStatus(), ['UNAVAILABLE', 'DEADLINE_EXCEEDED'])) {
103+
$allowed = [14 /* UNAVAILABLE */, 4 /* DEADLINE_EXCEEDED */];
104+
if ($i === 2 || !in_array($ex->getCode(), $allowed)) {
103105
throw $ex;
104106
}
105107
sleep(2);

Spanner/tests/System/LargeReadTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ class LargeReadTest extends SystemTestCase
4848
public static function setUpTestFixtures(): void
4949
{
5050
self::setUpTestDatabase();
51-
52-
51+
self::$database->delete(self::TABLE_NAME, new KeySet(['all' => true]));
5352

5453
$str = '';
5554
foreach (self::$data as $letter) {

Spanner/tests/System/PgBatchTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ public static function setUpTestFixtures(): void
5151
}
5252
self::setUpTestDatabase();
5353

54-
55-
56-
54+
self::$database->delete(self::TABLE_NAME, new \Google\Cloud\Spanner\KeySet(['all' => true]));
5755

5856
self::seedTable();
5957
self::$hasSetupBatch = true;
@@ -130,7 +128,7 @@ private static function seedTable()
130128
];
131129
}
132130

133-
self::$database->insertBatch(self::TABLE_NAME, $mutations, [
131+
self::$database->insertOrUpdateBatch(self::TABLE_NAME, $mutations, [
134132
'timeoutMillis' => 50000
135133
]);
136134
}

Spanner/tests/System/PgReadTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,9 @@ public static function setUpTestFixtures(): void
4646
{
4747
self::setUpTestDatabase();
4848

49-
50-
51-
52-
53-
54-
55-
5649
$db = self::$database;
57-
50+
$db->delete(self::READ_TABLE_NAME, new KeySet(['all' => true]));
51+
$db->delete(self::RANGE_TABLE_NAME, new KeySet(['all' => true]));
5852

5953
self::$dataset = self::generateDataset(20, true);
6054
$db->insertOrUpdateBatch(self::RANGE_TABLE_NAME, self::$dataset);

Spanner/tests/System/PgSystemTestCaseTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ protected static function setUpTestDatabase(): void
5151
'databaseDialect' => DatabaseDialect::POSTGRESQL
5252
]);
5353
$op->pollUntilComplete();
54+
} else {
55+
TestDatabaseManager::$pgHasSetUp = true;
56+
TestDatabaseManager::$client = self::$client;
57+
TestDatabaseManager::$instance = self::$instance;
58+
TestDatabaseManager::$pgDatabase = self::$database;
59+
TestDatabaseManager::$pgDbName = self::$dbName;
60+
self::$hasSetUp = true;
61+
return;
5462
}
5563

5664
self::$database->updateDdlBatch(

Spanner/tests/System/ReadTest.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,9 @@ public static function setUpTestFixtures(): void
5050
{
5151
self::setUpTestDatabase();
5252

53-
54-
55-
56-
57-
58-
59-
60-
61-
6253
$db = self::$database;
63-
54+
$db->delete(self::READ_TABLE_NAME, new KeySet(['all' => true]));
55+
$db->delete(self::RANGE_TABLE_NAME, new KeySet(['all' => true]));
6456

6557
self::$dataset = self::generateDataset(20, true);
6658
$db->insertOrUpdateBatch(self::RANGE_TABLE_NAME, self::$dataset);

Spanner/tests/System/SnapshotTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,13 @@ public function testOrderByInSnapshot()
238238
{
239239
$db = self::$database;
240240

241-
$db->insertBatch(self::TABLE_NAME, [
241+
$db->insertOrUpdateBatch(self::TABLE_NAME, [
242242
[
243-
'id' => rand(1, 346464),
243+
'id' => self::randId(),
244244
'number' => 1
245245
],
246246
[
247-
'id' => rand(1, 346464),
247+
'id' => self::randId(),
248248
'number' => 2
249249
]
250250
]);

Spanner/tests/System/SystemTestCaseTrait.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ private static function setUpTestDatabase(): void
119119
if (!self::$database->exists()) {
120120
$op = self::$instance->createDatabase(self::$dbName);
121121
$op->pollUntilComplete();
122+
} else {
123+
TestDatabaseManager::$sqlHasSetUp = true;
124+
TestDatabaseManager::$client = self::$client;
125+
TestDatabaseManager::$instance = self::$instance;
126+
TestDatabaseManager::$sqlDatabase = self::$database;
127+
TestDatabaseManager::$sqlDbName = self::$dbName;
128+
self::$hasSetUp = true;
129+
return;
122130
}
123131

124132
$op = self::$database->updateDdlBatch(

0 commit comments

Comments
 (0)