Skip to content

Commit e1e5d4a

Browse files
committed
fix tests and add vouchers and products endpoints
1 parent b5dd81a commit e1e5d4a

File tree

3 files changed

+154
-6
lines changed

3 files changed

+154
-6
lines changed

intbot/core/integrations/pretix.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
def get_event_url(event):
3030
assert event in PRETALX_EVENTS
3131

32-
pretix_url = f"https://tickets.europython.eu"
32+
pretix_url = "https://tickets.europython.eu"
3333
url = f"{pretix_url}/api/v1/organizers/europython/events/{event}/"
3434
return url
3535

@@ -73,9 +73,29 @@ def fetch_pretix_data(
7373
def download_latest_orders(event: str) -> PretixData:
7474
data = fetch_pretix_data(event, PretixData.PretixResources.orders)
7575

76-
pretalx_data = PretixData.objects.create(
76+
pretix_data = PretixData.objects.create(
7777
resource=PretixData.PretixResources.orders,
7878
content=data,
7979
)
8080

81-
return pretalx_data
81+
return pretix_data
82+
83+
def download_latest_products(event: str) -> PretixData:
84+
data = fetch_pretix_data(event, PretixData.PretixResources.products)
85+
86+
pretix_data = PretixData.objects.create(
87+
resource=PretixData.PretixResources.products,
88+
content=data,
89+
)
90+
91+
return pretix_data
92+
93+
def download_latest_vouchers(event: str) -> PretixData:
94+
data = fetch_pretix_data(event, PretixData.PretixResources.vouchers)
95+
96+
pretix_data = PretixData.objects.create(
97+
resource=PretixData.PretixResources.vouchers,
98+
content=data,
99+
)
100+
101+
return pretix_data
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from core.integrations.pretalx import (
2+
PRETIX_EVENTS,
3+
download_latest_orders,
4+
download_latest_products,
5+
download_latest_vouchers,
6+
)
7+
from django.core.management.base import BaseCommand
8+
9+
10+
class Command(BaseCommand):
11+
help = "Downloads latest pretix data"
12+
13+
def add_arguments(self, parser):
14+
# Add keyword argument event
15+
parser.add_argument(
16+
"--event",
17+
choices=PRETIX_EVENTS,
18+
help="slug of the event (for example `ep2025`)",
19+
required=True,
20+
)
21+
22+
def handle(self, **kwargs):
23+
event = kwargs["event"]
24+
25+
self.stdout.write(f"Downloading latest products from pretix... {event}")
26+
download_latest_products(event)
27+
28+
self.stdout.write(f"Downloading latest vouchers from pretix... {event}")
29+
download_latest_vouchers(event)
30+
31+
self.stdout.write(f"Downloading latest orders from pretix... {event}")
32+
download_latest_orders(event)

intbot/tests/test_integrations/test_pretix.py

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
6795
def 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
86148
def 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

Comments
 (0)