aboutsummaryrefslogtreecommitdiff
path: root/tests/integration/test_pdf.py
blob: 29cda02a7ea3530977c8926100e681d16c8932a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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