Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit eca57f6

Browse files
committed
Tests now connect to DBs only if being run (instead of on the module level)
1 parent 27a9d6f commit eca57f6

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

tests/common.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,17 @@ def get_git_revision_short_hash() -> str:
6363
db.Trino: TEST_TRINO_CONN_STRING,
6464
}
6565

66-
for k, v in CONN_STRINGS.items():
67-
if v is None:
68-
logging.warn(f"Connection to {k} not configured")
69-
else:
70-
logging.info(f"Testing database: {k}")
7166

67+
def _print_used_dbs():
68+
used = {k.__name__ for k, v in CONN_STRINGS.items() if v is not None}
69+
unused = {k.__name__ for k, v in CONN_STRINGS.items() if v is None}
70+
71+
logging.info(f"Testing databases: {', '.join(used)}")
72+
if unused:
73+
logging.info(f"Connection not configured; skipping tests for: {', '.join(unused)}")
74+
75+
76+
_print_used_dbs()
7277
CONN_STRINGS = {k: v for k, v in CONN_STRINGS.items() if v is not None}
7378

7479

tests/test_database_types.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@
2626
_drop_table_if_exists,
2727
)
2828

29+
CONNS = None
2930

30-
CONNS = {k: db.connect.connect(v, N_THREADS) for k, v in CONN_STRINGS.items()}
3131

32-
CONNS[db.MySQL].query("SET @@session.time_zone='+00:00'", None)
33-
oracle.SESSION_TIME_ZONE = postgresql.SESSION_TIME_ZONE = "UTC"
32+
def init_conns():
33+
global CONNS
34+
if CONNS is not None:
35+
return
36+
37+
CONNS = {k: db.connect.connect(v, N_THREADS) for k, v in CONN_STRINGS.items()}
38+
CONNS[db.MySQL].query("SET @@session.time_zone='+00:00'", None)
39+
oracle.SESSION_TIME_ZONE = postgresql.SESSION_TIME_ZONE = "UTC"
40+
3441

3542
DATABASE_TYPES = {
3643
db.PostgreSQL: {
@@ -374,7 +381,7 @@ def __iter__(self):
374381
) in source_type_categories.items(): # int, datetime, ..
375382
for source_type in source_types:
376383
for target_type in target_type_categories[type_category]:
377-
if CONNS.get(source_db, False) and CONNS.get(target_db, False):
384+
if CONN_STRINGS.get(source_db, False) and CONN_STRINGS.get(target_db, False):
378385
type_pairs.append(
379386
(
380387
source_db,
@@ -518,6 +525,9 @@ def _create_table_with_indexes(conn, table, type):
518525
class TestDiffCrossDatabaseTables(unittest.TestCase):
519526
maxDiff = 10000
520527

528+
def setUp(self) -> None:
529+
init_conns()
530+
521531
def tearDown(self) -> None:
522532
if not BENCHMARK:
523533
_drop_table_if_exists(self.src_conn, self.src_table)

tests/test_diff_tables.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@
2020
N_THREADS,
2121
)
2222

23+
DATABASE_INSTANCES = None
2324
DATABASE_URIS = {k.__name__: v for k, v in CONN_STRINGS.items()}
24-
DATABASE_INSTANCES = {k.__name__: connect(v, N_THREADS) for k, v in CONN_STRINGS.items()}
25+
26+
27+
def init_instances():
28+
global DATABASE_INSTANCES
29+
if DATABASE_INSTANCES is not None:
30+
return
31+
32+
DATABASE_INSTANCES = {k.__name__: connect(v, N_THREADS) for k, v in CONN_STRINGS.items()}
33+
2534

2635
TEST_DATABASES = {x.__name__ for x in (db.MySQL, db.PostgreSQL, db.Oracle, db.Redshift, db.Snowflake, db.BigQuery)}
2736

@@ -80,6 +89,7 @@ class TestPerDatabase(unittest.TestCase):
8089

8190
def setUp(self):
8291
assert self.db_name
92+
init_instances()
8393

8494
self.connection = DATABASE_INSTANCES[self.db_name]
8595
if self.with_preql:

0 commit comments

Comments
 (0)