Skip to content

Commit 2db5167

Browse files
authored
SNOW-629074 unstable sqlalchemy tests due to missing table/sequence (#318)
1 parent 4a610df commit 2db5167

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

DESCRIPTION.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Source code is also available at:
99

1010
# Release Notes
1111

12-
- v1.4.0(July 19, 2022)
12+
- v1.4.0(July 20, 2022)
1313

1414
- Added support for `regexp_match`, `regexp_replace` in `sqlalchemy.sql.expression.ColumnOperators`.
1515
- Added support for Identity Column.

tests/sqlalchemy_test_suite/conftest.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Copyright (c) 2012-2022 Snowflake Computing Inc. All rights reserved.
33
#
44

5+
import sqlalchemy.testing.config
6+
from sqlalchemy import util
57
from sqlalchemy.dialects import registry
68
from sqlalchemy.testing.plugin.pytestplugin import * # noqa
79
from sqlalchemy.testing.plugin.pytestplugin import (
@@ -15,10 +17,31 @@
1517
from snowflake.sqlalchemy import URL
1618

1719
from ..conftest import get_db_parameters
20+
from ..util import random_string
1821

1922
registry.register("snowflake", "snowflake.sqlalchemy", "dialect")
2023
registry.register("snowflake.snowflake", "snowflake.sqlalchemy", "dialect")
21-
sqlalchemy_test_schema = "TEST_SCHEMA"
24+
TEST_SCHEMA = f"test_schema_{random_string(5)}"
25+
TEST_SCHEMA_2 = f"{TEST_SCHEMA}_2"
26+
27+
28+
# patch sqlalchemy.testing.config.Confi.__init__ for schema name randomization
29+
# same schema name would result in conflict as we're running tests in parallel in the CI
30+
def config_patched__init__(self, db, db_opts, options, file_config):
31+
self._set_name(db)
32+
self.db = db
33+
self.db_opts = db_opts
34+
self.options = options
35+
self.file_config = file_config
36+
self.test_schema = TEST_SCHEMA
37+
self.test_schema_2 = TEST_SCHEMA_2
38+
39+
self.is_async = db.dialect.is_async and not util.asbool(
40+
db.url.query.get("async_fallback", False)
41+
)
42+
43+
44+
sqlalchemy.testing.config.Config.__init__ = config_patched__init__
2245

2346

2447
def pytest_sessionstart(session):
@@ -27,13 +50,13 @@ def pytest_sessionstart(session):
2750
# schema name with 'TEST_SCHEMA' is required by some tests of the sqlalchemy test suite
2851
with snowflake.connector.connect(**db_parameters) as con:
2952
con.cursor().execute(f"CREATE SCHEMA IF NOT EXISTS {db_parameters['schema']}")
30-
con.cursor().execute(f"CREATE SCHEMA IF NOT EXISTS {sqlalchemy_test_schema};")
53+
con.cursor().execute(f"CREATE SCHEMA IF NOT EXISTS {TEST_SCHEMA};")
3154
_pytest_sessionstart(session)
3255

3356

3457
def pytest_sessionfinish(session):
3558
db_parameters = get_db_parameters()
3659
with snowflake.connector.connect(**db_parameters) as con:
3760
con.cursor().execute(f"DROP SCHEMA IF EXISTS {db_parameters['schema']}")
38-
con.cursor().execute(f"DROP SCHEMA IF EXISTS f{sqlalchemy_test_schema}")
61+
con.cursor().execute(f"DROP SCHEMA IF EXISTS f{TEST_SCHEMA}")
3962
_pytest_sessionfinish(session)

tests/util.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Copyright (c) 2012-2022 Snowflake Computing Inc. All rights reserved.
3+
#
4+
5+
from __future__ import annotations
6+
7+
import random
8+
import string
9+
from typing import Sequence
10+
11+
12+
def random_string(
13+
length: int,
14+
prefix: str = "",
15+
suffix: str = "",
16+
choices: Sequence[str] = string.ascii_lowercase,
17+
) -> str:
18+
"""Our convenience function to generate random string for object names.
19+
20+
Args:
21+
length: How many random characters to choose from choices.
22+
prefix: Prefix to add to random string generated.
23+
suffix: Suffix to add to random string generated.
24+
choices: A generator of things to choose from.
25+
"""
26+
random_part = "".join([random.choice(choices) for _ in range(length)])
27+
return "".join([prefix, random_part, suffix])

0 commit comments

Comments
 (0)