From fc81f107eda106cf8de97cc9dd634b6308c2ba60 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Thu, 15 Sep 2016 16:33:19 +1000 Subject: [PATCH] =?UTF-8?q?When=20setting=20quantities=20on=20products,=20?= =?UTF-8?q?only=20raise=20errors=20if=20they=E2=80=99re=20due=20to=20chang?= =?UTF-8?q?es=20made=20during=20the=20current=20call=20to=20set=5Fquantiti?= =?UTF-8?q?es.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #54 --- registrasion/controllers/cart.py | 12 +++++++++++- registrasion/tests/test_flag.py | 12 ++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/registrasion/controllers/cart.py b/registrasion/controllers/cart.py index 204dc9f6..ff31bb95 100644 --- a/registrasion/controllers/cart.py +++ b/registrasion/controllers/cart.py @@ -132,6 +132,7 @@ class CartController(object): product_quantities = list(product_quantities) + # n.b need to add have the existing items first so that the new # items override the old ones. all_product_quantities = dict(itertools.chain( @@ -140,7 +141,16 @@ class CartController(object): )).items() # Validate that the limits we're adding are OK - self._test_limits(all_product_quantities) + products = set(product for product, q in product_quantities) + try: + self._test_limits(all_product_quantities) + except CartValidationError as ve: + # Only raise errors for products that we're explicitly + # Manipulating here. + for ve_field in ve.error_list: + product, message = ve_field.message + if product in products: + raise ve new_items = [] products = [] diff --git a/registrasion/tests/test_flag.py b/registrasion/tests/test_flag.py index 3d8914a1..0b446c26 100644 --- a/registrasion/tests/test_flag.py +++ b/registrasion/tests/test_flag.py @@ -382,3 +382,15 @@ class FlagTestCases(RegistrationCartTestCase): with self.assertRaises(ValidationError): cart2.add_to_cart(self.PROD_1, 1) + + def test_flag_failures_only_break_affected_products(self): + ''' If a flag fails, it should only affect its own products. ''' + + self.add_product_flag() + cart1 = TestingCartController.for_user(self.USER_1) + cart1.add_to_cart(self.PROD_2, 1) + cart1.add_to_cart(self.PROD_1, 1) + cart1.set_quantity(self.PROD_2, 0) + + # The following should not fail, as PROD_3 is not affected by flag. + cart1.add_to_cart(self.PROD_3, 1)