-
Notifications
You must be signed in to change notification settings - Fork 434
/
Copy pathtest_runner_datetime.py
331 lines (245 loc) · 11.7 KB
/
test_runner_datetime.py
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import importlib
import datetime
import pytest
from runner import get_specs, CULTURES
from recognizers_date_time import recognize_datetime
MODELFUNCTION = {
'DateTime': recognize_datetime
}
@pytest.mark.parametrize(
'culture, model, options, context, source, expected_results',
get_specs(recognizer='DateTime', entity='Extractor'))
def test_datetime_extractor(
culture,
model,
options,
context,
source,
expected_results):
reference_datetime = get_reference_date(context)
language = get_language(culture)
extractor = create_extractor(language, model, options)
result = extractor.extract(source, reference_datetime)
spec_info = type(extractor).__name__ + " : " + source
assert len(result) == len(expected_results)
for actual, expected in zip(result, expected_results):
simple_extractor_assert(spec_info, actual, expected, 'text', 'Text', True)
simple_extractor_assert(spec_info, actual, expected, 'type', 'Type')
simple_extractor_assert(spec_info, actual, expected, 'start', 'Start')
simple_extractor_assert(spec_info, actual, expected, 'length', 'Length')
@pytest.mark.parametrize(
'culture, model, options, context, source, expected_results',
get_specs(recognizer='DateTime', entity='Parser'))
def test_datetime_parser(
culture,
model,
options,
context,
source,
expected_results):
reference_datetime = get_reference_date(context)
language = get_language(culture)
extractor = create_extractor(language, model, options)
parser = create_parser(language, model, options)
spec_info = type(parser).__name__ + " : " + source
extract_results = extractor.extract(source, reference_datetime)
result = [parser.parse(x, reference_datetime) for x in extract_results]
assert len(result) == len(expected_results)
for actual, expected in zip(result, expected_results):
simple_parser_assert(spec_info, actual, expected, 'text', 'Text', True)
simple_parser_assert(spec_info, actual, expected, 'type', 'Type')
if 'Value' in expected:
assert_verbose(actual.value is None, False, spec_info)
if actual.value and 'Value' in expected:
simple_parser_assert(spec_info, actual.value, expected['Value'], 'timex', 'Timex')
simple_parser_assert(spec_info, actual.value, expected['Value'], 'mod', 'Mod')
simple_parser_assert(spec_info, actual.value, expected['Value'], 'future_resolution', 'FutureResolution')
simple_parser_assert(spec_info, actual.value, expected['Value'], 'past_resolution', 'PastResolution')
@pytest.mark.parametrize(
'culture, model, options, context, source, expected_results',
get_specs(recognizer='DateTime', entity='MergedParser'))
def test_datetime_mergedparser(
culture,
model,
options,
context,
source,
expected_results):
reference_datetime = get_reference_date(context)
language = get_language(culture)
extractor = create_extractor(language, model, options)
parser = create_parser(language, model, options)
extract_results = extractor.extract(source, reference_datetime)
result = [parser.parse(x, reference_datetime) for x in extract_results]
spec_info = type(parser).__name__ + " : " + source
assert len(result) == len(expected_results)
for actual, expected in zip(result, expected_results):
simple_extractor_assert(spec_info, actual, expected, 'text', 'Text')
simple_extractor_assert(spec_info, actual, expected, 'type', 'Type')
simple_extractor_assert(spec_info, actual, expected, 'start', 'Start')
simple_extractor_assert(spec_info, actual, expected, 'length', 'Length')
if 'Value' in expected:
assert_verbose(actual.value is None, False, spec_info)
if actual.value and 'Value' in expected:
if 'values' in expected['Value']:
assert isinstance(actual.value['values'], list)
assert len(expected['Value']) == len(actual.value)
for actual_values, expected_values in zip(actual.value['values'], expected['Value']['values']):
for key in expected_values.keys():
assert actual_values[key] == expected_values[key]
@pytest.mark.parametrize(
'culture, model, options, context, source, expected_results',
get_specs(recognizer='DateTime', entity='Model'))
def test_datetime_model(
culture,
model,
options,
context,
source,
expected_results):
reference_datetime = get_reference_date(context)
option_obj = get_option(options)
result = get_results(
culture,
model,
source,
option_obj,
reference_datetime)
spec_info = model + "Model : " + source
assert_verbose(len(result), len(expected_results), spec_info)
for actual, expected in zip(result, expected_results):
simple_parser_assert(spec_info, actual, expected, 'text', 'Text')
simple_parser_assert(spec_info, actual, expected, 'type_name', 'TypeName')
simple_parser_assert(spec_info, actual, expected, 'parent_text', 'ParentText')
simple_parser_assert(spec_info, actual, expected, 'start', 'Start')
simple_parser_assert(spec_info, actual, expected, 'end', 'End')
# Avoid TypError if Actual is None
assert_verbose(actual is None, False, spec_info)
assert_verbose(actual.resolution is None, False, spec_info)
assert_verbose(len(actual.resolution['values']), len(expected['Resolution']['values']), spec_info)
for actual_resolution_value in actual.resolution['values']:
assert_model_resolution(actual_resolution_value, expected['Resolution']['values'], spec_info)
def get_props(results, prop):
list_result = []
for result in results:
list_result.append(result.get(prop))
return list_result
def single_assert(actual, expected, prop, spec_info):
if expected.get(prop):
assert actual[prop] == expected[prop], \
"Actual: {} | Expected: {} | Context: {}".format(actual, expected, spec_info)
else:
assert actual.get(prop) is None, \
"Actual: 'None' | Expected: {} | Context: {}".format(expected, spec_info)
def assert_verbose(actual, expected, spec_info):
assert actual == expected, \
"Actual: {} | Expected: {} | Context: {}".format(actual, expected, spec_info)
def assert_prop(actual, expected, prop, spec_info):
actual_val = actual.get(prop)
expected_val = get_props(expected, prop)
assert actual_val in expected_val, \
"Actual: {} | Expected: {} | Prop: {} | Context: {}".format(actual, expected, prop, spec_info)
def assert_model_resolution(actual, expected, spec_info):
assert_prop(actual, expected, 'timex', spec_info)
assert_prop(actual, expected, 'type', spec_info)
assert_prop(actual, expected, 'value', spec_info)
assert_prop(actual, expected, 'start', spec_info)
assert_prop(actual, expected, 'end', spec_info)
assert_prop(actual, expected, 'Mod', spec_info)
def simple_extractor_assert(spec_info, actual, expected, prop, resolution, ignore_result_case=False):
if resolution in expected:
expected_normalize = expected[resolution] if not ignore_result_case else expected[resolution].lower()
actual_normalize = getattr(actual, prop) if not ignore_result_case else getattr(actual, prop).lower()
assert_verbose(actual_normalize, expected_normalize, spec_info)
def simple_parser_assert(spec_info, actual, expected, prop, resolution, ignore_result_case=False):
if resolution in expected:
expected_normalize = expected[resolution] if not ignore_result_case else expected[resolution].lower()
actual_normalize = getattr(actual, prop) if not ignore_result_case else getattr(actual, prop).lower()
assert_verbose(actual_normalize, expected_normalize, spec_info)
def create_extractor(language, model, options):
if language == "EnglishOthers":
language = "English"
extractor = get_class(
'recognizers_date_time',
f'{language}{model}Extractor')
if extractor:
return extractor()
extractor = get_class(
f'recognizers_date_time.date_time.{language.lower()}.{model.lower()}',
f'{language}{model}Extractor')
if extractor:
return extractor()
extractor = get_class(
f'recognizers_date_time.date_time.base_{model.lower()}',
f'Base{model}Extractor')
configuration = get_class(
f'recognizers_date_time.date_time.{language.lower()}.{model.lower()}_extractor_config',
f'{language}{model}ExtractorConfiguration')
if model == 'Merged':
option = get_option(options)
return extractor(configuration(), option)
return extractor(configuration())
def create_parser(language, model, options):
dmyDateFormat = False
if language == "EnglishOthers":
language = "English"
dmyDateFormat = True
parser = get_class(
f'recognizers_date_time.date_time.{language.lower()}.{model.lower()}_parser',
f'{language}{model}Parser')
if parser:
return parser()
parser = get_class(
f'recognizers_date_time.date_time.{language.lower()}.parsers',
f'{language}{model}Parser')
if not parser:
parser = get_class(
f'recognizers_date_time.date_time.base_{model.lower()}',
f'Base{model}Parser')
if model == 'TimeZone':
return parser()
configuration_class = get_class(
f'recognizers_date_time.date_time.{language.lower()}.{model.lower()}_parser_config',
f'{language}{model}ParserConfiguration')
language_configuration = get_class(
f'recognizers_date_time.date_time.{language.lower()}.common_configs',
f'{language}CommonDateTimeParserConfiguration')
if dmyDateFormat:
configuration = configuration_class(
language_configuration(), dmyDateFormat) if language_configuration else configuration_class()
else:
configuration = configuration_class(
language_configuration()) if language_configuration else configuration_class()
if model == 'Merged':
option = get_option(options)
return parser(configuration, option)
return parser(configuration)
def get_class(module_name, class_name):
try:
module = importlib.import_module(module_name)
except ImportError:
return None
return getattr(module, class_name) if hasattr(module, class_name) else None
def get_language(culture):
return [x for x in CULTURES if CULTURES[x] == culture][0]
def get_reference_date(context):
reference_datetime = context.get('ReferenceDateTime') if context else None
return datetime.datetime.strptime(reference_datetime[0:19],
'%Y-%m-%dT%H:%M:%S') if reference_datetime and not isinstance(reference_datetime,
datetime.datetime) else None
def get_results(culture, model, source, options, reference):
return MODELFUNCTION[model](source, culture, options, reference)
def get_option(option):
if not option:
option = 'None'
module = importlib.import_module('recognizers_date_time.date_time.utilities')
option_class = getattr(module, 'DateTimeOptions')
if option in ['CalendarMode']:
return option_class['CALENDAR']
elif option in ['SkipFromTo']:
return option_class['SKIP_FROM_TO_MERGE']
elif option in ['SplitDateAndTime']:
return option_class['SPLIT_DATE_AND_TIME']
return option_class['NONE']