refactor: remove db.expunge() anti-pattern from test fixtures
Remove db.expunge() calls that were causing DetachedInstanceError when accessing lazy-loaded relationships in tests. Changes: - conftest.py: Add documentation about fixture best practices - auth_fixtures: Remove expunge, keep objects attached to session - customer_fixtures: Remove expunge, add proper relationship loading - vendor_fixtures: Remove expunge, add test_company and other_company fixtures for proper company-vendor relationship setup - marketplace_import_job_fixtures: Remove expunge calls - marketplace_product_fixtures: Remove expunge calls The db fixture already provides test isolation by dropping/recreating tables after each test, so expunge is unnecessary and harmful. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
35
tests/fixtures/customer_fixtures.py
vendored
35
tests/fixtures/customer_fixtures.py
vendored
@@ -1,12 +1,19 @@
|
||||
# tests/fixtures/customer_fixtures.py
|
||||
"""
|
||||
Customer-related test fixtures.
|
||||
|
||||
Note: Fixtures should NOT use db.expunge() as it breaks lazy loading.
|
||||
See tests/conftest.py for details on fixture best practices.
|
||||
"""
|
||||
import pytest
|
||||
|
||||
from models.database.customer import Customer, CustomerAddress
|
||||
from models.database.order import Order
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_customer(db, test_vendor):
|
||||
"""Create a test customer"""
|
||||
"""Create a test customer."""
|
||||
customer = Customer(
|
||||
vendor_id=test_vendor.id,
|
||||
email="testcustomer@example.com",
|
||||
@@ -19,14 +26,12 @@ def test_customer(db, test_vendor):
|
||||
db.add(customer)
|
||||
db.commit()
|
||||
db.refresh(customer)
|
||||
# Expunge customer from session to prevent ResourceWarning about unclosed connections
|
||||
db.expunge(customer)
|
||||
return customer
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_customer_address(db, test_vendor, test_customer):
|
||||
"""Create a test customer address"""
|
||||
"""Create a test customer address."""
|
||||
address = CustomerAddress(
|
||||
vendor_id=test_vendor.id,
|
||||
customer_id=test_customer.id,
|
||||
@@ -42,6 +47,24 @@ def test_customer_address(db, test_vendor, test_customer):
|
||||
db.add(address)
|
||||
db.commit()
|
||||
db.refresh(address)
|
||||
# Expunge address from session to prevent ResourceWarning about unclosed connections
|
||||
db.expunge(address)
|
||||
return address
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_order(db, test_vendor, test_customer, test_customer_address):
|
||||
"""Create a test order."""
|
||||
order = Order(
|
||||
vendor_id=test_vendor.id,
|
||||
customer_id=test_customer.id,
|
||||
order_number="TEST-ORD-001",
|
||||
status="pending",
|
||||
subtotal=99.99,
|
||||
total_amount=99.99,
|
||||
currency="EUR",
|
||||
shipping_address_id=test_customer_address.id,
|
||||
billing_address_id=test_customer_address.id,
|
||||
)
|
||||
db.add(order)
|
||||
db.commit()
|
||||
db.refresh(order)
|
||||
return order
|
||||
|
||||
Reference in New Issue
Block a user