|
4 | 4 | from unittest.mock import Mock, patch |
5 | 5 |
|
6 | 6 | import pytest |
| 7 | +from rest_framework.exceptions import ParseError |
7 | 8 |
|
8 | 9 | from futurex_openedx_extensions.helpers import converters |
9 | | -from futurex_openedx_extensions.helpers.converters import DateMethods |
| 10 | +from futurex_openedx_extensions.helpers.converters import DateMethods, date_str_to_date_obj |
10 | 11 |
|
11 | 12 |
|
12 | 13 | @pytest.mark.parametrize('ids_string, expected', [ |
@@ -200,3 +201,32 @@ def test_to_arabic_numerals(input_text, expected_output, test_case): |
200 | 201 | def test_to_indian_numerals(input_text, expected_output, test_case): |
201 | 202 | """Verify that to_indian_numerals returns correct data""" |
202 | 203 | assert converters.to_indian_numerals(input_text) == expected_output, f'Failed: {test_case}' |
| 204 | + |
| 205 | + |
| 206 | +@pytest.mark.parametrize( |
| 207 | + 'usecase,input_value,expected,should_raise', |
| 208 | + [ |
| 209 | + ('Valid date', '2025-01-01', date(2025, 1, 1), False), |
| 210 | + ('Empty string returns None', '', None, False), |
| 211 | + ('None input returns None', None, None, False), |
| 212 | + ('Wrong format', '01-01-2025', None, True), |
| 213 | + ('Wrong separator', '2025/01/01', None, True), |
| 214 | + ('Invalid month >12', '2025-13-01', None, True), |
| 215 | + ('Invalid day 0', '2025-01-00', None, True), |
| 216 | + ('Random string', 'abcd-ef-gh', None, True), |
| 217 | + ('Integer input', 12345, None, True), |
| 218 | + ], |
| 219 | +) |
| 220 | +def test_date_str_to_date_obj(usecase, input_value, expected, should_raise): |
| 221 | + """Test date_str_to_date_obj with valid and invalid inputs.""" |
| 222 | + if should_raise: |
| 223 | + with pytest.raises(ParseError) as exc_info: |
| 224 | + date_str_to_date_obj(input_value, 'date_from') |
| 225 | + assert 'Invalid date_from' in str(exc_info.value), ( |
| 226 | + f"Failed usecase '{usecase}': input={repr(input_value)}, exception={repr(exc_info.value)}" |
| 227 | + ) |
| 228 | + else: |
| 229 | + result = date_str_to_date_obj(input_value, 'date_from') |
| 230 | + assert result == expected, ( |
| 231 | + f"Failed usecase '{usecase}': input={repr(input_value)}, expected={repr(expected)}, got={repr(result)}" |
| 232 | + ) |
0 commit comments