Skip to content

Commit 5b1975b

Browse files
authored
Add a None check before using property values, with tests (#2036)
1 parent 1c5657e commit 5b1975b

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/shop/models.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ def labels(self) -> list:
615615
"text": "Bundle",
616616
})
617617

618-
if self.stock_amount is not None:
618+
if self.left_in_stock is not None:
619619
if self.left_in_stock < 1 or not self.is_time_available:
620620
labels.insert(0, {
621621
"type": "sold_out",
@@ -631,11 +631,12 @@ def labels(self) -> list:
631631
"text": f"Only {self.left_in_stock} left!",
632632
})
633633

634-
if self.available_for_days < 20:
635-
labels.append({
636-
"type": "ending_soon",
637-
"text": f"Sales end in {self.available_for_days} days!",
638-
})
634+
if self.available_for_days is not None:
635+
if self.available_for_days > 0 and self.available_for_days < 20:
636+
labels.append({
637+
"type": "ending_soon",
638+
"text": f"Sales end in {self.available_for_days} days!",
639+
})
639640

640641
return labels
641642

src/shop/tests.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,29 @@ def test_labels_for_product_with_stock_below_or_equal_to_10(self):
151151

152152
def test_labels_for_product_ending_within_20_days(self):
153153
"""Test the product returns a 'ending_soon' label object."""
154+
# Without upper availability
154155
available_in = DateTimeTZRange(
155156
lower=timezone.now(),
156-
upper=timezone.now() + timezone.timedelta(6),
157+
upper=None,
158+
)
159+
product = ProductFactory(available_in=available_in)
160+
161+
assert len(product.labels) == 0
162+
163+
# Without lower availability
164+
available_in = DateTimeTZRange(
165+
lower=None,
166+
upper=timezone.now(),
157167
)
168+
product = ProductFactory(available_in=available_in)
169+
170+
assert len(product.labels) == 0
158171

159172
# With stock
173+
available_in = DateTimeTZRange(
174+
lower=timezone.now(),
175+
upper=timezone.now() + timezone.timedelta(6),
176+
)
160177
product = ProductFactory(stock_amount=11, available_in=available_in)
161178

162179
result = product.labels[0]

0 commit comments

Comments
 (0)