Debian Codegraph @

tests/test_utils_strptime.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 import locale
2 from datetime import datetime
3 from unittest import SkipTest
4
5 from parameterized import param, parameterized
6
7 from dateparser.utils.strptime import strptime
8 from tests import BaseTestCase
9
10
11 class TestStrptime(BaseTestCase):
12 def setUp(self):
13 super().setUp()
14
15 def given_system_locale_is(self, locale_str):
16 try:
17 locale.setlocale(locale.LC_ALL, locale_str)
18 except locale.Error:
19 raise SkipTest("Locale {} is not installed".format(locale_str))
20
21 def when_date_string_is_parsed(self, date_string, fmt):
22 try:
23 self.result = strptime(date_string, fmt)
24 except ValueError as e:
25 self.result = e
26
27 def when_date_string_is_parsed_using_datetime_strptime(self, date_string, fmt):
28 try:
29 self.result = datetime.strptime(date_string, fmt)
30 except ValueError as e:
31 self.result = e
32
33 def then_date_object_is(self, expected):
34 assert self.result == expected
35
36 def then_date_object_is_instance_of(self, expected):
37 assert isinstance(self.result, expected)
38
39 @parameterized.expand(
40 [
41 param("21 January 2010", "%d %B %Y", expected=datetime(2010, 1, 21, 0, 0)),
42 param("2 Mar 2010", "%d %b %Y", expected=datetime(2010, 3, 2, 0, 0)),
43 param(
44 "12 December 10 10:30",
45 "%d %B %y %H:%M",
46 expected=datetime(2010, 12, 12, 10, 30),
47 ),
48 param(
49 "12 December 10 22:41",
50 "%d %B %y %H:%M",
51 expected=datetime(2010, 12, 12, 22, 41),
52 ),
53 param(
54 "12 February 2016 11:41",
55 "%d %B %Y %I:%M",
56 expected=datetime(2016, 2, 12, 11, 41),
57 ),
58 param("21 Jan 2010", "%d %b %Y", expected=datetime(2010, 1, 21, 0, 0)),
59 param(
60 "12 Dec 10 10:30",
61 "%d %b %y %H:%M",
62 expected=datetime(2010, 12, 12, 10, 30),
63 ),
64 param(
65 "12 Feb 2016 11:41",
66 "%d %b %Y %I:%M",
67 expected=datetime(2016, 2, 12, 11, 41),
68 ),
69 ]
70 )
71 def test_dates_with_months_are_parsed_if_locale_is_non_english(
72 self, date_string, fmt, expected
73 ):
74 self.given_system_locale_is("fr_FR.UTF-8")
75 self.when_date_string_is_parsed(date_string, fmt)
76 self.then_date_object_is(expected)
77
78 @parameterized.expand(
79 [
80 param(
81 "Monday 21 January 2010",
82 "%A %d %B %Y",
83 expected=datetime(2010, 1, 21, 0, 0),
84 ),
85 param("Tue 2 Mar 2010", "%a %d %b %Y", expected=datetime(2010, 3, 2, 0, 0)),
86 param(
87 "Friday 12 December 10 10:30",
88 "%A %d %B %y %H:%M",
89 expected=datetime(2010, 12, 12, 10, 30),
90 ),
91 param(
92 "Wed 12 December 10 22:41",
93 "%a %d %B %y %H:%M",
94 expected=datetime(2010, 12, 12, 22, 41),
95 ),
96 param(
97 "Thu 12 February 2016 11:41",
98 "%a %d %B %Y %I:%M",
99 expected=datetime(2016, 2, 12, 11, 41),
100 ),
101 ]
102 )
103 def test_dates_with_days_are_parsed_if_locale_is_non_english(
104 self, date_string, fmt, expected
105 ):
106 self.given_system_locale_is("fr_FR.UTF-8")
107 self.when_date_string_is_parsed(date_string, fmt)
108 self.then_date_object_is(expected)
109
110 def test_parsing_date_should_fail_using_datetime_strptime_if_locale_is_non_english(
111 self,
112 ):
113 self.given_system_locale_is("fr_FR.UTF-8")
114 self.when_date_string_is_parsed_using_datetime_strptime(
115 "21 february 2010", "%d %B %Y"
116 )
117 self.then_date_object_is_instance_of(ValueError)
118
119 @parameterized.expand(
120 [
121 param(
122 "12 Dec 10 10:30:55.1",
123 "%d %b %y %H:%M:%S.%f",
124 expected=datetime(2010, 12, 12, 10, 30, 55, 100000),
125 ),
126 param(
127 "12 Dec 10 10:30:55.10",
128 "%d %b %y %H:%M:%S.%f",
129 expected=datetime(2010, 12, 12, 10, 30, 55, 100000),
130 ),
131 param(
132 "12 Dec 10 10:30:55.100",
133 "%d %b %y %H:%M:%S.%f",
134 expected=datetime(2010, 12, 12, 10, 30, 55, 100000),
135 ),
136 param(
137 "12 Dec 10 10:30:55.1000",
138 "%d %b %y %H:%M:%S.%f",
139 expected=datetime(2010, 12, 12, 10, 30, 55, 100000),
140 ),
141 param(
142 "12 Dec 10 10:30:55.100000",
143 "%d %b %y %H:%M:%S.%f",
144 expected=datetime(2010, 12, 12, 10, 30, 55, 100000),
145 ),
146 param(
147 "12 Dec 10 10:30:55.000001",
148 "%d %b %y %H:%M:%S.%f",
149 expected=datetime(2010, 12, 12, 10, 30, 55, 1),
150 ),
151 param(
152 "12 Dec 10 10:30:55.000011",
153 "%d %b %y %H:%M:%S.%f",
154 expected=datetime(2010, 12, 12, 10, 30, 55, 11),
155 ),
156 param(
157 "12 Dec 10 10:30:55.000111",
158 "%d %b %y %H:%M:%S.%f",
159 expected=datetime(2010, 12, 12, 10, 30, 55, 111),
160 ),
161 param(
162 "12 Feb 2016 11:41:23",
163 "%d %b %Y %I:%M:%S",
164 expected=datetime(2016, 2, 12, 11, 41, 23),
165 ),
166 param(
167 "11 Dec 10 10:30:2011.999999",
168 "%y %b %S %H:%M:%Y.%f",
169 expected=datetime(2011, 12, 1, 10, 30, 10, 999999),
170 ),
171 ]
172 )
173 def test_microseconds_are_parsed_correctly(self, date_string, fmt, expected):
174 self.when_date_string_is_parsed(date_string, fmt)
175 self.then_date_object_is(expected)