From 78ea2acd5950919ee65960ef8d87359d223a0068 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 1 Apr 2023 13:14:17 +0200 Subject: fix isolated running of unit tests They caused issues because they might not have created the database tables or the data directory. Since the cleanup job runs globally after every test, it should take that into consideration and not error out. --- tests/conftest.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/conftest.py b/tests/conftest.py index a499bec..d4394cd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,7 +12,7 @@ import pytest import transaction import webtest -from sqlalchemy import delete, select +from sqlalchemy import delete, inspect, select from fietsboek import main, models from fietsboek.data import DataManager @@ -59,12 +59,18 @@ def data_manager(app_settings): def _cleanup_data(app_settings): yield engine = models.get_engine(app_settings) + db_meta = inspect(engine) with engine.begin() as connection: for table in reversed(Base.metadata.sorted_tables): - connection.execute(table.delete()) - data_dir = Path(app_settings["fietsboek.data_dir"]) - if (data_dir / "tracks").is_dir(): - shutil.rmtree(data_dir / "tracks") + # The unit tests don't always set up the tables, so be gentle when + # tearing them down + if db_meta.has_table(table.name): + connection.execute(table.delete()) + # The unit tests also often don't have a data directory, so be gentle here as well + if "fietsboek.data_dir" in app_settings: + data_dir = Path(app_settings["fietsboek.data_dir"]) + if (data_dir / "tracks").is_dir(): + shutil.rmtree(data_dir / "tracks") @pytest.fixture(scope='session') def app(app_settings, dbengine, tmp_path_factory): -- cgit v1.2.3