Skip to content

Commit fc1bc56

Browse files
authored
tests: refactor samples fixtures (#423)
* tests: refactor quickstart sample to use shared fixtures * tests: refactor autocommit sample to use shared fixtures * tests: refactor backup sample to use shared fixtures * tests: refactor snippets to use shared fixtures * tests: add 'pytest-dependency' plugin Closes #390. Closes #418.
1 parent ee31e8a commit fc1bc56

8 files changed

+462
-383
lines changed

samples/samples/autocommit_test.py

+8-51
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,29 @@
44
# license that can be found in the LICENSE file or at
55
# https://developers.google.com/open-source/licenses/bsd
66

7-
import time
8-
import uuid
9-
107
from google.api_core.exceptions import Aborted
11-
from google.cloud import spanner
128
import pytest
139
from test_utils.retry import RetryErrors
1410

1511
import autocommit
16-
from snippets_test import cleanup_old_instances
17-
18-
19-
def unique_instance_id():
20-
"""Creates a unique id for the database."""
21-
return f"test-instance-{uuid.uuid4().hex[:10]}"
22-
23-
24-
def unique_database_id():
25-
"""Creates a unique id for the database."""
26-
return f"test-db-{uuid.uuid4().hex[:10]}"
27-
28-
29-
INSTANCE_ID = unique_instance_id()
30-
DATABASE_ID = unique_database_id()
3112

3213

3314
@pytest.fixture(scope="module")
34-
def spanner_instance():
35-
spanner_client = spanner.Client()
36-
cleanup_old_instances(spanner_client)
37-
instance_config = "{}/instanceConfigs/{}".format(
38-
spanner_client.project_name, "regional-us-central1"
39-
)
40-
instance = spanner_client.instance(
41-
INSTANCE_ID,
42-
instance_config,
43-
labels={
44-
"cloud_spanner_samples": "true",
45-
"sample_name": "autocommit",
46-
"created": str(int(time.time()))
47-
}
48-
)
49-
op = instance.create()
50-
op.result(120) # block until completion
51-
yield instance
52-
instance.delete()
53-
54-
55-
@pytest.fixture(scope="module")
56-
def database(spanner_instance):
57-
"""Creates a temporary database that is removed after testing."""
58-
db = spanner_instance.database(DATABASE_ID)
59-
db.create()
60-
yield db
61-
db.drop()
15+
def sample_name():
16+
return "autocommit"
6217

6318

6419
@RetryErrors(exception=Aborted, max_tries=2)
65-
def test_enable_autocommit_mode(capsys, database):
20+
def test_enable_autocommit_mode(capsys, instance_id, sample_database):
6621
# Delete table if it exists for retry attempts.
67-
table = database.table('Singers')
22+
table = sample_database.table('Singers')
6823
if table.exists():
69-
op = database.update_ddl(["DROP TABLE Singers"])
24+
op = sample_database.update_ddl(["DROP TABLE Singers"])
7025
op.result()
7126

72-
autocommit.enable_autocommit_mode(INSTANCE_ID, DATABASE_ID)
27+
autocommit.enable_autocommit_mode(
28+
instance_id, sample_database.database_id,
29+
)
7330
out, _ = capsys.readouterr()
7431
assert "Autocommit mode is enabled." in out
7532
assert "SingerId: 13, AlbumId: Russell, AlbumTitle: Morales" in out

samples/samples/backup_sample_test.py

+58-82
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,18 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
import time
1514
import uuid
1615

1716
from google.api_core.exceptions import DeadlineExceeded
18-
from google.cloud import spanner
1917
import pytest
2018
from test_utils.retry import RetryErrors
2119

2220
import backup_sample
23-
from snippets_test import cleanup_old_instances
2421

2522

26-
def unique_instance_id():
27-
""" Creates a unique id for the database. """
28-
return f"test-instance-{uuid.uuid4().hex[:10]}"
23+
@pytest.fixture(scope="module")
24+
def sample_name():
25+
return "backup"
2926

3027

3128
def unique_database_id():
@@ -38,8 +35,6 @@ def unique_backup_id():
3835
return f"test-backup-{uuid.uuid4().hex[:10]}"
3936

4037

41-
INSTANCE_ID = unique_instance_id()
42-
DATABASE_ID = unique_database_id()
4338
RESTORE_DB_ID = unique_database_id()
4439
BACKUP_ID = unique_backup_id()
4540
CMEK_RESTORE_DB_ID = unique_database_id()
@@ -48,121 +43,100 @@ def unique_backup_id():
4843
RETENTION_PERIOD = "7d"
4944

5045

51-
@pytest.fixture(scope="module")
52-
def spanner_instance():
53-
spanner_client = spanner.Client()
54-
cleanup_old_instances(spanner_client)
55-
instance_config = "{}/instanceConfigs/{}".format(
56-
spanner_client.project_name, "regional-us-central1"
57-
)
58-
instance = spanner_client.instance(
59-
INSTANCE_ID,
60-
instance_config,
61-
labels={
62-
"cloud_spanner_samples": "true",
63-
"sample_name": "backup",
64-
"created": str(int(time.time()))
65-
}
66-
)
67-
op = instance.create()
68-
op.result(120) # block until completion
69-
yield instance
70-
for database_pb in instance.list_databases():
71-
database = instance.database(database_pb.name.split("/")[-1])
72-
database.drop()
73-
for backup_pb in instance.list_backups():
74-
backup = instance.backup(backup_pb.name.split("/")[-1])
75-
backup.delete()
76-
instance.delete()
77-
78-
79-
@pytest.fixture(scope="module")
80-
def database(spanner_instance):
81-
""" Creates a temporary database that is removed after testing. """
82-
db = spanner_instance.database(DATABASE_ID)
83-
db.create()
84-
yield db
85-
db.drop()
86-
87-
88-
def test_create_backup(capsys, database):
46+
@pytest.mark.dependency(name="create_backup")
47+
def test_create_backup(capsys, instance_id, sample_database):
8948
version_time = None
90-
with database.snapshot() as snapshot:
49+
with sample_database.snapshot() as snapshot:
9150
results = snapshot.execute_sql("SELECT CURRENT_TIMESTAMP()")
9251
version_time = list(results)[0][0]
9352

94-
backup_sample.create_backup(INSTANCE_ID, DATABASE_ID, BACKUP_ID, version_time)
53+
backup_sample.create_backup(
54+
instance_id,
55+
sample_database.database_id,
56+
BACKUP_ID,
57+
version_time,
58+
)
9559
out, _ = capsys.readouterr()
9660
assert BACKUP_ID in out
9761

9862

99-
def test_create_backup_with_encryption_key(capsys, spanner_instance, database):
100-
kms_key_name = "projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}".format(
101-
spanner_instance._client.project, "us-central1", "spanner-test-keyring", "spanner-test-cmek"
63+
@pytest.mark.dependency(name="create_backup_with_encryption_key")
64+
def test_create_backup_with_encryption_key(
65+
capsys, instance_id, sample_database, kms_key_name,
66+
):
67+
backup_sample.create_backup_with_encryption_key(
68+
instance_id,
69+
sample_database.database_id,
70+
CMEK_BACKUP_ID,
71+
kms_key_name,
10272
)
103-
backup_sample.create_backup_with_encryption_key(INSTANCE_ID, DATABASE_ID, CMEK_BACKUP_ID, kms_key_name)
10473
out, _ = capsys.readouterr()
10574
assert CMEK_BACKUP_ID in out
10675
assert kms_key_name in out
10776

10877

109-
# Depends on test_create_backup having run first
78+
@pytest.mark.dependency(depends=["create_backup"])
11079
@RetryErrors(exception=DeadlineExceeded, max_tries=2)
111-
def test_restore_database(capsys):
112-
backup_sample.restore_database(INSTANCE_ID, RESTORE_DB_ID, BACKUP_ID)
80+
def test_restore_database(capsys, instance_id, sample_database):
81+
backup_sample.restore_database(instance_id, RESTORE_DB_ID, BACKUP_ID)
11382
out, _ = capsys.readouterr()
114-
assert (DATABASE_ID + " restored to ") in out
83+
assert (sample_database.database_id + " restored to ") in out
11584
assert (RESTORE_DB_ID + " from backup ") in out
11685
assert BACKUP_ID in out
11786

11887

119-
# Depends on test_create_backup having run first
88+
@pytest.mark.dependency(depends=["create_backup_with_encryption_key"])
12089
@RetryErrors(exception=DeadlineExceeded, max_tries=2)
121-
def test_restore_database_with_encryption_key(capsys, spanner_instance):
122-
kms_key_name = "projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}".format(
123-
spanner_instance._client.project, "us-central1", "spanner-test-keyring", "spanner-test-cmek"
124-
)
125-
backup_sample.restore_database_with_encryption_key(INSTANCE_ID, CMEK_RESTORE_DB_ID, CMEK_BACKUP_ID, kms_key_name)
90+
def test_restore_database_with_encryption_key(
91+
capsys, instance_id, sample_database, kms_key_name,
92+
):
93+
backup_sample.restore_database_with_encryption_key(
94+
instance_id, CMEK_RESTORE_DB_ID, CMEK_BACKUP_ID, kms_key_name)
12695
out, _ = capsys.readouterr()
127-
assert (DATABASE_ID + " restored to ") in out
96+
assert (sample_database.database_id + " restored to ") in out
12897
assert (CMEK_RESTORE_DB_ID + " from backup ") in out
12998
assert CMEK_BACKUP_ID in out
13099
assert kms_key_name in out
131100

132101

133-
# Depends on test_create_backup having run first
134-
def test_list_backup_operations(capsys, spanner_instance):
135-
backup_sample.list_backup_operations(INSTANCE_ID, DATABASE_ID)
102+
@pytest.mark.dependency(depends=["create_backup"])
103+
def test_list_backup_operations(capsys, instance_id, sample_database):
104+
backup_sample.list_backup_operations(
105+
instance_id, sample_database.database_id)
136106
out, _ = capsys.readouterr()
137107
assert BACKUP_ID in out
138-
assert DATABASE_ID in out
108+
assert sample_database.database_id in out
139109

140110

141-
# Depends on test_create_backup having run first
142-
def test_list_backups(capsys, spanner_instance):
143-
backup_sample.list_backups(INSTANCE_ID, DATABASE_ID, BACKUP_ID)
111+
@pytest.mark.dependency(depends=["create_backup"])
112+
def test_list_backups(capsys, instance_id, sample_database):
113+
backup_sample.list_backups(
114+
instance_id, sample_database.database_id, BACKUP_ID,
115+
)
144116
out, _ = capsys.readouterr()
145117
id_count = out.count(BACKUP_ID)
146118
assert id_count == 7
147119

148120

149-
# Depends on test_create_backup having run first
150-
def test_update_backup(capsys):
151-
backup_sample.update_backup(INSTANCE_ID, BACKUP_ID)
121+
@pytest.mark.dependency(depends=["create_backup"])
122+
def test_update_backup(capsys, instance_id):
123+
backup_sample.update_backup(instance_id, BACKUP_ID)
152124
out, _ = capsys.readouterr()
153125
assert BACKUP_ID in out
154126

155127

156-
# Depends on test_create_backup having run first
157-
def test_delete_backup(capsys, spanner_instance):
158-
backup_sample.delete_backup(INSTANCE_ID, BACKUP_ID)
128+
@pytest.mark.dependency(depends=["create_backup"])
129+
def test_delete_backup(capsys, instance_id):
130+
backup_sample.delete_backup(instance_id, BACKUP_ID)
159131
out, _ = capsys.readouterr()
160132
assert BACKUP_ID in out
161133

162134

163-
# Depends on test_create_backup having run first
164-
def test_cancel_backup(capsys):
165-
backup_sample.cancel_backup(INSTANCE_ID, DATABASE_ID, BACKUP_ID)
135+
@pytest.mark.dependency(depends=["create_backup"])
136+
def test_cancel_backup(capsys, instance_id, sample_database):
137+
backup_sample.cancel_backup(
138+
instance_id, sample_database.database_id, BACKUP_ID,
139+
)
166140
out, _ = capsys.readouterr()
167141
cancel_success = "Backup creation was successfully cancelled." in out
168142
cancel_failure = ("Backup was created before the cancel completed." in out) and (
@@ -172,10 +146,12 @@ def test_cancel_backup(capsys):
172146

173147

174148
@RetryErrors(exception=DeadlineExceeded, max_tries=2)
175-
def test_create_database_with_retention_period(capsys, spanner_instance):
176-
backup_sample.create_database_with_version_retention_period(INSTANCE_ID, RETENTION_DATABASE_ID, RETENTION_PERIOD)
149+
def test_create_database_with_retention_period(capsys, sample_instance):
150+
backup_sample.create_database_with_version_retention_period(
151+
sample_instance.instance_id, RETENTION_DATABASE_ID, RETENTION_PERIOD,
152+
)
177153
out, _ = capsys.readouterr()
178154
assert (RETENTION_DATABASE_ID + " created with ") in out
179155
assert ("retention period " + RETENTION_PERIOD) in out
180-
database = spanner_instance.database(RETENTION_DATABASE_ID)
156+
database = sample_instance.database(RETENTION_DATABASE_ID)
181157
database.drop()

0 commit comments

Comments
 (0)