From c9e6ed389acbeb5f52651a02ff44891d8cbb856a Mon Sep 17 00:00:00 2001 From: XiaolongZhang Date: Fri, 7 Aug 2026 18:05:31 +0800 Subject: [PATCH] fix: relax over-strict discount applied[] count/position assertions Two discount tests asserted exact lengths and a fixed position on discounts.applied[], but discount.md states applied carries code-based discounts *plus any automatic discounts*, whose count is not fixed and whose code is null. A conformant business that also applies an automatic discount would fail these assertions even though every submitted code is present. - test_multiple_discounts_accepted: drop `len(applied) == 2`; the two assertIn(code) checks already prove both codes applied. - test_multiple_discounts_one_rejected: drop `len(applied) == 1` and the `applied[0].code == valid_code` position assumption; assert membership instead, tolerant of automatic discounts ordered first. assertIn is safe against a list that may contain None entries from automatic discounts, since the submitted code is always a string. --- business_logic_test.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/business_logic_test.py b/business_logic_test.py index 957c556..ad2c153 100644 --- a/business_logic_test.py +++ b/business_logic_test.py @@ -300,12 +300,14 @@ def test_multiple_discounts_accepted(self): discounted_checkout, expected_price, expected_discount=expected_discount ) - # Verify both applied discounts are present + # Verify both submitted codes are present in the applied list. The applied + # list also carries any automatic discounts (discount.md: applied contains + # code-based + automatic), so assert membership rather than an exact count. discounts_data = getattr(discounted_checkout, "discounts", {}) discounts_obj = ( discount.DiscountsObject(**discounts_data) if discounts_data else None ) - self.assertTrue(discounts_obj and len(discounts_obj.applied) == 2) + self.assertTrue(discounts_obj and discounts_obj.applied) applied_codes = [d.code for d in discounts_obj.applied] self.assertIn(valid_code_1, applied_codes) self.assertIn(valid_code_2, applied_codes) @@ -337,13 +339,16 @@ def test_multiple_discounts_one_rejected(self): discounted_checkout, expected_price, expected_discount=expected_discount ) - # Verify only one applied discount is present + # Verify the valid code is present in the applied list. The applied list + # also carries any automatic discounts, so assert membership rather than an + # exact count or a fixed position (automatic discounts have no code). discounts_data = getattr(discounted_checkout, "discounts", {}) discounts_obj = ( discount.DiscountsObject(**discounts_data) if discounts_data else None ) - self.assertTrue(discounts_obj and len(discounts_obj.applied) == 1) - self.assertEqual(discounts_obj.applied[0].code, valid_code) + self.assertTrue(discounts_obj and discounts_obj.applied) + applied_codes = [d.code for d in discounts_obj.applied] + self.assertIn(valid_code, applied_codes) def test_fixed_amount_discount(self): """Test that a fixed-amount discount code decreases the total correctly.