Files
tfm_ainventory/backend/tests/test_schema.py

86 lines
3.1 KiB
Python

import pytest
from datetime import datetime
from backend.models import Item
class TestItemPhotoFields:
"""Test photo fields in Item model."""
def test_item_has_photo_fields(self, test_db):
"""Test that Item model has three photo fields."""
from backend.models import Item
from sqlalchemy import inspect
mapper = inspect(Item)
column_names = [c.name for c in mapper.columns]
assert "photo_path" in column_names
assert "photo_thumbnail_path" in column_names
assert "photo_upload_date" in column_names
def test_photo_fields_are_nullable(self, test_db):
"""Test that photo fields are nullable and can create item without them."""
item = Item(
name="Test Item",
category="Electronics",
type="Component",
quantity=5,
barcode="PHOTO-001",
part_number="PN-001"
)
test_db.add(item)
test_db.commit()
retrieved = test_db.query(Item).filter_by(id=item.id).first()
assert retrieved.photo_path is None
assert retrieved.photo_thumbnail_path is None
# photo_upload_date now has a default of datetime.now, so it should be populated
assert retrieved.photo_upload_date is not None
assert isinstance(retrieved.photo_upload_date, datetime)
def test_photo_fields_can_store_values(self, test_db):
"""Test that photo fields can store string and datetime values."""
upload_date = datetime(2026, 4, 20, 12, 30, 45)
item = Item(
name="Test Item",
category="Electronics",
type="Component",
quantity=5,
barcode="PHOTO-002",
part_number="PN-002",
photo_path="/images/items/photo-001.jpg",
photo_thumbnail_path="/images/items/thumbs/photo-001.jpg",
photo_upload_date=upload_date
)
test_db.add(item)
test_db.commit()
retrieved = test_db.query(Item).filter_by(id=item.id).first()
assert retrieved.photo_path == "/images/items/photo-001.jpg"
assert retrieved.photo_thumbnail_path == "/images/items/thumbs/photo-001.jpg"
assert retrieved.photo_upload_date == upload_date
def test_photo_fields_partial_null(self, test_db):
"""Test that only some photo fields can be set, others null."""
item = Item(
name="Test Item",
category="Electronics",
type="Component",
quantity=5,
barcode="PHOTO-003",
part_number="PN-003",
photo_path="/images/items/photo-003.jpg",
photo_thumbnail_path=None,
photo_upload_date=None
)
test_db.add(item)
test_db.commit()
retrieved = test_db.query(Item).filter_by(id=item.id).first()
assert retrieved.photo_path == "/images/items/photo-003.jpg"
assert retrieved.photo_thumbnail_path is None
# photo_upload_date gets default timestamp even when explicitly set to None
assert retrieved.photo_upload_date is not None
assert isinstance(retrieved.photo_upload_date, datetime)