forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_coupon.py
136 lines (116 loc) · 4.56 KB
/
test_coupon.py
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
from copy import deepcopy
from decimal import Decimal
import pytest
from django.test.testcases import TestCase
from djstripe.models import Coupon
from . import FAKE_COUPON
from .conftest import CreateAccountMixin
pytestmark = pytest.mark.django_db
class TransferTest(CreateAccountMixin, TestCase):
def test_retrieve_coupon(self):
coupon_data = deepcopy(FAKE_COUPON)
coupon = Coupon.sync_from_stripe_data(coupon_data)
self.assertEqual(coupon.id, FAKE_COUPON["id"])
class CouponTest(TestCase):
def test_blank_coupon_str(self):
coupon = Coupon()
self.assertEqual(str(coupon).strip(), "(invalid amount) off once")
def test___str__(self):
coupon = Coupon.objects.create(
id="coupon-test-amount-off-forever",
amount_off=10,
currency="usd",
duration="forever",
name="Test coupon",
)
self.assertEqual(str(coupon), "Test coupon")
def test_human_readable_usd_off_forever(self):
coupon = Coupon.objects.create(
id="coupon-test-amount-off-forever",
amount_off=10,
currency="usd",
duration="forever",
)
self.assertEqual(coupon.human_readable, "$10.00 USD off forever")
self.assertEqual(str(coupon), coupon.human_readable)
def test_human_readable_eur_off_forever(self):
coupon = Coupon.objects.create(
id="coupon-test-amount-off-forever",
amount_off=10,
currency="eur",
duration="forever",
)
self.assertEqual(coupon.human_readable, "€10.00 EUR off forever")
self.assertEqual(str(coupon), coupon.human_readable)
def test_human_readable_percent_off_forever(self):
coupon = Coupon.objects.create(
id="coupon-test-percent-off-forever",
percent_off=10.25,
currency="usd",
duration="forever",
)
self.assertEqual(coupon.human_readable, "10.25% off forever")
self.assertEqual(str(coupon), coupon.human_readable)
def test_human_readable_percent_off_once(self):
coupon = Coupon.objects.create(
id="coupon-test-percent-off-once",
percent_off=10.25,
currency="usd",
duration="once",
)
self.assertEqual(coupon.human_readable, "10.25% off once")
self.assertEqual(str(coupon), coupon.human_readable)
def test_human_readable_percent_off_one_month(self):
coupon = Coupon.objects.create(
id="coupon-test-percent-off-1month",
percent_off=10.25,
currency="usd",
duration="repeating",
duration_in_months=1,
)
self.assertEqual(coupon.human_readable, "10.25% off for 1 month")
self.assertEqual(str(coupon), coupon.human_readable)
def test_human_readable_percent_off_three_months(self):
coupon = Coupon.objects.create(
id="coupon-test-percent-off-3month",
percent_off=10.25,
currency="usd",
duration="repeating",
duration_in_months=3,
)
self.assertEqual(coupon.human_readable, "10.25% off for 3 months")
self.assertEqual(str(coupon), coupon.human_readable)
def test_human_readable_integer_percent_off_forever(self):
coupon = Coupon.objects.create(
id="coupon-test-percent-off-forever",
percent_off=10,
currency="usd",
duration="forever",
)
self.assertEqual(coupon.human_readable, "10% off forever")
self.assertEqual(str(coupon), coupon.human_readable)
class TestCouponDecimal(CreateAccountMixin):
@pytest.mark.parametrize(
"inputted,expected",
[
(Decimal("1"), Decimal("1.00")),
(Decimal("1.5234567"), Decimal("1.52")),
(Decimal("0"), Decimal("0.00")),
(Decimal("23.2345678"), Decimal("23.23")),
("1", Decimal("1.00")),
("1.5234567", Decimal("1.52")),
("0", Decimal("0.00")),
("23.2345678", Decimal("23.23")),
(1, Decimal("1.00")),
(1.5234567, Decimal("1.52")),
(0, Decimal("0.00")),
(23.2345678, Decimal("23.24")),
],
)
def test_decimal_percent_off_coupon(self, inputted, expected):
fake_coupon = deepcopy(FAKE_COUPON)
fake_coupon["percent_off"] = inputted
coupon = Coupon.sync_from_stripe_data(fake_coupon)
field_data = coupon.percent_off
assert isinstance(field_data, Decimal)
assert field_data == expected