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.
This commit is contained in:
Russell Ballestrini 2025-11-01 09:56:21 -04:00
parent fd8e8b6b4f
commit ca1a71bcce

View file

@ -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,