test_value_object.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import pytest
  2. from pydantic import ValidationError, validator
  3. from clean_python import BadRequest, ValueObject
  4. class Color(ValueObject):
  5. name: str
  6. @validator("name")
  7. def name_not_empty(cls, v):
  8. assert v != ""
  9. return v
  10. @pytest.fixture
  11. def color():
  12. return Color(name="green")
  13. def test_validator():
  14. with pytest.raises(ValidationError) as e:
  15. Color(name="")
  16. assert e.type is ValidationError # not BadRequest
  17. def test_create_err():
  18. with pytest.raises(BadRequest):
  19. Color.create(name="")
  20. def test_update(color):
  21. updated = color.update(name="red")
  22. assert color.name == "green"
  23. assert updated.name == "red"
  24. def test_update_validates(color):
  25. with pytest.raises(BadRequest):
  26. color.update(name="")
  27. def test_run_validation(color):
  28. assert color.run_validation() == color
  29. def test_run_validation_err():
  30. color = Color.construct(name="")
  31. with pytest.raises(BadRequest):
  32. color.run_validation()
  33. def test_hashable(color):
  34. assert len({color, color}) == 1
  35. def test_eq(color):
  36. assert color == color
  37. def test_neq(color):
  38. assert color != Color(name="red")