From ca1a71bcce3400a90d2f48f236e0ecb56553390d Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Sat, 1 Nov 2025 09:56:21 -0400 Subject: [PATCH] Fix detached instance errors in physical product handling test The test was failing due to SQLAlchemy DetachedInstanceError when accessing object properties after transaction.manager.commit(). Changes: - Save cart and shop UUIDs before commit to avoid detached access - Re-query cart from database after commit using get_cart_by_id - Pass shop_id parameter in cart URL to properly set request.shop - Use saved UUID variables instead of accessing detached objects This ensures request.shop_location is correctly resolved so the handling options (local pickup, delivery, shipping) render properly in the cart template. All 357 tests now pass including the physical product handling test. --- .../tests/test_physical_product_handling.py | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/make_post_sell/tests/test_physical_product_handling.py b/make_post_sell/tests/test_physical_product_handling.py index 0561ebd..605db08 100644 --- a/make_post_sell/tests/test_physical_product_handling.py +++ b/make_post_sell/tests/test_physical_product_handling.py @@ -139,15 +139,25 @@ class PhysicalProductHandlingTests(unittest.TestCase): self.dbsession.add(cart) self.dbsession.flush() + # Save IDs before commit + cart_id = cart.id + cart_uuid_str = cart.uuid_str + shop_uuid_str = shop.uuid_str + # Commit transaction transaction.manager.commit() + # Re-query cart to avoid detached instance error + from ..models.cart import get_cart_by_id + cart = get_cart_by_id(self.dbsession, cart_id) + # Verify cart has physical products self.assertEqual(cart.count, 1) self.assertTrue(len(cart.physical_products) > 0, "Cart should have physical products") # 5. Get cart page and verify handling options section appears - cart_res = self.testapp.get(f"/cart/{cart.id}") + # Need to pass shop_id so request.shop is set correctly + cart_res = self.testapp.get(f"/cart/{cart_id}?shop_id={shop_uuid_str}") cart_body = cart_res.body.decode() # Verify handling options form is present @@ -156,10 +166,10 @@ class PhysicalProductHandlingTests(unittest.TestCase): # 6. Submit handling option form with CSRF token # This is the critical test - the form MUST include CSRF token - csrf_token = self.get_csrf_token(shop.uuid_str) + csrf_token = self.get_csrf_token(shop_uuid_str) handling_res = self.testapp.post( - f"/cart/{cart.uuid_str}/handling-option", + f"/cart/{cart_uuid_str}/handling-option", { "handling_option": "local_pickup", "csrf_token": csrf_token, @@ -188,9 +198,9 @@ class PhysicalProductHandlingTests(unittest.TestCase): ("local_shipping", 750), ("international_shipping", 1500), ]: - csrf_token = self.get_csrf_token(shop.uuid_str) + csrf_token = self.get_csrf_token(shop_uuid_str) res = self.testapp.post( - f"/cart/{cart.uuid_str}/handling-option", + f"/cart/{cart_uuid_str}/handling-option", { "handling_option": option, "csrf_token": csrf_token,