| 1 |
import itertools |
| 2 |
from datetime import datetime |
| 3 |
|
| 4 |
import pytest |
| 5 |
from parameterized import param, parameterized |
| 6 |
from pytz import UnknownTimeZoneError, utc |
| 7 |
|
| 8 |
from dateparser.conf import settings |
| 9 |
from dateparser.utils import ( |
| 10 |
apply_timezone, |
| 11 |
apply_timezone_from_settings, |
| 12 |
find_date_separator, |
| 13 |
get_last_day_of_month, |
| 14 |
get_next_leap_year, |
| 15 |
get_previous_leap_year, |
| 16 |
localize_timezone, |
| 17 |
registry, |
| 18 |
) |
| 19 |
from tests import BaseTestCase |
| 20 |
|
| 21 |
|
| 22 |
class TestUtils(BaseTestCase): |
| 23 |
def setUp(self): |
| 24 |
super().setUp() |
| 25 |
self.date_format = None |
| 26 |
self.result = None |
| 27 |
|
| 28 |
def given_date_format(self, date_format): |
| 29 |
self.date_format = date_format |
| 30 |
|
| 31 |
def when_date_separator_is_parsed(self): |
| 32 |
self.result = find_date_separator(self.date_format) |
| 33 |
|
| 34 |
def then_date_separator_is(self, sep): |
| 35 |
self.assertEqual(self.result, sep) |
| 36 |
|
| 37 |
@staticmethod |
| 38 |
def make_class_without_get_keys(): |
| 39 |
class SomeClass: |
| 40 |
pass |
| 41 |
|
| 42 |
some_class = SomeClass |
| 43 |
return some_class |
| 44 |
|
| 45 |
@parameterized.expand( |
| 46 |
[ |
| 47 |
param(date_format=fmt.format(sep=sep), expected_sep=sep) |
| 48 |
for (fmt, sep) in itertools.product( |
| 49 |
["%d{sep}%m{sep}%Y", "%d{sep}%m{sep}%Y %H:%M"], ["/", ".", "-", ":"] |
| 50 |
) |
| 51 |
] |
| 52 |
) |
| 53 |
def test_separator_extraction(self, date_format, expected_sep): |
| 54 |
self.given_date_format(date_format) |
| 55 |
self.when_date_separator_is_parsed() |
| 56 |
self.then_date_separator_is(expected_sep) |
| 57 |
|
| 58 |
@parameterized.expand( |
| 59 |
[ |
| 60 |
param(datetime(2015, 12, 12), timezone="UTC", zone="UTC"), |
| 61 |
param(datetime(2015, 12, 12), timezone="Asia/Karachi", zone="Asia/Karachi"), |
| 62 |
param(datetime(2015, 12, 12, tzinfo=utc), timezone="UTC", zone="UTC"), |
| 63 |
] |
| 64 |
) |
| 65 |
def test_localize_timezone_function(self, date, timezone, zone): |
| 66 |
tzaware_dt = localize_timezone(date, timezone) |
| 67 |
self.assertEqual(tzaware_dt.tzinfo.zone, zone) |
| 68 |
|
| 69 |
@parameterized.expand( |
| 70 |
[ |
| 71 |
param(datetime(2015, 12, 12), timezone="UTB"), |
| 72 |
param(datetime(2015, 12, 12), timezone="Asia/Karach"), |
| 73 |
] |
| 74 |
) |
| 75 |
def test_localize_timezone_function_raise_error(self, date, timezone): |
| 76 |
self.assertRaises(UnknownTimeZoneError, localize_timezone, date, timezone) |
| 77 |
|
| 78 |
@parameterized.expand( |
| 79 |
[ |
| 80 |
param(datetime(2015, 12, 12), timezone="UTC+3", zone=r"UTC\+03:00"), |
| 81 |
] |
| 82 |
) |
| 83 |
def test_localize_timezone_function_exception(self, date, timezone, zone): |
| 84 |
tzaware_dt = localize_timezone(date, timezone) |
| 85 |
self.assertEqual(tzaware_dt.tzinfo._StaticTzInfo__name, zone) |
| 86 |
|
| 87 |
@parameterized.expand( |
| 88 |
[ |
| 89 |
param( |
| 90 |
datetime(2015, 12, 12, 10, 12), |
| 91 |
timezone="Asia/Karachi", |
| 92 |
expected=datetime(2015, 12, 12, 15, 12), |
| 93 |
), |
| 94 |
param( |
| 95 |
datetime(2015, 12, 12, 10, 12), |
| 96 |
timezone="-0500", |
| 97 |
expected=datetime(2015, 12, 12, 5, 12), |
| 98 |
), |
| 99 |
] |
| 100 |
) |
| 101 |
def test_apply_timezone_function(self, date, timezone, expected): |
| 102 |
result = apply_timezone(date, timezone) |
| 103 |
result = result.replace(tzinfo=None) |
| 104 |
self.assertEqual(expected, result) |
| 105 |
|
| 106 |
@parameterized.expand( |
| 107 |
[ |
| 108 |
param( |
| 109 |
datetime(2015, 12, 12, 10, 12), |
| 110 |
timezone="Asia/Karachi", |
| 111 |
expected=datetime(2015, 12, 12, 15, 12), |
| 112 |
), |
| 113 |
param( |
| 114 |
datetime(2015, 12, 12, 10, 12), |
| 115 |
timezone="-0500", |
| 116 |
expected=datetime(2015, 12, 12, 5, 12), |
| 117 |
), |
| 118 |
] |
| 119 |
) |
| 120 |
def test_apply_timezone_from_settings_function(self, date, timezone, expected): |
| 121 |
result = apply_timezone_from_settings( |
| 122 |
date, settings.replace(**{"TO_TIMEZONE": timezone, "TIMEZONE": "UTC"}) |
| 123 |
) |
| 124 |
self.assertEqual(expected, result) |
| 125 |
|
| 126 |
@parameterized.expand( |
| 127 |
[ |
| 128 |
param( |
| 129 |
datetime(2015, 12, 12, 10, 12), expected=datetime(2015, 12, 12, 10, 12) |
| 130 |
), |
| 131 |
] |
| 132 |
) |
| 133 |
def test_apply_timezone_from_settings_function_none_settings(self, date, expected): |
| 134 |
result = apply_timezone_from_settings(date, None) |
| 135 |
self.assertEqual(expected, result) |
| 136 |
|
| 137 |
@parameterized.expand( |
| 138 |
[ |
| 139 |
param( |
| 140 |
datetime(2015, 12, 12, 10, 12), |
| 141 |
), |
| 142 |
param( |
| 143 |
datetime(2015, 12, 12, 10, 12), |
| 144 |
), |
| 145 |
] |
| 146 |
) |
| 147 |
def test_apply_timezone_from_settings_function_should_return_tz(self, date): |
| 148 |
result = apply_timezone_from_settings( |
| 149 |
date, settings.replace(**{"RETURN_AS_TIMEZONE_AWARE": True}) |
| 150 |
) |
| 151 |
self.assertTrue(bool(result.tzinfo)) |
| 152 |
|
| 153 |
def test_registry_when_get_keys_not_implemented(self): |
| 154 |
cl = self.make_class_without_get_keys() |
| 155 |
self.assertRaises(NotImplementedError, registry, cl) |
| 156 |
|
| 157 |
@parameterized.expand( |
| 158 |
[ |
| 159 |
param(2111, 1, 31), |
| 160 |
param(1999, 2, 28), # normal year |
| 161 |
param(1996, 2, 29), # leap and not centurial year |
| 162 |
param(2000, 2, 29), # leap and centurial year |
| 163 |
param(1700, 2, 28), # no leap and centurial year (exception) |
| 164 |
param(2020, 3, 31), |
| 165 |
param(1987, 4, 30), |
| 166 |
param(1000, 5, 31), |
| 167 |
param(1534, 6, 30), |
| 168 |
param(1777, 7, 31), |
| 169 |
param(1234, 8, 31), |
| 170 |
param(1678, 9, 30), |
| 171 |
param(1947, 10, 31), |
| 172 |
param(2015, 11, 30), |
| 173 |
param(2300, 12, 31), |
| 174 |
] |
| 175 |
) |
| 176 |
def test_get_last_day_of_month(self, year, month, expected_last_day): |
| 177 |
assert get_last_day_of_month(year, month) == expected_last_day |
| 178 |
|
| 179 |
|
| 180 |
@pytest.mark.parametrize( |
| 181 |
"year,expected_previous_leap_year", |
| 182 |
[ |
| 183 |
(2020, 2016), |
| 184 |
(2000, 1996), # leap and centurial year |
| 185 |
(2104, 2096), # missing no leap centurial year |
| 186 |
(1704, 1696), |
| 187 |
(2396, 2392), |
| 188 |
(0, -4), # even if this is not a valid year, it is the expected result |
| 189 |
], |
| 190 |
) |
| 191 |
def test_get_previous_leap_year(year, expected_previous_leap_year): |
| 192 |
assert get_previous_leap_year(year) == expected_previous_leap_year |
| 193 |
|
| 194 |
|
| 195 |
@pytest.mark.parametrize( |
| 196 |
"year,expected_next_leap_year", |
| 197 |
[ |
| 198 |
(2020, 2024), |
| 199 |
(1996, 2000), # leap and centurial year |
| 200 |
(2096, 2104), # missing no leap centurial year |
| 201 |
(1696, 1704), |
| 202 |
(2396, 2400), |
| 203 |
(0, 4), |
| 204 |
], |
| 205 |
) |
| 206 |
def test_get_next_leap_year(year, expected_next_leap_year): |
| 207 |
assert get_next_leap_year(year) == expected_next_leap_year |