Skip to content

Commit 32fd479

Browse files
authored
tests(spanner): avoid table name collisions in tests (#18001)
# Fix Spanner Test Flakes: Avoid Generic Table Names ## Problem The `sqlalchemy-spanner` test suite occasionally fails with `Duplicate name in schema: t.` errors. This happens because multiple tests use the generic table name `t`. In a shared database environment, or due to buffered DDL statements in Spanner (which are non-transactional), a table from a previous test might still exist when a new test tries to create it. ## Solution Renamed the generic table name `t` to more specific names in several tests and fixtures to avoid collisions. - In `tests/conftest.py`, renamed `t` to `t_literal_round_trip_spanner`. - In `tests/test_suite_14.py`: - Renamed `t` to `t_nullable_reflection` in `test_nullable_reflection`. - Renamed `t` to `t_type_round_trip` in `_type_round_trip`. - Renamed `t` to `t_percent_signs` in `EscapingTest`. - Renamed `t` to `t_do_numeric` in `NumericTest`. ## Notes to Reviewers These changes are purely in test code and aim to improve test stability by eliminating shared resource name collisions.
1 parent 2207ca6 commit 32fd479

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

packages/sqlalchemy-spanner/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def run(
7171
compare=None,
7272
support_whereclause=True,
7373
):
74-
t = Table("t", metadata, Column("x", type_))
74+
t = Table("t_literal_round_trip_spanner", metadata, Column("x", type_))
7575
t.create(connection)
7676

7777
for value in input_:

packages/sqlalchemy-spanner/tests/test_suite_14.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import pytest
2626
import sqlalchemy
2727
from google.api_core.datetime_helpers import DatetimeWithNanoseconds
28+
from google.cloud import spanner_dbapi
2829
from google.cloud.spanner_v1 import Client, RequestOptions
30+
from google.cloud.sqlalchemy_spanner import version as sqlalchemy_spanner_version
2931
from sqlalchemy import (
3032
FLOAT,
3133
Boolean,
@@ -204,8 +206,6 @@
204206
)
205207
from sqlalchemy.types import Integer, Numeric, Text
206208

207-
from google.cloud import spanner_dbapi
208-
from google.cloud.sqlalchemy_spanner import version as sqlalchemy_spanner_version
209209
from tests._helpers import get_db_url, get_project
210210

211211
config.test_schema = ""
@@ -250,7 +250,7 @@ class ComponentReflectionTestExtra(_ComponentReflectionTestExtra):
250250
@testing.requires.table_reflection
251251
def test_nullable_reflection(self, connection, metadata):
252252
t = Table(
253-
"t",
253+
"t_nullable_reflection",
254254
metadata,
255255
Column("a", Integer, nullable=True),
256256
Column("b", Integer, nullable=False),
@@ -260,19 +260,21 @@ def test_nullable_reflection(self, connection, metadata):
260260
eq_(
261261
dict(
262262
(col["name"], col["nullable"])
263-
for col in inspect(connection).get_columns("t")
263+
for col in inspect(connection).get_columns("t_nullable_reflection")
264264
),
265265
{"a": True, "b": False},
266266
)
267267

268268
def _type_round_trip(self, connection, metadata, *types):
269269
t = Table(
270-
"t", metadata, *[Column("t%d" % i, type_) for i, type_ in enumerate(types)]
270+
"t_type_round_trip",
271+
metadata,
272+
*[Column("t%d" % i, type_) for i, type_ in enumerate(types)],
271273
)
272274
t.create(connection)
273275
connection.connection.commit()
274276

275-
return [c["type"] for c in inspect(connection).get_columns("t")]
277+
return [c["type"] for c in inspect(connection).get_columns("t_type_round_trip")]
276278

277279
@testing.requires.table_reflection
278280
def test_numeric_reflection(self, connection, metadata):
@@ -1207,7 +1209,7 @@ def test_percent_sign_round_trip(self):
12071209
Overriding the test to avoid the same failure.
12081210
"""
12091211
m = self.metadata
1210-
t = Table("t", m, Column("data", String(50)))
1212+
t = Table("t_percent_signs", m, Column("data", String(50)))
12111213
t.create(config.db)
12121214
with config.db.begin() as conn:
12131215
conn.execute(t.insert(), dict(data="some % value"))
@@ -1551,7 +1553,7 @@ def do_numeric_test(self, metadata, connection):
15511553
@testing.emits_warning(r".*does \*not\* support Decimal objects natively")
15521554
def run(type_, input_, output, filter_=None, check_scale=False):
15531555
t = Table(
1554-
"t",
1556+
"t_do_numeric",
15551557
metadata,
15561558
Column("x", type_),
15571559
Column("id", Integer, primary_key=True),

0 commit comments

Comments
 (0)