| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import datetime as real_datetime |
| 4 |
import os |
| 5 |
import unittest |
| 6 |
from datetime import datetime, timedelta |
| 7 |
from datetime import timezone as dttz |
| 8 |
from itertools import product |
| 9 |
from time import tzset |
| 10 |
from unittest.mock import Mock, patch |
| 11 |
|
| 12 |
import pytest |
| 13 |
from parameterized import param, parameterized |
| 14 |
|
| 15 |
import dateparser |
| 16 |
from dateparser import date |
| 17 |
from dateparser.conf import Settings |
| 18 |
from dateparser.date import DateData |
| 19 |
from tests import BaseTestCase |
| 20 |
|
| 21 |
|
| 22 |
class TestDateRangeFunction(BaseTestCase): |
| 23 |
def setUp(self): |
| 24 |
super().setUp() |
| 25 |
self.result = NotImplemented |
| 26 |
|
| 27 |
@parameterized.expand( |
| 28 |
[ |
| 29 |
param( |
| 30 |
begin=datetime(2014, 6, 15), |
| 31 |
end=datetime(2014, 6, 25), |
| 32 |
expected_length=10, |
| 33 |
) |
| 34 |
] |
| 35 |
) |
| 36 |
def test_date_range(self, begin, end, expected_length): |
| 37 |
self.when_date_range_generated(begin, end) |
| 38 |
self.then_range_length_is(expected_length) |
| 39 |
self.then_all_dates_in_range_are_present(begin, end) |
| 40 |
self.then_range_is_in_ascending_order() |
| 41 |
|
| 42 |
@parameterized.expand( |
| 43 |
[ |
| 44 |
param( |
| 45 |
begin=datetime(2014, 4, 15), |
| 46 |
end=datetime(2014, 6, 25), |
| 47 |
expected_months=[(2014, 4), (2014, 5), (2014, 6)], |
| 48 |
), |
| 49 |
param( |
| 50 |
begin=datetime(2014, 4, 25), |
| 51 |
end=datetime(2014, 5, 5), |
| 52 |
expected_months=[(2014, 4), (2014, 5)], |
| 53 |
), |
| 54 |
param( |
| 55 |
begin=datetime(2014, 4, 5), |
| 56 |
end=datetime(2014, 4, 25), |
| 57 |
expected_months=[(2014, 4)], |
| 58 |
), |
| 59 |
param( |
| 60 |
begin=datetime(2014, 4, 25), |
| 61 |
end=datetime(2014, 6, 5), |
| 62 |
expected_months=[(2014, 4), (2014, 5), (2014, 6)], |
| 63 |
), |
| 64 |
] |
| 65 |
) |
| 66 |
def test_one_date_for_each_month(self, begin, end, expected_months): |
| 67 |
self.when_date_range_generated(begin, end, months=1) |
| 68 |
self.then_expected_months_are(expected_months) |
| 69 |
|
| 70 |
@parameterized.expand( |
| 71 |
[ |
| 72 |
"year", |
| 73 |
"month", |
| 74 |
"week", |
| 75 |
"day", |
| 76 |
"hour", |
| 77 |
"minute", |
| 78 |
"second", |
| 79 |
] |
| 80 |
) |
| 81 |
def test_should_reject_easily_mistaken_dateutil_arguments(self, invalid_period): |
| 82 |
self.when_date_range_generated( |
| 83 |
begin=datetime(2014, 6, 15), |
| 84 |
end=datetime(2014, 6, 25), |
| 85 |
**{invalid_period: 1}, |
| 86 |
) |
| 87 |
self.then_period_was_rejected(invalid_period) |
| 88 |
|
| 89 |
def when_date_range_generated(self, begin, end, **size): |
| 90 |
try: |
| 91 |
self.result = list(date.date_range(begin, end, **size)) |
| 92 |
except Exception as error: |
| 93 |
self.error = error |
| 94 |
|
| 95 |
def then_expected_months_are(self, expected): |
| 96 |
self.assertEqual(expected, [(d.year, d.month) for d in self.result]) |
| 97 |
|
| 98 |
def then_range_length_is(self, expected_length): |
| 99 |
self.assertEqual(expected_length, len(self.result)) |
| 100 |
|
| 101 |
def then_all_dates_in_range_are_present(self, begin, end): |
| 102 |
date_under_test = begin |
| 103 |
while date_under_test < end: |
| 104 |
self.assertIn(date_under_test, self.result) |
| 105 |
date_under_test += timedelta(days=1) |
| 106 |
|
| 107 |
def then_range_is_in_ascending_order(self): |
| 108 |
for i in range(len(self.result) - 1): |
| 109 |
self.assertLess(self.result[i], self.result[i + 1]) |
| 110 |
|
| 111 |
def then_period_was_rejected(self, period): |
| 112 |
self.then_error_was_raised(ValueError, ["Invalid argument: {}".format(period)]) |
| 113 |
|
| 114 |
|
| 115 |
class TestGetIntersectingPeriodsFunction(BaseTestCase): |
| 116 |
def setUp(self): |
| 117 |
super().setUp() |
| 118 |
self.result = NotImplemented |
| 119 |
|
| 120 |
@parameterized.expand( |
| 121 |
[param(low=datetime(2014, 6, 15), high=datetime(2014, 6, 16), length=1)] |
| 122 |
) |
| 123 |
def test_date_arguments_and_date_range_with_default_post_days( |
| 124 |
self, low, high, length |
| 125 |
): |
| 126 |
self.when_intersecting_period_calculated(low, high, period_size="day") |
| 127 |
self.then_all_dates_in_range_are_present(begin=low, end=high) |
| 128 |
self.then_date_range_length_is(length) |
| 129 |
|
| 130 |
@parameterized.expand( |
| 131 |
[ |
| 132 |
param( |
| 133 |
low=datetime(2014, 4, 15), |
| 134 |
high=datetime(2014, 6, 25), |
| 135 |
expected_results=[ |
| 136 |
datetime(2014, 4, 1), |
| 137 |
datetime(2014, 5, 1), |
| 138 |
datetime(2014, 6, 1), |
| 139 |
], |
| 140 |
), |
| 141 |
param( |
| 142 |
low=datetime(2014, 4, 25), |
| 143 |
high=datetime(2014, 5, 5), |
| 144 |
expected_results=[datetime(2014, 4, 1), datetime(2014, 5, 1)], |
| 145 |
), |
| 146 |
param( |
| 147 |
low=datetime(2014, 4, 5), |
| 148 |
high=datetime(2014, 4, 25), |
| 149 |
expected_results=[datetime(2014, 4, 1)], |
| 150 |
), |
| 151 |
param( |
| 152 |
low=datetime(2014, 4, 25), |
| 153 |
high=datetime(2014, 6, 5), |
| 154 |
expected_results=[ |
| 155 |
datetime(2014, 4, 1), |
| 156 |
datetime(2014, 5, 1), |
| 157 |
datetime(2014, 6, 1), |
| 158 |
], |
| 159 |
), |
| 160 |
param( |
| 161 |
low=datetime(2014, 4, 25), |
| 162 |
high=datetime(2014, 4, 25), |
| 163 |
expected_results=[], |
| 164 |
), |
| 165 |
param( |
| 166 |
low=datetime(2014, 12, 31), |
| 167 |
high=datetime(2015, 1, 1), |
| 168 |
expected_results=[datetime(2014, 12, 1)], |
| 169 |
), |
| 170 |
] |
| 171 |
) |
| 172 |
def test_dates_in_intersecting_period_should_use_first_day_when_period_is_month( |
| 173 |
self, low, high, expected_results |
| 174 |
): |
| 175 |
self.when_intersecting_period_calculated(low, high, period_size="month") |
| 176 |
self.then_results_are(expected_results) |
| 177 |
|
| 178 |
@parameterized.expand( |
| 179 |
[ |
| 180 |
param( |
| 181 |
low=datetime(2012, 4, 18), |
| 182 |
high=datetime(2014, 9, 22), |
| 183 |
expected_results=[ |
| 184 |
datetime(2012, 1, 1, 0, 0), |
| 185 |
datetime(2013, 1, 1, 0, 0), |
| 186 |
datetime(2014, 1, 1, 0, 0), |
| 187 |
], |
| 188 |
), |
| 189 |
param( |
| 190 |
low=datetime(2013, 8, 5), |
| 191 |
high=datetime(2014, 5, 15), |
| 192 |
expected_results=[ |
| 193 |
datetime(2013, 1, 1, 0, 0), |
| 194 |
datetime(2014, 1, 1, 0, 0), |
| 195 |
], |
| 196 |
), |
| 197 |
param( |
| 198 |
low=datetime(2008, 4, 5), |
| 199 |
high=datetime(2010, 1, 1), |
| 200 |
expected_results=[ |
| 201 |
datetime(2008, 1, 1, 0, 0), |
| 202 |
datetime(2009, 1, 1, 0, 0), |
| 203 |
], |
| 204 |
), |
| 205 |
param( |
| 206 |
low=datetime(2014, 1, 1), |
| 207 |
high=datetime(2016, 8, 22), |
| 208 |
expected_results=[ |
| 209 |
datetime(2014, 1, 1, 0, 0), |
| 210 |
datetime(2015, 1, 1, 0, 0), |
| 211 |
datetime(2016, 1, 1, 0, 0), |
| 212 |
], |
| 213 |
), |
| 214 |
param( |
| 215 |
low=datetime(2001, 7, 11), |
| 216 |
high=datetime(2001, 10, 16), |
| 217 |
expected_results=[datetime(2001, 1, 1, 0, 0)], |
| 218 |
), |
| 219 |
param( |
| 220 |
low=datetime(2017, 1, 1), high=datetime(2017, 1, 1), expected_results=[] |
| 221 |
), |
| 222 |
] |
| 223 |
) |
| 224 |
def test_dates_in_intersecting_period_should_use_first_month_and_first_day_when_period_is_year( |
| 225 |
self, low, high, expected_results |
| 226 |
): |
| 227 |
self.when_intersecting_period_calculated(low, high, period_size="year") |
| 228 |
self.then_results_are(expected_results) |
| 229 |
|
| 230 |
@parameterized.expand( |
| 231 |
[ |
| 232 |
param( |
| 233 |
low=datetime(2014, 4, 15), |
| 234 |
high=datetime(2014, 5, 15), |
| 235 |
period_size="month", |
| 236 |
expected_results=[datetime(2014, 4, 1), datetime(2014, 5, 1)], |
| 237 |
), |
| 238 |
param( |
| 239 |
low=datetime(2014, 10, 30, 4, 30), |
| 240 |
high=datetime(2014, 11, 7, 5, 20), |
| 241 |
period_size="week", |
| 242 |
expected_results=[datetime(2014, 10, 27), datetime(2014, 11, 3)], |
| 243 |
), |
| 244 |
param( |
| 245 |
low=datetime(2014, 8, 13, 13, 21), |
| 246 |
high=datetime(2014, 8, 14, 14, 7), |
| 247 |
period_size="day", |
| 248 |
expected_results=[datetime(2014, 8, 13), datetime(2014, 8, 14)], |
| 249 |
), |
| 250 |
param( |
| 251 |
low=datetime(2014, 5, 11, 22, 4), |
| 252 |
high=datetime(2014, 5, 12, 0, 5), |
| 253 |
period_size="hour", |
| 254 |
expected_results=[ |
| 255 |
datetime(2014, 5, 11, 22, 0), |
| 256 |
datetime(2014, 5, 11, 23, 0), |
| 257 |
datetime(2014, 5, 12, 0, 0), |
| 258 |
], |
| 259 |
), |
| 260 |
param( |
| 261 |
low=datetime(2014, 4, 25, 11, 11, 11), |
| 262 |
high=datetime(2014, 4, 25, 11, 12, 11), |
| 263 |
period_size="minute", |
| 264 |
expected_results=[ |
| 265 |
datetime(2014, 4, 25, 11, 11, 0), |
| 266 |
datetime(2014, 4, 25, 11, 12, 0), |
| 267 |
], |
| 268 |
), |
| 269 |
param( |
| 270 |
low=datetime(2014, 12, 31, 23, 59, 58, 500), |
| 271 |
high=datetime(2014, 12, 31, 23, 59, 59, 600), |
| 272 |
period_size="second", |
| 273 |
expected_results=[ |
| 274 |
datetime(2014, 12, 31, 23, 59, 58, 0), |
| 275 |
datetime(2014, 12, 31, 23, 59, 59, 0), |
| 276 |
], |
| 277 |
), |
| 278 |
] |
| 279 |
) |
| 280 |
def test_periods(self, low, high, period_size, expected_results): |
| 281 |
self.when_intersecting_period_calculated(low, high, period_size=period_size) |
| 282 |
self.then_results_are(expected_results) |
| 283 |
|
| 284 |
@parameterized.expand( |
| 285 |
[ |
| 286 |
param("years"), |
| 287 |
param("months"), |
| 288 |
param("days"), |
| 289 |
param("hours"), |
| 290 |
param("minutes"), |
| 291 |
param("seconds"), |
| 292 |
param("microseconds"), |
| 293 |
param("some_period"), |
| 294 |
] |
| 295 |
) |
| 296 |
def test_should_reject_easily_mistaken_dateutil_arguments(self, period_size): |
| 297 |
self.when_intersecting_period_calculated( |
| 298 |
low=datetime(2014, 6, 15), |
| 299 |
high=datetime(2014, 6, 25), |
| 300 |
period_size=period_size, |
| 301 |
) |
| 302 |
self.then_error_was_raised(ValueError, ["Invalid period: " + str(period_size)]) |
| 303 |
|
| 304 |
@parameterized.expand( |
| 305 |
[ |
| 306 |
param( |
| 307 |
low=datetime(2014, 4, 15), |
| 308 |
high=datetime(2014, 4, 14), |
| 309 |
period_size="month", |
| 310 |
), |
| 311 |
param( |
| 312 |
low=datetime(2014, 4, 25), |
| 313 |
high=datetime(2014, 4, 25), |
| 314 |
period_size="month", |
| 315 |
), |
| 316 |
] |
| 317 |
) |
| 318 |
def test_empty_period(self, low, high, period_size): |
| 319 |
self.when_intersecting_period_calculated(low, high, period_size) |
| 320 |
self.then_period_is_empty() |
| 321 |
|
| 322 |
def when_intersecting_period_calculated(self, low, high, period_size): |
| 323 |
try: |
| 324 |
self.result = list( |
| 325 |
date.get_intersecting_periods(low, high, period=period_size) |
| 326 |
) |
| 327 |
except Exception as error: |
| 328 |
self.error = error |
| 329 |
|
| 330 |
def then_results_are(self, expected_results): |
| 331 |
self.assertEqual(expected_results, self.result) |
| 332 |
|
| 333 |
def then_date_range_length_is(self, size): |
| 334 |
self.assertEqual(size, len(self.result)) |
| 335 |
|
| 336 |
def then_all_dates_in_range_are_present(self, begin, end): |
| 337 |
date_under_test = begin |
| 338 |
while date_under_test < end: |
| 339 |
self.assertIn(date_under_test, self.result) |
| 340 |
date_under_test += timedelta(days=1) |
| 341 |
|
| 342 |
def then_period_is_empty(self): |
| 343 |
self.assertEqual([], self.result) |
| 344 |
|
| 345 |
|
| 346 |
class TestParseWithFormatsFunction(BaseTestCase): |
| 347 |
def setUp(self): |
| 348 |
super().setUp() |
| 349 |
self.result = NotImplemented |
| 350 |
|
| 351 |
@parameterized.expand( |
| 352 |
[ |
| 353 |
param(date_string="yesterday", date_formats=["%Y-%m-%d"]), |
| 354 |
] |
| 355 |
) |
| 356 |
def test_date_with_not_matching_format_is_not_parsed( |
| 357 |
self, date_string, date_formats |
| 358 |
): |
| 359 |
self.when_date_is_parsed_with_formats(date_string, date_formats) |
| 360 |
self.then_date_was_not_parsed() |
| 361 |
|
| 362 |
@parameterized.expand( |
| 363 |
[ |
| 364 |
param( |
| 365 |
date_string="25-03-14", |
| 366 |
date_formats=["%d-%m-%y"], |
| 367 |
expected_result=datetime(2014, 3, 25), |
| 368 |
), |
| 369 |
] |
| 370 |
) |
| 371 |
def test_should_parse_date(self, date_string, date_formats, expected_result): |
| 372 |
self.when_date_is_parsed_with_formats(date_string, date_formats) |
| 373 |
self.then_date_was_parsed() |
| 374 |
self.then_parsed_period_is("day") |
| 375 |
self.then_parsed_date_is(expected_result) |
| 376 |
|
| 377 |
@parameterized.expand( |
| 378 |
[ |
| 379 |
param( |
| 380 |
date_string="2023-100", |
| 381 |
date_formats=["%Y-%j"], |
| 382 |
expected_result=datetime(2023, 4, 10), |
| 383 |
), |
| 384 |
param( |
| 385 |
date_string="2024-060", |
| 386 |
date_formats=["%Y-%j"], |
| 387 |
expected_result=datetime(2024, 2, 29), |
| 388 |
), |
| 389 |
param( |
| 390 |
date_string="2023 060", |
| 391 |
date_formats=["%Y %j"], |
| 392 |
expected_result=datetime(2023, 3, 1), |
| 393 |
), |
| 394 |
] |
| 395 |
) |
| 396 |
def test_should_parse_day_of_year_format( |
| 397 |
self, date_string, date_formats, expected_result |
| 398 |
): |
| 399 |
"""Format %%j (day of year) should correctly set both month and day.""" |
| 400 |
self.when_date_is_parsed_with_formats(date_string, date_formats) |
| 401 |
self.then_date_was_parsed() |
| 402 |
self.then_parsed_period_is("day") |
| 403 |
self.then_parsed_date_is(expected_result) |
| 404 |
|
| 405 |
@parameterized.expand( |
| 406 |
[ |
| 407 |
param( |
| 408 |
date_string="1999001", |
| 409 |
date_formats=["%Y%j"], |
| 410 |
expected_result=datetime(1999, 1, 1), |
| 411 |
), |
| 412 |
param( |
| 413 |
date_string="1999050", |
| 414 |
date_formats=["%Y%j"], |
| 415 |
expected_result=datetime(1999, 2, 19), |
| 416 |
), |
| 417 |
param( |
| 418 |
date_string="1999032", |
| 419 |
date_formats=["%Y%j"], |
| 420 |
expected_result=datetime(1999, 2, 1), |
| 421 |
), |
| 422 |
param( |
| 423 |
date_string="1999059", |
| 424 |
date_formats=["%Y%j"], |
| 425 |
expected_result=datetime(1999, 2, 28), |
| 426 |
), |
| 427 |
param( |
| 428 |
date_string="1999060", |
| 429 |
date_formats=["%Y%j"], |
| 430 |
expected_result=datetime(1999, 3, 1), |
| 431 |
), |
| 432 |
param( |
| 433 |
date_string="1999365", |
| 434 |
date_formats=["%Y%j"], |
| 435 |
expected_result=datetime(1999, 12, 31), |
| 436 |
), |
| 437 |
param( |
| 438 |
date_string="2000060", |
| 439 |
date_formats=["%Y%j"], |
| 440 |
expected_result=datetime(2000, 2, 29), |
| 441 |
), |
| 442 |
param( |
| 443 |
date_string="2000366", |
| 444 |
date_formats=["%Y%j"], |
| 445 |
expected_result=datetime(2000, 12, 31), |
| 446 |
), |
| 447 |
] |
| 448 |
) |
| 449 |
def test_should_parse_day_of_year_boundaries( |
| 450 |
self, date_string, date_formats, expected_result |
| 451 |
): |
| 452 |
"""Format %%j (day of year) should handle month and leap year edges.""" |
| 453 |
self.when_date_is_parsed_with_formats(date_string, date_formats) |
| 454 |
self.then_date_was_parsed() |
| 455 |
self.then_parsed_period_is("day") |
| 456 |
self.then_parsed_date_is(expected_result) |
| 457 |
|
| 458 |
@parameterized.expand( |
| 459 |
[ |
| 460 |
# 366 is only a valid day of year in leap years, and must not be |
| 461 |
# rolled over into the next year. |
| 462 |
param(date_string="1999366", date_formats=["%Y%j"]), |
| 463 |
param(date_string="2023-366", date_formats=["%Y-%j"]), |
| 464 |
param(date_string="1900366", date_formats=["%Y%j"]), |
| 465 |
param(date_string="2100 366", date_formats=["%Y %j"]), |
| 466 |
# Values that are out of the %j range altogether. |
| 467 |
param(date_string="1999000", date_formats=["%Y%j"]), |
| 468 |
param(date_string="1999367", date_formats=["%Y%j"]), |
| 469 |
param(date_string="1999777", date_formats=["%Y%j"]), |
| 470 |
] |
| 471 |
) |
| 472 |
def test_should_not_parse_out_of_range_day_of_year(self, date_string, date_formats): |
| 473 |
"""Format %%j (day of year) should reject days the year does not have.""" |
| 474 |
self.when_date_is_parsed_with_formats(date_string, date_formats) |
| 475 |
self.then_date_was_not_parsed() |
| 476 |
|
| 477 |
@parameterized.expand( |
| 478 |
[ |
| 479 |
param( |
| 480 |
date_string="09.16", |
| 481 |
date_formats=["%m.%d"], |
| 482 |
expected_month=9, |
| 483 |
expected_day=16, |
| 484 |
), |
| 485 |
] |
| 486 |
) |
| 487 |
def test_should_use_current_year_for_dates_without_year( |
| 488 |
self, date_string, date_formats, expected_month, expected_day |
| 489 |
): |
| 490 |
self.given_now(2015, 2, 4) |
| 491 |
self.when_date_is_parsed_with_formats(date_string, date_formats) |
| 492 |
self.then_date_was_parsed() |
| 493 |
self.then_parsed_period_is("day") |
| 494 |
self.then_parsed_date_is(datetime(2015, expected_month, expected_day)) |
| 495 |
|
| 496 |
@parameterized.expand( |
| 497 |
[ |
| 498 |
param( |
| 499 |
date_string="August 2014", |
| 500 |
date_formats=["%B %Y"], |
| 501 |
expected_year=2014, |
| 502 |
expected_month=8, |
| 503 |
today_day=12, |
| 504 |
prefer_day_of_month="first", |
| 505 |
expected_day=1, |
| 506 |
), |
| 507 |
param( |
| 508 |
date_string="August 2014", |
| 509 |
date_formats=["%B %Y"], |
| 510 |
expected_year=2014, |
| 511 |
expected_month=8, |
| 512 |
today_day=12, |
| 513 |
prefer_day_of_month="last", |
| 514 |
expected_day=31, |
| 515 |
), |
| 516 |
param( |
| 517 |
date_string="August 2014", |
| 518 |
date_formats=["%B %Y"], |
| 519 |
expected_year=2014, |
| 520 |
expected_month=8, |
| 521 |
today_day=12, |
| 522 |
prefer_day_of_month="current", |
| 523 |
expected_day=12, |
| 524 |
), |
| 525 |
] |
| 526 |
) |
| 527 |
def test_should_use_correct_day_from_settings_for_dates_without_day( |
| 528 |
self, |
| 529 |
date_string, |
| 530 |
date_formats, |
| 531 |
expected_year, |
| 532 |
expected_month, |
| 533 |
today_day, |
| 534 |
prefer_day_of_month, |
| 535 |
expected_day, |
| 536 |
): |
| 537 |
self.given_now(2014, 8, today_day) |
| 538 |
settings_mod = Settings() |
| 539 |
settings_mod.PREFER_DAY_OF_MONTH = prefer_day_of_month |
| 540 |
self.when_date_is_parsed_with_formats(date_string, date_formats, settings_mod) |
| 541 |
self.then_date_was_parsed() |
| 542 |
self.then_parsed_period_is("month") |
| 543 |
self.then_parsed_date_is( |
| 544 |
datetime(year=expected_year, month=expected_month, day=expected_day) |
| 545 |
) |
| 546 |
|
| 547 |
@parameterized.expand( |
| 548 |
[ |
| 549 |
param( |
| 550 |
date_string="2014", |
| 551 |
date_formats=["%Y"], |
| 552 |
expected_year=2014, |
| 553 |
prefer_month_of_year="first", |
| 554 |
current_month=7, |
| 555 |
expected_month=1, |
| 556 |
expected_day=1, |
| 557 |
), |
| 558 |
param( |
| 559 |
date_string="2014", |
| 560 |
date_formats=["%Y"], |
| 561 |
expected_year=2014, |
| 562 |
prefer_month_of_year="current", |
| 563 |
current_month=7, |
| 564 |
expected_month=7, |
| 565 |
expected_day=1, |
| 566 |
), |
| 567 |
param( |
| 568 |
date_string="2014", |
| 569 |
date_formats=["%Y"], |
| 570 |
expected_year=2014, |
| 571 |
prefer_month_of_year="last", |
| 572 |
current_month=7, |
| 573 |
expected_month=12, |
| 574 |
expected_day=1, |
| 575 |
), |
| 576 |
] |
| 577 |
) |
| 578 |
def test_should_use_correct_month_from_settings_for_dates_without_month( |
| 579 |
self, |
| 580 |
date_string, |
| 581 |
date_formats, |
| 582 |
expected_year, |
| 583 |
prefer_month_of_year, |
| 584 |
current_month, |
| 585 |
expected_month, |
| 586 |
expected_day, |
| 587 |
): |
| 588 |
self.given_now(2014, current_month, 1) |
| 589 |
settings_mod = Settings() |
| 590 |
settings_mod.PREFER_MONTH_OF_YEAR = prefer_month_of_year |
| 591 |
self.when_date_is_parsed_with_formats(date_string, date_formats, settings_mod) |
| 592 |
self.then_date_was_parsed() |
| 593 |
self.then_parsed_period_is("year") |
| 594 |
self.then_parsed_date_is( |
| 595 |
datetime(year=expected_year, month=expected_month, day=expected_day) |
| 596 |
) |
| 597 |
|
| 598 |
@parameterized.expand( |
| 599 |
[ |
| 600 |
param( |
| 601 |
date_string="2014", |
| 602 |
date_formats=["%Y"], |
| 603 |
current_day=15, |
| 604 |
current_month=4, |
| 605 |
prefer_day_of_month="last", |
| 606 |
prefer_month_of_year="last", |
| 607 |
expected_year=2014, |
| 608 |
expected_month=12, |
| 609 |
expected_day=31, |
| 610 |
) |
| 611 |
] |
| 612 |
) |
| 613 |
def test_should_use_correct_day_n_month_from_settings_for_dates_without_day_n_month( |
| 614 |
self, |
| 615 |
date_string, |
| 616 |
date_formats, |
| 617 |
current_day, |
| 618 |
current_month, |
| 619 |
prefer_day_of_month, |
| 620 |
prefer_month_of_year, |
| 621 |
expected_year, |
| 622 |
expected_month, |
| 623 |
expected_day, |
| 624 |
): |
| 625 |
self.given_now(2014, current_month, current_day) |
| 626 |
settings_mod = Settings() |
| 627 |
settings_mod.PREFER_DAY_OF_MONTH = prefer_day_of_month |
| 628 |
settings_mod.PREFER_MONTH_OF_YEAR = prefer_month_of_year |
| 629 |
self.when_date_is_parsed_with_formats(date_string, date_formats, settings_mod) |
| 630 |
self.then_date_was_parsed() |
| 631 |
self.then_parsed_period_is("year") |
| 632 |
self.then_parsed_date_is( |
| 633 |
datetime(year=expected_year, month=expected_month, day=expected_day) |
| 634 |
) |
| 635 |
|
| 636 |
def given_now(self, year, month, day, **time): |
| 637 |
now = datetime(year, month, day, **time) |
| 638 |
datetime_mock = Mock(wraps=datetime) |
| 639 |
datetime_mock.utcnow = Mock(return_value=now) |
| 640 |
datetime_mock.now = Mock(return_value=now) |
| 641 |
datetime_mock.today = Mock(return_value=now) |
| 642 |
self.add_patch(patch("dateparser.date.datetime", new=datetime_mock)) |
| 643 |
self.add_patch(patch("dateparser.utils.datetime", new=datetime_mock)) |
| 644 |
|
| 645 |
def when_date_is_parsed_with_formats( |
| 646 |
self, date_string, date_formats, custom_settings=None |
| 647 |
): |
| 648 |
self.result = date.parse_with_formats( |
| 649 |
date_string, date_formats, custom_settings or Settings() |
| 650 |
) |
| 651 |
|
| 652 |
def then_date_was_not_parsed(self): |
| 653 |
self.assertIsNotNone(self.result) |
| 654 |
self.assertIsNone(self.result["date_obj"]) |
| 655 |
|
| 656 |
def then_date_was_parsed(self): |
| 657 |
self.assertIsNotNone(self.result) |
| 658 |
self.assertIsNotNone(self.result["date_obj"]) |
| 659 |
|
| 660 |
def then_parsed_date_is(self, date_obj): |
| 661 |
self.assertEqual(date_obj.date(), self.result["date_obj"].date()) |
| 662 |
|
| 663 |
def then_parsed_period_is(self, period): |
| 664 |
self.assertEqual(period, self.result["period"]) |
| 665 |
|
| 666 |
|
| 667 |
class TestDateDataParser(BaseTestCase): |
| 668 |
def setUp(self): |
| 669 |
super().setUp() |
| 670 |
self.parser = NotImplemented |
| 671 |
self.result = NotImplemented |
| 672 |
self.multiple_results = NotImplemented |
| 673 |
|
| 674 |
@parameterized.expand( |
| 675 |
[ |
| 676 |
param("10:04am EDT"), |
| 677 |
] |
| 678 |
) |
| 679 |
def test_time_without_date_should_use_today(self, date_string): |
| 680 |
self.given_parser(settings={"RELATIVE_BASE": datetime(2020, 7, 19)}) |
| 681 |
self.when_date_string_is_parsed(date_string) |
| 682 |
self.then_date_was_parsed() |
| 683 |
self.then_parsed_date_is(datetime(2020, 7, 19).date()) |
| 684 |
|
| 685 |
@parameterized.expand( |
| 686 |
[ |
| 687 |
# Today |
| 688 |
param("today", days_ago=0), |
| 689 |
param("Today", days_ago=0), |
| 690 |
param("TODAY", days_ago=0), |
| 691 |
param("Сегодня", days_ago=0), |
| 692 |
param("Hoje", days_ago=0), |
| 693 |
param("Oggi", days_ago=0), |
| 694 |
# Yesterday |
| 695 |
param("yesterday", days_ago=1), |
| 696 |
param(" Yesterday \n", days_ago=1), |
| 697 |
param("Ontem", days_ago=1), |
| 698 |
param("Ieri", days_ago=1), |
| 699 |
param("вчера", days_ago=1), |
| 700 |
param("снощи", days_ago=1), |
| 701 |
# Day before yesterday |
| 702 |
param("the day before yesterday", days_ago=2), |
| 703 |
param("The DAY before Yesterday", days_ago=2), |
| 704 |
param("Anteontem", days_ago=2), |
| 705 |
param("Avant-hier", days_ago=2), |
| 706 |
param("вчера", days_ago=1), |
| 707 |
param("снощи", days_ago=1), |
| 708 |
] |
| 709 |
) |
| 710 |
def test_temporal_nouns_are_parsed(self, date_string, days_ago): |
| 711 |
self.given_parser() |
| 712 |
self.when_date_string_is_parsed(date_string) |
| 713 |
self.then_date_was_parsed() |
| 714 |
self.then_date_is_n_days_ago(days=days_ago) |
| 715 |
|
| 716 |
def test_should_not_assume_language_too_early(self): |
| 717 |
dates_to_parse = { |
| 718 |
"07/07/2014": datetime(2014, 7, 7).date(), # any language |
| 719 |
"07.jul.2014 | 12:52": datetime(2014, 7, 7).date(), # en, es, pt, nl |
| 720 |
"07.ago.2014 | 12:52": datetime(2014, 8, 7).date(), # es, it, pt |
| 721 |
"07.feb.2014 | 12:52": datetime( |
| 722 |
2014, 2, 7 |
| 723 |
).date(), # en, de, es, it, nl, ro |
| 724 |
"07.ene.2014 | 12:52": datetime(2014, 1, 7).date(), # es |
| 725 |
} |
| 726 |
|
| 727 |
self.given_parser( |
| 728 |
restrict_to_languages=["en", "de", "fr", "it", "pt", "nl", "ro", "es", "ru"] |
| 729 |
) |
| 730 |
self.when_multiple_dates_are_parsed(dates_to_parse.keys()) |
| 731 |
self.then_all_results_were_parsed() |
| 732 |
self.then_parsed_dates_are(list(dates_to_parse.values())) |
| 733 |
|
| 734 |
@parameterized.expand( |
| 735 |
[ |
| 736 |
param(date_string="11 Marzo, 2014", locale="es"), |
| 737 |
param(date_string="13 Septiembre, 2014", locale="es"), |
| 738 |
param(date_string="Сегодня", locale="ru"), |
| 739 |
param(date_string="Avant-hier", locale="fr"), |
| 740 |
param(date_string="Anteontem", locale="pt"), |
| 741 |
param(date_string="ธันวาคม 11, 2014, 08:55:08 PM", locale="th"), |
| 742 |
param(date_string="Anteontem", locale="pt"), |
| 743 |
param(date_string="14 aprilie 2014", locale="ro"), |
| 744 |
param(date_string="11 Ağustos, 2014", locale="tr"), |
| 745 |
param(date_string="pon 16. čer 2014 10:07:43", locale="cs"), |
| 746 |
param(date_string="24 януари 2015г.", locale="bg"), |
| 747 |
] |
| 748 |
) |
| 749 |
def test_returned_detected_locale_should_be(self, date_string, locale): |
| 750 |
self.given_parser() |
| 751 |
self.when_date_string_is_parsed(date_string) |
| 752 |
self.then_detected_locale(locale) |
| 753 |
|
| 754 |
def test_try_previous_locales_false(self): |
| 755 |
self.given_parser(try_previous_locales=False) |
| 756 |
self.when_date_string_is_parsed("Mañana") # es |
| 757 |
self.then_detected_locale("es") |
| 758 |
self.when_date_string_is_parsed("2020-05-01") |
| 759 |
self.then_detected_locale("en") |
| 760 |
|
| 761 |
def test_try_previous_locales_true(self): |
| 762 |
self.given_parser(try_previous_locales=True) |
| 763 |
self.when_date_string_is_parsed("Mañana") # es |
| 764 |
self.then_detected_locale("es") |
| 765 |
self.when_date_string_is_parsed("2020-05-01") |
| 766 |
self.then_detected_locale("es") |
| 767 |
|
| 768 |
def test_try_previous_locales_order_deterministic(self): |
| 769 |
self.given_parser(try_previous_locales=True) |
| 770 |
self.when_date_string_is_parsed("Mañana") # es |
| 771 |
self.then_detected_locale("es") |
| 772 |
self.when_date_string_is_parsed("Понедельник") # ru |
| 773 |
self.then_detected_locale("ru") |
| 774 |
self.then_previous_locales_is(["es", "ru"]) |
| 775 |
|
| 776 |
@parameterized.expand( |
| 777 |
[ |
| 778 |
param("2014-10-09T17:57:39+00:00"), |
| 779 |
] |
| 780 |
) |
| 781 |
def test_get_date_data_should_not_strip_timezone_info(self, date_string): |
| 782 |
self.given_parser() |
| 783 |
self.when_date_string_is_parsed(date_string) |
| 784 |
self.then_date_was_parsed() |
| 785 |
self.then_parsed_date_has_timezone() |
| 786 |
|
| 787 |
def test_ignore_surrounding_text_setting_parses_wrapped_date(self): |
| 788 |
# Issue #518: a date wrapped in harmless extra text is parsed when the |
| 789 |
# ``IGNORE_SURROUNDING_TEXT`` setting is enabled. |
| 790 |
self.given_parser( |
| 791 |
restrict_to_languages=["fr"], settings={"IGNORE_SURROUNDING_TEXT": True} |
| 792 |
) |
| 793 |
self.when_date_string_is_parsed("Actualisé le 17 avril 2019") |
| 794 |
self.then_date_was_parsed() |
| 795 |
self.then_parsed_datetime_is(datetime(2019, 4, 17)) |
| 796 |
self.then_detected_locale("fr") |
| 797 |
self.then_period_is("day") |
| 798 |
|
| 799 |
def test_get_date_data_ignores_surrounding_text_only_when_enabled(self): |
| 800 |
# The safety property of the ``IGNORE_SURROUNDING_TEXT`` setting: by |
| 801 |
# default, ``get_date_data`` keeps requiring the whole string to be a |
| 802 |
# date, so language detection and ``search_dates`` are unaffected. |
| 803 |
self.given_parser(restrict_to_languages=["fr"]) |
| 804 |
self.when_date_string_is_parsed("Actualisé le 17 avril 2019") |
| 805 |
self.then_date_was_not_parsed() |
| 806 |
|
| 807 |
def test_ignore_surrounding_text_with_try_previous_locales(self): |
| 808 |
self.given_parser( |
| 809 |
restrict_to_languages=["fr", "en"], |
| 810 |
try_previous_locales=True, |
| 811 |
settings={"IGNORE_SURROUNDING_TEXT": True}, |
| 812 |
) |
| 813 |
self.when_date_string_is_parsed("Actualisé le 17 avril 2019") |
| 814 |
self.then_parsed_datetime_is(datetime(2019, 4, 17)) |
| 815 |
self.then_previous_locales_is(["fr"]) |
| 816 |
self.when_date_string_is_parsed("Publié le 16 avril 2019") |
| 817 |
self.then_parsed_datetime_is(datetime(2019, 4, 16)) |
| 818 |
self.then_previous_locales_is(["fr"]) |
| 819 |
|
| 820 |
@parameterized.expand( |
| 821 |
[ |
| 822 |
param( |
| 823 |
date_string="14 giu 13", |
| 824 |
date_formats=["%y %B %d"], |
| 825 |
expected_result=datetime(2014, 6, 13), |
| 826 |
), |
| 827 |
param( |
| 828 |
date_string="14_luglio_15", |
| 829 |
date_formats=["%y_%B_%d"], |
| 830 |
expected_result=datetime(2014, 7, 15), |
| 831 |
), |
| 832 |
param( |
| 833 |
date_string="14_LUGLIO_15", |
| 834 |
date_formats=["%y_%B_%d"], |
| 835 |
expected_result=datetime(2014, 7, 15), |
| 836 |
), |
| 837 |
param( |
| 838 |
date_string="10.01.2016, 20:35", |
| 839 |
date_formats=["%d.%m.%Y, %H:%M"], |
| 840 |
expected_result=datetime(2016, 1, 10, 20, 35), |
| 841 |
), |
| 842 |
] |
| 843 |
) |
| 844 |
def test_parse_date_using_format(self, date_string, date_formats, expected_result): |
| 845 |
self.given_local_tz_offset(0) |
| 846 |
self.given_parser() |
| 847 |
self.when_date_string_is_parsed(date_string, date_formats) |
| 848 |
self.then_date_was_parsed() |
| 849 |
self.then_period_is("day") |
| 850 |
self.then_parsed_datetime_is(expected_result) |
| 851 |
|
| 852 |
@parameterized.expand( |
| 853 |
[ |
| 854 |
param( |
| 855 |
date_string="11/09/2007", date_formats={"date_formats": ["%d/%m/%Y"]} |
| 856 |
), |
| 857 |
param(date_string="16.09.03 11:55", date_formats=111), |
| 858 |
param(date_string="08-01-1998", date_formats=12.56), |
| 859 |
] |
| 860 |
) |
| 861 |
def test_parsing_date_using_invalid_type_date_format_must_raise_error( |
| 862 |
self, date_string, date_formats |
| 863 |
): |
| 864 |
self.given_local_tz_offset(0) |
| 865 |
self.given_parser() |
| 866 |
self.when_date_string_is_parsed(date_string, date_formats) |
| 867 |
self.then_error_was_raised( |
| 868 |
TypeError, |
| 869 |
[ |
| 870 |
"Date formats should be list, tuple or set of strings", |
| 871 |
"'{}' object is not iterable".format(type(date_formats).__name__), |
| 872 |
], |
| 873 |
) |
| 874 |
|
| 875 |
@parameterized.expand( |
| 876 |
[ |
| 877 |
param(date_string={"date": "12/11/1998"}), |
| 878 |
param(date_string=[2017, 12, 1]), |
| 879 |
param(date_string=2018), |
| 880 |
param(date_string=12.2000), |
| 881 |
param(date_string=datetime(year=2009, month=12, day=7)), |
| 882 |
] |
| 883 |
) |
| 884 |
def test_parsing_date_using_invalid_type_date_string_must_raise_error( |
| 885 |
self, date_string |
| 886 |
): |
| 887 |
self.given_parser() |
| 888 |
self.when_date_string_is_parsed(date_string) |
| 889 |
self.then_error_was_raised(TypeError, ["Input type must be str"]) |
| 890 |
|
| 891 |
@parameterized.expand( |
| 892 |
[ |
| 893 |
param( |
| 894 |
date_string="2014/11/17 14:56 EDT", |
| 895 |
expected_result=datetime(2014, 11, 17, 18, 56), |
| 896 |
), |
| 897 |
] |
| 898 |
) |
| 899 |
def test_parse_date_with_timezones_not_using_formats( |
| 900 |
self, date_string, expected_result |
| 901 |
): |
| 902 |
self.given_parser(settings={"TO_TIMEZONE": "UTC"}) |
| 903 |
self.when_date_string_is_parsed(date_string) |
| 904 |
self.then_date_was_parsed() |
| 905 |
self.then_period_is("day") |
| 906 |
self.result["date_obj"] = self.result["date_obj"].replace(tzinfo=None) |
| 907 |
self.then_parsed_datetime_is(expected_result) |
| 908 |
|
| 909 |
@parameterized.expand( |
| 910 |
[ |
| 911 |
param( |
| 912 |
date_string="2014/11/17 14:56 EDT", |
| 913 |
date_formats=["%Y/%m/%d %H:%M EDT"], |
| 914 |
expected_result=datetime(2014, 11, 17, 14, 56), |
| 915 |
), |
| 916 |
] |
| 917 |
) |
| 918 |
def test_parse_date_with_timezones_using_formats_ignore_timezone( |
| 919 |
self, date_string, date_formats, expected_result |
| 920 |
): |
| 921 |
self.given_local_tz_offset(0) |
| 922 |
self.given_parser() |
| 923 |
self.when_date_string_is_parsed(date_string, date_formats) |
| 924 |
self.then_date_was_parsed() |
| 925 |
self.then_period_is("day") |
| 926 |
self.then_parsed_datetime_is(expected_result) |
| 927 |
|
| 928 |
@parameterized.expand( |
| 929 |
[ |
| 930 |
param( |
| 931 |
date_string="08-08-2014\xa018:29", |
| 932 |
expected_result=datetime(2014, 8, 8, 18, 29), |
| 933 |
), |
| 934 |
] |
| 935 |
) |
| 936 |
def test_should_parse_with_no_break_space_in_dates( |
| 937 |
self, date_string, expected_result |
| 938 |
): |
| 939 |
self.given_parser() |
| 940 |
self.when_date_string_is_parsed(date_string) |
| 941 |
self.then_date_was_parsed() |
| 942 |
self.then_period_is("day") |
| 943 |
self.then_parsed_datetime_is(expected_result) |
| 944 |
|
| 945 |
@parameterized.expand( |
| 946 |
[ |
| 947 |
param( |
| 948 |
date_string="12 jan 1876", |
| 949 |
expected_result=(datetime(1876, 1, 12, 0, 0), "day", "en"), |
| 950 |
), |
| 951 |
param( |
| 952 |
date_string="02/09/16", |
| 953 |
expected_result=(datetime(2016, 2, 9, 0, 0), "day", "en"), |
| 954 |
), |
| 955 |
param( |
| 956 |
date_string="10 giu 2018", |
| 957 |
expected_result=(datetime(2018, 6, 10, 0, 0), "day", "it"), |
| 958 |
), |
| 959 |
] |
| 960 |
) |
| 961 |
def test_get_date_tuple(self, date_string, expected_result): |
| 962 |
self.given_parser() |
| 963 |
self.when_get_date_tuple_is_called(date_string) |
| 964 |
self.then_returned_tuple_is(expected_result) |
| 965 |
|
| 966 |
@parameterized.expand( |
| 967 |
[ |
| 968 |
param( |
| 969 |
"Mo", |
| 970 |
datetime(2025, 7, 28, 0, 0), |
| 971 |
), |
| 972 |
param( |
| 973 |
"Tu", |
| 974 |
datetime(2025, 7, 29, 0, 0), |
| 975 |
), |
| 976 |
param( |
| 977 |
"We", |
| 978 |
datetime(2025, 7, 30, 0, 0), |
| 979 |
), |
| 980 |
param( |
| 981 |
"Th", |
| 982 |
datetime(2025, 7, 31, 0, 0), |
| 983 |
), |
| 984 |
param( |
| 985 |
"Fr", |
| 986 |
datetime(2025, 8, 1, 0, 0), |
| 987 |
), |
| 988 |
param( |
| 989 |
"Sa", |
| 990 |
datetime(2025, 7, 26, 0, 0), |
| 991 |
), |
| 992 |
param( |
| 993 |
"Su", |
| 994 |
datetime(2025, 7, 27, 0, 0), |
| 995 |
), |
| 996 |
] |
| 997 |
) |
| 998 |
def test_short_weekday_names(self, date_string, expected): |
| 999 |
if "Mo" in date_string: |
| 1000 |
pytest.xfail( |
| 1001 |
"Known bug: 'Mo' is being interpreted as a month instead of a weekday and needs to be fixed." |
| 1002 |
) |
| 1003 |
|
| 1004 |
self.given_parser(["en"]) |
| 1005 |
self.given_now(2025, 8, 1) |
| 1006 |
self.when_date_string_is_parsed(date_string) |
| 1007 |
self.then_parsed_datetime_is(expected) |
| 1008 |
|
| 1009 |
def given_now(self, year, month, day, **time): |
| 1010 |
now = real_datetime.datetime(year, month, day, **time) |
| 1011 |
|
| 1012 |
# Patch the datetime *class* in each target module |
| 1013 |
class DateParserDateTime(real_datetime.datetime): |
| 1014 |
@classmethod |
| 1015 |
def now(cls, tz=None): |
| 1016 |
return now.replace(tzinfo=tz) if tz else now |
| 1017 |
|
| 1018 |
@classmethod |
| 1019 |
def utcnow(cls): |
| 1020 |
return now |
| 1021 |
|
| 1022 |
@classmethod |
| 1023 |
def today(cls): |
| 1024 |
return now |
| 1025 |
|
| 1026 |
self.add_patch(patch("dateparser.date.datetime", DateParserDateTime)) |
| 1027 |
self.add_patch(patch("dateparser.parser.datetime", DateParserDateTime)) |
| 1028 |
|
| 1029 |
def given_parser(self, restrict_to_languages=None, **params): |
| 1030 |
self.parser = date.DateDataParser(languages=restrict_to_languages, **params) |
| 1031 |
|
| 1032 |
def given_local_tz_offset(self, offset): |
| 1033 |
self.add_patch( |
| 1034 |
patch.object( |
| 1035 |
dateparser.timezone_parser, |
| 1036 |
"local_tz_offset", |
| 1037 |
new=timedelta(seconds=3600 * offset), |
| 1038 |
) |
| 1039 |
) |
| 1040 |
|
| 1041 |
def when_date_string_is_parsed(self, date_string, date_formats=None): |
| 1042 |
try: |
| 1043 |
self.result = self.parser.get_date_data(date_string, date_formats) |
| 1044 |
except Exception as error: |
| 1045 |
self.error = error |
| 1046 |
|
| 1047 |
def when_multiple_dates_are_parsed(self, date_strings): |
| 1048 |
self.multiple_results = [] |
| 1049 |
for date_string in date_strings: |
| 1050 |
try: |
| 1051 |
result = self.parser.get_date_data(date_string) |
| 1052 |
except Exception as error: |
| 1053 |
result = error |
| 1054 |
finally: |
| 1055 |
self.multiple_results.append(result) |
| 1056 |
|
| 1057 |
def when_get_date_tuple_is_called(self, date_string): |
| 1058 |
self.result = self.parser.get_date_tuple(date_string) |
| 1059 |
|
| 1060 |
def then_date_was_parsed(self): |
| 1061 |
self.assertIsNotNone(self.result["date_obj"]) |
| 1062 |
|
| 1063 |
def then_date_was_not_parsed(self): |
| 1064 |
self.assertIsNone(self.result["date_obj"]) |
| 1065 |
|
| 1066 |
def then_date_locale(self): |
| 1067 |
self.assertIsNotNone(self.result["locale"]) |
| 1068 |
|
| 1069 |
def then_date_is_n_days_ago(self, days): |
| 1070 |
today = datetime.now().date() |
| 1071 |
expected_date = today - timedelta(days=days) |
| 1072 |
self.assertEqual(expected_date, self.result["date_obj"].date()) |
| 1073 |
|
| 1074 |
def then_all_results_were_parsed(self): |
| 1075 |
self.assertNotIn(None, self.multiple_results) |
| 1076 |
|
| 1077 |
def then_parsed_dates_are(self, expected_dates): |
| 1078 |
self.assertEqual( |
| 1079 |
expected_dates, |
| 1080 |
[result["date_obj"].date() for result in self.multiple_results], |
| 1081 |
) |
| 1082 |
|
| 1083 |
def then_detected_locale(self, locale): |
| 1084 |
self.assertEqual(locale, self.result["locale"]) |
| 1085 |
|
| 1086 |
def then_period_is(self, day): |
| 1087 |
self.assertEqual(day, self.result["period"]) |
| 1088 |
|
| 1089 |
def then_parsed_datetime_is(self, expected_datetime): |
| 1090 |
self.assertEqual(expected_datetime, self.result["date_obj"]) |
| 1091 |
|
| 1092 |
def then_parsed_date_is(self, expected_date): |
| 1093 |
self.assertEqual(expected_date, self.result["date_obj"].date()) |
| 1094 |
|
| 1095 |
def then_parsed_date_has_timezone(self): |
| 1096 |
self.assertTrue(hasattr(self.result["date_obj"], "tzinfo")) |
| 1097 |
|
| 1098 |
def then_returned_tuple_is(self, expected_tuple): |
| 1099 |
self.assertEqual(expected_tuple, self.result) |
| 1100 |
|
| 1101 |
def then_previous_locales_is(self, expected_list): |
| 1102 |
self.assertEqual( |
| 1103 |
expected_list, [locale.shortname for locale in self.parser.previous_locales] |
| 1104 |
) |
| 1105 |
|
| 1106 |
|
| 1107 |
class TestParserInitialization(BaseTestCase): |
| 1108 |
def setUp(self): |
| 1109 |
super().setUp() |
| 1110 |
self.parser = NotImplemented |
| 1111 |
|
| 1112 |
@parameterized.expand( |
| 1113 |
[ |
| 1114 |
param(languages="en"), |
| 1115 |
param(languages={"languages": ["en", "he", "it"]}), |
| 1116 |
] |
| 1117 |
) |
| 1118 |
def test_error_raised_for_invalid_languages_argument(self, languages): |
| 1119 |
self.when_parser_is_initialized(languages=languages) |
| 1120 |
self.then_error_was_raised( |
| 1121 |
TypeError, |
| 1122 |
["languages argument must be a list (%r given)" % type(languages)], |
| 1123 |
) |
| 1124 |
|
| 1125 |
@parameterized.expand( |
| 1126 |
[ |
| 1127 |
param(locales="en-001"), |
| 1128 |
param(locales={"locales": ["zh-Hant-HK", "ha-NE", "se-SE"]}), |
| 1129 |
] |
| 1130 |
) |
| 1131 |
def test_error_raised_for_invalid_locales_argument(self, locales): |
| 1132 |
self.when_parser_is_initialized(locales=locales) |
| 1133 |
self.then_error_was_raised( |
| 1134 |
TypeError, ["locales argument must be a list (%r given)" % type(locales)] |
| 1135 |
) |
| 1136 |
|
| 1137 |
@parameterized.expand( |
| 1138 |
[ |
| 1139 |
param(region=["AW", "BE"]), |
| 1140 |
param(region=150), |
| 1141 |
] |
| 1142 |
) |
| 1143 |
def test_error_raised_for_invalid_region_argument(self, region): |
| 1144 |
self.when_parser_is_initialized(region=region) |
| 1145 |
self.then_error_was_raised( |
| 1146 |
TypeError, ["region argument must be str (%r given)" % type(region)] |
| 1147 |
) |
| 1148 |
|
| 1149 |
@parameterized.expand( |
| 1150 |
[ |
| 1151 |
param(try_previous_locales=["ar-OM", "pt-PT", "fr-CG", "uk"]), |
| 1152 |
param(try_previous_locales="uk"), |
| 1153 |
param(try_previous_locales={"try_previous_locales": True}), |
| 1154 |
param(try_previous_locales=0), |
| 1155 |
] |
| 1156 |
) |
| 1157 |
def test_error_raised_for_invalid_try_previous_locales_argument( |
| 1158 |
self, try_previous_locales |
| 1159 |
): |
| 1160 |
self.when_parser_is_initialized(try_previous_locales=try_previous_locales) |
| 1161 |
self.then_error_was_raised( |
| 1162 |
TypeError, |
| 1163 |
[ |
| 1164 |
"try_previous_locales argument must be a boolean (%r given)" |
| 1165 |
% type(try_previous_locales) |
| 1166 |
], |
| 1167 |
) |
| 1168 |
|
| 1169 |
@parameterized.expand( |
| 1170 |
[ |
| 1171 |
param(use_given_order=["da", "pt", "ja", "sv"]), |
| 1172 |
param(use_given_order="uk"), |
| 1173 |
param(use_given_order={"use_given_order": True}), |
| 1174 |
param(use_given_order=1), |
| 1175 |
] |
| 1176 |
) |
| 1177 |
def test_error_raised_for_invalid_use_given_order_argument(self, use_given_order): |
| 1178 |
self.when_parser_is_initialized( |
| 1179 |
locales=["en", "es"], use_given_order=use_given_order |
| 1180 |
) |
| 1181 |
self.then_error_was_raised( |
| 1182 |
TypeError, |
| 1183 |
[ |
| 1184 |
"use_given_order argument must be a boolean (%r given)" |
| 1185 |
% type(use_given_order) |
| 1186 |
], |
| 1187 |
) |
| 1188 |
|
| 1189 |
def test_error_is_raised_when_use_given_order_is_True_and_locales_and_languages_is_None( |
| 1190 |
self, |
| 1191 |
): |
| 1192 |
self.when_parser_is_initialized(use_given_order=True) |
| 1193 |
self.then_error_was_raised( |
| 1194 |
ValueError, |
| 1195 |
["locales or languages must be given if use_given_order is True"], |
| 1196 |
) |
| 1197 |
|
| 1198 |
def test_no_error_for_order_with_languages_without_locales(self): |
| 1199 |
self.when_parser_is_initialized(languages=["en", "fr"], use_given_order=True) |
| 1200 |
|
| 1201 |
def when_parser_is_initialized( |
| 1202 |
self, |
| 1203 |
languages=None, |
| 1204 |
locales=None, |
| 1205 |
region=None, |
| 1206 |
try_previous_locales=True, |
| 1207 |
use_given_order=False, |
| 1208 |
): |
| 1209 |
try: |
| 1210 |
self.parser = date.DateDataParser( |
| 1211 |
languages=languages, |
| 1212 |
locales=locales, |
| 1213 |
region=region, |
| 1214 |
try_previous_locales=try_previous_locales, |
| 1215 |
use_given_order=use_given_order, |
| 1216 |
) |
| 1217 |
except Exception as error: |
| 1218 |
self.error = error |
| 1219 |
|
| 1220 |
|
| 1221 |
class TestSanitizeDate(BaseTestCase): |
| 1222 |
def test_remove_year_in_russian(self): |
| 1223 |
self.assertEqual(date.sanitize_date("2005г."), "2005") |
| 1224 |
self.assertEqual(date.sanitize_date("2005г. 20:37"), "2005 20:37") |
| 1225 |
self.assertEqual(date.sanitize_date("2005 г."), "2005") |
| 1226 |
self.assertEqual(date.sanitize_date("2005 г. 15:24"), "2005 15:24") |
| 1227 |
self.assertEqual(date.sanitize_date("Авг."), "Авг") |
| 1228 |
|
| 1229 |
def test_sanitize_date_colons(self): |
| 1230 |
self.assertEqual(date.sanitize_date("2019:"), "2019") |
| 1231 |
self.assertEqual(date.sanitize_date("31/07/2019:"), "31/07/2019") |
| 1232 |
|
| 1233 |
|
| 1234 |
class TestDateLocaleParser(BaseTestCase): |
| 1235 |
def setUp(self): |
| 1236 |
super().setUp() |
| 1237 |
|
| 1238 |
@parameterized.expand( |
| 1239 |
[ |
| 1240 |
param( |
| 1241 |
date_data=DateData(date_obj=datetime(1999, 10, 1, 0, 0)) |
| 1242 |
), # missing period |
| 1243 |
param(date_data=DateData(period="day")), # missing date_obj |
| 1244 |
param( |
| 1245 |
date_data={"date": datetime(2007, 1, 22, 0, 0), "period": "day"} |
| 1246 |
), # wrong format (dict) |
| 1247 |
param( |
| 1248 |
date_data=DateData(date_obj=datetime(2020, 10, 29, 0, 0), period="hour") |
| 1249 |
), # wrong period |
| 1250 |
param( |
| 1251 |
date_data=[datetime(2007, 1, 22, 0, 0), "day"] |
| 1252 |
), # wrong format (list) |
| 1253 |
param(date_data=DateData(date_obj=None, period="day")), # date_obj is None |
| 1254 |
param( |
| 1255 |
date_data=DateData(date_obj="29/05/1994", period="day") |
| 1256 |
), # wrong date_obj format |
| 1257 |
] |
| 1258 |
) |
| 1259 |
def test_is_valid_date_data(self, date_data): |
| 1260 |
self.given_parser( |
| 1261 |
language=["en"], |
| 1262 |
date_string="10 jan 2000", |
| 1263 |
date_formats=None, |
| 1264 |
settings=Settings(), |
| 1265 |
) |
| 1266 |
self.when_date_object_is_validated(date_data) |
| 1267 |
self.then_date_object_is_invalid() |
| 1268 |
|
| 1269 |
def given_parser(self, language, date_string, date_formats, settings): |
| 1270 |
self.parser = date._DateLocaleParser( |
| 1271 |
language, date_string, date_formats, settings |
| 1272 |
) |
| 1273 |
|
| 1274 |
def when_date_object_is_validated(self, date_data): |
| 1275 |
self.is_valid_date_obj = self.parser._is_valid_date_data(date_data) |
| 1276 |
|
| 1277 |
def then_date_object_is_invalid(self): |
| 1278 |
self.assertFalse(self.is_valid_date_obj) |
| 1279 |
|
| 1280 |
|
| 1281 |
class TestTimestampParser(BaseTestCase): |
| 1282 |
def given_parser(self, **params): |
| 1283 |
self.parser = date.DateDataParser(**params) |
| 1284 |
|
| 1285 |
def given_tzstr(self, tzstr): |
| 1286 |
# Save the existing value |
| 1287 |
self.old_tzstr = os.environ["TZ"] if "TZ" in os.environ else None |
| 1288 |
|
| 1289 |
# Overwrite the value, or remove it |
| 1290 |
if tzstr is not None: |
| 1291 |
os.environ["TZ"] = tzstr |
| 1292 |
elif "TZ" in os.environ: |
| 1293 |
del os.environ["TZ"] |
| 1294 |
|
| 1295 |
# Call tzset |
| 1296 |
tzset() |
| 1297 |
|
| 1298 |
def reset_tzstr(self): |
| 1299 |
# If we never set it with given_tzstr, don't bother resetting it |
| 1300 |
if not hasattr(self, "old_tzstr"): |
| 1301 |
return |
| 1302 |
|
| 1303 |
# Restore the old value, or remove it if null |
| 1304 |
if self.old_tzstr is not None: |
| 1305 |
os.environ["TZ"] = self.old_tzstr |
| 1306 |
elif "TZ" in os.environ: |
| 1307 |
del os.environ["TZ"] |
| 1308 |
|
| 1309 |
# Remove the local attribute |
| 1310 |
del self.old_tzstr |
| 1311 |
|
| 1312 |
# Restore the old timezone behavior |
| 1313 |
tzset() |
| 1314 |
|
| 1315 |
def test_timestamp_in_milliseconds(self): |
| 1316 |
self.assertEqual( |
| 1317 |
date.get_date_from_timestamp("1570308760263", None), |
| 1318 |
datetime.fromtimestamp(1570308760).replace(microsecond=263000), |
| 1319 |
) |
| 1320 |
|
| 1321 |
@parameterized.expand( |
| 1322 |
product( |
| 1323 |
["1570308760"], |
| 1324 |
["EDT", "EST", "PDT", "PST", "UTC", "local"], |
| 1325 |
[None, "EDT", "EST", "PDT", "PST", "UTC"], |
| 1326 |
["EST5EDT4", "UTC0", "PST8PDT7", None], |
| 1327 |
) |
| 1328 |
) |
| 1329 |
def test_timestamp_with_different_timestr( |
| 1330 |
self, timestamp, timezone, to_timezone, tzstr |
| 1331 |
): |
| 1332 |
settings = { |
| 1333 |
"RETURN_AS_TIMEZONE_AWARE": True, |
| 1334 |
"TIMEZONE": timezone, |
| 1335 |
} |
| 1336 |
|
| 1337 |
# is TO_TIMEZONE supposed to be allowed to be False, or None ??? |
| 1338 |
if to_timezone is not None: |
| 1339 |
settings["TO_TIMEZONE"] = to_timezone |
| 1340 |
|
| 1341 |
self.given_parser(settings=settings) |
| 1342 |
|
| 1343 |
self.given_tzstr(tzstr) |
| 1344 |
|
| 1345 |
self.assertEqual( |
| 1346 |
self.parser.get_date_data(timestamp)["date_obj"], |
| 1347 |
datetime.fromtimestamp(int(timestamp), dttz.utc), |
| 1348 |
) |
| 1349 |
|
| 1350 |
self.reset_tzstr() |
| 1351 |
|
| 1352 |
def test_timestamp_in_microseconds(self): |
| 1353 |
self.assertEqual( |
| 1354 |
date.get_date_from_timestamp("1570308760263111", None), |
| 1355 |
datetime.fromtimestamp(1570308760).replace(microsecond=263111), |
| 1356 |
) |
| 1357 |
|
| 1358 |
@parameterized.expand( |
| 1359 |
[ |
| 1360 |
param( |
| 1361 |
input_timestamp="-1570308760", |
| 1362 |
negative=True, |
| 1363 |
result=datetime.fromtimestamp(-1570308760), |
| 1364 |
), |
| 1365 |
param(input_timestamp="-1570308760", negative=False, result=None), |
| 1366 |
param(input_timestamp="1570308760", negative=True, result=None), |
| 1367 |
param( |
| 1368 |
input_timestamp="1570308760", |
| 1369 |
negative=False, |
| 1370 |
result=datetime.fromtimestamp(1570308760), |
| 1371 |
), |
| 1372 |
] |
| 1373 |
) |
| 1374 |
def test_timestamp_with_negative(self, input_timestamp, negative, result): |
| 1375 |
self.assertEqual( |
| 1376 |
date.get_date_from_timestamp(input_timestamp, None, negative=negative), |
| 1377 |
result, |
| 1378 |
) |
| 1379 |
|
| 1380 |
@parameterized.expand( |
| 1381 |
[ |
| 1382 |
param(date_string="15703087602631"), |
| 1383 |
param(date_string="157030876026xx"), |
| 1384 |
param(date_string="1570308760263x"), |
| 1385 |
param(date_string="157030876026311"), |
| 1386 |
param(date_string="15703087602631x"), |
| 1387 |
param(date_string="15703087602631xx"), |
| 1388 |
param(date_string="15703087602631111"), |
| 1389 |
param(date_string="1570308760263111x"), |
| 1390 |
param(date_string="1570308760263111xx"), |
| 1391 |
param(date_string="1570308760263111222"), |
| 1392 |
] |
| 1393 |
) |
| 1394 |
def test_timestamp_with_wrong_length(self, date_string): |
| 1395 |
self.assertEqual(date.get_date_from_timestamp(date_string, None), None) |
| 1396 |
|
| 1397 |
|
| 1398 |
if __name__ == "__main__": |
| 1399 |
unittest.main() |