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:
34
tests/fixtures/auth_fixtures.py
vendored
34
tests/fixtures/auth_fixtures.py
vendored
@@ -1,4 +1,10 @@
|
||||
# tests/fixtures/auth_fixtures.py
|
||||
"""
|
||||
Authentication-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 uuid
|
||||
|
||||
import pytest
|
||||
@@ -9,14 +15,14 @@ from models.database.user import User
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def auth_manager():
|
||||
"""Create auth manager instance (session scope since it's stateless)"""
|
||||
"""Create auth manager instance (session scope since it's stateless)."""
|
||||
return AuthManager()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_user(db, auth_manager):
|
||||
"""Create a test user with unique username"""
|
||||
unique_id = str(uuid.uuid4())[:8] # Short unique identifier
|
||||
"""Create a test user with unique username."""
|
||||
unique_id = str(uuid.uuid4())[:8]
|
||||
hashed_password = auth_manager.hash_password("testpass123")
|
||||
user = User(
|
||||
email=f"test_{unique_id}@example.com",
|
||||
@@ -28,16 +34,13 @@ def test_user(db, auth_manager):
|
||||
db.add(user)
|
||||
db.commit()
|
||||
db.refresh(user)
|
||||
# Expunge user from session to prevent ResourceWarning about unclosed connections
|
||||
# This detaches the object from the session so it doesn't hold a reference
|
||||
db.expunge(user)
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_admin(db, auth_manager):
|
||||
"""Create a test admin user with unique username"""
|
||||
unique_id = str(uuid.uuid4())[:8] # Short unique identifier
|
||||
"""Create a test admin user with unique username."""
|
||||
unique_id = str(uuid.uuid4())[:8]
|
||||
hashed_password = auth_manager.hash_password("adminpass123")
|
||||
admin = User(
|
||||
email=f"admin_{unique_id}@example.com",
|
||||
@@ -49,15 +52,13 @@ def test_admin(db, auth_manager):
|
||||
db.add(admin)
|
||||
db.commit()
|
||||
db.refresh(admin)
|
||||
# Expunge admin from session to prevent ResourceWarning about unclosed connections
|
||||
db.expunge(admin)
|
||||
return admin
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def another_admin(db, auth_manager):
|
||||
"""Create another test admin user for testing admin-to-admin interactions"""
|
||||
unique_id = str(uuid.uuid4())[:8] # Short unique identifier
|
||||
"""Create another test admin user for testing admin-to-admin interactions."""
|
||||
unique_id = str(uuid.uuid4())[:8]
|
||||
hashed_password = auth_manager.hash_password("anotheradminpass123")
|
||||
admin = User(
|
||||
email=f"another_admin_{unique_id}@example.com",
|
||||
@@ -69,14 +70,12 @@ def another_admin(db, auth_manager):
|
||||
db.add(admin)
|
||||
db.commit()
|
||||
db.refresh(admin)
|
||||
# Expunge admin from session to prevent ResourceWarning about unclosed connections
|
||||
db.expunge(admin)
|
||||
return admin
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def other_user(db, auth_manager):
|
||||
"""Create a different user for testing access controls"""
|
||||
"""Create a different user for testing access controls."""
|
||||
unique_id = str(uuid.uuid4())[:8]
|
||||
hashed_password = auth_manager.hash_password("otherpass123")
|
||||
user = User(
|
||||
@@ -89,8 +88,6 @@ def other_user(db, auth_manager):
|
||||
db.add(user)
|
||||
db.commit()
|
||||
db.refresh(user)
|
||||
# Expunge user from session to prevent ResourceWarning about unclosed connections
|
||||
db.expunge(user)
|
||||
return user
|
||||
|
||||
|
||||
@@ -120,7 +117,7 @@ def admin_headers(client, test_admin):
|
||||
|
||||
@pytest.fixture
|
||||
def test_vendor_user(db, auth_manager):
|
||||
"""Create a test vendor user with unique username"""
|
||||
"""Create a test vendor user with unique username."""
|
||||
unique_id = str(uuid.uuid4())[:8]
|
||||
hashed_password = auth_manager.hash_password("vendorpass123")
|
||||
user = User(
|
||||
@@ -133,7 +130,6 @@ def test_vendor_user(db, auth_manager):
|
||||
db.add(user)
|
||||
db.commit()
|
||||
db.refresh(user)
|
||||
db.expunge(user)
|
||||
return user
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user