Skip to content

Commit 266767f

Browse files
committed
Add tests for Coupon API
1 parent 7f7e722 commit 266767f

File tree

1 file changed

+203
-0
lines changed

1 file changed

+203
-0
lines changed

tests/v3/messaging/test_coupon.py

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
import json
2+
import unittest
3+
from urllib.parse import urlparse, parse_qs
4+
from pytest_httpserver import HTTPServer
5+
from linebot.v3.messaging import (
6+
Configuration,
7+
ApiClient,
8+
MessagingApi,
9+
CouponCreateRequest,
10+
LotteryAcquisitionConditionRequest,
11+
CouponDiscountRewardRequest,
12+
DiscountFixedPriceInfoRequest,
13+
CouponMessage
14+
)
15+
16+
class TestCouponAPI(unittest.TestCase):
17+
18+
def test_coupon_create(self):
19+
expected_coupon_id = "COUPON123"
20+
expected_response_create = {
21+
"couponId": expected_coupon_id
22+
}
23+
expected_body = {
24+
"acquisitionCondition": {
25+
"type": "lottery",
26+
"lotteryProbability": 50,
27+
"maxAcquireCount": 1000
28+
},
29+
"barcodeImageUrl": "https://example.com/barcode.png",
30+
"couponCode": "UNIQUECODE123",
31+
"description": "Get 100 Yen off your purchase",
32+
"endTimestamp": 1700000000,
33+
"imageUrl": "https://example.com/image.png",
34+
"maxUseCountPerTicket": 1,
35+
"startTimestamp": 1600000000,
36+
"title": "100 Yen OFF",
37+
"usageCondition": "Minimum purchase of 500 Yen",
38+
"reward": {
39+
"type": "discount",
40+
"priceInfo": {
41+
"type": "fixed",
42+
"fixedAmount": 100
43+
}
44+
},
45+
"visibility": "PUBLIC",
46+
"timezone": "ASIA_TOKYO"
47+
}
48+
49+
with HTTPServer() as httpserver:
50+
httpserver.expect_request(
51+
uri="/v2/bot/coupon",
52+
method="POST",
53+
).respond_with_json(
54+
expected_response_create,
55+
status=200
56+
)
57+
58+
configuration = Configuration(
59+
access_token="dummy-channel-access-token",
60+
host=httpserver.url_for("/")
61+
)
62+
63+
with ApiClient(configuration) as api_client:
64+
line_bot_api = MessagingApi(api_client)
65+
66+
req = CouponCreateRequest(
67+
acquisitionCondition=LotteryAcquisitionConditionRequest(
68+
lotteryProbability=50,
69+
maxAcquireCount=1000
70+
),
71+
barcodeImageUrl="https://example.com/barcode.png",
72+
couponCode="UNIQUECODE123",
73+
description="Get 100 Yen off your purchase",
74+
endTimestamp=1700000000,
75+
imageUrl="https://example.com/image.png",
76+
maxUseCountPerTicket=1,
77+
startTimestamp=1600000000,
78+
title="100 Yen OFF",
79+
usageCondition="Minimum purchase of 500 Yen",
80+
reward=CouponDiscountRewardRequest(
81+
priceInfo=DiscountFixedPriceInfoRequest(
82+
fixedAmount=100
83+
)
84+
),
85+
visibility="PUBLIC",
86+
timezone="ASIA_TOKYO"
87+
)
88+
89+
response = line_bot_api.create_coupon(coupon_create_request=req)
90+
91+
self.assertEqual(response.coupon_id, expected_coupon_id)
92+
self.assertEqual(len(httpserver.log), 1)
93+
94+
request, _ = httpserver.log[0]
95+
got_body = json.loads(request.data.decode('utf-8'))
96+
self.assertEqual(got_body, expected_body)
97+
98+
def test_coupon_close(self):
99+
expected_coupon_id = "COUPON123"
100+
101+
with HTTPServer() as httpserver:
102+
httpserver.expect_request(
103+
uri=f"/v2/bot/coupon/{expected_coupon_id}/close",
104+
method="PUT",
105+
).respond_with_json({}, status=200)
106+
107+
configuration = Configuration(
108+
access_token="dummy-channel-access-token",
109+
host=httpserver.url_for("/")
110+
)
111+
112+
with ApiClient(configuration) as api_client:
113+
line_bot_api = MessagingApi(api_client)
114+
response = line_bot_api.close_coupon(expected_coupon_id)
115+
116+
self.assertIsNone(response)
117+
self.assertEqual(len(httpserver.log), 1)
118+
119+
def test_list_coupon(self):
120+
expected_response_list = {
121+
"items": [
122+
{"couponId": "COUPON123", "title": "Discount Coupon"},
123+
{"couponId": "COUPON456", "title": "Special Offer"}
124+
],
125+
"next": "nextPageToken"
126+
}
127+
128+
with HTTPServer() as httpserver:
129+
httpserver.expect_request(
130+
uri="/v2/bot/coupon",
131+
method="GET",
132+
).respond_with_json(
133+
expected_response_list,
134+
status=200
135+
)
136+
137+
configuration = Configuration(
138+
access_token="dummy-channel-access-token",
139+
host=httpserver.url_for("/")
140+
)
141+
142+
with ApiClient(configuration) as api_client:
143+
line_bot_api = MessagingApi(api_client)
144+
response = line_bot_api.list_coupon(
145+
status=["RUNNING", "CLOSED"],
146+
limit=10,
147+
)
148+
149+
self.assertEqual(len(response.items), 2)
150+
self.assertEqual(response.items[0].coupon_id, "COUPON123")
151+
self.assertEqual(response.items[0].title, "Discount Coupon")
152+
self.assertEqual(response.items[1].coupon_id, "COUPON456")
153+
self.assertEqual(response.items[1].title, "Special Offer")
154+
self.assertEqual(response.next, expected_response_list["next"])
155+
self.assertEqual(len(httpserver.log), 1)
156+
157+
request, _ = httpserver.log[0]
158+
parsed_url = urlparse(request.url)
159+
query_params = parse_qs(parsed_url.query)
160+
161+
self.assertIn("status", query_params)
162+
self.assertIn("limit", query_params)
163+
self.assertEqual(query_params["status"], ["RUNNING", "CLOSED"])
164+
self.assertEqual(query_params["limit"], ["10"])
165+
166+
def test_get_coupon_detail(self):
167+
expected_coupon_id = "COUPON123"
168+
expected_response_detail = {
169+
"couponId": expected_coupon_id,
170+
"title": "Discount Coupon",
171+
"status": "RUNNING"
172+
}
173+
174+
with HTTPServer() as httpserver:
175+
httpserver.expect_request(
176+
uri=f"/v2/bot/coupon/{expected_coupon_id}",
177+
method="GET",
178+
).respond_with_json(
179+
expected_response_detail,
180+
status=200
181+
)
182+
183+
configuration = Configuration(
184+
access_token="dummy-channel-access-token",
185+
host=httpserver.url_for("/")
186+
)
187+
188+
with ApiClient(configuration) as api_client:
189+
line_bot_api = MessagingApi(api_client)
190+
response = line_bot_api.get_coupon_detail(expected_coupon_id)
191+
192+
self.assertEqual(response.coupon_id, expected_coupon_id)
193+
self.assertEqual(response.title, expected_response_detail["title"])
194+
self.assertEqual(len(httpserver.log), 1)
195+
196+
def test_coupon_message(self):
197+
expected_coupon_id = "COUPON123"
198+
coupon_message = CouponMessage(couponId=expected_coupon_id)
199+
self.assertEqual(coupon_message.to_dict()["couponId"], expected_coupon_id)
200+
self.assertEqual(coupon_message.to_dict()["type"], "coupon")
201+
202+
if __name__ == '__main__':
203+
unittest.main()

0 commit comments

Comments
 (0)