import datetime
import uuid
from typing import Optional
import pytest
from sqlalchemy import String
from sqlalchemy.orm import Mapped, mapped_column
from nomy_data_models.models.base import BaseModel
class ModelExample(BaseModel):
__abstract__ = False
name: Mapped[str] = mapped_column(String(50), nullable=False)
description: Mapped[Optional[str]] = mapped_column(String(200), nullable=True)
@pytest.fixture
def test_model():
model = ModelExample()
model.id = uuid.uuid4()
model.name = "Test"
model.description = "Test description"
model.created_at = datetime.datetime.now()
model.updated_at = datetime.datetime.now()
return model
class TestBase:
def test_tablename_generation(self):
assert ModelExample.__tablename__ == "model_example"
def test_to_dict_method(self, test_model):
model_dict = test_model.to_dict()
assert "id" in model_dict
assert "name" in model_dict
assert "description" in model_dict
assert "created_at" in model_dict
assert "updated_at" in model_dict
assert model_dict["id"] == test_model.id
assert model_dict["name"] == "Test"
assert model_dict["description"] == "Test description"
assert model_dict["created_at"] == test_model.created_at
assert model_dict["updated_at"] == test_model.updated_at
def test_from_dict_method(self):
test_id = uuid.uuid4()
now = datetime.datetime.now()
data = {
"id": test_id,
"name": "Test",
"description": "Test description",
"created_at": now,
"updated_at": now,
"unknown_field": "This should be ignored",
}
model_from_dict = ModelExample.from_dict(data)
expected_attrs = ["id", "created_at", "updated_at"]
for attr in expected_attrs:
assert hasattr(model_from_dict, attr)
assert getattr(model_from_dict, attr) == data[attr]
assert not hasattr(model_from_dict, "unknown_field")
def test_update_from_dict_method(self, test_model):
test_model.update_from_dict(
{"name": "Updated", "unknown_field": "This should be ignored"}
)
assert test_model.name == "Updated"
assert test_model.description == "Test description"
assert not hasattr(test_model, "unknown_field")
def test_repr_method(self, test_model):
assert str(test_model) == f"<ModelExample(id={test_model.id})>"
def test_db_integration(self, session):
model = ModelExample(name="DB Test", description="Testing with DB session")
session.add(model)
session.commit()
retrieved = session.query(ModelExample).filter_by(name="DB Test").first()
assert retrieved is not None
assert retrieved.name == "DB Test"
assert retrieved.description == "Testing with DB session"
assert retrieved.id is not None
assert retrieved.created_at is not None
assert retrieved.updated_at is not None
def test_dict_method(self, test_model):
model_dict = test_model.dict()
assert "id" in model_dict
assert "name" in model_dict
assert "description" in model_dict
assert "created_at" in model_dict
assert "updated_at" in model_dict
assert model_dict["id"] == test_model.id
assert model_dict["name"] == test_model.name
assert model_dict["description"] == test_model.description
assert model_dict["created_at"] == test_model.created_at
assert model_dict["updated_at"] == test_model.updated_at