test_model.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import pytest
  2. import sqlmodel
  3. from pynecone.model import Model
  4. @pytest.fixture
  5. def model_default_primary() -> Model:
  6. """Returns a model object with no defined primary key.
  7. Returns:
  8. Model: Model object.
  9. """
  10. class ChildModel(Model):
  11. name: str
  12. return ChildModel(name="name") # type: ignore
  13. @pytest.fixture
  14. def model_custom_primary() -> Model:
  15. """Returns a model object with a custom primary key.
  16. Returns:
  17. Model: Model object.
  18. """
  19. class ChildModel(Model):
  20. custom_id: int = sqlmodel.Field(default=None, primary_key=True)
  21. name: str
  22. return ChildModel(name="name") # type: ignore
  23. def test_default_primary_key(model_default_primary):
  24. """Test that if a primary key is not defined a default is added.
  25. Args:
  26. model_default_primary: Fixture.
  27. """
  28. assert "id" in model_default_primary.__class__.__fields__
  29. def test_custom_primary_key(model_custom_primary):
  30. """Test that if a primary key is defined no default key is added.
  31. Args:
  32. model_custom_primary: Fixture.
  33. """
  34. assert "id" not in model_custom_primary.__class__.__fields__