From f431d0d82ddfd38f9a7e53042070a7e49ee67019 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 15 Dec 2022 23:23:14 +0100 Subject: fix integration & playwright tests --- tests/conftest.py | 24 +++++++++++++++++++++--- tests/integration/test_browse.py | 18 +++++++++++------- tests/playwright/test_basic.py | 17 +++++++++-------- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 8f7f77c..a203775 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,10 @@ +import os +import shutil +from pathlib import Path + import alembic import alembic.config import alembic.command -import os from pyramid.paster import get_appsettings from pyramid.scripting import prepare from pyramid.testing import DummyRequest, testConfig @@ -11,8 +14,8 @@ import webtest from sqlalchemy import delete, select -from fietsboek import main -from fietsboek import models +from fietsboek import main, models +from fietsboek.data import DataManager from fietsboek.models.meta import Base @@ -48,6 +51,21 @@ def dbengine(app_settings, ini_file): Base.metadata.drop_all(bind=engine) alembic.command.stamp(alembic_cfg, None, purge=True) +@pytest.fixture +def data_manager(app_settings): + return DataManager(Path(app_settings["fietsboek.data_dir"])) + +@pytest.fixture(autouse=True) +def _cleanup_data(app_settings): + yield + engine = models.get_engine(app_settings) + connection = engine.connect() + 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") + @pytest.fixture(scope='session') def app(app_settings, dbengine, tmp_path_factory): app_settings["fietsboek.data_dir"] = str(tmp_path_factory.mktemp("data")) diff --git a/tests/integration/test_browse.py b/tests/integration/test_browse.py index 493239f..603f301 100644 --- a/tests/integration/test_browse.py +++ b/tests/integration/test_browse.py @@ -2,14 +2,16 @@ import io import zipfile from contextlib import contextmanager from datetime import datetime +from pathlib import Path from testutils import load_gpx_asset from fietsboek import models from fietsboek.models.track import Visibility +from fietsboek.data import DataManager @contextmanager -def added_tracks(tm, dbsession, owner): +def added_tracks(tm, dbsession, owner, data_manager): """Adds some tracks to the database session. This function should be used as a context manager and it ensures that the @@ -33,8 +35,9 @@ def added_tracks(tm, dbsession, owner): tagged_people=[], ) track.date = datetime(2022, 3, 14, 9, 26, 54) - track.gpx_data = load_gpx_asset("MyTourbook_1.gpx.gz") dbsession.add(track) + dbsession.flush() + data_manager.initialize(track.id).compress_gpx(load_gpx_asset("MyTourbook_1.gpx.gz")) tracks.append(track) track = models.Track( @@ -47,8 +50,9 @@ def added_tracks(tm, dbsession, owner): tagged_people=[], ) track.date = datetime(2022, 10, 29, 13, 37, 11) - track.gpx_data = load_gpx_asset("Teasi_1.gpx.gz") dbsession.add(track) + dbsession.flush() + data_manager.initialize(track.id).compress_gpx(load_gpx_asset("Teasi_1.gpx.gz")) tracks.append(track) tm.begin() @@ -65,9 +69,9 @@ def added_tracks(tm, dbsession, owner): tm.doom() -def test_browse(testapp, dbsession, route_path, logged_in, tm): +def test_browse(testapp, dbsession, route_path, logged_in, tm, data_manager): # Ensure there are some tracks in the database - with added_tracks(tm, dbsession, logged_in): + with added_tracks(tm, dbsession, logged_in, data_manager): # Now go to the browse page browse = testapp.get(route_path('browse')) @@ -75,8 +79,8 @@ def test_browse(testapp, dbsession, route_path, logged_in, tm): assert "Barfoo" in browse.text -def test_archive(testapp, dbsession, route_path, logged_in, tm): - with added_tracks(tm, dbsession, logged_in): +def test_archive(testapp, dbsession, route_path, logged_in, tm, data_manager): + with added_tracks(tm, dbsession, logged_in, data_manager): archive = testapp.get( route_path('track-archive', _query=[("track_id[]", "1"), ("track_id[]", "2")]) ) diff --git a/tests/playwright/test_basic.py b/tests/playwright/test_basic.py index b3e340d..f2031d2 100644 --- a/tests/playwright/test_basic.py +++ b/tests/playwright/test_basic.py @@ -18,15 +18,13 @@ def john_doe(dbaccess): This fixture either returns the existing John or creates a new one. """ - query = models.User.query_by_email("john@doe.com") - result = dbaccess.execute(query).scalar_one_or_none() - if result: - return result with dbaccess: user = models.User(name="John Doe", email="john@doe.com", is_verified=True) user.set_password("password") dbaccess.add(user) dbaccess.commit() + dbaccess.refresh(user, ["id"]) + dbaccess.expunge(user) return user @@ -113,19 +111,20 @@ def test_upload(page: Page, john_doe, app_settings, tmp_path, dbaccess): assert track.description == "Beschreibung der tollen Tour" -def test_edit(page: Page, john_doe, app_settings, dbaccess): +def test_edit(page: Page, john_doe, app_settings, dbaccess, data_manager): do_login(app_settings, page, john_doe) with dbaccess: + john_doe = dbaccess.merge(john_doe) track = models.Track( title="Another awesome track", visibility=Visibility.PRIVATE, description="Another description", ) track.date = datetime.datetime.now(datetime.timezone.utc) - track.gpx_data = load_gpx_asset("Teasi_1.gpx.gz") john_doe.tracks.append(track) dbaccess.flush() track_id = track.id + data_manager.initialize(track_id).compress_gpx(load_gpx_asset("Teasi_1.gpx.gz")) dbaccess.commit() page.goto(f"/track/{track_id}") @@ -152,9 +151,10 @@ def test_edit(page: Page, john_doe, app_settings, dbaccess): assert track.description == "Not so descriptive anymore" -def test_browse(page: Page, john_doe, app_settings, dbaccess): +def test_browse(page: Page, john_doe, app_settings, dbaccess, data_manager): do_login(app_settings, page, john_doe) with dbaccess: + john_doe = dbaccess.merge(john_doe) track = models.Track( title="We're looking for this track", visibility=Visibility.PRIVATE, @@ -162,8 +162,9 @@ def test_browse(page: Page, john_doe, app_settings, dbaccess): type=TrackType.ORGANIC, ) track.date = datetime.datetime.now(datetime.timezone.utc) - track.gpx_data = load_gpx_asset("Teasi_1.gpx.gz") john_doe.tracks.append(track) + dbaccess.flush() + data_manager.initialize(track.id).compress_gpx(load_gpx_asset("Teasi_1.gpx.gz")) dbaccess.commit() page.goto("/") -- cgit v1.2.3