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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
import io
import zipfile
from contextlib import contextmanager
from datetime import datetime
from testutils import load_gpx_asset
from fietsboek import models
from fietsboek.models.track import Visibility
@contextmanager
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
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()
tracks = []
track_ids = []
with tm:
track = models.Track(
owner=owner,
title="Foobar",
visibility=Visibility.PUBLIC,
description="A foo'd track",
badges=[],
link_secret="foobar",
tagged_people=[],
)
track.date = datetime(2022, 3, 14, 9, 26, 54)
dbsession.add(track)
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,
title="Barfoo",
visibility=Visibility.PUBLIC,
description="A bar'd track",
badges=[],
link_secret="barfoo",
tagged_people=[],
)
track.date = datetime(2022, 10, 29, 13, 37, 11)
dbsession.add(track)
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 track_ids
finally:
tm.abort()
with tm:
for track in tracks:
dbsession.delete(track)
tm.begin()
tm.doom()
@contextmanager
def a_lot_of_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
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()
gpx_data = load_gpx_asset("MyTourbook_1.gpx.gz")
tracks = []
track_ids = []
with tm:
for index in range(50):
track = models.Track(
owner=owner,
title=f"Traxi {index}",
visibility=Visibility.PUBLIC,
description="One of many",
badges=[],
link_secret="foobar",
tagged_people=[],
)
track.date = datetime(2022 - index, 3, 14, 9, 26, 59)
dbsession.add(track)
dbsession.flush()
data_manager.initialize(track.id).compress_gpx(gpx_data)
tracks.append(track)
track_ids.append(track.id)
tm.begin()
tm.doom()
try:
yield track_ids
finally:
tm.abort()
with tm:
for track in tracks:
dbsession.delete(track)
tm.begin()
tm.doom()
def test_browse(testapp, dbsession, route_path, logged_in, tm, data_manager):
# pylint: disable=too-many-positional-arguments
# Ensure there are some tracks in the database
with added_tracks(tm, dbsession, logged_in, data_manager):
# Now go to the browse page
browse = testapp.get(route_path('browse'))
assert "Foobar" in browse.text
assert "Barfoo" in browse.text
def test_browse_paged(testapp, dbsession, route_path, logged_in, tm, data_manager):
# pylint: disable=too-many-positional-arguments
with a_lot_of_tracks(tm, dbsession, logged_in, data_manager):
page_1 = testapp.get(route_path("browse", _query=[("page", 1)]))
assert "Traxi 0" in page_1.text
assert "Traxi 10" in page_1.text
assert "Traxi 20" not in page_1.text
assert "Traxi 30" not in page_1.text
assert "Traxi 40" not in page_1.text
page_2 = testapp.get(route_path("browse", _query=[("page", 2)]))
assert "Traxi 0" not in page_2.text
assert "Traxi 10" not in page_2.text
assert "Traxi 20" in page_2.text
assert "Traxi 30" in page_2.text
assert "Traxi 40" not in page_2.text
page_3 = testapp.get(route_path("browse", _query=[("page", 3)]))
assert "Traxi 0" not in page_3.text
assert "Traxi 10" not in page_3.text
assert "Traxi 20" not in page_3.text
assert "Traxi 30" not in page_3.text
assert "Traxi 40" in page_3.text
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])],
)
)
result = io.BytesIO(archive.body)
with zipfile.ZipFile(result, 'r') as zipped:
assert len(zipped.namelist()) == 2
assert f"track_{tracks[0]}.gpx" in zipped.namelist()
assert f"track_{tracks[1]}.gpx" in zipped.namelist()
|