| 1 |
from operator import attrgetter |
| 2 |
|
| 3 |
import regex as re |
| 4 |
from parameterized import param, parameterized |
| 5 |
|
| 6 |
from dateparser.languages.loader import default_loader |
| 7 |
from tests import BaseTestCase |
| 8 |
|
| 9 |
|
| 10 |
class TestLoading(BaseTestCase): |
| 11 |
def setUp(self): |
| 12 |
super().setUp() |
| 13 |
self.locale_generator = NotImplemented |
| 14 |
|
| 15 |
@classmethod |
| 16 |
def setUpClass(cls): |
| 17 |
cls.data_loader = default_loader |
| 18 |
cls.data_loader._loaded_locales = {} |
| 19 |
cls.data_loader._loaded_languages = {} |
| 20 |
|
| 21 |
@parameterized.expand( |
| 22 |
[ |
| 23 |
param( |
| 24 |
given_languages=["es", "fr", "en"], |
| 25 |
given_locales=None, |
| 26 |
given_region=None, |
| 27 |
loaded_languages=["en", "es", "fr"], |
| 28 |
loaded_locales=["en", "es", "fr"], |
| 29 |
expected_locales=["es", "fr", "en"], |
| 30 |
), |
| 31 |
param( |
| 32 |
given_languages=None, |
| 33 |
given_locales=["fr-BF", "ar-SO", "asa"], |
| 34 |
given_region=None, |
| 35 |
loaded_languages=["en", "es", "fr", "ar", "asa"], |
| 36 |
loaded_locales=["en", "es", "fr", "fr-BF", "ar-SO", "asa"], |
| 37 |
expected_locales=["fr-BF", "ar-SO", "asa"], |
| 38 |
), |
| 39 |
param( |
| 40 |
given_languages=None, |
| 41 |
given_locales=["nl-CW", "so-KE", "sr-Latn-XK", "zgh"], |
| 42 |
given_region=None, |
| 43 |
loaded_languages=[ |
| 44 |
"en", |
| 45 |
"es", |
| 46 |
"fr", |
| 47 |
"ar", |
| 48 |
"asa", |
| 49 |
"nl", |
| 50 |
"so", |
| 51 |
"sr-Latn", |
| 52 |
"zgh", |
| 53 |
], |
| 54 |
loaded_locales=[ |
| 55 |
"en", |
| 56 |
"es", |
| 57 |
"fr", |
| 58 |
"fr-BF", |
| 59 |
"ar-SO", |
| 60 |
"asa", |
| 61 |
"nl-CW", |
| 62 |
"so-KE", |
| 63 |
"sr-Latn-XK", |
| 64 |
"zgh", |
| 65 |
], |
| 66 |
expected_locales=["nl-CW", "so-KE", "sr-Latn-XK", "zgh"], |
| 67 |
), |
| 68 |
param( |
| 69 |
given_languages=["pt", "zh-Hant", "zh-Hans"], |
| 70 |
given_locales=None, |
| 71 |
given_region="MO", |
| 72 |
loaded_languages=[ |
| 73 |
"en", |
| 74 |
"es", |
| 75 |
"fr", |
| 76 |
"ar", |
| 77 |
"asa", |
| 78 |
"nl", |
| 79 |
"so", |
| 80 |
"sr-Latn", |
| 81 |
"zgh", |
| 82 |
"pt", |
| 83 |
"zh-Hant", |
| 84 |
"zh-Hans", |
| 85 |
], |
| 86 |
loaded_locales=[ |
| 87 |
"en", |
| 88 |
"es", |
| 89 |
"fr", |
| 90 |
"fr-BF", |
| 91 |
"ar-SO", |
| 92 |
"asa", |
| 93 |
"nl-CW", |
| 94 |
"so-KE", |
| 95 |
"sr-Latn-XK", |
| 96 |
"zgh", |
| 97 |
"pt-MO", |
| 98 |
"zh-Hant-MO", |
| 99 |
"zh-Hans-MO", |
| 100 |
], |
| 101 |
expected_locales=["pt-MO", "zh-Hant-MO", "zh-Hans-MO"], |
| 102 |
), |
| 103 |
param( |
| 104 |
given_languages=["pt", "he"], |
| 105 |
given_locales=["ru-UA", "ckb-IR", "sq-XK"], |
| 106 |
given_region="MO", |
| 107 |
loaded_languages=[ |
| 108 |
"en", |
| 109 |
"es", |
| 110 |
"fr", |
| 111 |
"ar", |
| 112 |
"asa", |
| 113 |
"nl", |
| 114 |
"so", |
| 115 |
"sr-Latn", |
| 116 |
"zgh", |
| 117 |
"pt", |
| 118 |
"zh-Hant", |
| 119 |
"zh-Hans", |
| 120 |
"ru", |
| 121 |
"ckb", |
| 122 |
"sq", |
| 123 |
], |
| 124 |
loaded_locales=[ |
| 125 |
"en", |
| 126 |
"es", |
| 127 |
"fr", |
| 128 |
"fr-BF", |
| 129 |
"ar-SO", |
| 130 |
"asa", |
| 131 |
"nl-CW", |
| 132 |
"so-KE", |
| 133 |
"sr-Latn-XK", |
| 134 |
"zgh", |
| 135 |
"pt-MO", |
| 136 |
"zh-Hant-MO", |
| 137 |
"zh-Hans-MO", |
| 138 |
"ru-UA", |
| 139 |
"ckb-IR", |
| 140 |
"sq-XK", |
| 141 |
], |
| 142 |
expected_locales=["ru-UA", "ckb-IR", "sq-XK"], |
| 143 |
), |
| 144 |
param( |
| 145 |
given_languages=["da", "ja"], |
| 146 |
given_locales=["shi-Latn", "teo-KE", "ewo", "vun"], |
| 147 |
given_region="AO", |
| 148 |
loaded_languages=[ |
| 149 |
"en", |
| 150 |
"es", |
| 151 |
"fr", |
| 152 |
"ar", |
| 153 |
"asa", |
| 154 |
"nl", |
| 155 |
"so", |
| 156 |
"sr-Latn", |
| 157 |
"zgh", |
| 158 |
"pt", |
| 159 |
"zh-Hant", |
| 160 |
"zh-Hans", |
| 161 |
"ru", |
| 162 |
"ckb", |
| 163 |
"sq", |
| 164 |
"shi-Latn", |
| 165 |
"teo", |
| 166 |
"ewo", |
| 167 |
"vun", |
| 168 |
], |
| 169 |
loaded_locales=[ |
| 170 |
"en", |
| 171 |
"es", |
| 172 |
"fr", |
| 173 |
"fr-BF", |
| 174 |
"ar-SO", |
| 175 |
"asa", |
| 176 |
"nl-CW", |
| 177 |
"so-KE", |
| 178 |
"sr-Latn-XK", |
| 179 |
"zgh", |
| 180 |
"pt-MO", |
| 181 |
"zh-Hant-MO", |
| 182 |
"zh-Hans-MO", |
| 183 |
"ru-UA", |
| 184 |
"ckb-IR", |
| 185 |
"sq-XK", |
| 186 |
"shi-Latn", |
| 187 |
"teo-KE", |
| 188 |
"ewo", |
| 189 |
"vun", |
| 190 |
], |
| 191 |
expected_locales=["shi-Latn", "teo-KE", "ewo", "vun"], |
| 192 |
), |
| 193 |
param( |
| 194 |
given_languages=["en"], |
| 195 |
given_locales=None, |
| 196 |
given_region="US", |
| 197 |
loaded_languages=[ |
| 198 |
"en", |
| 199 |
"es", |
| 200 |
"fr", |
| 201 |
"ar", |
| 202 |
"asa", |
| 203 |
"nl", |
| 204 |
"so", |
| 205 |
"sr-Latn", |
| 206 |
"zgh", |
| 207 |
"pt", |
| 208 |
"zh-Hant", |
| 209 |
"zh-Hans", |
| 210 |
"ru", |
| 211 |
"ckb", |
| 212 |
"sq", |
| 213 |
"shi-Latn", |
| 214 |
"teo", |
| 215 |
"ewo", |
| 216 |
"vun", |
| 217 |
], |
| 218 |
loaded_locales=[ |
| 219 |
"en", |
| 220 |
"es", |
| 221 |
"fr", |
| 222 |
"fr-BF", |
| 223 |
"ar-SO", |
| 224 |
"asa", |
| 225 |
"nl-CW", |
| 226 |
"so-KE", |
| 227 |
"sr-Latn-XK", |
| 228 |
"zgh", |
| 229 |
"pt-MO", |
| 230 |
"zh-Hant-MO", |
| 231 |
"zh-Hans-MO", |
| 232 |
"ru-UA", |
| 233 |
"ckb-IR", |
| 234 |
"sq-XK", |
| 235 |
"shi-Latn", |
| 236 |
"teo-KE", |
| 237 |
"ewo", |
| 238 |
"vun", |
| 239 |
"en-US", |
| 240 |
], |
| 241 |
expected_locales=["en-US"], |
| 242 |
), |
| 243 |
] |
| 244 |
) |
| 245 |
def test_loading( |
| 246 |
self, |
| 247 |
given_languages, |
| 248 |
given_locales, |
| 249 |
given_region, |
| 250 |
loaded_languages, |
| 251 |
loaded_locales, |
| 252 |
expected_locales, |
| 253 |
): |
| 254 |
self.load_data( |
| 255 |
languages=given_languages, locales=given_locales, region=given_region |
| 256 |
) |
| 257 |
self.then_locales_are_yielded_in_order(expected_locales) |
| 258 |
self.then_loaded_languages_are(loaded_languages) |
| 259 |
self.then_loaded_locales_are(loaded_locales) |
| 260 |
|
| 261 |
def load_data(self, languages, locales, region): |
| 262 |
self.locale_generator = self.data_loader.get_locales( |
| 263 |
languages=languages, locales=locales, region=region, use_given_order=True |
| 264 |
) |
| 265 |
|
| 266 |
def then_loaded_languages_are(self, loaded_languages): |
| 267 |
self.assertCountEqual( |
| 268 |
loaded_languages, self.data_loader._loaded_languages.keys() |
| 269 |
) |
| 270 |
|
| 271 |
def then_loaded_locales_are(self, loaded_locales): |
| 272 |
self.assertCountEqual(loaded_locales, self.data_loader._loaded_locales.keys()) |
| 273 |
|
| 274 |
def then_locales_are_yielded_in_order(self, expected_locales): |
| 275 |
self.assertEqual( |
| 276 |
list(map(attrgetter("shortname"), list(self.locale_generator))), |
| 277 |
expected_locales, |
| 278 |
) |
| 279 |
|
| 280 |
|
| 281 |
class TestLocaleDataLoader(BaseTestCase): |
| 282 |
UNKNOWN_LANGUAGES_EXCEPTION_RE = re.compile(r"Unknown language\(s\): (.+)") |
| 283 |
UNKNOWN_LOCALES_EXCEPTION_RE = re.compile(r"Unknown locale\(s\): (.+)") |
| 284 |
|
| 285 |
def setUp(self): |
| 286 |
super().setUp() |
| 287 |
self.data_loader = default_loader |
| 288 |
self.data_loader._loaded_languages = {} |
| 289 |
self.data_loader._loaded_locales = {} |
| 290 |
|
| 291 |
@parameterized.expand( |
| 292 |
[ |
| 293 |
param(given_locales=["es-MX", "ar-EG", "fr-DJ"]), |
| 294 |
param(given_locales=["pt-MO", "ru-KZ", "es-CU"]), |
| 295 |
param(given_locales=["zh-Hans-HK", "en-VG", "my"]), |
| 296 |
param(given_locales=["tl", "nl-SX", "de-BE"]), |
| 297 |
] |
| 298 |
) |
| 299 |
def test_loading_with_given_order(self, given_locales): |
| 300 |
self.load_data(given_locales, use_given_order=True) |
| 301 |
self.then_locales_are_yielded_in_order(given_locales) |
| 302 |
|
| 303 |
@parameterized.expand( |
| 304 |
[ |
| 305 |
param( |
| 306 |
given_locales=["os-RU", "ln-CF", "ee-TG"], |
| 307 |
expected_locales=["ee-TG", "ln-CF", "os-RU"], |
| 308 |
), |
| 309 |
param(given_locales=["fo-DK", "khq"], expected_locales=["khq", "fo-DK"]), |
| 310 |
param( |
| 311 |
given_locales=["en-CC", "fr-BE", "ar-KW"], |
| 312 |
expected_locales=["en-CC", "fr-BE", "ar-KW"], |
| 313 |
), |
| 314 |
] |
| 315 |
) |
| 316 |
def test_loading_without_given_order(self, given_locales, expected_locales): |
| 317 |
self.load_data(given_locales, use_given_order=False) |
| 318 |
self.then_locales_are_yielded_in_order(expected_locales) |
| 319 |
|
| 320 |
@parameterized.expand( |
| 321 |
[ |
| 322 |
param(given_locales=["sw-KE", "ru-UA", "he"]), |
| 323 |
param(given_locales=["de-IT", "ta-MY", "pa-Arab"]), |
| 324 |
param(given_locales=["bn-IN", "pt-ST", "ko-KP", "ta"]), |
| 325 |
param(given_locales=["fr-NE", "ar-SY"]), |
| 326 |
] |
| 327 |
) |
| 328 |
def test_get_locale_map_with_given_order(self, given_locales): |
| 329 |
self.given_locale_map(locales=given_locales, use_given_order=True) |
| 330 |
self.then_locale_map_in_order(given_locales) |
| 331 |
|
| 332 |
@parameterized.expand( |
| 333 |
[ |
| 334 |
param( |
| 335 |
given_locales=["en-FJ", "pt-CV", "fr-RW"], |
| 336 |
expected_locales=["en-FJ", "fr-RW", "pt-CV"], |
| 337 |
), |
| 338 |
param( |
| 339 |
given_locales=["pt-AO", "hi", "zh-Hans-SG", "vi"], |
| 340 |
expected_locales=["pt-AO", "zh-Hans-SG", "vi", "hi"], |
| 341 |
), |
| 342 |
param( |
| 343 |
given_locales=["gsw-FR", "es-BZ", "ca-IT", "qu-EC"], |
| 344 |
expected_locales=["es-BZ", "ca-IT", "qu-EC", "gsw-FR"], |
| 345 |
), |
| 346 |
] |
| 347 |
) |
| 348 |
def test_get_locale_map_without_given_order(self, given_locales, expected_locales): |
| 349 |
self.given_locale_map(locales=given_locales, use_given_order=False) |
| 350 |
self.then_locale_map_in_order(expected_locales) |
| 351 |
|
| 352 |
@parameterized.expand( |
| 353 |
[ |
| 354 |
param( |
| 355 |
given_languages=["es", "ar-001", "xx"], |
| 356 |
unknown_languages=["ar-001", "xx"], |
| 357 |
), |
| 358 |
param( |
| 359 |
given_languages=["sr-Latn", "sq", "ii-Latn"], |
| 360 |
unknown_languages=["ii-Latn"], |
| 361 |
), |
| 362 |
param(given_languages=["vol", "bc"], unknown_languages=["vol", "bc"]), |
| 363 |
] |
| 364 |
) |
| 365 |
def test_error_raised_for_unknown_languages( |
| 366 |
self, given_languages, unknown_languages |
| 367 |
): |
| 368 |
self.given_locale_map(languages=given_languages) |
| 369 |
self.then_error_for_unknown_languages_raised(unknown_languages) |
| 370 |
|
| 371 |
@parameterized.expand( |
| 372 |
[ |
| 373 |
param( |
| 374 |
given_locales=["es-AB", "ar-001", "fr-DJ"], |
| 375 |
unknown_locales=["es-AB", "ar-001"], |
| 376 |
), |
| 377 |
param( |
| 378 |
given_locales=["ru-MD", "my-MY", "zz"], unknown_locales=["my-MY", "zz"] |
| 379 |
), |
| 380 |
param(given_locales=["nl-SX", "be-BE", "ca-FR"], unknown_locales=["be-BE"]), |
| 381 |
] |
| 382 |
) |
| 383 |
def test_error_raised_for_unknown_locales(self, given_locales, unknown_locales): |
| 384 |
self.given_locale_map(locales=given_locales) |
| 385 |
self.then_error_for_unknown_locales_raised(unknown_locales) |
| 386 |
|
| 387 |
@parameterized.expand( |
| 388 |
[ |
| 389 |
param(given_locales=["en-TK", "en-TO", "zh"]), |
| 390 |
param(given_locales=["es-PY", "es-IC", "ja", "es-DO"]), |
| 391 |
param(given_locales=["ca-TA", "ca", "fr"]), |
| 392 |
] |
| 393 |
) |
| 394 |
def test_error_raised_for_conflicting_locales(self, given_locales): |
| 395 |
self.given_locale_map(locales=given_locales) |
| 396 |
self.then_error_was_raised( |
| 397 |
ValueError, "Locales should not have same language and different region" |
| 398 |
) |
| 399 |
|
| 400 |
@parameterized.expand( |
| 401 |
[ |
| 402 |
param(given_locales=["en-TK", "en-TO", "zh"]), |
| 403 |
param(given_locales=["es-PY", "es-IC", "ja", "es-DO"]), |
| 404 |
param(given_locales=["af-NA", "da", "af"]), |
| 405 |
] |
| 406 |
) |
| 407 |
def test_conflicting_locales_load_if_allow_conflicting_locales(self, given_locales): |
| 408 |
self.load_data( |
| 409 |
given_locales, use_given_order=True, allow_conflicting_locales=True |
| 410 |
) |
| 411 |
self.then_locales_are_yielded_in_order(given_locales) |
| 412 |
|
| 413 |
def load_data( |
| 414 |
self, given_locales, use_given_order=False, allow_conflicting_locales=False |
| 415 |
): |
| 416 |
self.locale_generator = self.data_loader.get_locales( |
| 417 |
locales=given_locales, |
| 418 |
use_given_order=use_given_order, |
| 419 |
allow_conflicting_locales=allow_conflicting_locales, |
| 420 |
) |
| 421 |
|
| 422 |
def given_locale_map(self, languages=None, locales=None, use_given_order=True): |
| 423 |
try: |
| 424 |
self.locale_map = self.data_loader.get_locale_map( |
| 425 |
languages=languages, locales=locales, use_given_order=use_given_order |
| 426 |
) |
| 427 |
except Exception as error: |
| 428 |
self.error = error |
| 429 |
|
| 430 |
def then_locales_are_yielded_in_order(self, expected_locales): |
| 431 |
self.assertEqual( |
| 432 |
list(map(attrgetter("shortname"), list(self.locale_generator))), |
| 433 |
expected_locales, |
| 434 |
) |
| 435 |
|
| 436 |
def then_locale_map_in_order(self, expected_locales): |
| 437 |
self.assertEqual(list(self.locale_map.keys()), expected_locales) |
| 438 |
|
| 439 |
def then_error_for_unknown_languages_raised(self, unknown_languages): |
| 440 |
self.assertIsInstance(self.error, ValueError) |
| 441 |
match = self.UNKNOWN_LANGUAGES_EXCEPTION_RE.match(str(self.error)) |
| 442 |
self.assertTrue(match) |
| 443 |
languages = match.group(1).split(", ") |
| 444 |
self.assertCountEqual( |
| 445 |
languages, |
| 446 |
[repr(unknown_language) for unknown_language in unknown_languages], |
| 447 |
) |
| 448 |
|
| 449 |
def then_error_for_unknown_locales_raised(self, unknown_locales): |
| 450 |
self.assertIsInstance(self.error, ValueError) |
| 451 |
match = self.UNKNOWN_LOCALES_EXCEPTION_RE.match(str(self.error)) |
| 452 |
self.assertTrue(match) |
| 453 |
locales = match.group(1).split(", ") |
| 454 |
self.assertCountEqual( |
| 455 |
locales, [repr(unknown_locale) for unknown_locale in unknown_locales] |
| 456 |
) |