From 33c16b9d14b605a46e1f2dbc1fe5bafe961225e3 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 7 Jun 2025 22:28:58 +0200 Subject: bootstrap test: install psycopg --- tests/bootstrap/test_new_instance.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'tests') diff --git a/tests/bootstrap/test_new_instance.py b/tests/bootstrap/test_new_instance.py index dc3076e..00f9833 100644 --- a/tests/bootstrap/test_new_instance.py +++ b/tests/bootstrap/test_new_instance.py @@ -58,6 +58,11 @@ def test_setup_via_fietsupdate(tmpdir): LOGGER.info("Installing Fietsboek into clean env") binaries_path = install_fietsboek(tmpdir / "venv") + LOGGER.info("Installing additional SQL engines") + subprocess.check_call( + [binaries_path / "pip", "install", "psycopg2"] + ) + LOGGER.info("Creating a test configuration") create_config(Path("testing.ini")) -- cgit v1.2.3 From bf75c2c550344469be3221f5e2b55d7876c1831d Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 7 Jun 2025 23:11:39 +0200 Subject: clean up database after bootstrap test --- tests/bootstrap/test_new_instance.py | 96 ++++++++++++++++++++++++------------ 1 file changed, 65 insertions(+), 31 deletions(-) (limited to 'tests') diff --git a/tests/bootstrap/test_new_instance.py b/tests/bootstrap/test_new_instance.py index 00f9833..7686fde 100644 --- a/tests/bootstrap/test_new_instance.py +++ b/tests/bootstrap/test_new_instance.py @@ -2,6 +2,7 @@ script, as described in the documentation. """ +import configparser import contextlib import logging import os @@ -10,6 +11,8 @@ import subprocess import venv from pathlib import Path +import sqlalchemy + LOGGER = logging.getLogger(__name__) REPO_BASE = Path(__file__).parent.parent.parent @@ -51,36 +54,67 @@ def create_config(config_name: Path): Path("data").mkdir() +def cleanup_database(config_name: Path): + """Connects to the database and ensures everything is reset. + + :param config_name: Path to the config file. + """ + if not config_name.exists(): + return + + parser = configparser.ConfigParser() + parser["DEFAULT"]["here"] = str(config_name.parent) + parser.read(config_name) + + db_url = parser["app:main"]["sqlalchemy.url"] + engine = sqlalchemy.create_engine(db_url) + + match engine.name: + case "sqlite": + pass + case "postgresql": + with engine.connect() as connection: + connection.execute(sqlalchemy.text("DROP SCHEMA public CASCADE;")) + connection.execute(sqlalchemy.text("CREATE SCHEMA public;")) + connection.commit() + + def test_setup_via_fietsupdate(tmpdir): with chdir(tmpdir): - # We create a new temporary virtual environment with a fresh install, just - # to be sure there's as little interference as possible. - LOGGER.info("Installing Fietsboek into clean env") - binaries_path = install_fietsboek(tmpdir / "venv") - - LOGGER.info("Installing additional SQL engines") - subprocess.check_call( - [binaries_path / "pip", "install", "psycopg2"] - ) - - LOGGER.info("Creating a test configuration") - create_config(Path("testing.ini")) - - # Try to run the migrations - subprocess.check_call( - [binaries_path / "fietsupdate", "update", "-c", "testing.ini", "-f"] - ) - - # Also try to add an administrator - subprocess.check_call([ - binaries_path / "fietsctl", - "user", - "add", - "-c", "testing.ini", - "--email", "foobar@example.com", - "--name", "Foo Bar", - "--password", "raboof", - "--admin", - ]) - - assert True + try: + # We create a new temporary virtual environment with a fresh install, just + # to be sure there's as little interference as possible. + LOGGER.info("Installing Fietsboek into clean env") + binaries_path = install_fietsboek(tmpdir / "venv") + + LOGGER.info("Installing additional SQL engines") + subprocess.check_call( + [binaries_path / "pip", "install", "psycopg2"] + ) + + LOGGER.info("Creating a test configuration") + create_config(Path("testing.ini")) + + # Try to run the migrations + subprocess.check_call( + [binaries_path / "fietsupdate", "update", "-c", "testing.ini", "-f"] + ) + + # Also try to add an administrator + subprocess.check_call([ + binaries_path / "fietsctl", + "user", + "add", + "-c", "testing.ini", + "--email", "foobar@example.com", + "--name", "Foo Bar", + "--password", "raboof", + "--admin", + ]) + + assert True + finally: + # Clean up the database. This is important with anything but SQLite, as + # the tables would otherwise persist and interfere with the other tests. + cleanup_database(Path("testing.ini")) + -- cgit v1.2.3 From 94d295b366420d9b227a6192d66e73ee4c16718b Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sun, 8 Jun 2025 00:54:58 +0200 Subject: close SQL connections in tests Otherwise Postgres does not let us drop the tables and hangs forever --- tests/conftest.py | 1 + tests/playwright/conftest.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/conftest.py b/tests/conftest.py index cd74b0b..652d443 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -52,6 +52,7 @@ def dbengine(app_settings, ini_file): yield engine + engine.dispose() Base.metadata.drop_all(bind=engine) alembic.command.stamp(alembic_cfg, None, purge=True) diff --git a/tests/playwright/conftest.py b/tests/playwright/conftest.py index e914b01..adf5ef3 100644 --- a/tests/playwright/conftest.py +++ b/tests/playwright/conftest.py @@ -57,7 +57,11 @@ def dbaccess(app): through and the running WSGI app cannot read them. """ session_factory = app.registry["dbsession_factory"] - return session_factory() + factory = session_factory() + + yield factory + + factory.close() class Helper: -- cgit v1.2.3 From 41cb0a5b2eef055c0dc7ac864469068c7b393d14 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sun, 8 Jun 2025 01:05:58 +0200 Subject: fix archive test We might not always get the IDs as 1 and 2, so we need to adapt. --- tests/integration/test_browse.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/integration/test_browse.py b/tests/integration/test_browse.py index 875821d..412b042 100644 --- a/tests/integration/test_browse.py +++ b/tests/integration/test_browse.py @@ -22,6 +22,7 @@ def added_tracks(tm, dbsession, owner, data_manager): tm.abort() tracks = [] + track_ids = [] with tm: track = models.Track( owner=owner, @@ -37,6 +38,7 @@ def added_tracks(tm, dbsession, owner, data_manager): dbsession.flush() data_manager.initialize(track.id).compress_gpx(load_gpx_asset("MyTourbook_1.gpx.gz")) tracks.append(track) + track_ids.append(track.id) track = models.Track( owner=owner, @@ -52,12 +54,13 @@ def added_tracks(tm, dbsession, owner, data_manager): dbsession.flush() data_manager.initialize(track.id).compress_gpx(load_gpx_asset("Teasi_1.gpx.gz")) tracks.append(track) + track_ids.append(track.id) tm.begin() tm.doom() try: - yield tracks + yield track_ids finally: tm.abort() with tm: @@ -80,9 +83,9 @@ def test_browse(testapp, dbsession, route_path, logged_in, tm, data_manager): def test_archive(testapp, dbsession, route_path, logged_in, tm, data_manager): # pylint: disable=too-many-positional-arguments - with added_tracks(tm, dbsession, logged_in, data_manager): + with added_tracks(tm, dbsession, logged_in, data_manager) as tracks: archive = testapp.get( - route_path('track-archive', _query=[("track_id[]", "1"), ("track_id[]", "2")]) + route_path('track-archive', _query=[("track_id[]", tracks[0]), ("track_id[]", tracks[1])]) ) result = io.BytesIO(archive.body) -- cgit v1.2.3 From 77c58903358cfcca8983b8301d0c14bd2ba73f19 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sun, 8 Jun 2025 01:33:01 +0200 Subject: fix filenames in archive test --- tests/integration/test_browse.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/integration/test_browse.py b/tests/integration/test_browse.py index 412b042..6913479 100644 --- a/tests/integration/test_browse.py +++ b/tests/integration/test_browse.py @@ -91,5 +91,5 @@ def test_archive(testapp, dbsession, route_path, logged_in, tm, data_manager): with zipfile.ZipFile(result, 'r') as zipped: assert len(zipped.namelist()) == 2 - assert "track_1.gpx" in zipped.namelist() - assert "track_2.gpx" in zipped.namelist() + assert f"track_{tracks[0]}.gpx" in zipped.namelist() + assert f"track_{tracks[1]}.gpx" in zipped.namelist() -- cgit v1.2.3 From c781fdfd19b5c975ebf52821ab12f0a569ba7a4e Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 10 Jun 2025 20:29:08 +0200 Subject: fix lints --- tests/bootstrap/test_new_instance.py | 1 - tests/integration/test_browse.py | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/bootstrap/test_new_instance.py b/tests/bootstrap/test_new_instance.py index 7686fde..05076f4 100644 --- a/tests/bootstrap/test_new_instance.py +++ b/tests/bootstrap/test_new_instance.py @@ -117,4 +117,3 @@ def test_setup_via_fietsupdate(tmpdir): # Clean up the database. This is important with anything but SQLite, as # the tables would otherwise persist and interfere with the other tests. cleanup_database(Path("testing.ini")) - diff --git a/tests/integration/test_browse.py b/tests/integration/test_browse.py index 6913479..46ec329 100644 --- a/tests/integration/test_browse.py +++ b/tests/integration/test_browse.py @@ -85,7 +85,10 @@ def test_archive(testapp, dbsession, route_path, logged_in, tm, data_manager): # pylint: disable=too-many-positional-arguments with added_tracks(tm, dbsession, logged_in, data_manager) as tracks: archive = testapp.get( - route_path('track-archive', _query=[("track_id[]", tracks[0]), ("track_id[]", tracks[1])]) + route_path( + 'track-archive', + _query=[("track_id[]", tracks[0]), ("track_id[]", tracks[1])], + ) ) result = io.BytesIO(archive.body) -- cgit v1.2.3