Skip to content

Commit a4173a8

Browse files
committed
test(spanner): Consolidate DDL operations into test setup suite
1 parent 5eafa2a commit a4173a8

16 files changed

Lines changed: 379 additions & 482 deletions

Spanner/tests/System/BatchTest.php

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -31,75 +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-
63-
if (!self::isEmulatorUsed()) {
64-
$statements[] = sprintf(
65-
'GRANT SELECT(id) ON TABLE %s TO ROLE %s',
66-
self::$tableName,
67-
self::$restrictiveDbRole
68-
);
69-
}
70-
71-
$statements[] = sprintf(
72-
'GRANT SELECT ON TABLE %s TO ROLE %s',
73-
self::$tableName,
74-
self::$dbRole
75-
);
76-
77-
self::$database->updateDdlBatch($statements)->pollUntilComplete();
78-
}
79-
80-
self::seedTable();
81-
self::$isSetup = true;
82-
}
83-
84-
private static function seedTable()
85-
{
86-
$decades = [1950, 1960, 1970, 1980, 1990, 2000];
87-
for ($i = 0; $i < 250; $i++) {
88-
self::$database->insert(self::$tableName, [
89-
'id' => self::randId(),
90-
'decade' => array_rand($decades)
91-
], [
92-
'timeoutMillis' => 50000
93-
]);
94-
}
9547
}
9648

9749
public function testBatch()
9850
{
9951
$query = 'SELECT
10052
id,
10153
decade
102-
FROM ' . self::$tableName . '
54+
FROM ' . self::TABLE_NAME . '
10355
WHERE
10456
decade > @earlyBound
10557
AND
@@ -131,7 +83,7 @@ public function testBatch()
13183
]
13284
]);
13385

134-
$partitions = $snapshot->partitionRead(self::$tableName, $keySet, ['id', 'decade']);
86+
$partitions = $snapshot->partitionRead(self::TABLE_NAME, $keySet, ['id', 'decade']);
13587
$this->assertEquals(count($resultSet), $this->executePartitions($batch, $snapshot, $partitions));
13688
}
13789

@@ -146,7 +98,7 @@ public function testBatchWithDbRole($dbRole, $expected)
14698
$query = 'SELECT
14799
id,
148100
decade
149-
FROM ' . self::$tableName . '
101+
FROM ' . self::TABLE_NAME . '
150102
WHERE
151103
decade > @earlyBound
152104
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/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/PgBatchTest.php

Lines changed: 6 additions & 30 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,34 +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 IF NOT EXISTS %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-
66-
if (!self::isEmulatorUsed()) {
67-
$statements[] = sprintf(
68-
'GRANT SELECT(id) ON TABLE %s TO %s',
69-
self::$tableName,
70-
self::$restrictiveDbRole
71-
);
72-
$statements[] = sprintf(
73-
'GRANT SELECT ON TABLE %s TO %s',
74-
self::$tableName,
75-
self::$dbRole
76-
);
77-
}
54+
7855

79-
self::$database->updateDdlBatch($statements)->pollUntilComplete();
80-
}
56+
8157

8258
self::seedTable();
8359
self::$hasSetupBatch = true;
@@ -95,7 +71,7 @@ public function testBatchWithDbRole($dbRole, $expected)
9571
$query = 'SELECT
9672
id,
9773
decade
98-
FROM ' . self::$tableName . '
74+
FROM ' . self::TABLE_NAME . '
9975
WHERE
10076
decade > $1
10177
AND
@@ -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 IF NOT EXISTS Singers (
44-
singerid bigint NOT NULL,
45-
firstname varchar(1024),
46-
lastname varchar(1024),
47-
PRIMARY KEY (singerid)
48-
)',
49-
'CREATE TABLE IF NOT EXISTS 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/PgQueryTest.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,6 @@ public static function setUpTestFixtures(): void
5252
{
5353
self::setUpTestDatabase();
5454

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

7357
self::$database->delete(self::TABLE_NAME, new KeySet(['all' => true]));

0 commit comments

Comments
 (0)