from contextlib import contextmanager from datetime import datetime from testutils import load_gpx_asset from fietsboek import convert, models from fietsboek.models.track import Visibility @contextmanager def a_track(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 added tracks are deleted again after the test, to make a clean slate for the next test. """ # The normal transaction is "doomed", so we need to abort it, start a fresh # one, and then explicitely commit it, otherwise we will not persist the # objects to the database. tm.abort() with tm: track = models.Track( owner=owner, title="Goober", visibility=Visibility.PUBLIC, description="A bar'd track", badges=[], link_secret="raboof", tagged_people=[], ) track.date = datetime(2027, 3, 14, 9, 26, 54) track.set_path(convert.smart_convert(load_gpx_asset("MyTourbook_1.gpx.gz")).path()) dbsession.add(track) dbsession.flush() data_manager.initialize(track.id) track_id = track.id tm.begin() tm.doom() try: yield track_id finally: tm.abort() with tm: dbsession.delete(track) data_manager.purge(track_id) tm.begin() tm.doom() def test_pdf(testapp, dbsession, route_path, logged_in, tm, data_manager): # pylint: disable=too-many-positional-arguments # Ensure there are some tracks in the database with a_track(tm, dbsession, logged_in, data_manager) as track_id: pdf = testapp.get(route_path("track-pdf", track_id=track_id)) assert pdf