[7fc5df]: / tests / surrogates / generators / test_date.py

Download this file

319 lines (251 with data), 10.6 kB

  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
import pytest
from deidentify.surrogates.generators import Date, DateSurrogates, RandomData
from deidentify.surrogates.generators.date import (adjust_long_date_span, fully_parsed,
infer_format, max_date,
minimum_season_offsets, year_span)
def test_infer_format():
assert infer_format('Tuesday 5 March 2019 08:59').format == '%A %d %B %Y %H:%M'
assert infer_format('Friday (10 June)').format == '%A (%d %B)'
assert infer_format('10 November').format == '%d %B'
assert infer_format('August 3, 2009').format == '%B %d, %Y'
assert infer_format('2 May 1980').format == '%d %b %Y'
assert infer_format('20-08-2016').format == '%d-%m-%Y'
assert infer_format('Friday').format == '%A'
assert infer_format('23 feb.').format == '%d %b.'
assert infer_format('01 01 2015').format == '%d %m %Y'
assert infer_format('22/05/13').format == '%d/%m/%y'
assert infer_format('0411').format == '%Y'
# Odd edge case which results into a parsing including timezone information.
# This should be handled gracefully.
assert infer_format('15-34-2019').format == '%H-%M%z'
def test_infer_format_multiple_languages():
date = infer_format('dinsdag 5 maart 2019 08:59')
assert date.format == '%A %d %B %Y %H:%M'
assert date.locale == 'nl_NL.UTF-8'
date = infer_format('Tuesday 5 March 2019 08:59')
assert date.format == '%A %d %B %Y %H:%M'
assert date.locale == 'en_US.UTF-8'
date = infer_format('Dienstag 5 März 2019 08:59')
assert date.format == '%A %d %B %Y %H:%M'
assert date.locale == 'de_DE.UTF-8'
def test_infer_format_unanchored_weekdays():
date = infer_format('vrijdag')
assert date.format == '%A'
assert date.locale == 'nl_NL.UTF-8'
assert date.datetime.year == 1900
assert date.datetime.day == 15
assert date.datetime.month == 1
def test_infer_format_invalid_date():
date = infer_format('Tuesday 5 March 2019 08:59')
assert date.format == '%A %d %B %Y %H:%M'
assert date.locale == 'en_US.UTF-8'
assert date.date_string == 'Tuesday 5 March 2019 08:59'
with pytest.raises(ValueError):
date = infer_format('Tuesday 5 Marc 2019 08:59')
def test_fully_parsed():
assert fully_parsed('%d-%m-%Y')
assert fully_parsed('%A %d %B %Y %H:%M')
assert fully_parsed('%Y-%m-%dT%H:%M:%S%z')
assert not fully_parsed('20082016')
assert not fully_parsed('%d082016')
assert not fully_parsed('%d/082016')
assert not fully_parsed('%d-%m-2016')
assert not fully_parsed('%d-%m-%Y January')
assert not fully_parsed('January')
def test_year_anchored():
assert Date('01-01-1905', '%d-%m-%Y').year_anchored()
assert Date('01-01-18', '%d-%m-%y').year_anchored()
assert not Date('01-01', '%d-%m').year_anchored()
def test_day_anchored():
assert Date('01-01-1905', '%d-%m-%Y').day_anchored()
assert not Date('01-1905', '%m-%Y').day_anchored()
def test_max_date():
dates = [
Date('01-01-1905', '%d-%m-%Y'),
Date('01-02', '%d-%m'),
Date('01-01-1989', '%d-%m-%Y'),
]
most_recent = max_date(dates)
assert most_recent == Date('01-01-1989', '%d-%m-%Y')
dates = [
Date('01-01-1905', '%d-%m-%Y'),
Date('01-01-1905', '%d-%m-%Y')
]
most_recent = max_date(dates)
assert most_recent == Date('01-01-1905', '%d-%m-%Y')
dates = [
Date('01-01', '%d-%m'),
Date('01-01-1905', '%d-%m-%Y'),
]
most_recent = max_date(dates)
assert most_recent == Date('01-01-1905', '%d-%m-%Y')
def test_max_dates_raises_unanchored():
dates = [
Date('01-01', '%d-%m'),
Date('01-12', '%d-%m'),
]
with pytest.raises(ValueError) as exec_info:
_ = max_date(dates)
expected_exc = 'max_date() arg does not contain any year-anchored date'
assert expected_exc in str(exec_info.value).lower()
def test_year_span():
date_a = Date('01-01-1905', '%d-%m-%Y')
date_b = Date('01-01-1989', '%d-%m-%Y')
assert year_span(date_a, date_b) == 84
def test_adjust_long_date_span():
dates_given = [
Date('03-11-1899', '%d-%m-%Y'),
Date('04-05-1900', '%d-%m-%Y'),
Date('01-01-1915', '%d-%m-%Y'),
Date('01-01-1995', '%d-%m-%Y')
]
dates_expected = [
Date('03-11-1904', '%d-%m-%Y'),
Date('04-05-1904', '%d-%m-%Y'),
Date('01-01-1915', '%d-%m-%Y'),
Date('01-01-1995', '%d-%m-%Y'),
]
most_recent = Date('01-01-1995', '%d-%m-%Y')
for given, expected in zip(dates_given, dates_expected):
shifted = adjust_long_date_span(given, most_recent=most_recent, max_delta=90)
assert shifted == expected
def test_adjust_long_date_span_disregard_timezones():
dt_aware = Date('15-34-2019', '%H-%M%z')
dt_unaware = Date('ma', '%a', date_locale='nl_NL.UTF-8')
assert adjust_long_date_span(dt_unaware, most_recent=dt_aware, max_delta=90) == dt_unaware
def test_adjust_long_date_span_preserves_locale():
dates_given = [
Date('03 November 1899', '%d %B %Y', date_locale='de_DE.UTF-8'),
Date('04 Mai 1900', '%d %B %Y', date_locale='de_DE.UTF-8'),
Date('01 Januar 1915', '%d %B %Y', date_locale='de_DE.UTF-8'),
Date('01 January 1995', '%d %B %Y', date_locale='en_US.UTF-8')
]
dates_expected = [
Date('03 November 1904', '%d %B %Y', date_locale='de_DE.UTF-8'),
Date('04 Mai 1904', '%d %B %Y', date_locale='de_DE.UTF-8'),
Date('01 Januar 1915', '%d %B %Y', date_locale='de_DE.UTF-8'),
Date('01 January 1995', '%d %B %Y', date_locale='en_US.UTF-8')
]
most_recent = Date('01-01-1995', '%d-%m-%Y')
for given, expected in zip(dates_given, dates_expected):
shifted = adjust_long_date_span(given, most_recent=most_recent, max_delta=90)
assert shifted == expected
def test_season_offsets():
fmt = '%Y-%m-%d'
spring_date = Date('2018-04-24', fmt)
summer_date = Date('2018-7-7', fmt)
autumn_date = Date('2018-11-11', fmt)
winter_date_end_of_year = Date('2018-12-22', fmt)
winter_date_start_of_year = Date('2019-1-7', fmt)
border_date = Date('2018-12-21', fmt)
assert spring_date.season_offsets() == (34, 57)
assert summer_date.season_offsets() == (16, 77)
assert autumn_date.season_offsets() == (49, 39)
assert winter_date_end_of_year.season_offsets() == (1, 88)
assert winter_date_start_of_year.season_offsets() == (17, 72)
assert border_date.season_offsets() == (0, 89)
def test_season_offsets_unanchored_dates():
missing_year = Date('04-24', '%m-%d')
missing_day = Date('2018-04', '%Y-%m')
assert missing_year.season_offsets() == (34, 57)
assert missing_day.season_offsets() == (25, 66)
def test_minimum_season_offsets():
fmt = '%Y-%m-%d'
dates = [
Date('2018-04-24', fmt),
Date('2018-7-7', fmt),
Date('2018-11-11', fmt),
Date('2018-12-22', fmt),
Date('2019-1-7', fmt),
Date('2018-12-21', fmt)
]
negative_shift, positive_shift = minimum_season_offsets(dates)
assert (negative_shift, positive_shift) == (0, 39)
def test_random_year_day_shift():
annotations = [
'2018-04-24',
'2018-7-7',
'2018-11-11',
'2018-12-22',
'2019-1-7',
'2018-12-21'
]
for _ in range(100):
date_surrogates = DateSurrogates(annotations,
year_shift_base=65,
year_shift_fuzz=10,
random_data=None)
# number of years to shift should be between 65+-10 years
# according to year_shift_base and year_shift_fuzz
assert date_surrogates.year_shift in list(range(55, 76))
# number of days to shift should be in [1,39] see test_minimum_season_offsets test case
assert date_surrogates.day_shift in list(range(1, 40))
def test_date_surrogate_generator():
annotations = [
'01 januari 1915', '01-02', 'marc 2001', 'February 2001', '01-02-2010',
]
date_surrogates = DateSurrogates(annotations, random_data=RandomData(42))
assert date_surrogates.replace_all() == [
'09 januari 2006',
'09-02',
None,
'February 2086',
'09-02-2095'
]
def test_date_surrogate_generator_unanchored_weekdays():
class RandomDataMock(RandomData):
def randint(self, a, b):
# 0 year fuzz
if a == 0 and b == 0:
return 0
# shift by 2 days
return 2
annotations = [
'01 januari 1915',
'01-02',
# unanchored Friday will be set to 15/01/1900 (which is not a Friday, but this is ignored)
'vrijdag',
]
date_surrogates = DateSurrogates(annotations,
year_shift_base=10,
year_shift_fuzz=0,
random_data=RandomDataMock())
assert date_surrogates.replace_all() == [
'03 januari 1925',
'03-02',
'maandag', # 17/01/1910 is a monday
]
def test_date_surrogate_generator_all_unanchored():
class RandomDataMock(RandomData):
def randint(self, a, b):
# 0 year fuzz
if a == 0 and b == 0:
return 0
# shift by 2 days
return 2
annotations = [
'01 januari',
'01-02',
# unanchored Friday will be set to 15/01/1900 (which is not a Friday, but this is ignored)
'vrijdag',
]
date_surrogates = DateSurrogates(annotations,
year_shift_base=10,
year_shift_fuzz=0,
random_data=RandomDataMock())
assert date_surrogates.replace_all() == [
'03 januari',
'03-02',
'maandag', # 17/01/1910 is a monday
]
def test_switch_locale():
import pydateinfer as dateinfer
import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
assert dateinfer.infer(['12 January']) == '%d %B'
assert dateinfer.infer(['12 januari']).split()[1] == 'januari'
locale.setlocale(locale.LC_ALL, 'nl_NL.UTF-8')
assert dateinfer.infer(['12 januari']) == '%d %B'
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
assert dateinfer.infer(['12 January']) == '%d %B'
assert dateinfer.infer(['12 januari']).split()[1] == 'januari'