| 1 |
from itertools import chain |
| 2 |
|
| 3 |
import regex as re |
| 4 |
from parameterized import parameterized |
| 5 |
|
| 6 |
from dateparser.data import language_locale_dict |
| 7 |
from dateparser.languages import default_loader |
| 8 |
from tests import BaseTestCase |
| 9 |
|
| 10 |
DEFAULT_MONTH_PATTERN = re.compile(r"^M?\d+$", re.U) |
| 11 |
INVALID_AM_PM_PATTERN = re.compile(r"^[AaPp]\.?\s+[Mm]$") |
| 12 |
|
| 13 |
all_locale_shortnames = list( |
| 14 |
chain(language_locale_dict.keys(), *language_locale_dict.values()) |
| 15 |
) |
| 16 |
all_locales = list( |
| 17 |
default_loader.get_locales( |
| 18 |
locales=all_locale_shortnames, allow_conflicting_locales=True |
| 19 |
) |
| 20 |
) |
| 21 |
all_locale_params = [[locale] for locale in all_locales] |
| 22 |
|
| 23 |
VALID_KEYS = [ |
| 24 |
"name", |
| 25 |
"date_order", |
| 26 |
"skip", |
| 27 |
"pertain", |
| 28 |
"simplifications", |
| 29 |
"no_word_spacing", |
| 30 |
"ago", |
| 31 |
"in", |
| 32 |
"monday", |
| 33 |
"tuesday", |
| 34 |
"wednesday", |
| 35 |
"thursday", |
| 36 |
"friday", |
| 37 |
"saturday", |
| 38 |
"sunday", |
| 39 |
"january", |
| 40 |
"february", |
| 41 |
"march", |
| 42 |
"april", |
| 43 |
"may", |
| 44 |
"june", |
| 45 |
"july", |
| 46 |
"august", |
| 47 |
"september", |
| 48 |
"october", |
| 49 |
"november", |
| 50 |
"december", |
| 51 |
"decade", |
| 52 |
"year", |
| 53 |
"month", |
| 54 |
"week", |
| 55 |
"day", |
| 56 |
"hour", |
| 57 |
"minute", |
| 58 |
"second", |
| 59 |
"am", |
| 60 |
"pm", |
| 61 |
"relative-type", |
| 62 |
"relative-type-regex", |
| 63 |
"sentence_splitter_group", |
| 64 |
] |
| 65 |
|
| 66 |
NECESSARY_KEYS = [ |
| 67 |
"name", |
| 68 |
"monday", |
| 69 |
"tuesday", |
| 70 |
"wednesday", |
| 71 |
"thursday", |
| 72 |
"friday", |
| 73 |
"saturday", |
| 74 |
"sunday", |
| 75 |
"january", |
| 76 |
"february", |
| 77 |
"march", |
| 78 |
"april", |
| 79 |
"may", |
| 80 |
"june", |
| 81 |
"july", |
| 82 |
"august", |
| 83 |
"september", |
| 84 |
"october", |
| 85 |
"november", |
| 86 |
"december", |
| 87 |
"year", |
| 88 |
"month", |
| 89 |
"week", |
| 90 |
"day", |
| 91 |
"hour", |
| 92 |
"minute", |
| 93 |
"second", |
| 94 |
] |
| 95 |
|
| 96 |
MONTH_NAMES = [ |
| 97 |
"january", |
| 98 |
"february", |
| 99 |
"march", |
| 100 |
"april", |
| 101 |
"may", |
| 102 |
"june", |
| 103 |
"july", |
| 104 |
"august", |
| 105 |
"september", |
| 106 |
"october", |
| 107 |
"november", |
| 108 |
"december", |
| 109 |
] |
| 110 |
|
| 111 |
|
| 112 |
def is_invalid_translation(translation): |
| 113 |
return (not (translation and isinstance(translation, str))) or "." in translation |
| 114 |
|
| 115 |
|
| 116 |
def is_invalid_month_translation(translation): |
| 117 |
return ( |
| 118 |
(not (translation and isinstance(translation, str))) |
| 119 |
or "." in translation |
| 120 |
or DEFAULT_MONTH_PATTERN.match(translation) |
| 121 |
) |
| 122 |
|
| 123 |
|
| 124 |
def is_invalid_am_pm_translation(translation): |
| 125 |
return ( |
| 126 |
(not (translation and isinstance(translation, str))) |
| 127 |
or "." in translation |
| 128 |
or INVALID_AM_PM_PATTERN.match(translation) |
| 129 |
) |
| 130 |
|
| 131 |
|
| 132 |
def is_invalid_simplification(simplification): |
| 133 |
if not isinstance(simplification, dict) or len(simplification) != 1: |
| 134 |
return True |
| 135 |
key, value = list(simplification.items())[0] |
| 136 |
return not isinstance(key, str) or not isinstance(value, str) |
| 137 |
|
| 138 |
|
| 139 |
def is_invalid_relative_mapping(relative_mapping): |
| 140 |
key, value = relative_mapping |
| 141 |
if not (key and value and isinstance(key, str) and isinstance(value, list)): |
| 142 |
return True |
| 143 |
return not all([isinstance(x, str) for x in value]) |
| 144 |
|
| 145 |
|
| 146 |
def is_invalid_relative_regex_mapping(relative_regex_mapping): |
| 147 |
key, value = relative_regex_mapping |
| 148 |
if not (key and value and isinstance(key, str) and isinstance(value, list)): |
| 149 |
return True |
| 150 |
return not all([isinstance(x, str) for x in value]) |
| 151 |
|
| 152 |
|
| 153 |
class TestLocaleInfo(BaseTestCase): |
| 154 |
def setUp(self): |
| 155 |
super().setUp() |
| 156 |
self.info = NotImplemented |
| 157 |
self.shortname = NotImplemented |
| 158 |
|
| 159 |
@parameterized.expand(all_locale_params) |
| 160 |
def test_extra_keys(self, locale): |
| 161 |
self.given_locale_info(locale) |
| 162 |
extra_keys = list(set(self.info.keys()) - set(VALID_KEYS)) |
| 163 |
self.assertFalse( |
| 164 |
extra_keys, "Extra keys found for {}: {}".format(self.shortname, extra_keys) |
| 165 |
) |
| 166 |
|
| 167 |
@parameterized.expand(all_locale_params) |
| 168 |
def test_necessary_keys(self, locale): |
| 169 |
self.given_locale_info(locale) |
| 170 |
self.then_keys_present_in_info(NECESSARY_KEYS) |
| 171 |
self.then_translations_present_in_info(NECESSARY_KEYS) |
| 172 |
|
| 173 |
@parameterized.expand(all_locale_params) |
| 174 |
def test_name(self, locale): |
| 175 |
self.given_locale_info(locale) |
| 176 |
self.then_name_is_valid() |
| 177 |
|
| 178 |
@parameterized.expand(all_locale_params) |
| 179 |
def test_date_order(self, locale): |
| 180 |
self.given_locale_info(locale) |
| 181 |
self.then_date_order_is_valid() |
| 182 |
|
| 183 |
@parameterized.expand(all_locale_params) |
| 184 |
def test_january_translation(self, locale): |
| 185 |
self.given_locale_info(locale) |
| 186 |
self.then_month_translations_are_valid("january") |
| 187 |
|
| 188 |
@parameterized.expand(all_locale_params) |
| 189 |
def test_february_translation(self, locale): |
| 190 |
self.given_locale_info(locale) |
| 191 |
self.then_month_translations_are_valid("february") |
| 192 |
|
| 193 |
@parameterized.expand(all_locale_params) |
| 194 |
def test_march_translation(self, locale): |
| 195 |
self.given_locale_info(locale) |
| 196 |
self.then_month_translations_are_valid("march") |
| 197 |
|
| 198 |
@parameterized.expand(all_locale_params) |
| 199 |
def test_april_translation(self, locale): |
| 200 |
self.given_locale_info(locale) |
| 201 |
self.then_month_translations_are_valid("april") |
| 202 |
|
| 203 |
@parameterized.expand(all_locale_params) |
| 204 |
def test_may_translation(self, locale): |
| 205 |
self.given_locale_info(locale) |
| 206 |
self.then_month_translations_are_valid("may") |
| 207 |
|
| 208 |
@parameterized.expand(all_locale_params) |
| 209 |
def test_june_translation(self, locale): |
| 210 |
self.given_locale_info(locale) |
| 211 |
self.then_month_translations_are_valid("june") |
| 212 |
|
| 213 |
@parameterized.expand(all_locale_params) |
| 214 |
def test_july_translation(self, locale): |
| 215 |
self.given_locale_info(locale) |
| 216 |
self.then_month_translations_are_valid("july") |
| 217 |
|
| 218 |
@parameterized.expand(all_locale_params) |
| 219 |
def test_august_translation(self, locale): |
| 220 |
self.given_locale_info(locale) |
| 221 |
self.then_month_translations_are_valid("august") |
| 222 |
|
| 223 |
@parameterized.expand(all_locale_params) |
| 224 |
def test_september_translation(self, locale): |
| 225 |
self.given_locale_info(locale) |
| 226 |
self.then_month_translations_are_valid("september") |
| 227 |
|
| 228 |
@parameterized.expand(all_locale_params) |
| 229 |
def test_october_translation(self, locale): |
| 230 |
self.given_locale_info(locale) |
| 231 |
self.then_month_translations_are_valid("october") |
| 232 |
|
| 233 |
@parameterized.expand(all_locale_params) |
| 234 |
def test_november_translation(self, locale): |
| 235 |
self.given_locale_info(locale) |
| 236 |
self.then_month_translations_are_valid("november") |
| 237 |
|
| 238 |
@parameterized.expand(all_locale_params) |
| 239 |
def test_december_translation(self, locale): |
| 240 |
self.given_locale_info(locale) |
| 241 |
self.then_month_translations_are_valid("december") |
| 242 |
|
| 243 |
@parameterized.expand(all_locale_params) |
| 244 |
def test_am_translation(self, locale): |
| 245 |
self.given_locale_info(locale) |
| 246 |
self.then_am_pm_translations_are_valid("am") |
| 247 |
|
| 248 |
@parameterized.expand(all_locale_params) |
| 249 |
def test_pm_translation(self, locale): |
| 250 |
self.given_locale_info(locale) |
| 251 |
self.then_am_pm_translations_are_valid("pm") |
| 252 |
|
| 253 |
@parameterized.expand(all_locale_params) |
| 254 |
def test_sunday_translation(self, locale): |
| 255 |
self.given_locale_info(locale) |
| 256 |
self.then_translations_are_valid("sunday") |
| 257 |
|
| 258 |
@parameterized.expand(all_locale_params) |
| 259 |
def test_monday_translation(self, locale): |
| 260 |
self.given_locale_info(locale) |
| 261 |
self.then_translations_are_valid("monday") |
| 262 |
|
| 263 |
@parameterized.expand(all_locale_params) |
| 264 |
def test_tuesday_translation(self, locale): |
| 265 |
self.given_locale_info(locale) |
| 266 |
self.then_translations_are_valid("tuesday") |
| 267 |
|
| 268 |
@parameterized.expand(all_locale_params) |
| 269 |
def test_wednesday_translation(self, locale): |
| 270 |
self.given_locale_info(locale) |
| 271 |
self.then_translations_are_valid("wednesday") |
| 272 |
|
| 273 |
@parameterized.expand(all_locale_params) |
| 274 |
def test_thursday_translation(self, locale): |
| 275 |
self.given_locale_info(locale) |
| 276 |
self.then_translations_are_valid("thursday") |
| 277 |
|
| 278 |
@parameterized.expand(all_locale_params) |
| 279 |
def test_friday_translation(self, locale): |
| 280 |
self.given_locale_info(locale) |
| 281 |
self.then_translations_are_valid("friday") |
| 282 |
|
| 283 |
@parameterized.expand(all_locale_params) |
| 284 |
def test_saturday_translation(self, locale): |
| 285 |
self.given_locale_info(locale) |
| 286 |
self.then_translations_are_valid("saturday") |
| 287 |
|
| 288 |
@parameterized.expand(all_locale_params) |
| 289 |
def test_year_translation(self, locale): |
| 290 |
self.given_locale_info(locale) |
| 291 |
self.then_translations_are_valid("year") |
| 292 |
|
| 293 |
@parameterized.expand(all_locale_params) |
| 294 |
def test_month_translation(self, locale): |
| 295 |
self.given_locale_info(locale) |
| 296 |
self.then_translations_are_valid("month") |
| 297 |
|
| 298 |
@parameterized.expand(all_locale_params) |
| 299 |
def test_week_translation(self, locale): |
| 300 |
self.given_locale_info(locale) |
| 301 |
self.then_translations_are_valid("week") |
| 302 |
|
| 303 |
@parameterized.expand(all_locale_params) |
| 304 |
def test_day_translation(self, locale): |
| 305 |
self.given_locale_info(locale) |
| 306 |
self.then_translations_are_valid("day") |
| 307 |
|
| 308 |
@parameterized.expand(all_locale_params) |
| 309 |
def test_hour_translation(self, locale): |
| 310 |
self.given_locale_info(locale) |
| 311 |
self.then_translations_are_valid("hour") |
| 312 |
|
| 313 |
@parameterized.expand(all_locale_params) |
| 314 |
def test_minute_translation(self, locale): |
| 315 |
self.given_locale_info(locale) |
| 316 |
self.then_translations_are_valid("minute") |
| 317 |
|
| 318 |
@parameterized.expand(all_locale_params) |
| 319 |
def test_second_translation(self, locale): |
| 320 |
self.given_locale_info(locale) |
| 321 |
self.then_translations_are_valid("second") |
| 322 |
|
| 323 |
@parameterized.expand(all_locale_params) |
| 324 |
def test_ago_translation(self, locale): |
| 325 |
self.given_locale_info(locale) |
| 326 |
self.then_translations_are_valid("ago") |
| 327 |
|
| 328 |
@parameterized.expand(all_locale_params) |
| 329 |
def test_in_translation(self, locale): |
| 330 |
self.given_locale_info(locale) |
| 331 |
self.then_translations_are_valid("in") |
| 332 |
|
| 333 |
@parameterized.expand(all_locale_params) |
| 334 |
def test_skip_tokens(self, locale): |
| 335 |
self.given_locale_info(locale) |
| 336 |
self.then_skip_pertain_tokens_are_valid("skip") |
| 337 |
|
| 338 |
@parameterized.expand(all_locale_params) |
| 339 |
def test_pertain_tokens(self, locale): |
| 340 |
self.given_locale_info(locale) |
| 341 |
self.then_skip_pertain_tokens_are_valid("pertain") |
| 342 |
|
| 343 |
@parameterized.expand(all_locale_params) |
| 344 |
def test_simplifications(self, locale): |
| 345 |
self.given_locale_info(locale) |
| 346 |
self.then_simplifications_are_valid() |
| 347 |
|
| 348 |
@parameterized.expand(all_locale_params) |
| 349 |
def test_relative_type(self, locale): |
| 350 |
self.given_locale_info(locale) |
| 351 |
self.then_relative_type_is_valid() |
| 352 |
|
| 353 |
@parameterized.expand(all_locale_params) |
| 354 |
def test_relative_type_regex(self, locale): |
| 355 |
self.given_locale_info(locale) |
| 356 |
self.then_relative_type_regex_is_valid() |
| 357 |
|
| 358 |
@parameterized.expand(all_locale_params) |
| 359 |
def test_no_word_spacing(self, locale): |
| 360 |
self.given_locale_info(locale) |
| 361 |
self.then_no_word_spacing_is_valid() |
| 362 |
|
| 363 |
def given_locale_info(self, locale): |
| 364 |
self.info = locale.info |
| 365 |
self.shortname = locale.shortname |
| 366 |
|
| 367 |
def then_keys_present_in_info(self, keys_list): |
| 368 |
absent_keys = list(set(keys_list) - set(self.info.keys())) |
| 369 |
self.assertFalse( |
| 370 |
absent_keys, |
| 371 |
"{} not found in locale {}".format(", ".join(absent_keys), self.shortname), |
| 372 |
) |
| 373 |
|
| 374 |
def then_translations_present_in_info(self, keys_list): |
| 375 |
no_translation_keys = [key for key in keys_list if not self.info.get(key)] |
| 376 |
self.assertFalse( |
| 377 |
no_translation_keys, |
| 378 |
"Translations for {} not found in locale {}".format( |
| 379 |
", ".join(no_translation_keys), self.shortname |
| 380 |
), |
| 381 |
) |
| 382 |
|
| 383 |
def then_name_is_valid(self): |
| 384 |
name = self.info["name"] |
| 385 |
self.assertIsInstance( |
| 386 |
name, |
| 387 |
str, |
| 388 |
"Invalid type for name: {} for locale {}".format( |
| 389 |
type(name).__name__, self.shortname |
| 390 |
), |
| 391 |
) |
| 392 |
self.assertEqual( |
| 393 |
name, |
| 394 |
self.shortname, |
| 395 |
"Invalid name: {} for locale {}".format(name, self.shortname), |
| 396 |
) |
| 397 |
|
| 398 |
def then_date_order_is_valid(self): |
| 399 |
if "date_order" in self.info: |
| 400 |
date_order = self.info["date_order"] |
| 401 |
self.assertIsInstance( |
| 402 |
date_order, |
| 403 |
str, |
| 404 |
"Invalid type for date_order: {} for locale {}".format( |
| 405 |
type(date_order).__name__, self.shortname |
| 406 |
), |
| 407 |
) |
| 408 |
self.assertIn( |
| 409 |
date_order, |
| 410 |
["DMY", "DYM", "MDY", "MYD", "YDM", "YMD"], |
| 411 |
"Invalid date_order {} for {}".format(date_order, self.shortname), |
| 412 |
) |
| 413 |
|
| 414 |
def then_month_translations_are_valid(self, month): |
| 415 |
if month in self.info: |
| 416 |
month_translations = self.info[month] |
| 417 |
self.assertIsInstance( |
| 418 |
month_translations, |
| 419 |
list, |
| 420 |
"Invalid type for {}: {} for locale {}".format( |
| 421 |
month, type(month_translations).__name__, self.shortname |
| 422 |
), |
| 423 |
) |
| 424 |
invalid_translations = list( |
| 425 |
filter(is_invalid_month_translation, month_translations) |
| 426 |
) |
| 427 |
self.assertFalse( |
| 428 |
invalid_translations, |
| 429 |
"Invalid translations for {}: {} for locale {}".format( |
| 430 |
month, ", ".join(map(repr, invalid_translations)), self.shortname |
| 431 |
), |
| 432 |
) |
| 433 |
|
| 434 |
def then_am_pm_translations_are_valid(self, key): |
| 435 |
if key in self.info: |
| 436 |
translations_list = self.info[key] |
| 437 |
self.assertIsInstance( |
| 438 |
translations_list, |
| 439 |
list, |
| 440 |
"Invalid type for {}: {} for locale {}".format( |
| 441 |
key, type(translations_list).__name__, self.shortname |
| 442 |
), |
| 443 |
) |
| 444 |
invalid_translations = list( |
| 445 |
filter(is_invalid_am_pm_translation, translations_list) |
| 446 |
) |
| 447 |
self.assertFalse( |
| 448 |
invalid_translations, |
| 449 |
"Invalid translations for {}: {} for locale {}".format( |
| 450 |
key, ", ".join(map(repr, invalid_translations)), self.shortname |
| 451 |
), |
| 452 |
) |
| 453 |
|
| 454 |
def then_translations_are_valid(self, key): |
| 455 |
if key in self.info: |
| 456 |
translations_list = self.info[key] |
| 457 |
self.assertIsInstance( |
| 458 |
translations_list, |
| 459 |
list, |
| 460 |
"Invalid type for {}: {} for locale {}".format( |
| 461 |
key, type(translations_list).__name__, self.shortname |
| 462 |
), |
| 463 |
) |
| 464 |
invalid_translations = list( |
| 465 |
filter(is_invalid_translation, translations_list) |
| 466 |
) |
| 467 |
self.assertFalse( |
| 468 |
invalid_translations, |
| 469 |
"Invalid translations for {}: {} for locale {}".format( |
| 470 |
key, ", ".join(map(repr, invalid_translations)), self.shortname |
| 471 |
), |
| 472 |
) |
| 473 |
|
| 474 |
def then_skip_pertain_tokens_are_valid(self, key): |
| 475 |
if key in self.info: |
| 476 |
tokens_list = self.info[key] |
| 477 |
self.assertIsInstance( |
| 478 |
tokens_list, |
| 479 |
list, |
| 480 |
"Invalid type for {}: {} for locale {}".format( |
| 481 |
key, type(tokens_list).__name__, self.shortname |
| 482 |
), |
| 483 |
) |
| 484 |
invalid_tokens = [ |
| 485 |
token |
| 486 |
for token in tokens_list |
| 487 |
if not token or not isinstance(token, str) |
| 488 |
] |
| 489 |
self.assertFalse( |
| 490 |
invalid_tokens, |
| 491 |
"Invalid tokens for {}: {} for locale {}".format( |
| 492 |
key, ", ".join(map(repr, invalid_tokens)), self.shortname |
| 493 |
), |
| 494 |
) |
| 495 |
|
| 496 |
def then_simplifications_are_valid(self): |
| 497 |
if "simplifications" in self.info: |
| 498 |
simplifications_list = self.info["simplifications"] |
| 499 |
self.assertIsInstance( |
| 500 |
simplifications_list, |
| 501 |
list, |
| 502 |
"Invalid type for simplifications: {} for locale {}".format( |
| 503 |
type(simplifications_list).__name__, self.shortname |
| 504 |
), |
| 505 |
) |
| 506 |
invalid_simplifications = list( |
| 507 |
filter(is_invalid_simplification, simplifications_list) |
| 508 |
) |
| 509 |
self.assertFalse( |
| 510 |
invalid_simplifications, |
| 511 |
"Invalid simplifications {} for locale {}".format( |
| 512 |
", ".join(map(repr, invalid_simplifications)), self.shortname |
| 513 |
) |
| 514 |
+ ", each simplification must be a single string to string mapping", |
| 515 |
) |
| 516 |
|
| 517 |
def then_relative_type_is_valid(self): |
| 518 |
if "relative-type" in self.info: |
| 519 |
relative_type_dict = self.info["relative-type"] |
| 520 |
self.assertIsInstance( |
| 521 |
relative_type_dict, |
| 522 |
dict, |
| 523 |
"Invalid type for relative-type: {} for locale {}".format( |
| 524 |
type(relative_type_dict).__name__, self.shortname |
| 525 |
), |
| 526 |
) |
| 527 |
invalid_relative_type = list( |
| 528 |
filter(is_invalid_relative_mapping, relative_type_dict.items()) |
| 529 |
) |
| 530 |
self.assertFalse( |
| 531 |
invalid_relative_type, |
| 532 |
"Invalid relative-type mappings {} for locale {}".format( |
| 533 |
", ".join(map(repr, invalid_relative_type)), self.shortname |
| 534 |
) |
| 535 |
+ ", each mapping must be a string to list (of strings) mapping", |
| 536 |
) |
| 537 |
|
| 538 |
def then_relative_type_regex_is_valid(self): |
| 539 |
if "relative-type-regex" in self.info: |
| 540 |
relative_type_dict = self.info["relative-type-regex"] |
| 541 |
self.assertIsInstance( |
| 542 |
relative_type_dict, |
| 543 |
dict, |
| 544 |
"Invalid type for relative-type-regex: {} for locale {}".format( |
| 545 |
type(relative_type_dict).__name__, self.shortname |
| 546 |
), |
| 547 |
) |
| 548 |
invalid_relative_type_regex = list( |
| 549 |
filter(is_invalid_relative_regex_mapping, relative_type_dict.items()) |
| 550 |
) |
| 551 |
self.assertFalse( |
| 552 |
invalid_relative_type_regex, |
| 553 |
"Invalid relative-type-regex mappings {} for locale {}".format( |
| 554 |
", ".join(map(repr, invalid_relative_type_regex)), self.shortname |
| 555 |
) |
| 556 |
+ ", each mapping must be a string to list (of strings) mapping", |
| 557 |
) |
| 558 |
|
| 559 |
def then_no_word_spacing_is_valid(self): |
| 560 |
if "no_word_spacing" in self.info: |
| 561 |
no_word_spacing = self.info["no_word_spacing"] |
| 562 |
self.assertIsInstance( |
| 563 |
no_word_spacing, |
| 564 |
str, |
| 565 |
"Invalid type for no_word_spacing: {} for locale {}".format( |
| 566 |
type(no_word_spacing).__name__, self.shortname |
| 567 |
), |
| 568 |
) |
| 569 |
self.assertIn( |
| 570 |
no_word_spacing, |
| 571 |
["True", "False"], |
| 572 |
"Invalid no_word_spacing {} for {}".format( |
| 573 |
no_word_spacing, self.shortname |
| 574 |
), |
| 575 |
) |