Skip to content

Commit 29c7fb0

Browse files
authored
test: use random suffix in DML test table (#114)
This should avoid flakes. Also, adds a table expiration so that if cleanup fails, BigQuery will still eventually remove the table. Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-bigquery-sqlalchemy/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #111 🦕
1 parent b3b0a0a commit 29c7fb0

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

tests/system/conftest.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
# license that can be found in the LICENSE file or at
55
# https://opensource.org/licenses/MIT.
66

7+
import datetime
78
import pathlib
9+
import random
810

911
import pytest
1012
import google.api_core.exceptions
@@ -16,6 +18,12 @@
1618
DATA_DIR = pathlib.Path(__file__).parent / "data"
1719

1820

21+
def temp_suffix():
22+
timestamp = datetime.datetime.utcnow().strftime("%y%m%d_%H%M%S")
23+
random_string = hex(random.randrange(1000000))[2:]
24+
return f"{timestamp}_{random_string}"
25+
26+
1927
def load_sample_data(
2028
full_table_id: str,
2129
bigquery_client: bigquery.Client,
@@ -84,12 +92,36 @@ def bigquery_dataset(
8492

8593

8694
@pytest.fixture(scope="session", autouse=True)
87-
def bigquery_empty_table(bigquery_dataset, bigquery_client, bigquery_schema):
95+
def bigquery_dml_dataset(bigquery_client: bigquery.Client):
96+
project_id = bigquery_client.project
97+
dataset_id = "test_pybigquery_dml"
98+
dataset = bigquery.Dataset(f"{project_id}.{dataset_id}")
99+
# Add default table expiration in case cleanup fails.
100+
dataset.default_table_expiration_ms = 1000 * int(
101+
datetime.timedelta(days=1).total_seconds()
102+
)
103+
dataset = bigquery_client.create_dataset(dataset, exists_ok=True)
104+
return dataset_id
105+
106+
107+
@pytest.fixture(scope="session", autouse=True)
108+
def bigquery_empty_table(
109+
bigquery_dataset: str,
110+
bigquery_dml_dataset: str,
111+
bigquery_client: bigquery.Client,
112+
bigquery_schema: List[bigquery.SchemaField],
113+
):
88114
project_id = bigquery_client.project
89-
dataset_id = bigquery_dataset
90-
table_id = f"{project_id}.{dataset_id}.sample_dml"
115+
# Cleanup the sample_dml table, if it exists.
116+
old_table_id = f"{project_id}.{bigquery_dataset}.sample_dml"
117+
bigquery_client.delete_table(old_table_id, not_found_ok=True)
118+
# Create new table in its own dataset.
119+
dataset_id = bigquery_dml_dataset
120+
table_id = f"{project_id}.{dataset_id}.sample_dml_{temp_suffix()}"
91121
empty_table = bigquery.Table(table_id, schema=bigquery_schema)
92-
bigquery_client.create_table(empty_table, exists_ok=True)
122+
bigquery_client.create_table(empty_table)
123+
yield table_id
124+
bigquery_client.delete_table(empty_table)
93125

94126

95127
@pytest.fixture(scope="session", autouse=True)

tests/system/test_sqlalchemy_bigquery.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ def table_one_row(engine):
174174

175175

176176
@pytest.fixture(scope="session")
177-
def table_dml(engine):
178-
return Table("test_pybigquery.sample_dml", MetaData(bind=engine), autoload=True)
177+
def table_dml(engine, bigquery_empty_table):
178+
return Table(bigquery_empty_table, MetaData(bind=engine), autoload=True)
179179

180180

181181
@pytest.fixture(scope="session")
@@ -355,13 +355,11 @@ def test_tables_list(engine, engine_using_test_dataset):
355355
tables = engine.table_names()
356356
assert "test_pybigquery.sample" in tables
357357
assert "test_pybigquery.sample_one_row" in tables
358-
assert "test_pybigquery.sample_dml" in tables
359358
assert "test_pybigquery.sample_view" not in tables
360359

361360
tables = engine_using_test_dataset.table_names()
362361
assert "sample" in tables
363362
assert "sample_one_row" in tables
364-
assert "sample_dml" in tables
365363
assert "sample_view" not in tables
366364

367365

@@ -520,7 +518,7 @@ def test_dml(engine, session, table_dml):
520518
{"string": "updated_row"}, synchronize_session=False
521519
)
522520
updated_result = table_dml.select().execute().fetchone()
523-
assert updated_result["test_pybigquery.sample_dml_string"] == "updated_row"
521+
assert updated_result[table_dml.c.string] == "updated_row"
524522

525523
# test delete
526524
session.query(table_dml).filter(table_dml.c.string == "updated_row").delete(
@@ -576,16 +574,14 @@ def test_table_names_in_schema(inspector, inspector_using_test_dataset):
576574
tables = inspector.get_table_names("test_pybigquery")
577575
assert "test_pybigquery.sample" in tables
578576
assert "test_pybigquery.sample_one_row" in tables
579-
assert "test_pybigquery.sample_dml" in tables
580577
assert "test_pybigquery.sample_view" not in tables
581-
assert len(tables) == 3
578+
assert len(tables) == 2
582579

583580
tables = inspector_using_test_dataset.get_table_names()
584581
assert "sample" in tables
585582
assert "sample_one_row" in tables
586-
assert "sample_dml" in tables
587583
assert "sample_view" not in tables
588-
assert len(tables) == 3
584+
assert len(tables) == 2
589585

590586

591587
def test_view_names(inspector, inspector_using_test_dataset):

0 commit comments

Comments
 (0)