# tests/unit/models/test_database_models.py import pytest from models.database.marketplace_product import MarketplaceProduct from models.database.vendor import Vendor from models.database.stock import Stock from models.database.user import User @pytest.mark.unit @pytest.mark.database class TestDatabaseModels: def test_user_model(self, db): """Test User model creation and relationships""" user = User( email="db_test@example.com", username="dbtest", hashed_password="hashed_password_123", role="user", is_active=True, ) db.add(user) db.commit() db.refresh(user) assert user.id is not None assert user.email == "db_test@example.com" assert user.created_at is not None assert user.updated_at is not None def test_product_model(self, db): """Test MarketplaceProduct model creation""" marketplace_product = MarketplaceProduct( marketplace_product_id="DB_TEST_001", title="Database Test MarketplaceProduct", description="Testing product model", price="25.99", currency="USD", brand="DBTest", gtin="1234567890123", availability="in stock", marketplace="TestDB", vendor_name="DBTestVendor", ) db.add(marketplace_product) db.commit() db.refresh(marketplace_product) assert marketplace_product.id is not None assert marketplace_product.marketplace_product_id == "DB_TEST_001" assert marketplace_product.created_at is not None def test_stock_model(self, db): """Test Stock model creation""" stock = Stock(gtin="1234567890123", location="DB_WAREHOUSE", quantity=150) db.add(stock) db.commit() db.refresh(stock) assert stock.id is not None assert stock.gtin == "1234567890123" assert stock.location == "DB_WAREHOUSE" assert stock.quantity == 150 def test_vendor_model_with_owner(self, db, test_user): """Test Vendor model with owner relationship""" vendor = Vendor( vendor_code="DBTEST", vendor_name="Database Test Vendor", description="Testing vendor model", owner_id=test_user.id, is_active=True, is_verified=False, ) db.add(vendor) db.commit() db.refresh(vendor) assert vendor.id is not None assert vendor.vendor_code == "DBTEST" assert vendor.owner_id == test_user.id assert vendor.owner.username == test_user.username def test_database_constraints(self, db): """Test database constraints and unique indexes""" # Test unique marketplace_product_id constraint product1 = MarketplaceProduct(marketplace_product_id="UNIQUE_001", title="MarketplaceProduct 1") db.add(product1) db.commit() # This should raise an integrity error with pytest.raises(Exception): # Could be IntegrityError or similar product2 = MarketplaceProduct(marketplace_product_id="UNIQUE_001", title="MarketplaceProduct 2") db.add(product2) db.commit()