From c24cd34a6bfb6603c8639c299b8077b288678cb5 Mon Sep 17 00:00:00 2001 From: rlskoeser Date: Fri, 6 Dec 2024 10:52:40 +0100 Subject: [PATCH] Test exceptions and parser type errors more specific --- .../test_hebrew/test_hebrew_converter.py | 7 ++++++ .../test_hebrew/test_hebrew_parser.py | 24 ++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/tests/test_converters/test_calendars/test_hebrew/test_hebrew_converter.py b/tests/test_converters/test_calendars/test_hebrew/test_hebrew_converter.py index 1c05632..319b551 100644 --- a/tests/test_converters/test_calendars/test_hebrew/test_hebrew_converter.py +++ b/tests/test_converters/test_calendars/test_hebrew/test_hebrew_converter.py @@ -68,6 +68,13 @@ def test_parse_error(self): with pytest.raises(ValueError): HebrewDateConverter().parse("") + # non-string input should raise a type error + with pytest.raises(TypeError): + HebrewDateConverter().parse(42) + + with pytest.raises(TypeError): + HebrewDateConverter().parse({"foo": "bar"}) + def test_partially_known(self): # hebrew dates get existing partially unknown behavior diff --git a/tests/test_converters/test_calendars/test_hebrew/test_hebrew_parser.py b/tests/test_converters/test_calendars/test_hebrew/test_hebrew_parser.py index e4894b1..69b929e 100644 --- a/tests/test_converters/test_calendars/test_hebrew/test_hebrew_parser.py +++ b/tests/test_converters/test_calendars/test_hebrew/test_hebrew_parser.py @@ -1,4 +1,6 @@ import pytest +from lark.exceptions import UnexpectedCharacters, UnexpectedEOF + from undate.converters.calendars.hebrew.parser import hebrew_parser @@ -41,23 +43,23 @@ def test_should_parse(date_string): error_cases = [ # invalid days - "0 Tammuz 5403", - "31 Tishri 5403", + ("0 Tammuz 5403", UnexpectedCharacters), + ("31 Tishri 5403", UnexpectedCharacters), # month alone - "Tishri", + ("Tishri", UnexpectedEOF), # month day only - "12 Heshvan", + ("12 Heshvan", UnexpectedEOF), # invalid month - "Foo 383", + ("Foo 383", UnexpectedCharacters), # wrong format - "2024-10-02", + ("2024-10-02", UnexpectedCharacters), # year month day not supported - "5403 Adar", - "5403 Adar 14", + ("5403 Adar", UnexpectedCharacters), + ("5403 Adar 14", UnexpectedCharacters), ] -@pytest.mark.parametrize("date_string", error_cases) -def test_should_error(date_string): - with pytest.raises(Exception): +@pytest.mark.parametrize("date_string,exception", error_cases) +def test_should_error(date_string, exception): + with pytest.raises(exception): hebrew_parser.parse(date_string)