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