|
| 1 | +import unittest |
| 2 | +import logging |
| 3 | +from datetime import date |
| 4 | +from lib.aip.utils.date_utils import validate_and_convert_date |
| 5 | + |
| 6 | + |
| 7 | +# Suppress logging messages below CRITICAL level |
| 8 | +# to just get the result of the tests. |
| 9 | +logging.disable(logging.CRITICAL) |
| 10 | + |
| 11 | +class TestValidateAndConvertDate(unittest.TestCase): |
| 12 | + |
| 13 | + def test_valid_date(self): |
| 14 | + # Test with a valid date |
| 15 | + self.assertEqual(validate_and_convert_date("2024-10-27"), date(2024, 10, 27)) |
| 16 | + |
| 17 | + def test_empty_string(self): |
| 18 | + # Test with an empty string |
| 19 | + with self.assertRaises(ValueError): |
| 20 | + validate_and_convert_date("") |
| 21 | + |
| 22 | + def test_invalid_format(self): |
| 23 | + # Test with various invalid formats |
| 24 | + with self.assertRaises(ValueError): |
| 25 | + validate_and_convert_date("2024/10/27") |
| 26 | + |
| 27 | + with self.assertRaises(ValueError): |
| 28 | + validate_and_convert_date("27-10-2024") |
| 29 | + |
| 30 | + with self.assertRaises(ValueError): |
| 31 | + validate_and_convert_date("October 27, 2024") |
| 32 | + |
| 33 | + def test_nonexistent_date(self): |
| 34 | + # Test not existing date Feb 30th |
| 35 | + with self.assertRaises(ValueError): |
| 36 | + validate_and_convert_date("2024-02-30") |
| 37 | + |
| 38 | + # Test not existing month 13 |
| 39 | + with self.assertRaises(ValueError): |
| 40 | + validate_and_convert_date("2024-13-01") |
| 41 | + |
| 42 | + def test_none_value(self): |
| 43 | + # Test with None as input |
| 44 | + with self.assertRaises(TypeError): |
| 45 | + validate_and_convert_date(None) |
| 46 | + |
| 47 | + def test_edge_case(self): |
| 48 | + # Test leap years |
| 49 | + self.assertEqual(validate_and_convert_date("2024-02-29"), date(2024, 2, 29)) |
| 50 | + |
| 51 | +if __name__ == '__main__': |
| 52 | + unittest.main() |
0 commit comments