208 lines
No EOL
6.3 KiB
Python
208 lines
No EOL
6.3 KiB
Python
"""Test models"""
|
|
import pytest
|
|
from decimal import Decimal
|
|
from datetime import datetime
|
|
from app.models import User, Product, Order, OrderItem
|
|
|
|
|
|
class TestUserModel:
|
|
"""Test User model"""
|
|
|
|
@pytest.mark.unit
|
|
def test_user_creation(self, db_session):
|
|
"""Test creating a user"""
|
|
user = User(
|
|
email='test@example.com',
|
|
username='testuser',
|
|
first_name='Test',
|
|
last_name='User',
|
|
is_admin=False,
|
|
is_active=True
|
|
)
|
|
user.set_password('password123')
|
|
db_session.add(user)
|
|
db_session.commit()
|
|
|
|
assert user.id is not None
|
|
assert user.email == 'test@example.com'
|
|
assert user.username == 'testuser'
|
|
assert user.first_name == 'Test'
|
|
assert user.last_name == 'User'
|
|
|
|
@pytest.mark.unit
|
|
def test_user_password_hashing(self, db_session):
|
|
"""Test password hashing and verification"""
|
|
user = User(email='test@example.com', username='testuser')
|
|
user.set_password('password123')
|
|
db_session.add(user)
|
|
db_session.commit()
|
|
|
|
assert user.check_password('password123') is True
|
|
assert user.check_password('wrongpassword') is False
|
|
|
|
@pytest.mark.unit
|
|
def test_user_to_dict(self, db_session):
|
|
"""Test user serialization to dictionary"""
|
|
user = User(
|
|
email='test@example.com',
|
|
username='testuser',
|
|
first_name='Test',
|
|
last_name='User'
|
|
)
|
|
user.set_password('password123')
|
|
db_session.add(user)
|
|
db_session.commit()
|
|
|
|
user_dict = user.to_dict()
|
|
assert user_dict['email'] == 'test@example.com'
|
|
assert user_dict['username'] == 'testuser'
|
|
assert 'password' not in user_dict
|
|
assert 'password_hash' not in user_dict
|
|
|
|
@pytest.mark.unit
|
|
def test_user_repr(self, db_session):
|
|
"""Test user string representation"""
|
|
user = User(email='test@example.com', username='testuser')
|
|
user.set_password('password123')
|
|
db_session.add(user)
|
|
db_session.commit()
|
|
|
|
assert repr(user) == '<User testuser>'
|
|
|
|
|
|
class TestProductModel:
|
|
"""Test Product model"""
|
|
|
|
@pytest.mark.unit
|
|
def test_product_creation(self, db_session):
|
|
"""Test creating a product"""
|
|
product = Product(
|
|
name='Test Product',
|
|
description='A test product',
|
|
price=Decimal('99.99'),
|
|
stock=10,
|
|
image_url='https://example.com/product.jpg'
|
|
)
|
|
db_session.add(product)
|
|
db_session.commit()
|
|
|
|
assert product.id is not None
|
|
assert product.name == 'Test Product'
|
|
assert product.price == Decimal('99.99')
|
|
assert product.stock == 10
|
|
assert product.is_active is True
|
|
|
|
@pytest.mark.unit
|
|
def test_product_to_dict(self, db_session):
|
|
"""Test product serialization to dictionary"""
|
|
product = Product(
|
|
name='Test Product',
|
|
description='A test product',
|
|
price=Decimal('99.99'),
|
|
stock=10
|
|
)
|
|
db_session.add(product)
|
|
db_session.commit()
|
|
|
|
product_dict = product.to_dict()
|
|
assert product_dict['name'] == 'Test Product'
|
|
assert product_dict['price'] == 99.99
|
|
assert isinstance(product_dict['created_at'], str)
|
|
assert isinstance(product_dict['updated_at'], str)
|
|
|
|
@pytest.mark.unit
|
|
def test_product_defaults(self, db_session):
|
|
"""Test product default values"""
|
|
product = Product(
|
|
name='Test Product',
|
|
price=Decimal('9.99')
|
|
)
|
|
db_session.add(product)
|
|
db_session.commit()
|
|
|
|
assert product.stock == 0
|
|
assert product.is_active is True
|
|
assert product.description is None
|
|
assert product.image_url is None
|
|
|
|
@pytest.mark.unit
|
|
def test_product_repr(self, db_session):
|
|
"""Test product string representation"""
|
|
product = Product(name='Test Product', price=Decimal('9.99'))
|
|
db_session.add(product)
|
|
db_session.commit()
|
|
|
|
assert repr(product) == '<Product Test Product>'
|
|
|
|
|
|
class TestOrderModel:
|
|
"""Test Order model"""
|
|
|
|
@pytest.mark.unit
|
|
def test_order_creation(self, db_session, regular_user):
|
|
"""Test creating an order"""
|
|
order = Order(
|
|
user_id=regular_user.id,
|
|
total_amount=Decimal('199.99'),
|
|
shipping_address='123 Test St'
|
|
)
|
|
db_session.add(order)
|
|
db_session.commit()
|
|
|
|
assert order.id is not None
|
|
assert order.user_id == regular_user.id
|
|
assert order.total_amount == Decimal('199.99')
|
|
|
|
@pytest.mark.unit
|
|
def test_order_to_dict(self, db_session, regular_user):
|
|
"""Test order serialization to dictionary"""
|
|
order = Order(
|
|
user_id=regular_user.id,
|
|
total_amount=Decimal('199.99'),
|
|
shipping_address='123 Test St'
|
|
)
|
|
db_session.add(order)
|
|
db_session.commit()
|
|
|
|
order_dict = order.to_dict()
|
|
assert order_dict['user_id'] == regular_user.id
|
|
assert order_dict['total_amount'] == 199.99
|
|
assert isinstance(order_dict['created_at'], str)
|
|
|
|
|
|
class TestOrderItemModel:
|
|
"""Test OrderItem model"""
|
|
|
|
@pytest.mark.unit
|
|
def test_order_item_creation(self, db_session, order, product):
|
|
"""Test creating an order item"""
|
|
order_item = OrderItem(
|
|
order_id=order.id,
|
|
product_id=product.id,
|
|
quantity=2,
|
|
price=product.price
|
|
)
|
|
db_session.add(order_item)
|
|
db_session.commit()
|
|
|
|
assert order_item.id is not None
|
|
assert order_item.order_id == order.id
|
|
assert order_item.product_id == product.id
|
|
assert order_item.quantity == 2
|
|
|
|
@pytest.mark.unit
|
|
def test_order_item_to_dict(self, db_session, order, product):
|
|
"""Test order item serialization to dictionary"""
|
|
order_item = OrderItem(
|
|
order_id=order.id,
|
|
product_id=product.id,
|
|
quantity=2,
|
|
price=product.price
|
|
)
|
|
db_session.add(order_item)
|
|
db_session.commit()
|
|
|
|
item_dict = order_item.to_dict()
|
|
assert item_dict['order_id'] == order.id
|
|
assert item_dict['product_id'] == product.id
|
|
assert item_dict['quantity'] == 2 |