aboutsummaryrefslogtreecommitdiff
path: root/tests/conftest.py
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2023-04-01 13:14:17 +0200
committerDaniel Schadt <kingdread@gmx.de>2023-04-01 13:14:17 +0200
commit78ea2acd5950919ee65960ef8d87359d223a0068 (patch)
tree9dc32b7dad9a8fbbccef980cf9ccaffb2e49dec3 /tests/conftest.py
parente37e056c36272e5f0c0e23bd993290208e14a979 (diff)
downloadfietsboek-78ea2acd5950919ee65960ef8d87359d223a0068.tar.gz
fietsboek-78ea2acd5950919ee65960ef8d87359d223a0068.tar.bz2
fietsboek-78ea2acd5950919ee65960ef8d87359d223a0068.zip
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.
Diffstat (limited to 'tests/conftest.py')
-rw-r--r--tests/conftest.py16
1 files changed, 11 insertions, 5 deletions
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):