99 lines
2.9 KiB
Python
99 lines
2.9 KiB
Python
# tests/test_database.py
|
|
import pytest
|
|
from sqlalchemy import text
|
|
from models.database_models import User, Product, Stock, Shop
|
|
|
|
|
|
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 Product model creation"""
|
|
product = Product(
|
|
product_id="DB_TEST_001",
|
|
title="Database Test Product",
|
|
description="Testing product model",
|
|
price="25.99",
|
|
currency="USD",
|
|
brand="DBTest",
|
|
gtin="1234567890123",
|
|
availability="in stock",
|
|
marketplace="TestDB",
|
|
shop_name="DBTestShop"
|
|
)
|
|
|
|
db.add(product)
|
|
db.commit()
|
|
db.refresh(product)
|
|
|
|
assert product.id is not None
|
|
assert product.product_id == "DB_TEST_001"
|
|
assert 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 product_id constraint
|
|
product1 = Product(product_id="UNIQUE_001", title="Product 1")
|
|
db.add(product1)
|
|
db.commit()
|
|
|
|
# This should raise an integrity error
|
|
with pytest.raises(Exception): # Could be IntegrityError or similar
|
|
product2 = Product(product_id="UNIQUE_001", title="Product 2")
|
|
db.add(product2)
|
|
db.commit()
|