forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_price.py
271 lines (217 loc) · 9.8 KB
/
test_price.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"""
dj-stripe Price model tests
"""
from copy import deepcopy
from unittest.mock import patch
import pytest
import stripe
from django.test import TestCase
from djstripe.enums import PriceType, PriceUsageType
from djstripe.models import Price, Product, Subscription
from djstripe.settings import djstripe_settings
from . import (
FAKE_PRICE,
FAKE_PRICE_METERED,
FAKE_PRICE_ONETIME,
FAKE_PRICE_TIER,
FAKE_PRODUCT,
AssertStripeFksMixin,
)
pytestmark = pytest.mark.django_db
class PriceCreateTest(AssertStripeFksMixin, TestCase):
def setUp(self):
with patch(
"stripe.Product.retrieve",
return_value=deepcopy(FAKE_PRODUCT),
autospec=True,
):
self.stripe_product = Product(id=FAKE_PRODUCT["id"]).api_retrieve()
@patch(
"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
)
@patch("stripe.Price.create", return_value=deepcopy(FAKE_PRICE), autospec=True)
def test_create_from_product_id(self, price_create_mock, product_retrieve_mock):
fake_price = deepcopy(FAKE_PRICE)
fake_price["unit_amount"] /= 100
assert isinstance(fake_price["product"], str)
price = Price.create(**fake_price)
expected_create_kwargs = deepcopy(FAKE_PRICE)
expected_create_kwargs["api_key"] = djstripe_settings.STRIPE_SECRET_KEY
price_create_mock.assert_called_once_with(**expected_create_kwargs)
self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})
@patch(
"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
)
@patch("stripe.Price.create", return_value=deepcopy(FAKE_PRICE), autospec=True)
def test_create_from_stripe_product(self, price_create_mock, product_retrieve_mock):
fake_price = deepcopy(FAKE_PRICE)
fake_price["product"] = self.stripe_product
fake_price["unit_amount"] /= 100
assert isinstance(fake_price["product"], dict)
price = Price.create(**fake_price)
expected_create_kwargs = deepcopy(FAKE_PRICE)
expected_create_kwargs["product"] = self.stripe_product
price_create_mock.assert_called_once_with(
api_key=djstripe_settings.STRIPE_SECRET_KEY, **expected_create_kwargs
)
self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})
@patch(
"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
)
@patch("stripe.Price.create", return_value=deepcopy(FAKE_PRICE), autospec=True)
def test_create_from_djstripe_product(
self, price_create_mock, product_retrieve_mock
):
fake_price = deepcopy(FAKE_PRICE)
fake_price["product"] = Product.sync_from_stripe_data(self.stripe_product)
fake_price["unit_amount"] /= 100
assert isinstance(fake_price["product"], Product)
price = Price.create(**fake_price)
price_create_mock.assert_called_once_with(
api_key=djstripe_settings.STRIPE_SECRET_KEY, **FAKE_PRICE
)
self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})
@patch(
"stripe.Product.retrieve", return_value=deepcopy(FAKE_PRODUCT), autospec=True
)
@patch("stripe.Price.create", return_value=deepcopy(FAKE_PRICE), autospec=True)
def test_create_with_metadata(self, price_create_mock, product_retrieve_mock):
metadata = {"other_data": "more_data"}
fake_price = deepcopy(FAKE_PRICE)
fake_price["unit_amount"] /= 100
fake_price["metadata"] = metadata
assert isinstance(fake_price["product"], str)
price = Price.create(**fake_price)
expected_create_kwargs = deepcopy(FAKE_PRICE)
expected_create_kwargs["metadata"] = metadata
price_create_mock.assert_called_once_with(
api_key=djstripe_settings.STRIPE_SECRET_KEY, **expected_create_kwargs
)
self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})
class PriceTest(AssertStripeFksMixin, TestCase):
def setUp(self):
self.price_data = deepcopy(FAKE_PRICE)
with patch(
"stripe.Product.retrieve",
return_value=deepcopy(FAKE_PRODUCT),
autospec=True,
):
self.price = Price.sync_from_stripe_data(self.price_data)
@patch("stripe.Price.retrieve", return_value=FAKE_PRICE, autospec=True)
def test_stripe_price(self, price_retrieve_mock):
stripe_price = self.price.api_retrieve()
price_retrieve_mock.assert_called_once_with(
id=self.price_data["id"],
api_key=djstripe_settings.STRIPE_SECRET_KEY,
expand=["product", "tiers"],
stripe_account=self.price.djstripe_owner_account.id,
)
price = Price.sync_from_stripe_data(stripe_price)
self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})
assert price.human_readable_price == "$20.00 USD/month"
@patch("stripe.Price.retrieve", autospec=True)
def test_stripe_tier_price(self, price_retrieve_mock):
price_data = deepcopy(FAKE_PRICE_TIER)
price = Price.sync_from_stripe_data(price_data)
assert price.id == price_data["id"]
assert price.unit_amount is None
assert price.tiers is not None
self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})
@patch("stripe.Price.retrieve", autospec=True)
def test_stripe_metered_price(self, price_retrieve_mock):
price_data = deepcopy(FAKE_PRICE_METERED)
price = Price.sync_from_stripe_data(price_data)
assert price.id == price_data["id"]
assert price.recurring["usage_type"] == PriceUsageType.metered
assert price.unit_amount is not None
self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})
@patch("stripe.Price.retrieve", autospec=True)
def test_stripe_onetime_price(self, price_retrieve_mock):
price_data = deepcopy(FAKE_PRICE_ONETIME)
price = Price.sync_from_stripe_data(price_data)
assert price.id == price_data["id"]
assert price.unit_amount is not None
assert not price.recurring
assert price.type == PriceType.one_time
self.assert_fks(price, expected_blank_fks={"djstripe.Customer.coupon"})
class TestStrPrice:
@pytest.mark.parametrize(
"fake_price_data",
[
deepcopy(FAKE_PRICE),
deepcopy(FAKE_PRICE_ONETIME),
deepcopy(FAKE_PRICE_TIER),
deepcopy(FAKE_PRICE_METERED),
],
)
def test___str__(self, fake_price_data, monkeypatch):
def mock_product_get(*args, **kwargs):
return deepcopy(FAKE_PRODUCT)
def mock_price_get(*args, **kwargs):
return fake_price_data
# monkeypatch stripe.Product.retrieve and stripe.Price.retrieve calls to return
# the desired json response.
monkeypatch.setattr(stripe.Product, "retrieve", mock_product_get)
monkeypatch.setattr(stripe.Price, "retrieve", mock_price_get)
if not fake_price_data["recurring"]:
price = Price.sync_from_stripe_data(fake_price_data)
assert (f"{price.human_readable_price} for {FAKE_PRODUCT['name']}") == str(
price
)
else:
price = Price.sync_from_stripe_data(fake_price_data)
subscriptions = Subscription.objects.filter(plan__id=price.id).count()
assert (
f"{price.human_readable_price} for {FAKE_PRODUCT['name']} ({subscriptions} subscriptions)"
) == str(price)
class TestHumanReadablePrice:
#
# Helpers
#
def get_fake_price_NONE_flat_amount():
FAKE_PRICE_TIER_NONE_FLAT_AMOUNT = deepcopy(FAKE_PRICE_TIER)
FAKE_PRICE_TIER_NONE_FLAT_AMOUNT["tiers"][0]["flat_amount"] = None
FAKE_PRICE_TIER_NONE_FLAT_AMOUNT["tiers"][0]["flat_amount_decimal"] = None
return FAKE_PRICE_TIER_NONE_FLAT_AMOUNT
def get_fake_price_0_flat_amount():
FAKE_PRICE_TIER_0_FLAT_AMOUNT = deepcopy(FAKE_PRICE_TIER)
FAKE_PRICE_TIER_0_FLAT_AMOUNT["tiers"][0]["flat_amount"] = 0
FAKE_PRICE_TIER_0_FLAT_AMOUNT["tiers"][0]["flat_amount_decimal"] = 0
return FAKE_PRICE_TIER_0_FLAT_AMOUNT
def get_fake_price_0_amount():
FAKE_PRICE_TIER_0_AMOUNT = deepcopy(FAKE_PRICE)
FAKE_PRICE_TIER_0_AMOUNT["unit_amount"] = 0
FAKE_PRICE_TIER_0_AMOUNT["unit_amount_decimal"] = 0
return FAKE_PRICE_TIER_0_AMOUNT
@pytest.mark.parametrize(
"fake_price_data, expected_str",
[
(deepcopy(FAKE_PRICE), "$20.00 USD/month"),
(get_fake_price_0_amount(), "$0.00 USD/month"),
(deepcopy(FAKE_PRICE_ONETIME), "$20.00 USD (one time)"),
(
deepcopy(FAKE_PRICE_TIER),
"Starts at $10.00 USD per unit + $49.00 USD/month",
),
(
get_fake_price_0_flat_amount(),
"Starts at $10.00 USD per unit + $0.00 USD/month",
),
(
get_fake_price_NONE_flat_amount(),
"Starts at $10.00 USD per unit/month",
),
(deepcopy(FAKE_PRICE_METERED), "$2.00 USD/month"),
],
)
def test_human_readable(self, fake_price_data, expected_str, monkeypatch):
def mock_product_get(*args, **kwargs):
return deepcopy(FAKE_PRODUCT)
def mock_price_get(*args, **kwargs):
return fake_price_data
# monkeypatch stripe.Product.retrieve and stripe.Price.retrieve calls to return
# the desired json response.
monkeypatch.setattr(stripe.Product, "retrieve", mock_product_get)
monkeypatch.setattr(stripe.Price, "retrieve", mock_price_get)
price = Price.sync_from_stripe_data(fake_price_data)
assert price.human_readable_price == expected_str