From 5c2f54d3ae3d13d05c5da7a37644380995a717a1 Mon Sep 17 00:00:00 2001 From: Christian Henriksen Date: Sun, 8 Feb 2026 15:09:16 +0100 Subject: [PATCH] Add a None check before using property values, with tests --- src/shop/models.py | 13 +++++++------ src/shop/tests.py | 19 ++++++++++++++++++- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/shop/models.py b/src/shop/models.py index 32dcc81bb..087cc5a24 100644 --- a/src/shop/models.py +++ b/src/shop/models.py @@ -615,7 +615,7 @@ def labels(self) -> list: "text": "Bundle", }) - if self.stock_amount is not None: + if self.left_in_stock is not None: if self.left_in_stock < 1 or not self.is_time_available: labels.insert(0, { "type": "sold_out", @@ -631,11 +631,12 @@ def labels(self) -> list: "text": f"Only {self.left_in_stock} left!", }) - if self.available_for_days < 20: - labels.append({ - "type": "ending_soon", - "text": f"Sales end in {self.available_for_days} days!", - }) + if self.available_for_days is not None: + if self.available_for_days > 0 and self.available_for_days < 20: + labels.append({ + "type": "ending_soon", + "text": f"Sales end in {self.available_for_days} days!", + }) return labels diff --git a/src/shop/tests.py b/src/shop/tests.py index e4f243b7d..11a49b9be 100644 --- a/src/shop/tests.py +++ b/src/shop/tests.py @@ -151,12 +151,29 @@ def test_labels_for_product_with_stock_below_or_equal_to_10(self): def test_labels_for_product_ending_within_20_days(self): """Test the product returns a 'ending_soon' label object.""" + # Without upper availability available_in = DateTimeTZRange( lower=timezone.now(), - upper=timezone.now() + timezone.timedelta(6), + upper=None, + ) + product = ProductFactory(available_in=available_in) + + assert len(product.labels) == 0 + + # Without lower availability + available_in = DateTimeTZRange( + lower=None, + upper=timezone.now(), ) + product = ProductFactory(available_in=available_in) + + assert len(product.labels) == 0 # With stock + available_in = DateTimeTZRange( + lower=timezone.now(), + upper=timezone.now() + timezone.timedelta(6), + ) product = ProductFactory(stock_amount=11, available_in=available_in) result = product.labels[0]