7
7
from dateutil import tz
8
8
9
9
@pytest .mark .pipeline
10
- def test_hierarchical (test_data : TestData ):
10
+ def test_hierarchical (td : TestData ):
11
11
"""
12
12
HierarchicalObject serialization and deserialization test.
13
13
This test checks that BaseObject.Type field filled automatically by SDK
14
14
and properly used in serialization and deserialization
15
15
"""
16
- calendarFile = _create_calendar (test_data )
17
- calendar = test_data .email .get_calendar (requests .GetCalendarRequest (calendarFile , test_data .folder , test_data .storage ))
16
+ calendarFile = _create_calendar (td )
17
+ calendar = td .email .get_calendar (requests .GetCalendarRequest (calendarFile , td .folder , td .storage ))
18
18
assert calendar .name == 'CALENDAR'
19
19
assert calendar .type == 'HierarchicalObject'
20
20
primitive_properties = list (filter (lambda x : x .type == 'PrimitiveObject' , calendar .internal_properties ))
21
21
assert len (primitive_properties ) >= 3
22
22
assert primitive_properties [0 ].value is not None
23
23
24
24
@pytest .mark .pipeline
25
- def test_async (test_data : TestData ):
25
+ def test_async (td : TestData ):
26
26
"""
27
27
Asynchronous API call test
28
28
"""
29
- calendarFile = _create_calendar (test_data )
30
- calendar = test_data .email .get_calendar_async (requests .GetCalendarRequest (calendarFile , test_data .folder , test_data .storage )).get ()
29
+ calendarFile = _create_calendar (td )
30
+ calendar = td .email .get_calendar_async (requests .GetCalendarRequest (calendarFile , td .folder , td .storage )).get ()
31
31
assert calendar .name == 'CALENDAR'
32
32
33
33
@pytest .mark .pipeline
34
- def test_file (test_data : TestData ):
34
+ def test_file (td : TestData ):
35
35
"""
36
36
File support test
37
37
"""
38
38
sample = os .path .join (os .path .dirname (__file__ ), '..' , 'data' , 'sample.ics' )
39
39
file_name = str (uuid .uuid4 ()) + ".ics"
40
- storage_location = test_data .folder + "/" + file_name
41
- test_data .email .upload_file (requests .UploadFileRequest (storage_location , sample , test_data .storage ))
42
- rq = requests .UploadFileRequest (storage_location , sample , test_data .storage )
43
- downloaded = test_data .email .download_file (requests .DownloadFileRequest (storage_location , test_data .storage ))
40
+ storage_location = td .folder + "/" + file_name
41
+ td .email .upload_file (requests .UploadFileRequest (storage_location , sample , td .storage ))
42
+ rq = requests .UploadFileRequest (storage_location , sample , td .storage )
43
+ downloaded = td .email .download_file (requests .DownloadFileRequest (storage_location , td .storage ))
44
44
with open (downloaded , 'r' ) as f :
45
45
filedata = f .read ()
46
46
assert 'Broadway' in filedata
47
47
48
48
@pytest .mark .pipeline
49
- def test_contact_format (test_data : TestData ):
49
+ def test_contact_format (td : TestData ):
50
50
"""
51
51
Contact format specified as Enum, but SDK represents it as a string.
52
52
Test checks that value parsing works properly
53
53
"""
54
54
for contact_format in ['vcard' , 'msg' ]:
55
55
extension = '.vcf' if contact_format == 'vcard' else '.msg'
56
56
name = str (uuid .uuid4 ()) + extension
57
- test_data .email .create_contact (
57
+ td .email .create_contact (
58
58
requests .CreateContactRequest (
59
59
contact_format ,
60
60
name ,
61
61
models .HierarchicalObjectRequest (
62
62
models .HierarchicalObject ('CONTACT' , internal_properties = []),
63
- models .StorageFolderLocation (test_data .storage , test_data .folder ))))
64
- object_exist = test_data .email .object_exists (requests .ObjectExistsRequest (
65
- test_data .folder + "/" + name ,
66
- test_data .storage ))
63
+ models .StorageFolderLocation (td .storage , td .folder ))))
64
+ object_exist = td .email .object_exists (requests .ObjectExistsRequest (
65
+ td .folder + "/" + name ,
66
+ td .storage ))
67
67
assert object_exist .exists
68
68
pass
69
69
70
70
@pytest .mark .pipeline
71
- def test_date_time (test_data : TestData ):
71
+ def test_date_time (td : TestData ):
72
72
"""
73
73
Test datetime serialization and deserialization.
74
74
Checks that SDK and Backend do not change datetime during processing.
75
75
In most cases developer should carefully serialize and deserialize datetime
76
76
"""
77
77
start_date = datetime .today () + timedelta (days = 1 )
78
78
start_date = start_date .replace (microsecond = 0 , tzinfo = tz .tzutc ())
79
- calendar_storage = _create_calendar (test_data , start_date )
80
- calendar = test_data .email .get_calendar (requests .GetCalendarRequest (
79
+ calendar_storage = _create_calendar (td , start_date )
80
+ calendar = td .email .get_calendar (requests .GetCalendarRequest (
81
81
calendar_storage ,
82
- test_data .folder ,
83
- test_data .storage )) # type: models.HierarchicalObject
82
+ td .folder ,
83
+ td .storage )) # type: models.HierarchicalObject
84
84
start_date_property = list (filter (lambda item : item .name == 'STARTDATE' , calendar .internal_properties ))[0 ]
85
85
fact_start_date = dateutil .parser .parse (start_date_property .value )
86
86
assert start_date == fact_start_date
87
87
88
88
@pytest .mark .ai
89
- def test_ai_bcr_parse_storage (test_data : TestData ):
89
+ def test_ai_bcr_parse_storage (td : TestData ):
90
90
file_name = str (uuid .uuid4 ())+ ".png"
91
91
image_file = os .path .join (os .path .dirname (__file__ ), '..' , 'data' , 'test_single_0001.png' )
92
92
# 1) Upload business card image to storage
93
- storage_location = test_data .folder + "/" + file_name
94
- test_data .email .upload_file (requests .UploadFileRequest (storage_location , image_file , test_data .storage ))
93
+ storage_location = td .folder + "/" + file_name
94
+ td .email .upload_file (requests .UploadFileRequest (storage_location , image_file , td .storage ))
95
95
96
96
out_folder = str (uuid .uuid4 ())
97
- out_folder_path = test_data .folder + "/" + out_folder
98
- test_data .email .create_folder (requests .CreateFolderRequest (out_folder_path , test_data .storage ))
97
+ out_folder_path = td .folder + "/" + out_folder
98
+ td .email .create_folder (requests .CreateFolderRequest (out_folder_path , td .storage ))
99
99
# 2) Call business card recognition action
100
- result = test_data .email .ai_bcr_parse_storage (requests .AiBcrParseStorageRequest (
100
+ result = td .email .ai_bcr_parse_storage (requests .AiBcrParseStorageRequest (
101
101
models .AiBcrParseStorageRq (
102
- images = [models .AiBcrImageStorageFile (True , models .StorageFileLocation (test_data .storage , test_data .folder , file_name ))],
103
- out_folder = models .StorageFolderLocation (test_data .storage , out_folder_path )))) # type: models.ListResponseOfStorageFileLocation
102
+ images = [models .AiBcrImageStorageFile (True , models .StorageFileLocation (td .storage , td .folder , file_name ))],
103
+ out_folder = models .StorageFolderLocation (td .storage , out_folder_path )))) # type: models.ListResponseOfStorageFileLocation
104
104
# Check that only one file produced
105
105
assert len (result .value ) == 1
106
106
# 3) Get file name from recognition result
107
107
contact_file = result .value [0 ] # type: models.StorageFileLocation
108
108
# 4) Download VCard file, produced by recognition method, check it contains text "Thomas"
109
- downloaded = test_data .email .download_file (requests .DownloadFileRequest (
109
+ downloaded = td .email .download_file (requests .DownloadFileRequest (
110
110
contact_file .folder_path + "/" + contact_file .file_name ,
111
- test_data .storage ))
111
+ td .storage ))
112
112
with open (downloaded , 'r' ) as f :
113
113
filedata = f .read ()
114
114
assert 'Thomas' in filedata
115
115
# 5) Get VCard object properties list, check that there are 3 properties or more
116
- contact_properties = test_data .email .get_contact_properties (requests .GetContactPropertiesRequest (
116
+ contact_properties = td .email .get_contact_properties (requests .GetContactPropertiesRequest (
117
117
'VCard' , contact_file .file_name , contact_file .folder_path , contact_file .storage )) # type: models.HierarchicalObject
118
118
assert len (contact_properties .internal_properties ) >= 3
119
119
120
120
@pytest .mark .ai
121
- def test_ai_bcr_parse (test_data : TestData ):
121
+ def test_ai_bcr_parse (td : TestData ):
122
122
image_file = os .path .join (os .path .dirname (__file__ ), '..' , 'data' , 'test_single_0001.png' )
123
123
image_data = None
124
124
with open (image_file , 'rb' ) as f :
125
125
filedata = f .read ()
126
126
image_data = str (base64 .b64encode (filedata ), 'utf-8' )
127
- result = test_data .email .ai_bcr_parse (requests .AiBcrParseRequest (
127
+ result = td .email .ai_bcr_parse (requests .AiBcrParseRequest (
128
128
models .AiBcrBase64Rq (images = [models .AiBcrBase64Image (True , image_data )]))) # type: models.ListResponseOfHierarchicalObject
129
129
assert len (result .value ) == 1
130
130
display_name = list (filter (
@@ -133,52 +133,52 @@ def test_ai_bcr_parse(test_data: TestData):
133
133
assert 'Thomas' in display_name .value
134
134
135
135
@pytest .mark .ai
136
- def test_ai_name_genderize (test_data : TestData ):
136
+ def test_ai_name_genderize (td : TestData ):
137
137
""" Test name gender detection """
138
- result = test_data .email .ai_name_genderize (
138
+ result = td .email .ai_name_genderize (
139
139
requests .AiNameGenderizeRequest ('John Cane' )) # type: models.ListResponseOfAiNameGenderHypothesis
140
140
assert len (result .value ) >= 1
141
141
assert result .value [0 ].gender == 'Male'
142
142
143
143
@pytest .mark .ai
144
- def test_ai_name_format (test_data : TestData ):
145
- result = test_data .email .ai_name_format (
144
+ def test_ai_name_format (td : TestData ):
145
+ result = td .email .ai_name_format (
146
146
requests .AiNameFormatRequest (
147
147
'Mr. John Michael Cane' ,
148
148
format = '%t%L%f%m' )) # type: models.AiNameFormatted
149
149
assert result .name == 'Mr. Cane J. M.'
150
150
151
151
@pytest .mark .ai
152
- def test_ai_name_match (test_data : TestData ):
152
+ def test_ai_name_match (td : TestData ):
153
153
first = 'John Michael Cane'
154
154
second = 'Cane J.'
155
- result = test_data .email .ai_name_match (
155
+ result = td .email .ai_name_match (
156
156
requests .AiNameMatchRequest (first , second )) # type: models.AiNameMatchResult
157
157
assert result .similarity >= 0.5
158
158
159
159
@pytest .mark .ai
160
- def test_ai_name_expand (test_data : TestData ):
160
+ def test_ai_name_expand (td : TestData ):
161
161
name = 'Smith Bobby'
162
- result = test_data .email .ai_name_expand (
162
+ result = td .email .ai_name_expand (
163
163
requests .AiNameExpandRequest (name )) # type: models.AiNameWeightedVariants
164
164
expandedNames = list (weighted .name for weighted in result .names )
165
165
assert 'Mr. Smith' in expandedNames
166
166
assert 'B. Smith' in expandedNames
167
167
168
168
@pytest .mark .ai
169
- def test_ai_name_complete (test_data : TestData ):
169
+ def test_ai_name_complete (td : TestData ):
170
170
prefix = 'Dav'
171
- result = test_data .email .ai_name_complete (
171
+ result = td .email .ai_name_complete (
172
172
requests .AiNameCompleteRequest (prefix )) # type: models.AiNameWeightedVariants
173
173
names = list (prefix + weighted .name for weighted in result .names )
174
174
assert 'David' in names
175
175
assert 'Dave' in names
176
176
assert 'Davis' in names
177
177
178
178
@pytest .mark .ai
179
- def test_ai_name_parse_email_address (test_data : TestData ):
179
+ def test_ai_name_parse_email_address (td : TestData ):
180
180
181
- result = test_data .email .ai_name_parse_email_address (
181
+ result = td .email .ai_name_parse_email_address (
182
182
requests .AiNameParseEmailAddressRequest (address ))
183
183
names = (extracted .name for extracted in result .value )
184
184
extracted_values = list (functools .reduce (lambda a ,b : a + b , names ))
@@ -187,8 +187,8 @@ def test_ai_name_parse_email_address(test_data: TestData):
187
187
assert given_name .value == 'John'
188
188
assert surname .value == 'Cane'
189
189
190
- @pytest .mark .ai
191
- def test_create_calendar_email (test_data : TestData ):
190
+ @pytest .mark .pipeline
191
+ def test_create_calendar_email (td : TestData ):
192
192
calendar = models .CalendarDto ()
193
193
calendar .
attendees = [
models .
MailAddress (
'Attendee Name' ,
'[email protected] ' ,
'Accepted' )]
194
194
calendar .description = 'Some description'
@@ -198,48 +198,87 @@ def test_create_calendar_email(test_data: TestData):
198
198
calendar .end_date = calendar .start_date + timedelta (hours = 1 )
199
199
calendar .location = 'Some location'
200
200
201
- folder_location = models .StorageFolderLocation (test_data .storage , test_data .folder )
201
+ folder_location = models .StorageFolderLocation (td .storage , td .folder )
202
202
calendar_file = str (uuid .uuid4 ()) + '.ics'
203
- test_data .email .save_calendar_model (
203
+ td .email .save_calendar_model (
204
204
requests .SaveCalendarModelRequest (
205
205
calendar_file ,
206
206
models .StorageModelRqOfCalendarDto (calendar , folder_location )))
207
- exist_result = test_data .email .object_exists (
208
- requests .ObjectExistsRequest (test_data .folder + '/' + calendar_file , test_data .storage ))
207
+ exist_result = td .email .object_exists (
208
+ requests .ObjectExistsRequest (td .folder + '/' + calendar_file , td .storage ))
209
209
assert exist_result .exists
210
210
211
- alternate = test_data .email .convert_calendar_model_to_alternate (
211
+ alternate = td .email .convert_calendar_model_to_alternate (
212
212
requests .ConvertCalendarModelToAlternateRequest (
213
213
models .CalendarDtoAlternateRq (calendar , 'Create' )))
214
214
215
- email = models .EmailDto ()
216
- email . alternate_views = [alternate ]
217
- # 'from' is reserved, so from field is named '_from':
218
- email . _from = models .
MailAddress (
'From Name' ,
'[email protected] ' )
219
- email . to = [
models .
MailAddress (
'To Name' ,
'[email protected] ' )]
220
- email . subject = 'Some subject'
221
- email . body = 'Some body'
215
+ email = models .EmailDto (
216
+ alternate_views = [alternate ],
217
+ # 'from' is reserved, so from field is named '_from':
218
+ _from = models .
MailAddress (
'From Name' ,
'[email protected] ' )
,
219
+ to = [
models .
MailAddress (
'To Name' ,
'[email protected] ' )]
,
220
+ subject = 'Some subject' ,
221
+ body = 'Some body' )
222
222
223
223
email_file = str (uuid .uuid4 ())+ '.eml'
224
- test_data .email .save_email_model (
224
+ td .email .save_email_model (
225
225
requests .SaveEmailModelRequest (
226
226
'Eml' , email_file ,
227
227
models .StorageModelRqOfEmailDto (email , folder_location )))
228
228
229
- downloaded = test_data .email .download_file (
229
+ downloaded = td .email .download_file (
230
230
requests .DownloadFileRequest (
231
- test_data .folder + '/' + email_file ,
232
- test_data .storage ))
231
+ td .folder + '/' + email_file ,
232
+ td .storage ))
233
233
with open (downloaded , 'r' ) as f :
234
234
file_data = f .read ()
235
235
assert '[email protected] ' in file_data
236
236
237
237
238
- def _create_calendar (test_data , start_date_param = None ):
238
+ @pytest .mark .pipeline
239
+ def test_contact_model (td : TestData ):
240
+ contact = models .ContactDto (
241
+ gender = 'Male' ,
242
+ surname = 'Thomas' ,
243
+ given_name = 'Alex' ,
244
+ email_addresses = [
245
+ models .EmailAddress (
246
+ models .EnumWithCustomOfEmailAddressCategory ('Work' ),
247
+ 'Alex Thomas' ,
True ,
address = '[email protected] ' )],
248
+ phone_numbers = [
249
+ models .PhoneNumber (
250
+ models .EnumWithCustomOfPhoneNumberCategory ('Work' ),
251
+ '+49211424721' , True )])
252
+
253
+ contact_file = str (uuid .uuid4 ())+ '.vcf'
254
+ td .email .save_contact_model (
255
+ requests .SaveContactModelRequest (
256
+ 'VCard' , contact_file ,
257
+ models .StorageModelRqOfContactDto (
258
+ contact ,
259
+ models .StorageFolderLocation (td .storage , td .folder ))))
260
+ exist_result = td .email .object_exists (
261
+ requests .ObjectExistsRequest (td .folder + '/' + contact_file , td .storage ))
262
+ assert exist_result .exists
263
+
264
+ @pytest .mark .ai
265
+ def test_ai_bcr_parse_model (td : TestData ):
266
+ image_file = os .path .join (os .path .dirname (__file__ ), '..' , 'data' , 'test_single_0001.png' )
267
+ image_data = None
268
+ with open (image_file , 'rb' ) as f :
269
+ filedata = f .read ()
270
+ image_data = str (base64 .b64encode (filedata ), 'utf-8' )
271
+ result = td .email .ai_bcr_parse_model (requests .AiBcrParseModelRequest (
272
+ models .AiBcrBase64Rq (images = [models .AiBcrBase64Image (True , image_data )])))
273
+ assert len (result .value ) == 1
274
+ first_vcard = result .value [0 ]
275
+ assert 'Thomas' in first_vcard .display_name
276
+
277
+ def _create_calendar (td , start_date_param = None ):
239
278
name = str (uuid .uuid4 ())+ '.ics'
240
- start_date =
279
+ start_date = (
241
280
start_date_param if start_date_param is not None
242
- else datetime .today () + timedelta (days = 1 )
281
+ else datetime .today () + timedelta (days = 1 ))
243
282
end_date = start_date + timedelta (hours = 1 )
244
283
request = requests .CreateCalendarRequest (
245
284
name ,
@@ -259,7 +298,7 @@ def _create_calendar(test_data, start_date_param=None):
259
298
])
260
299
])
261
300
]),
262
- models .StorageFolderLocation (test_data .storage , test_data .folder )
301
+ models .StorageFolderLocation (td .storage , td .folder )
263
302
))
264
- test_data .email .create_calendar (request )
303
+ td .email .create_calendar (request )
265
304
return name
0 commit comments