Merge pull request '#67_unittests_for_DTOs' (#69) from #67_unittests_for_DTOs into main
Reviewed-on: #69
This commit was merged in pull request #69.
This commit is contained in:
@@ -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
|
||||
)
|
||||
|
||||
@@ -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__])
|
||||
Reference in New Issue
Block a user