Refactoring code for modular approach
This commit is contained in:
@@ -9,7 +9,8 @@ from sqlalchemy.pool import StaticPool
|
||||
|
||||
from main import app
|
||||
from app.core.database import get_db, Base
|
||||
from models.database_models import User, Product, Stock, Shop
|
||||
# Import all models to ensure they're registered with Base metadata
|
||||
from models.database_models import User, Product, Stock, Shop, MarketplaceImportJob, ShopProduct
|
||||
from middleware.auth import AuthManager
|
||||
|
||||
# Use in-memory SQLite database for tests
|
||||
@@ -35,38 +36,41 @@ def testing_session_local(engine):
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def db(engine, testing_session_local):
|
||||
"""Create a fresh database for each test"""
|
||||
"""Create a database session for direct database operations"""
|
||||
# Create all tables
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
# Create session
|
||||
db = testing_session_local()
|
||||
db_session = testing_session_local()
|
||||
|
||||
# Override the dependency
|
||||
try:
|
||||
yield db_session
|
||||
finally:
|
||||
db_session.rollback()
|
||||
db_session.close()
|
||||
# Tables will be dropped by the client fixture
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def client(db): # Now client depends on db
|
||||
"""Create a test client with database dependency override"""
|
||||
|
||||
# Override the dependency to use our test database
|
||||
def override_get_db():
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
pass # Don't close here, we'll close in cleanup
|
||||
pass # Don't close here, the db fixture handles it
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
try:
|
||||
yield db
|
||||
client = TestClient(app)
|
||||
yield client
|
||||
finally:
|
||||
db.rollback() # Rollback any uncommitted changes
|
||||
db.close()
|
||||
# Clean up the dependency override
|
||||
if get_db in app.dependency_overrides:
|
||||
del app.dependency_overrides[get_db]
|
||||
# Drop all tables for next test
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def client(db):
|
||||
"""Create a test client with database dependency override"""
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
@@ -174,10 +178,11 @@ def test_shop(db, test_user):
|
||||
def test_stock(db, test_product, test_shop):
|
||||
"""Create test stock entry"""
|
||||
stock = Stock(
|
||||
product_id=test_product.product_id,
|
||||
shop_code=test_shop.shop_code,
|
||||
gtin=test_product.gtin, # Fixed: use gtin instead of product_id
|
||||
location=test_shop.shop_code, # Fixed: use location instead of shop_code
|
||||
quantity=10,
|
||||
reserved_quantity=0
|
||||
reserved_quantity=0,
|
||||
shop_id=test_shop.id # Add shop_id reference
|
||||
)
|
||||
db.add(stock)
|
||||
db.commit()
|
||||
|
||||
Reference in New Issue
Block a user