From e570bc0abc73281a9283718f534fba1675f236a7 Mon Sep 17 00:00:00 2001 From: George Hickman Date: Fri, 24 Nov 2023 16:15:24 +0000 Subject: [PATCH] Turn table existence check into a fixture for easier use --- tests/conftest.py | 10 +++++++++- tests/metrics/timescaledb/test_writer.py | 8 ++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 547d08d7..c0c5896f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,5 @@ import pytest -from sqlalchemy import create_engine +from sqlalchemy import create_engine, inspect from sqlalchemy.engine import make_url from sqlalchemy_utils import create_database, database_exists, drop_database @@ -23,3 +23,11 @@ def engine(): # drop the database on test suite exit drop_database(url) + + +@pytest.fixture +def has_table(engine): + def checker(table_name): + return inspect(engine).has_table(table_name) + + return checker diff --git a/tests/metrics/timescaledb/test_writer.py b/tests/metrics/timescaledb/test_writer.py index 6a4e5903..e344c5ba 100644 --- a/tests/metrics/timescaledb/test_writer.py +++ b/tests/metrics/timescaledb/test_writer.py @@ -1,7 +1,7 @@ from datetime import date import pytest -from sqlalchemy import TIMESTAMP, Column, Integer, Table, inspect, select, text +from sqlalchemy import TIMESTAMP, Column, Integer, Table, select, text from sqlalchemy.engine import make_url from metrics.timescaledb.tables import metadata @@ -43,15 +43,15 @@ def table(): ) -def test_timescaledbwriter(engine, table): +def test_timescaledbwriter(engine, has_table, table): # check ensure_table is setting up the table - assert not inspect(engine).has_table(table.name) + assert not has_table(table.name) with TimescaleDBWriter(table, engine) as writer: for i in range(1, 4): writer.write(date(2023, 11, i), i) - assert inspect(engine).has_table(table.name) + assert has_table(table.name) # check there are timescaledb child tables # https://stackoverflow.com/questions/1461722/how-to-find-child-tables-that-inherit-from-another-table-in-psql