File: test_utils_strptime.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 (175 lines) | stat: -rw-r--r-- 6,000 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
import locale
from datetime import datetime
from unittest import SkipTest

from parameterized import param, parameterized

from dateparser.utils.strptime import strptime
from tests import BaseTestCase


class TestStrptime(BaseTestCase):
    def setUp(self):
        super().setUp()

    def given_system_locale_is(self, locale_str):
        try:
            locale.setlocale(locale.LC_ALL, locale_str)
        except locale.Error:
            raise SkipTest("Locale {} is not installed".format(locale_str))

    def when_date_string_is_parsed(self, date_string, fmt):
        try:
            self.result = strptime(date_string, fmt)
        except ValueError as e:
            self.result = e

    def when_date_string_is_parsed_using_datetime_strptime(self, date_string, fmt):
        try:
            self.result = datetime.strptime(date_string, fmt)
        except ValueError as e:
            self.result = e

    def then_date_object_is(self, expected):
        assert self.result == expected

    def then_date_object_is_instance_of(self, expected):
        assert isinstance(self.result, expected)

    @parameterized.expand(
        [
            param("21 January 2010", "%d %B %Y", expected=datetime(2010, 1, 21, 0, 0)),
            param("2 Mar 2010", "%d %b %Y", expected=datetime(2010, 3, 2, 0, 0)),
            param(
                "12 December 10 10:30",
                "%d %B %y %H:%M",
                expected=datetime(2010, 12, 12, 10, 30),
            ),
            param(
                "12 December 10 22:41",
                "%d %B %y %H:%M",
                expected=datetime(2010, 12, 12, 22, 41),
            ),
            param(
                "12 February 2016 11:41",
                "%d %B %Y %I:%M",
                expected=datetime(2016, 2, 12, 11, 41),
            ),
            param("21 Jan 2010", "%d %b %Y", expected=datetime(2010, 1, 21, 0, 0)),
            param(
                "12 Dec 10 10:30",
                "%d %b %y %H:%M",
                expected=datetime(2010, 12, 12, 10, 30),
            ),
            param(
                "12 Feb 2016 11:41",
                "%d %b %Y %I:%M",
                expected=datetime(2016, 2, 12, 11, 41),
            ),
        ]
    )
    def test_dates_with_months_are_parsed_if_locale_is_non_english(
        self, date_string, fmt, expected
    ):
        self.given_system_locale_is("fr_FR.UTF-8")
        self.when_date_string_is_parsed(date_string, fmt)
        self.then_date_object_is(expected)

    @parameterized.expand(
        [
            param(
                "Monday 21 January 2010",
                "%A %d %B %Y",
                expected=datetime(2010, 1, 21, 0, 0),
            ),
            param("Tue 2 Mar 2010", "%a %d %b %Y", expected=datetime(2010, 3, 2, 0, 0)),
            param(
                "Friday 12 December 10 10:30",
                "%A %d %B %y %H:%M",
                expected=datetime(2010, 12, 12, 10, 30),
            ),
            param(
                "Wed 12 December 10 22:41",
                "%a %d %B %y %H:%M",
                expected=datetime(2010, 12, 12, 22, 41),
            ),
            param(
                "Thu 12 February 2016 11:41",
                "%a %d %B %Y %I:%M",
                expected=datetime(2016, 2, 12, 11, 41),
            ),
        ]
    )
    def test_dates_with_days_are_parsed_if_locale_is_non_english(
        self, date_string, fmt, expected
    ):
        self.given_system_locale_is("fr_FR.UTF-8")
        self.when_date_string_is_parsed(date_string, fmt)
        self.then_date_object_is(expected)

    def test_parsing_date_should_fail_using_datetime_strptime_if_locale_is_non_english(
        self,
    ):
        self.given_system_locale_is("fr_FR.UTF-8")
        self.when_date_string_is_parsed_using_datetime_strptime(
            "21 february 2010", "%d %B %Y"
        )
        self.then_date_object_is_instance_of(ValueError)

    @parameterized.expand(
        [
            param(
                "12 Dec 10 10:30:55.1",
                "%d %b %y %H:%M:%S.%f",
                expected=datetime(2010, 12, 12, 10, 30, 55, 100000),
            ),
            param(
                "12 Dec 10 10:30:55.10",
                "%d %b %y %H:%M:%S.%f",
                expected=datetime(2010, 12, 12, 10, 30, 55, 100000),
            ),
            param(
                "12 Dec 10 10:30:55.100",
                "%d %b %y %H:%M:%S.%f",
                expected=datetime(2010, 12, 12, 10, 30, 55, 100000),
            ),
            param(
                "12 Dec 10 10:30:55.1000",
                "%d %b %y %H:%M:%S.%f",
                expected=datetime(2010, 12, 12, 10, 30, 55, 100000),
            ),
            param(
                "12 Dec 10 10:30:55.100000",
                "%d %b %y %H:%M:%S.%f",
                expected=datetime(2010, 12, 12, 10, 30, 55, 100000),
            ),
            param(
                "12 Dec 10 10:30:55.000001",
                "%d %b %y %H:%M:%S.%f",
                expected=datetime(2010, 12, 12, 10, 30, 55, 1),
            ),
            param(
                "12 Dec 10 10:30:55.000011",
                "%d %b %y %H:%M:%S.%f",
                expected=datetime(2010, 12, 12, 10, 30, 55, 11),
            ),
            param(
                "12 Dec 10 10:30:55.000111",
                "%d %b %y %H:%M:%S.%f",
                expected=datetime(2010, 12, 12, 10, 30, 55, 111),
            ),
            param(
                "12 Feb 2016 11:41:23",
                "%d %b %Y %I:%M:%S",
                expected=datetime(2016, 2, 12, 11, 41, 23),
            ),
            param(
                "11 Dec 10 10:30:2011.999999",
                "%y %b %S %H:%M:%Y.%f",
                expected=datetime(2011, 12, 1, 10, 30, 10, 999999),
            ),
        ]
    )
    def test_microseconds_are_parsed_correctly(self, date_string, fmt, expected):
        self.when_date_string_is_parsed(date_string, fmt)
        self.then_date_object_is(expected)