100 lines
3.2 KiB
Python
100 lines
3.2 KiB
Python
# tests/unit/models/test_database_models.py
|
|
import pytest
|
|
|
|
from models.database.marketplace_product import MarketplaceProduct
|
|
from models.database.shop import Shop
|
|
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",
|
|
shop_name="DBTestShop",
|
|
)
|
|
|
|
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_shop_model_with_owner(self, db, test_user):
|
|
"""Test Shop model with owner relationship"""
|
|
shop = Shop(
|
|
shop_code="DBTEST",
|
|
shop_name="Database Test Shop",
|
|
description="Testing shop model",
|
|
owner_id=test_user.id,
|
|
is_active=True,
|
|
is_verified=False,
|
|
)
|
|
|
|
db.add(shop)
|
|
db.commit()
|
|
db.refresh(shop)
|
|
|
|
assert shop.id is not None
|
|
assert shop.shop_code == "DBTEST"
|
|
assert shop.owner_id == test_user.id
|
|
assert shop.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()
|