Skip to content

Commit f242f6c

Browse files
authored
Merge pull request #13 from nnnLik/feature/get-coupons-status
Add PromoOffersRevocationStatus class and corresponding tests
2 parents 00befea + 8474f40 commit f242f6c

File tree

2 files changed

+146
-1
lines changed

2 files changed

+146
-1
lines changed

admitad/items/promo_offers.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
__all__ = [
55
'PromoOffersForCampaign',
66
'PromoOfferRequestTrackingCode',
7+
'PromoOffersRevocationStatus',
78
]
89

910

@@ -107,3 +108,39 @@ def get(self, request_id: int) -> dict:
107108
.set_data(request_data)
108109
.request(url=self.GET_STATUS_URL)
109110
)
111+
112+
113+
class PromoOffersRevocationStatus(Item):
114+
"""
115+
Get revocation status of promo codes for specific promo offer
116+
"""
117+
118+
SCOPE = 'public_data'
119+
120+
REVOCATION_STATUS_URL = Item.prepare_url('promo_offers/revocation_status/%(pk)s')
121+
122+
def get(self, _id: int, website_id: int) -> dict:
123+
"""
124+
Args:
125+
_id (int): promo offer id
126+
website_id (int): website id
127+
128+
Returns:
129+
list: Revocation status of promo codes that have history for this website
130+
[
131+
{"promocode_value": "promocode1", "status": "active"},
132+
{"promocode_value": "promocode2", "status": "revoked"},
133+
...
134+
]
135+
"""
136+
request_data = {
137+
'url': self.REVOCATION_STATUS_URL,
138+
'pk': Item.sanitize_id(_id),
139+
'website_id': Item.sanitize_id(website_id)
140+
}
141+
142+
return (
143+
self.transport.get()
144+
.set_data(request_data)
145+
.request(url=self.REVOCATION_STATUS_URL, pk=Item.sanitize_id(_id))
146+
)

admitad/tests/test_promo_offers.py

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import responses
33

44
from admitad.exceptions import HttpException
5-
from admitad.items import PromoOffersForCampaign, PromoOfferRequestTrackingCode
5+
from admitad.items import PromoOffersForCampaign, PromoOfferRequestTrackingCode, PromoOffersRevocationStatus
66
from admitad.tests.base import BaseTestCase
77

88

@@ -171,5 +171,113 @@ def test_get_tracking_promo_code_request_invalid_id(self) -> None:
171171
self.client.PromoOfferRequestTrackingCode.get(request_id=0)
172172

173173

174+
class PromoOffersRevocationStatusTestCase(BaseTestCase):
175+
176+
def test_get_promo_offers_revocation_status_success_multiple_codes(self) -> None:
177+
with responses.RequestsMock() as resp:
178+
resp.add(
179+
resp.GET,
180+
self.prepare_url(PromoOffersRevocationStatus.REVOCATION_STATUS_URL, pk=123, params={
181+
'website_id': 456
182+
}),
183+
match_querystring=True,
184+
json=[
185+
{"promocode_value": "SAVE20", "status": "active"},
186+
{"promocode_value": "DISCOUNT15", "status": "revoked"},
187+
{"promocode_value": "PROMO50", "status": "active"}
188+
],
189+
status=200
190+
)
191+
result = self.client.PromoOffersRevocationStatus.get(
192+
_id=123,
193+
website_id=456
194+
)
195+
196+
self.assertEqual(len(result), 3)
197+
self.assertEqual(result[0]['promocode_value'], 'SAVE20')
198+
self.assertEqual(result[0]['status'], 'active')
199+
self.assertEqual(result[1]['promocode_value'], 'DISCOUNT15')
200+
self.assertEqual(result[1]['status'], 'revoked')
201+
self.assertEqual(result[2]['promocode_value'], 'PROMO50')
202+
self.assertEqual(result[2]['status'], 'active')
203+
204+
def test_get_promo_offers_revocation_status_empty_response(self) -> None:
205+
with responses.RequestsMock() as resp:
206+
resp.add(
207+
resp.GET,
208+
self.prepare_url(PromoOffersRevocationStatus.REVOCATION_STATUS_URL, pk=123, params={
209+
'website_id': 456
210+
}),
211+
match_querystring=True,
212+
json=[],
213+
status=200
214+
)
215+
result = self.client.PromoOffersRevocationStatus.get(
216+
_id=123,
217+
website_id=456
218+
)
219+
220+
self.assertEqual(result, [])
221+
222+
def test_get_promo_offers_revocation_status_missing_website_id(self) -> None:
223+
with responses.RequestsMock() as resp:
224+
resp.add(
225+
resp.GET,
226+
self.prepare_url(PromoOffersRevocationStatus.REVOCATION_STATUS_URL, pk=123),
227+
json={"error": "Missing required parameter: website_id"},
228+
status=400
229+
)
230+
with self.assertRaises(HttpException):
231+
self.client.PromoOffersRevocationStatus.get(_id=123)
232+
233+
def test_get_promo_offers_revocation_status_invalid_website_id(self) -> None:
234+
with responses.RequestsMock() as resp:
235+
resp.add(
236+
resp.GET,
237+
self.prepare_url(PromoOffersRevocationStatus.REVOCATION_STATUS_URL, pk=123, params={
238+
'website_id': 'invalid'
239+
}),
240+
match_querystring=True,
241+
json={"error": "website_id must be an integer"},
242+
status=400
243+
)
244+
with self.assertRaises(HttpException):
245+
self.client.PromoOffersRevocationStatus.get(
246+
_id=123,
247+
website_id='invalid'
248+
)
249+
250+
def test_get_promo_offers_revocation_status_offer_not_found(self) -> None:
251+
with responses.RequestsMock() as resp:
252+
resp.add(
253+
resp.GET,
254+
self.prepare_url(PromoOffersRevocationStatus.REVOCATION_STATUS_URL, pk=999, params={
255+
'website_id': 456
256+
}),
257+
match_querystring=True,
258+
json={"error": "Promo offer not found or has no active promo codes"},
259+
status=404
260+
)
261+
with self.assertRaises(HttpException):
262+
self.client.PromoOffersRevocationStatus.get(
263+
_id=999,
264+
website_id=456
265+
)
266+
267+
def test_get_promo_offers_revocation_status_invalid_offer_id(self) -> None:
268+
with self.assertRaises(ValueError):
269+
self.client.PromoOffersRevocationStatus.get(
270+
_id=0,
271+
website_id=456
272+
)
273+
274+
def test_get_promo_offers_revocation_status_invalid_website_id_zero(self) -> None:
275+
with self.assertRaises(ValueError):
276+
self.client.PromoOffersRevocationStatus.get(
277+
_id=123,
278+
website_id=0
279+
)
280+
281+
174282
if __name__ == '__main__':
175283
unittest.main()

0 commit comments

Comments
 (0)