File: test_data.py

package info (click to toggle)
dateparser 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,140 kB
  • sloc: python: 52,721; makefile: 155; sh: 15
file content (575 lines) | stat: -rw-r--r-- 19,054 bytes parent folder | download | duplicates (2)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
from itertools import chain

import regex as re
from parameterized import parameterized

from dateparser.data import language_locale_dict
from dateparser.languages import default_loader
from tests import BaseTestCase

DEFAULT_MONTH_PATTERN = re.compile(r"^M?\d+$", re.U)
INVALID_AM_PM_PATTERN = re.compile(r"^[AaPp]\.?\s+[Mm]$")

all_locale_shortnames = list(
    chain(language_locale_dict.keys(), *language_locale_dict.values())
)
all_locales = list(
    default_loader.get_locales(
        locales=all_locale_shortnames, allow_conflicting_locales=True
    )
)
all_locale_params = [[locale] for locale in all_locales]

VALID_KEYS = [
    "name",
    "date_order",
    "skip",
    "pertain",
    "simplifications",
    "no_word_spacing",
    "ago",
    "in",
    "monday",
    "tuesday",
    "wednesday",
    "thursday",
    "friday",
    "saturday",
    "sunday",
    "january",
    "february",
    "march",
    "april",
    "may",
    "june",
    "july",
    "august",
    "september",
    "october",
    "november",
    "december",
    "decade",
    "year",
    "month",
    "week",
    "day",
    "hour",
    "minute",
    "second",
    "am",
    "pm",
    "relative-type",
    "relative-type-regex",
    "sentence_splitter_group",
]

NECESSARY_KEYS = [
    "name",
    "monday",
    "tuesday",
    "wednesday",
    "thursday",
    "friday",
    "saturday",
    "sunday",
    "january",
    "february",
    "march",
    "april",
    "may",
    "june",
    "july",
    "august",
    "september",
    "october",
    "november",
    "december",
    "year",
    "month",
    "week",
    "day",
    "hour",
    "minute",
    "second",
]

MONTH_NAMES = [
    "january",
    "february",
    "march",
    "april",
    "may",
    "june",
    "july",
    "august",
    "september",
    "october",
    "november",
    "december",
]


def is_invalid_translation(translation):
    return (not (translation and isinstance(translation, str))) or "." in translation


def is_invalid_month_translation(translation):
    return (
        (not (translation and isinstance(translation, str)))
        or "." in translation
        or DEFAULT_MONTH_PATTERN.match(translation)
    )


def is_invalid_am_pm_translation(translation):
    return (
        (not (translation and isinstance(translation, str)))
        or "." in translation
        or INVALID_AM_PM_PATTERN.match(translation)
    )


def is_invalid_simplification(simplification):
    if not isinstance(simplification, dict) or len(simplification) != 1:
        return True
    key, value = list(simplification.items())[0]
    return not isinstance(key, str) or not isinstance(value, str)


def is_invalid_relative_mapping(relative_mapping):
    key, value = relative_mapping
    if not (key and value and isinstance(key, str) and isinstance(value, list)):
        return True
    return not all([isinstance(x, str) for x in value])


def is_invalid_relative_regex_mapping(relative_regex_mapping):
    key, value = relative_regex_mapping
    if not (key and value and isinstance(key, str) and isinstance(value, list)):
        return True
    return not all([isinstance(x, str) for x in value])


class TestLocaleInfo(BaseTestCase):
    def setUp(self):
        super().setUp()
        self.info = NotImplemented
        self.shortname = NotImplemented

    @parameterized.expand(all_locale_params)
    def test_extra_keys(self, locale):
        self.given_locale_info(locale)
        extra_keys = list(set(self.info.keys()) - set(VALID_KEYS))
        self.assertFalse(
            extra_keys, "Extra keys found for {}: {}".format(self.shortname, extra_keys)
        )

    @parameterized.expand(all_locale_params)
    def test_necessary_keys(self, locale):
        self.given_locale_info(locale)
        self.then_keys_present_in_info(NECESSARY_KEYS)
        self.then_translations_present_in_info(NECESSARY_KEYS)

    @parameterized.expand(all_locale_params)
    def test_name(self, locale):
        self.given_locale_info(locale)
        self.then_name_is_valid()

    @parameterized.expand(all_locale_params)
    def test_date_order(self, locale):
        self.given_locale_info(locale)
        self.then_date_order_is_valid()

    @parameterized.expand(all_locale_params)
    def test_january_translation(self, locale):
        self.given_locale_info(locale)
        self.then_month_translations_are_valid("january")

    @parameterized.expand(all_locale_params)
    def test_february_translation(self, locale):
        self.given_locale_info(locale)
        self.then_month_translations_are_valid("february")

    @parameterized.expand(all_locale_params)
    def test_march_translation(self, locale):
        self.given_locale_info(locale)
        self.then_month_translations_are_valid("march")

    @parameterized.expand(all_locale_params)
    def test_april_translation(self, locale):
        self.given_locale_info(locale)
        self.then_month_translations_are_valid("april")

    @parameterized.expand(all_locale_params)
    def test_may_translation(self, locale):
        self.given_locale_info(locale)
        self.then_month_translations_are_valid("may")

    @parameterized.expand(all_locale_params)
    def test_june_translation(self, locale):
        self.given_locale_info(locale)
        self.then_month_translations_are_valid("june")

    @parameterized.expand(all_locale_params)
    def test_july_translation(self, locale):
        self.given_locale_info(locale)
        self.then_month_translations_are_valid("july")

    @parameterized.expand(all_locale_params)
    def test_august_translation(self, locale):
        self.given_locale_info(locale)
        self.then_month_translations_are_valid("august")

    @parameterized.expand(all_locale_params)
    def test_september_translation(self, locale):
        self.given_locale_info(locale)
        self.then_month_translations_are_valid("september")

    @parameterized.expand(all_locale_params)
    def test_october_translation(self, locale):
        self.given_locale_info(locale)
        self.then_month_translations_are_valid("october")

    @parameterized.expand(all_locale_params)
    def test_november_translation(self, locale):
        self.given_locale_info(locale)
        self.then_month_translations_are_valid("november")

    @parameterized.expand(all_locale_params)
    def test_december_translation(self, locale):
        self.given_locale_info(locale)
        self.then_month_translations_are_valid("december")

    @parameterized.expand(all_locale_params)
    def test_am_translation(self, locale):
        self.given_locale_info(locale)
        self.then_am_pm_translations_are_valid("am")

    @parameterized.expand(all_locale_params)
    def test_pm_translation(self, locale):
        self.given_locale_info(locale)
        self.then_am_pm_translations_are_valid("pm")

    @parameterized.expand(all_locale_params)
    def test_sunday_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("sunday")

    @parameterized.expand(all_locale_params)
    def test_monday_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("monday")

    @parameterized.expand(all_locale_params)
    def test_tuesday_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("tuesday")

    @parameterized.expand(all_locale_params)
    def test_wednesday_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("wednesday")

    @parameterized.expand(all_locale_params)
    def test_thursday_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("thursday")

    @parameterized.expand(all_locale_params)
    def test_friday_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("friday")

    @parameterized.expand(all_locale_params)
    def test_saturday_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("saturday")

    @parameterized.expand(all_locale_params)
    def test_year_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("year")

    @parameterized.expand(all_locale_params)
    def test_month_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("month")

    @parameterized.expand(all_locale_params)
    def test_week_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("week")

    @parameterized.expand(all_locale_params)
    def test_day_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("day")

    @parameterized.expand(all_locale_params)
    def test_hour_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("hour")

    @parameterized.expand(all_locale_params)
    def test_minute_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("minute")

    @parameterized.expand(all_locale_params)
    def test_second_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("second")

    @parameterized.expand(all_locale_params)
    def test_ago_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("ago")

    @parameterized.expand(all_locale_params)
    def test_in_translation(self, locale):
        self.given_locale_info(locale)
        self.then_translations_are_valid("in")

    @parameterized.expand(all_locale_params)
    def test_skip_tokens(self, locale):
        self.given_locale_info(locale)
        self.then_skip_pertain_tokens_are_valid("skip")

    @parameterized.expand(all_locale_params)
    def test_pertain_tokens(self, locale):
        self.given_locale_info(locale)
        self.then_skip_pertain_tokens_are_valid("pertain")

    @parameterized.expand(all_locale_params)
    def test_simplifications(self, locale):
        self.given_locale_info(locale)
        self.then_simplifications_are_valid()

    @parameterized.expand(all_locale_params)
    def test_relative_type(self, locale):
        self.given_locale_info(locale)
        self.then_relative_type_is_valid()

    @parameterized.expand(all_locale_params)
    def test_relative_type_regex(self, locale):
        self.given_locale_info(locale)
        self.then_relative_type_regex_is_valid()

    @parameterized.expand(all_locale_params)
    def test_no_word_spacing(self, locale):
        self.given_locale_info(locale)
        self.then_no_word_spacing_is_valid()

    def given_locale_info(self, locale):
        self.info = locale.info
        self.shortname = locale.shortname

    def then_keys_present_in_info(self, keys_list):
        absent_keys = list(set(keys_list) - set(self.info.keys()))
        self.assertFalse(
            absent_keys,
            "{} not found in locale {}".format(", ".join(absent_keys), self.shortname),
        )

    def then_translations_present_in_info(self, keys_list):
        no_translation_keys = [key for key in keys_list if not self.info.get(key)]
        self.assertFalse(
            no_translation_keys,
            "Translations for {} not found in locale {}".format(
                ", ".join(no_translation_keys), self.shortname
            ),
        )

    def then_name_is_valid(self):
        name = self.info["name"]
        self.assertIsInstance(
            name,
            str,
            "Invalid type for name: {} for locale {}".format(
                type(name).__name__, self.shortname
            ),
        )
        self.assertEqual(
            name,
            self.shortname,
            "Invalid name: {} for locale {}".format(name, self.shortname),
        )

    def then_date_order_is_valid(self):
        if "date_order" in self.info:
            date_order = self.info["date_order"]
            self.assertIsInstance(
                date_order,
                str,
                "Invalid type for date_order: {} for locale {}".format(
                    type(date_order).__name__, self.shortname
                ),
            )
            self.assertIn(
                date_order,
                ["DMY", "DYM", "MDY", "MYD", "YDM", "YMD"],
                "Invalid date_order {} for {}".format(date_order, self.shortname),
            )

    def then_month_translations_are_valid(self, month):
        if month in self.info:
            month_translations = self.info[month]
            self.assertIsInstance(
                month_translations,
                list,
                "Invalid type for {}: {} for locale {}".format(
                    month, type(month_translations).__name__, self.shortname
                ),
            )
            invalid_translations = list(
                filter(is_invalid_month_translation, month_translations)
            )
            self.assertFalse(
                invalid_translations,
                "Invalid translations for {}: {} for locale {}".format(
                    month, ", ".join(map(repr, invalid_translations)), self.shortname
                ),
            )

    def then_am_pm_translations_are_valid(self, key):
        if key in self.info:
            translations_list = self.info[key]
            self.assertIsInstance(
                translations_list,
                list,
                "Invalid type for {}: {} for locale {}".format(
                    key, type(translations_list).__name__, self.shortname
                ),
            )
            invalid_translations = list(
                filter(is_invalid_am_pm_translation, translations_list)
            )
            self.assertFalse(
                invalid_translations,
                "Invalid translations for {}: {} for locale {}".format(
                    key, ", ".join(map(repr, invalid_translations)), self.shortname
                ),
            )

    def then_translations_are_valid(self, key):
        if key in self.info:
            translations_list = self.info[key]
            self.assertIsInstance(
                translations_list,
                list,
                "Invalid type for {}: {} for locale {}".format(
                    key, type(translations_list).__name__, self.shortname
                ),
            )
            invalid_translations = list(
                filter(is_invalid_translation, translations_list)
            )
            self.assertFalse(
                invalid_translations,
                "Invalid translations for {}: {} for locale {}".format(
                    key, ", ".join(map(repr, invalid_translations)), self.shortname
                ),
            )

    def then_skip_pertain_tokens_are_valid(self, key):
        if key in self.info:
            tokens_list = self.info[key]
            self.assertIsInstance(
                tokens_list,
                list,
                "Invalid type for {}: {} for locale {}".format(
                    key, type(tokens_list).__name__, self.shortname
                ),
            )
            invalid_tokens = [
                token
                for token in tokens_list
                if not token or not isinstance(token, str)
            ]
            self.assertFalse(
                invalid_tokens,
                "Invalid tokens for {}: {} for locale {}".format(
                    key, ", ".join(map(repr, invalid_tokens)), self.shortname
                ),
            )

    def then_simplifications_are_valid(self):
        if "simplifications" in self.info:
            simplifications_list = self.info["simplifications"]
            self.assertIsInstance(
                simplifications_list,
                list,
                "Invalid type for simplifications: {} for locale {}".format(
                    type(simplifications_list).__name__, self.shortname
                ),
            )
            invalid_simplifications = list(
                filter(is_invalid_simplification, simplifications_list)
            )
            self.assertFalse(
                invalid_simplifications,
                "Invalid simplifications {} for locale {}".format(
                    ", ".join(map(repr, invalid_simplifications)), self.shortname
                )
                + ", each simplification must be a single string to string mapping",
            )

    def then_relative_type_is_valid(self):
        if "relative-type" in self.info:
            relative_type_dict = self.info["relative-type"]
            self.assertIsInstance(
                relative_type_dict,
                dict,
                "Invalid type for relative-type: {} for locale {}".format(
                    type(relative_type_dict).__name__, self.shortname
                ),
            )
            invalid_relative_type = list(
                filter(is_invalid_relative_mapping, relative_type_dict.items())
            )
            self.assertFalse(
                invalid_relative_type,
                "Invalid relative-type mappings {} for locale {}".format(
                    ", ".join(map(repr, invalid_relative_type)), self.shortname
                )
                + ", each mapping must be a string to list (of strings) mapping",
            )

    def then_relative_type_regex_is_valid(self):
        if "relative-type-regex" in self.info:
            relative_type_dict = self.info["relative-type-regex"]
            self.assertIsInstance(
                relative_type_dict,
                dict,
                "Invalid type for relative-type-regex: {} for locale {}".format(
                    type(relative_type_dict).__name__, self.shortname
                ),
            )
            invalid_relative_type_regex = list(
                filter(is_invalid_relative_regex_mapping, relative_type_dict.items())
            )
            self.assertFalse(
                invalid_relative_type_regex,
                "Invalid relative-type-regex mappings {} for locale {}".format(
                    ", ".join(map(repr, invalid_relative_type_regex)), self.shortname
                )
                + ", each mapping must be a string to list (of strings) mapping",
            )

    def then_no_word_spacing_is_valid(self):
        if "no_word_spacing" in self.info:
            no_word_spacing = self.info["no_word_spacing"]
            self.assertIsInstance(
                no_word_spacing,
                str,
                "Invalid type for no_word_spacing: {} for locale {}".format(
                    type(no_word_spacing).__name__, self.shortname
                ),
            )
            self.assertIn(
                no_word_spacing,
                ["True", "False"],
                "Invalid no_word_spacing {} for {}".format(
                    no_word_spacing, self.shortname
                ),
            )