Skip to content

Commit

Permalink
add test for generics when data is an empty dict
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderegg committed Oct 18, 2021
1 parent 98ec7b7 commit 12f0268
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/models-library/src/models_library/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ class DataEnveloped(GenericModel, Generic[DataT]):
@validator("error")
@classmethod
def data_and_error_cannot_be_populated_together(cls, v, values):
if v is not None and values.get("data") is not None:
data = values.get("data")
if v is not None and data:
raise ValueError(
"both data and error cannot contain values at the same time"
f"both data and error cannot contain values at the same time. received data: {values.get('data')}, received error: {v}"
)
return v
5 changes: 5 additions & 0 deletions packages/models-library/tests/test_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,10 @@ def test_data_enveloped():
assert not some_enveloped_bool.data
assert some_enveloped_bool.error == "some error happened"


def test_data_enveloped_validator():
# empty data should validate also with an empty dict
DataEnveloped[int](data={}, error="some error message")
#
with pytest.raises(ValueError):
DataEnveloped[int](data=213, error="some error message")

0 comments on commit 12f0268

Please sign in to comment.