@@ -34,7 +34,35 @@ def orders_pages_generator(url):
3434 )
3535
3636
37- def speaker_pages_generator (url ):
37+ def vouchers_pages_generator (url ):
38+ """
39+ Generator to simulate pagination.
40+
41+ Extracted to a generator because we use it in multiple places
42+ """
43+ yield Response (
44+ 200 ,
45+ json = {
46+ "results" : [
47+ {"hello" : "world" },
48+ ],
49+ "next" : f"{ url } &page=2" ,
50+ },
51+ )
52+
53+ yield Response (
54+ 200 ,
55+ json = {
56+ "results" : [
57+ {"foo" : "bar" },
58+ ],
59+ # It's important to make it last page in tests.
60+ # Otherwise it will be infinite loop :)
61+ "next" : None ,
62+ },
63+ )
64+
65+ def products_pages_generator (url ):
3866 """
3967 Generator to simulate pagination.
4068
@@ -65,7 +93,7 @@ def speaker_pages_generator(url):
6593
6694@respx .mock
6795def test_fetch_orders_from_pretix ():
68- url = "https://tickets.europython.eu/api/v1/organizers/europython/ep2025/orders/"
96+ url = "https://tickets.europython.eu/api/v1/organizers/europython/events/ ep2025/orders/"
6997 data = orders_pages_generator (url )
7098 respx .get (url ).mock (return_value = next (data ))
7199 respx .get (url + "&page=2" ).mock (return_value = next (data ))
@@ -80,11 +108,45 @@ def test_fetch_orders_from_pretix():
80108 {"foo" : "bar" },
81109 ]
82110
111+ @respx .mock
112+ def test_fetch_vouchers_from_pretix ():
113+ url = "https://tickets.europython.eu/api/v1/organizers/europython/events/ep2025/vouchers/"
114+ data = vouchers_pages_generator (url )
115+ respx .get (url ).mock (return_value = next (data ))
116+ respx .get (url + "&page=2" ).mock (return_value = next (data ))
117+
118+ orders = pretix .fetch_pretix_data (
119+ "ep2025" ,
120+ PretixData .PretixResources .vouchers ,
121+ )
122+
123+ assert orders == [
124+ {"hello" : "world" },
125+ {"foo" : "bar" },
126+ ]
127+
128+ @respx .mock
129+ def test_fetch_products_from_pretix ():
130+ url = "https://tickets.europython.eu/api/v1/organizers/europython/events/ep2025/items/"
131+ data = products_pages_generator (url )
132+ respx .get (url ).mock (return_value = next (data ))
133+ respx .get (url + "&page=2" ).mock (return_value = next (data ))
134+
135+ orders = pretix .fetch_pretix_data (
136+ "ep2025" ,
137+ PretixData .PretixResources .products ,
138+ )
139+
140+ assert orders == [
141+ {"hello" : "world" },
142+ {"foo" : "bar" },
143+ ]
144+
83145
84146@respx .mock
85147@pytest .mark .django_db
86148def test_download_latest_orders ():
87- url = "https://tickets.europython.eu/api/v1/organizers/europython/ep2025/orders/"
149+ url = "https://tickets.europython.eu/api/v1/organizers/europython/events/ ep2025/orders/"
88150 data = orders_pages_generator (url )
89151 respx .get (url ).mock (return_value = next (data ))
90152 respx .get (url + "&page=2" ).mock (return_value = next (data ))
@@ -97,3 +159,37 @@ def test_download_latest_orders():
97159 {"hello" : "world" },
98160 {"foo" : "bar" },
99161 ]
162+
163+ @respx .mock
164+ @pytest .mark .django_db
165+ def test_download_latest_products ():
166+ url = "https://tickets.europython.eu/api/v1/organizers/europython/events/ep2025/items/"
167+ data = orders_pages_generator (url )
168+ respx .get (url ).mock (return_value = next (data ))
169+ respx .get (url + "&page=2" ).mock (return_value = next (data ))
170+
171+ pretix .download_latest_products ("ep2025" )
172+
173+ pd = PretixData .objects .get (resource = PretixData .PretixResources .products )
174+ assert pd .resource == "products"
175+ assert pd .content == [
176+ {"hello" : "world" },
177+ {"foo" : "bar" },
178+ ]
179+
180+ @respx .mock
181+ @pytest .mark .django_db
182+ def test_download_latest_vouchers ():
183+ url = "https://tickets.europython.eu/api/v1/organizers/europython/events/ep2025/vouchers/"
184+ data = orders_pages_generator (url )
185+ respx .get (url ).mock (return_value = next (data ))
186+ respx .get (url + "&page=2" ).mock (return_value = next (data ))
187+
188+ pretix .download_latest_vouchers ("ep2025" )
189+
190+ pd = PretixData .objects .get (resource = PretixData .PretixResources .vouchers )
191+ assert pd .resource == "vouchers"
192+ assert pd .content == [
193+ {"hello" : "world" },
194+ {"foo" : "bar" },
195+ ]
0 commit comments