Skip to content

Commit

Permalink
Turn table existence check into a fixture for easier use
Browse files Browse the repository at this point in the history
  • Loading branch information
ghickman committed Nov 28, 2023
1 parent 1c67529 commit 2c62d2a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 9 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
8 changes: 4 additions & 4 deletions tests/metrics/timescaledb/test_writer.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2c62d2a

Please sign in to comment.