Skip to content

Commit

Permalink
[fix] field_validator raise ValueError + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KyrianC committed Nov 23, 2023
1 parent 841fbcc commit c89e83d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class SegmentSentimentAnalysisDataClass(NoRaiseBaseModel):
@classmethod
def valid_segment(cls, value):
if not isinstance(value, str):
raise TypeError(f"Segment must be a string, not {type(value)}")
raise ValueError(f"Segment must be a string, not {type(value)}")
return value

@field_validator("sentiment", mode="before")
@classmethod
def valid_sentiment(cls, value):
if not isinstance(value, str):
raise TypeError(f"Sentiment must be a string, not {type(value)}")
raise ValueError(f"Sentiment must be a string, not {type(value)}")
value = value.title()
if not value in ["Positive", "Negative", "Neutral"]:
raise ValueError(
Expand All @@ -49,7 +49,7 @@ def valid_sentiment_rate(cls, value):
if value is None:
return value
if not isinstance(value, (float, int)):
raise TypeError(f"Sentiment rate must be a float, not {type(value)}")
raise ValueError(f"Sentiment rate must be a float, not {type(value)}")
return round(value, 2)


Expand All @@ -70,7 +70,7 @@ class SentimentAnalysisDataClass(NoRaiseBaseModel):
@classmethod
def valid_general_sentiment(cls, value):
if not isinstance(value, str):
raise TypeError(f"General sentiment must be a string, not {type(value)}")
raise ValueError(f"General sentiment must be a string, not {type(value)}")
value = value.title()
if not value in ["Positive", "Negative", "Neutral"]:
raise ValueError(
Expand All @@ -84,7 +84,7 @@ def valid_general_sentiment_rate(cls, value):
if value is None:
return value
if not isinstance(value, (float, int)):
raise TypeError(
raise ValueError(
f"General sentiment rate must be a float, not {type(value)}"
)
return round(value, 2)
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def test_valid_input(self, kwargs, expected):
sentiment="invalid sentiment",
sentiment_rate=0,
expected={
'invalid_field': 'sentiment',
"raise_type": ValueError,
"raise_message": "Sentiment must be 'Positive' or 'Negative' or 'Neutral'",
},
Expand All @@ -89,7 +90,8 @@ def test_valid_input(self, kwargs, expected):
sentiment=1,
sentiment_rate=0,
expected={
"raise_type": TypeError,
"invalid_field": 'sentiment',
"raise_type": ValueError,
"raise_message": "Sentiment must be a string",
},
),
Expand All @@ -98,7 +100,8 @@ def test_valid_input(self, kwargs, expected):
sentiment="Positive",
sentiment_rate=0,
expected={
"raise_type": TypeError,
"invalid_field": 'segment',
"raise_type": ValueError,
"raise_message": "Segment must be a string",
},
),
Expand All @@ -107,32 +110,11 @@ def test_valid_input(self, kwargs, expected):
sentiment=SentimentEnum.POSITIVE.value,
sentiment_rate="0",
expected={
"raise_type": TypeError,
'invalid_field': 'sentiment_rate',
"raise_type": ValueError,
"raise_message": "Sentiment rate must be a float",
},
),
# _assign_markers_parametrize(
# segment="Valid segment",
# sentiment=SentimentEnum.POSITIVE.value,
# sentiment_rate=-1,
# expected={
# "raise_type": ValidationError,
# "raise_message": re.escape(
# "1 validation error for SegmentSentimentAnalysisDataClass\nsentiment_rate\n)"
# ),
# },
# ),
# _assign_markers_parametrize(
# segment="Valid segment",
# sentiment=SentimentEnum.POSITIVE.value,
# sentiment_rate=2,
# expected={
# "raise_type": ValidationError,
# "raise_message": re.escape(
# "1 validation error for SegmentSentimentAnalysisDataClass\nsentiment_rate\n)"
# ),
# },
# ),
],
ids=[
"test_with_bad_sentiment_format",
Expand All @@ -144,10 +126,8 @@ def test_valid_input(self, kwargs, expected):
],
)
def test_invalid_input(self, kwargs, expected):
with pytest.raises(
(expected["raise_type"], ValidationError), match=expected["raise_message"]
):
segment_sentiment_class = SegmentSentimentAnalysisDataClass(**kwargs)
segment_sentiment_class = SegmentSentimentAnalysisDataClass(**kwargs)
assert getattr(segment_sentiment_class, expected['invalid_field']) is None


class TestSentimentAnalysisDataClass:
Expand Down

0 comments on commit c89e83d

Please sign in to comment.