diff --git a/shared/repositories/src/dto/type_checking_base_model.py b/shared/repositories/src/dto/type_checking_base_model.py index f4fbee1..34e6dd1 100644 --- a/shared/repositories/src/dto/type_checking_base_model.py +++ b/shared/repositories/src/dto/type_checking_base_model.py @@ -7,6 +7,5 @@ class TypeCheckingBaseModel(BaseModel): """BaseModel with added type checking on input types.""" model_config = ConfigDict( - validate_assignment=True, # argument type checking frozen=True, # ensure data immutability ) diff --git a/shared/repositories/tests/unit/type_checking_base_model_test.py b/shared/repositories/tests/unit/type_checking_base_model_test.py new file mode 100644 index 0000000..fcf9f6c --- /dev/null +++ b/shared/repositories/tests/unit/type_checking_base_model_test.py @@ -0,0 +1,46 @@ +"""Definition of unittests for TypeCheckingBaseModel class.""" + +import unittest + +import pytest +from pydantic import ValidationError + +from shared.repositories.src.dto.type_checking_base_model import TypeCheckingBaseModel + + +class UUT(TypeCheckingBaseModel): + """Test class for TypeCheckingBaseModel.""" + + string_field: str + int_field: int + + +class TestTypeCheckingBaseModel(unittest.TestCase): + """Test class for TypeCheckingBaseModel.""" + + def setUp(self): + """Set up the test case.""" + self.uut = UUT + self.string_field = 'test' + self.int_field = 123 + + def test_type_checking_on_instantiation(self): + """Test type checking on instantiation.""" + with pytest.raises(ValidationError): + _ = self.uut( + string_field=self.int_field, + int_field=self.string_field, + ) + + def test_attributes_frozen(self): + """Test that attributes cannot be updated.""" + uut_instance = self.uut( + string_field=self.string_field, + int_field=self.int_field, + ) + with pytest.raises(ValidationError): + uut_instance.string_field = self.int_field + + +if __name__ == '__main__': + pytest.main(['-s', '-v', __file__])