Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions appstoreserverlibrary/models/HelperValidationUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ def sku_validator(instance, attribute, value):
@staticmethod
def period_count_validator(instance, attribute, value):
"""
Validates periodCount is not None and between `MIN_PERIOD` and `MAX_PERIOD` inclusive.
Validates periodCount is an integer between `MIN_PERIOD` and `MAX_PERIOD` inclusive.

Raises:
ValueError: If period_count is out of range
ValueError: If period_count is not an integer or is out of range
"""
if (value < HelperValidationUtils.MIN_PERIOD or
if (not isinstance(value, int) or isinstance(value, bool) or
value < HelperValidationUtils.MIN_PERIOD or
value > HelperValidationUtils.MAX_PERIOD):
raise ValueError(
f"Period count must be between "
f"Period count must be an integer between "
f"{HelperValidationUtils.MIN_PERIOD} and "
f"{HelperValidationUtils.MAX_PERIOD}"
)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_advanced_commerce_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,26 @@ def test_validation_utils_period_count(self):
with self.assertRaises(ValueError):
HelperValidationUtils.period_count_validator(None, None, 13)

def test_offer_rejects_non_integer_period_counts(self):
for count in [1.5, 1.0, float('nan'), True, False, None, '1']:
with self.subTest(count=count):
with self.assertRaises(ValueError):
AdvancedCommerceOffer(
periodCount=count, price=1000,
period=AdvancedCommerceOfferPeriod.P1M,
reason=AdvancedCommerceOfferReason.ACQUISITION
)

def test_offer_accepts_integer_period_counts(self):
for count in range(1, 13):
with self.subTest(count=count):
offer = AdvancedCommerceOffer(
periodCount=count, price=1000,
period=AdvancedCommerceOfferPeriod.P1M,
reason=AdvancedCommerceOfferReason.ACQUISITION
)
self.assertEqual(offer.periodCount, count)

def test_validation_utils_items(self):
valid_list = [
AdvancedCommerceOneTimeChargeItem(
Expand Down