Skip to content

Commit 6dedc64

Browse files
committed
test(spanner): add IF NOT EXISTS to PostgreSQL DDL statements
test(spanner): consolidate test database provisioning fix(spanner): fix PgReadTest index creation on shared database test(spanner): Consolidate DDL operations into test setup suite test(spanner): fix schema mismatches for partitionedDml and PgQueryTest test(spanner): rename PgQueryTest_2 back to PgQueryTest test(spanner): fix PostgreSQL column name mismatches for test tables
1 parent 8c99025 commit 6dedc64

21 files changed

Lines changed: 480 additions & 504 deletions

Spanner/tests/System/BatchTest.php

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -31,78 +31,27 @@
3131
*/
3232
class BatchTest extends SystemTestCase
3333
{
34+
const TABLE_NAME = 'BatchTest';
3435
use SystemTestCaseTrait;
3536
use DatabaseRoleTrait;
3637

37-
private static $tableName;
38+
3839
private static $isSetup = false;
3940

4041
/**
4142
* @beforeClass
4243
*/
4344
public static function setUpTestFixtures(): void
4445
{
45-
if (self::$isSetup) {
46-
return;
47-
}
4846
self::setUpTestDatabase();
49-
50-
self::$tableName = uniqid(self::TESTING_PREFIX);
51-
52-
self::$database->updateDdl(sprintf(
53-
'CREATE TABLE %s (
54-
id INT64 NOT NULL,
55-
decade INT64 NOT NULL
56-
) PRIMARY KEY (id)',
57-
self::$tableName
58-
))->pollUntilComplete();
59-
60-
if (self::$database->info()['databaseDialect'] == DatabaseDialect::GOOGLE_STANDARD_SQL) {
61-
$statements = [
62-
sprintf('CREATE ROLE %s', self::$dbRole),
63-
sprintf('CREATE ROLE %s', self::$restrictiveDbRole),
64-
];
65-
66-
if (!self::isEmulatorUsed()) {
67-
$statements[] = sprintf(
68-
'GRANT SELECT(id) ON TABLE %s TO ROLE %s',
69-
self::$tableName,
70-
self::$restrictiveDbRole
71-
);
72-
}
73-
74-
$statements[] = sprintf(
75-
'GRANT SELECT ON TABLE %s TO ROLE %s',
76-
self::$tableName,
77-
self::$dbRole
78-
);
79-
80-
self::$database->updateDdlBatch($statements)->pollUntilComplete();
81-
}
82-
83-
self::seedTable();
84-
self::$isSetup = true;
85-
}
86-
87-
private static function seedTable()
88-
{
89-
$decades = [1950, 1960, 1970, 1980, 1990, 2000];
90-
for ($i = 0; $i < 250; $i++) {
91-
self::$database->insert(self::$tableName, [
92-
'id' => self::randId(),
93-
'decade' => array_rand($decades)
94-
], [
95-
'timeoutMillis' => 50000
96-
]);
97-
}
9847
}
9948

10049
public function testBatch()
10150
{
10251
$query = 'SELECT
10352
id,
10453
decade
105-
FROM ' . self::$tableName . '
54+
FROM ' . self::TABLE_NAME . '
10655
WHERE
10756
decade > @earlyBound
10857
AND
@@ -134,7 +83,7 @@ public function testBatch()
13483
]
13584
]);
13685

137-
$partitions = $snapshot->partitionRead(self::$tableName, $keySet, ['id', 'decade']);
86+
$partitions = $snapshot->partitionRead(self::TABLE_NAME, $keySet, ['id', 'decade']);
13887
$this->assertEquals(count($resultSet), $this->executePartitions($batch, $snapshot, $partitions));
13988
}
14089

@@ -149,7 +98,7 @@ public function testBatchWithDbRole($dbRole, $expected)
14998
$query = 'SELECT
15099
id,
151100
decade
152-
FROM ' . self::$tableName . '
101+
FROM ' . self::TABLE_NAME . '
153102
WHERE
154103
decade > @earlyBound
155104
AND

Spanner/tests/System/BatchWriteTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,6 @@ public static function setUpTestFixtures(): void
3535
{
3636
self::skipEmulatorTests();
3737
self::setUpTestDatabase();
38-
39-
self::$database->updateDdlBatch([
40-
'CREATE TABLE Singers (
41-
SingerId INT64 NOT NULL,
42-
FirstName STRING(1024),
43-
LastName STRING(1024),
44-
) PRIMARY KEY (SingerId)',
45-
'CREATE TABLE Albums (
46-
SingerId INT64 NOT NULL,
47-
AlbumId INT64 NOT NULL,
48-
AlbumTitle STRING(1024),
49-
) PRIMARY KEY (SingerId, AlbumId),
50-
INTERLEAVE IN PARENT Singers ON DELETE CASCADE'
51-
])->pollUntilComplete();
5238
}
5339

5440
public function testBatchWrite()

Spanner/tests/System/DatabaseRoleTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
*/
2525
trait DatabaseRoleTrait
2626
{
27-
private static $restrictiveDbRole = 'restrictiveReaderRole';
28-
private static $dbRole = 'readerRole';
27+
private static $restrictiveDbRole = 'RestrictiveReader';
28+
private static $dbRole = 'Reader';
2929

3030
abstract public static function setUpBeforeClass();
3131

Spanner/tests/System/LargeReadTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
*/
2828
class LargeReadTest extends SystemTestCase
2929
{
30+
const TABLE_NAME = 'LargeReadTable';
3031
use SystemTestCaseTrait;
3132

32-
private static $tableName;
33+
3334
private static $row = [];
3435

3536
//@codingStandardsIgnoreStart
@@ -48,7 +49,7 @@ public static function setUpTestFixtures(): void
4849
{
4950
self::setUpTestDatabase();
5051

51-
self::$tableName = uniqid(self::TESTING_PREFIX);
52+
5253

5354
$str = '';
5455
foreach (self::$data as $letter) {
@@ -67,7 +68,7 @@ public static function setUpTestFixtures(): void
6768
stringArrayColumn ARRAY<STRING(MAX)> NOT NULL,
6869
bytesArrayColumn ARRAY<BYTES(MAX)> NOT NULL
6970
) PRIMARY KEY (id)',
70-
self::$tableName
71+
self::TABLE_NAME
7172
))->pollUntilComplete();
7273

7374
self::seedTable();
@@ -84,7 +85,7 @@ private static function seedTable()
8485
];
8586

8687
for ($i = 0; $i < 10; $i++) {
87-
self::$database->insert(self::$tableName, self::$row + ['id' => self::randId()], [
88+
self::$database->insert(self::TABLE_NAME, self::$row + ['id' => self::randId()], [
8889
'timeoutMillis' => 50000
8990
]);
9091
}
@@ -98,7 +99,7 @@ public function testLargeRead()
9899
$db = self::$database;
99100

100101
$keyset = new KeySet(['all' => true]);
101-
$read = $db->read(self::$tableName, $keyset, array_keys(self::$row));
102+
$read = $db->read(self::TABLE_NAME, $keyset, array_keys(self::$row));
102103

103104
foreach ($read->rows() as $row) {
104105
$this->runAssertionsOnRow($row);
@@ -112,7 +113,7 @@ public function testLargeExecute()
112113
{
113114
$db = self::$database;
114115

115-
$execute = $db->execute('SELECT * FROM ' . self::$tableName);
116+
$execute = $db->execute('SELECT * FROM ' . self::TABLE_NAME);
116117

117118
foreach ($execute->rows() as $row) {
118119
$this->runAssertionsOnRow($row);

Spanner/tests/System/OperationsTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,14 @@ public function testRead()
9797
public function testUpdate()
9898
{
9999
$db = self::$database;
100+
$newName = uniqid('Doug');
100101
$row = $this->getRow();
101-
$row['name'] = 'Doug';
102+
$row['name'] = $newName;
102103

103104
$db->update('Users', $row);
104105

105106
$row = $this->getRow();
106-
$this->assertEquals('Doug', $row['name']);
107+
$this->assertEquals($newName, $row['name']);
107108
}
108109

109110
public function testInsertOrUpdate()

Spanner/tests/System/PgBatchTest.php

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030
*/
3131
class PgBatchTest extends SystemTestCase
3232
{
33+
const TABLE_NAME = 'PgBatchTest';
3334
use PgSystemTestCaseTrait;
3435
use DatabaseRoleTrait;
3536

36-
private static $tableName;
37+
3738
private static $hasSetupBatch = false;
3839

3940
/**
@@ -50,37 +51,9 @@ public static function setUpTestFixtures(): void
5051
}
5152
self::setUpTestDatabase();
5253

53-
self::$tableName = uniqid(self::TESTING_PREFIX);
54-
55-
self::$database->updateDdl(sprintf(
56-
'CREATE TABLE %s (
57-
id INTEGER PRIMARY KEY,
58-
decade INTEGER NOT NULL
59-
)',
60-
self::$tableName
61-
))->pollUntilComplete();
62-
63-
if (self::$database->info()['databaseDialect'] == DatabaseDialect::POSTGRESQL) {
64-
$statements = [
65-
sprintf('CREATE ROLE %s', self::$dbRole),
66-
sprintf('CREATE ROLE %s', self::$restrictiveDbRole),
67-
];
68-
69-
if (!self::isEmulatorUsed()) {
70-
$statements[] = sprintf(
71-
'GRANT SELECT(id) ON TABLE %s TO %s',
72-
self::$tableName,
73-
self::$restrictiveDbRole
74-
);
75-
$statements[] = sprintf(
76-
'GRANT SELECT ON TABLE %s TO %s',
77-
self::$tableName,
78-
self::$dbRole
79-
);
80-
}
54+
8155

82-
self::$database->updateDdlBatch($statements)->pollUntilComplete();
83-
}
56+
8457

8558
self::seedTable();
8659
self::$hasSetupBatch = true;
@@ -98,7 +71,7 @@ public function testBatchWithDbRole($dbRole, $expected)
9871
$query = 'SELECT
9972
id,
10073
decade
101-
FROM ' . self::$tableName . '
74+
FROM ' . self::TABLE_NAME . '
10275
WHERE
10376
decade > $1
10477
AND
@@ -119,6 +92,9 @@ public function testBatchWithDbRole($dbRole, $expected)
11992
try {
12093
$partitions = $snapshot->partitionQuery($query, ['parameters' => $parameters]);
12194
} catch (ServiceException $e) {
95+
if (is_null($expected)) {
96+
throw $e;
97+
}
12298
$error = $e;
12399
}
124100

@@ -146,7 +122,7 @@ private static function seedTable()
146122
{
147123
$decades = [1950, 1960, 1970, 1980, 1990, 2000];
148124
for ($i = 0; $i < 250; $i++) {
149-
self::$database->insert(self::$tableName, [
125+
self::$database->insert(self::TABLE_NAME, [
150126
'id' => self::randId(),
151127
'decade' => array_rand($decades)
152128
], [

Spanner/tests/System/PgBatchWriteTest.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,6 @@ public static function setUpTestFixtures(): void
3838
// against the emulator.
3939
self::skipEmulatorTests();
4040
self::setUpTestDatabase();
41-
42-
self::$database->updateDdlBatch([
43-
'CREATE TABLE Singers (
44-
singerid bigint NOT NULL,
45-
firstname varchar(1024),
46-
lastname varchar(1024),
47-
PRIMARY KEY (singerid)
48-
)',
49-
'CREATE TABLE Albums (
50-
singerid bigint NOT NULL,
51-
albumid bigint NOT NULL,
52-
albumtitle varchar(1024),
53-
PRIMARY KEY (singerid, albumid)
54-
) INTERLEAVE IN PARENT singers ON DELETE CASCADE'
55-
])->pollUntilComplete();
5641
}
5742

5843
public function testBatchWrite()

Spanner/tests/System/PgPartitionedDmlTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ public function testPdml()
4747

4848
$db = self::$database;
4949

50-
$db->updateDdl('CREATE TABLE IF NOT EXISTS ' . self::PDML_TABLE . '(
51-
id bigint NOT NULL,
52-
stringField varchar(1024),
53-
boolField BOOL,
54-
PRIMARY KEY(id)
55-
)')->pollUntilComplete();
5650

5751
$this->seedTable();
5852

Spanner/tests/System/PgQueryTest.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Google\Cloud\Spanner\Database;
2626
use Google\Cloud\Spanner\Date;
2727
use Google\Cloud\Spanner\Interval;
28+
use Google\Cloud\Spanner\KeySet;
2829
use Google\Cloud\Spanner\PgJsonb;
2930
use Google\Cloud\Spanner\PgNumeric;
3031
use Google\Cloud\Spanner\Timestamp;
@@ -40,7 +41,7 @@ class PgQueryTest extends SystemTestCase
4041
{
4142
use PgSystemTestCaseTrait;
4243

43-
const TABLE_NAME = 'test';
44+
const TABLE_NAME = 'PgQueryTest';
4445

4546
public static $timestampVal;
4647

@@ -51,24 +52,10 @@ public static function setUpTestFixtures(): void
5152
{
5253
self::setUpTestDatabase();
5354

54-
self::$database->updateDdl(
55-
'CREATE TABLE ' . self::TABLE_NAME . ' (
56-
id bigint NOT NULL,
57-
name varchar(1024),
58-
registered bool,
59-
age numeric,
60-
rating float,
61-
bytes_col bytea,
62-
created_at timestamptz,
63-
dt date,
64-
data jsonb,
65-
weight float4,
66-
PRIMARY KEY (id)
67-
)'
68-
)->pollUntilComplete();
69-
7055
self::$timestampVal = new Timestamp(new \DateTime());
7156

57+
self::$database->delete(self::TABLE_NAME, new KeySet(['all' => true]));
58+
7259
self::$database->insertOrUpdateBatch(self::TABLE_NAME, [
7360
[
7461
'id' => 1,

0 commit comments

Comments
 (0)