|
| 1 | +from datetime import datetime, date, time, timezone |
| 2 | + |
| 3 | +from mappers import datetime_from_str, datetime_to_str |
| 4 | + |
| 5 | +DATETIME_NO_TIME = datetime.combine(date(2024, 4, 6), time(tzinfo=timezone.utc), tzinfo=timezone.utc) |
| 6 | +DATETIME_TIME_NO_FRACTIONAL = DATETIME_NO_TIME.replace(hour=13, minute=30, second=45) |
| 7 | +DATETIME_TIME_WITH_FRACTIONAL_LONG = DATETIME_TIME_NO_FRACTIONAL.replace(microsecond=123456) |
| 8 | + |
| 9 | +DATETIME_TIME_WITH_FRACTIONAL_SHORT = DATETIME_TIME_NO_FRACTIONAL.replace(microsecond=123000) |
| 10 | + |
| 11 | + |
| 12 | +def test_datetime_from_str_none_expect_none(): |
| 13 | + assert datetime_from_str(None) is None |
| 14 | + |
| 15 | + |
| 16 | +def test_datetime_from_str_empty_string_expect_none(): |
| 17 | + assert datetime_from_str('') is None |
| 18 | + |
| 19 | + |
| 20 | +def test_datetime_from_str_empty_string_not_trimmed_expect_none(): |
| 21 | + assert datetime_from_str(' \t\n') is None |
| 22 | + |
| 23 | + |
| 24 | +def test_datetime_from_str_date_without_time(): |
| 25 | + assert datetime_from_str('2024-04-06') == DATETIME_NO_TIME |
| 26 | + |
| 27 | + |
| 28 | +def test_datetime_from_str_datetime_no_fractional_seconds_no_timezone(): |
| 29 | + assert datetime_from_str('2024-04-06T13:30:45') == DATETIME_TIME_NO_FRACTIONAL |
| 30 | + |
| 31 | + |
| 32 | +def test_datetime_from_str_datetime_no_fractional_seconds_utc_z_timezone(): |
| 33 | + assert datetime_from_str('2024-04-06T13:30:45Z') == DATETIME_TIME_NO_FRACTIONAL |
| 34 | + |
| 35 | + |
| 36 | +def test_datetime_from_str_datetime_no_fractional_seconds_utc_offset_timezone(): |
| 37 | + assert datetime_from_str('2024-04-06T13:30:45+00:00') == DATETIME_TIME_NO_FRACTIONAL |
| 38 | + |
| 39 | + |
| 40 | +def test_datetime_from_str_datetime_0_digits_fractional_seconds_no_timezone(): |
| 41 | + assert datetime_from_str('2024-04-06T13:30:45.0') == DATETIME_TIME_NO_FRACTIONAL |
| 42 | + |
| 43 | + |
| 44 | +def test_datetime_from_str_datetime_0_digits_fractional_utc_z_timezone(): |
| 45 | + assert datetime_from_str('2024-04-06T13:30:45.0Z') == DATETIME_TIME_NO_FRACTIONAL |
| 46 | + |
| 47 | + |
| 48 | +def test_datetime_from_str_datetime_0_digits_fractional_utc_offset_timezone(): |
| 49 | + assert datetime_from_str('2024-04-06T13:30:45.0+00:00') == DATETIME_TIME_NO_FRACTIONAL |
| 50 | + |
| 51 | + |
| 52 | +def test_datetime_from_str_datetime_no_timezone(): |
| 53 | + assert datetime_from_str('2024-04-06T13:30:45.123456') == DATETIME_TIME_WITH_FRACTIONAL_LONG |
| 54 | + |
| 55 | + |
| 56 | +def test_datetime_from_str_datetime_utc_z_timezone(): |
| 57 | + assert datetime_from_str('2024-04-06T13:30:45.123456Z') == DATETIME_TIME_WITH_FRACTIONAL_LONG |
| 58 | + |
| 59 | + |
| 60 | +def test_datetime_from_str_datetime_utc_offset_timezone(): |
| 61 | + assert datetime_from_str('2024-04-06T13:30:45.123456+00:00') == DATETIME_TIME_WITH_FRACTIONAL_LONG |
| 62 | + |
| 63 | + |
| 64 | +def test_datetime_from_str_datetime_short_fractional_seconds_no_timezone(): |
| 65 | + assert datetime_from_str('2024-04-06T13:30:45.123') == DATETIME_TIME_WITH_FRACTIONAL_SHORT |
| 66 | + |
| 67 | + |
| 68 | +def test_datetime_from_str_datetime_short_fractional_seconds_utc_z_timezone(): |
| 69 | + assert datetime_from_str('2024-04-06T13:30:45.123Z') == DATETIME_TIME_WITH_FRACTIONAL_SHORT |
| 70 | + |
| 71 | + |
| 72 | +def test_datetime_from_str_datetime_short_fractional_seconds_utc_offset_timezone(): |
| 73 | + assert datetime_from_str('2024-04-06T13:30:45.123+00:00') == DATETIME_TIME_WITH_FRACTIONAL_SHORT |
| 74 | + |
| 75 | + |
| 76 | +def test_datetime_from_str_datetime_7_digits_fractional_seconds_no_timezone(): |
| 77 | + assert datetime_from_str('2024-04-06T13:30:45.1234567') == DATETIME_TIME_WITH_FRACTIONAL_LONG |
| 78 | + |
| 79 | + |
| 80 | +def test_datetime_from_str_datetime_7_digits_fractional_seconds_utc_z_timezone(): |
| 81 | + assert datetime_from_str('2024-04-06T13:30:45.1234567Z') == DATETIME_TIME_WITH_FRACTIONAL_LONG |
| 82 | + |
| 83 | + |
| 84 | +def test_datetime_from_str_datetime_7_digits_fractional_seconds_utc_offset_timezone(): |
| 85 | + assert datetime_from_str('2024-04-06T13:30:45.1234567+00:00') == DATETIME_TIME_WITH_FRACTIONAL_LONG |
| 86 | + |
| 87 | + |
| 88 | +def test_datetime_to_str_no_time_no_time(): |
| 89 | + assert datetime_to_str(DATETIME_NO_TIME) == '2024-04-06T00:00:00.000000Z' |
| 90 | + |
| 91 | + |
| 92 | +def test_datetime_to_str_no_fractional_seconds(): |
| 93 | + assert datetime_to_str(DATETIME_TIME_NO_FRACTIONAL) == '2024-04-06T13:30:45.000000Z' |
| 94 | + |
| 95 | + |
| 96 | +def test_datetime_to_str_short_fractional_seconds(): |
| 97 | + assert datetime_to_str(DATETIME_TIME_WITH_FRACTIONAL_SHORT) == '2024-04-06T13:30:45.123000Z' |
| 98 | + |
| 99 | + |
| 100 | +def test_datetime_to_str_long_fractional_seconds(): |
| 101 | + assert datetime_to_str(DATETIME_TIME_WITH_FRACTIONAL_LONG) == '2024-04-06T13:30:45.123456Z' |
| 102 | + |
| 103 | + |
| 104 | +def test_datetime_to_str_no_timezone(): |
| 105 | + datetime_no_tz = DATETIME_TIME_WITH_FRACTIONAL_LONG.replace(tzinfo=None) |
| 106 | + assert datetime_to_str(datetime_no_tz) == '2024-04-06T13:30:45.123456Z' |
0 commit comments