Skip to content

Commit f923de4

Browse files
committed
ContactDto tests added
1 parent 5918c8f commit f923de4

File tree

2 files changed

+110
-71
lines changed

2 files changed

+110
-71
lines changed

tests/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def pytest_configure(config):
1919
config.addinivalue_line("markers", "ai")
2020

2121
@pytest.fixture(scope="class")
22-
def test_data(request):
22+
def td(request):
2323
config = _get_config(request)
2424
app_sid = config["appsid"]
2525
app_key = config["appkey"]

tests/tests/sdk_test.py

+109-70
Original file line numberDiff line numberDiff line change
@@ -7,124 +7,124 @@
77
from dateutil import tz
88

99
@pytest.mark.pipeline
10-
def test_hierarchical(test_data: TestData):
10+
def test_hierarchical(td: TestData):
1111
"""
1212
HierarchicalObject serialization and deserialization test.
1313
This test checks that BaseObject.Type field filled automatically by SDK
1414
and properly used in serialization and deserialization
1515
"""
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))
1818
assert calendar.name == 'CALENDAR'
1919
assert calendar.type == 'HierarchicalObject'
2020
primitive_properties = list(filter(lambda x: x.type == 'PrimitiveObject', calendar.internal_properties))
2121
assert len(primitive_properties) >= 3
2222
assert primitive_properties[0].value is not None
2323

2424
@pytest.mark.pipeline
25-
def test_async(test_data: TestData):
25+
def test_async(td: TestData):
2626
"""
2727
Asynchronous API call test
2828
"""
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()
3131
assert calendar.name == 'CALENDAR'
3232

3333
@pytest.mark.pipeline
34-
def test_file(test_data: TestData):
34+
def test_file(td: TestData):
3535
"""
3636
File support test
3737
"""
3838
sample = os.path.join(os.path.dirname(__file__), '..', 'data', 'sample.ics')
3939
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))
4444
with open(downloaded, 'r') as f:
4545
filedata = f.read()
4646
assert 'Broadway' in filedata
4747

4848
@pytest.mark.pipeline
49-
def test_contact_format(test_data: TestData):
49+
def test_contact_format(td: TestData):
5050
"""
5151
Contact format specified as Enum, but SDK represents it as a string.
5252
Test checks that value parsing works properly
5353
"""
5454
for contact_format in ['vcard', 'msg']:
5555
extension = '.vcf' if contact_format == 'vcard' else '.msg'
5656
name = str(uuid.uuid4()) + extension
57-
test_data.email.create_contact(
57+
td.email.create_contact(
5858
requests.CreateContactRequest(
5959
contact_format,
6060
name,
6161
models.HierarchicalObjectRequest(
6262
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))
6767
assert object_exist.exists
6868
pass
6969

7070
@pytest.mark.pipeline
71-
def test_date_time(test_data: TestData):
71+
def test_date_time(td: TestData):
7272
"""
7373
Test datetime serialization and deserialization.
7474
Checks that SDK and Backend do not change datetime during processing.
7575
In most cases developer should carefully serialize and deserialize datetime
7676
"""
7777
start_date = datetime.today() + timedelta(days=1)
7878
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(
8181
calendar_storage,
82-
test_data.folder,
83-
test_data.storage)) # type: models.HierarchicalObject
82+
td.folder,
83+
td.storage)) # type: models.HierarchicalObject
8484
start_date_property = list(filter(lambda item: item.name == 'STARTDATE', calendar.internal_properties))[0]
8585
fact_start_date = dateutil.parser.parse(start_date_property.value)
8686
assert start_date == fact_start_date
8787

8888
@pytest.mark.ai
89-
def test_ai_bcr_parse_storage(test_data: TestData):
89+
def test_ai_bcr_parse_storage(td: TestData):
9090
file_name = str(uuid.uuid4())+ ".png"
9191
image_file = os.path.join(os.path.dirname(__file__), '..', 'data', 'test_single_0001.png')
9292
# 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))
9595

9696
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))
9999
# 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(
101101
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
104104
# Check that only one file produced
105105
assert len(result.value) == 1
106106
# 3) Get file name from recognition result
107107
contact_file = result.value[0] # type: models.StorageFileLocation
108108
# 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(
110110
contact_file.folder_path + "/" + contact_file.file_name,
111-
test_data.storage))
111+
td.storage))
112112
with open(downloaded, 'r') as f:
113113
filedata = f.read()
114114
assert 'Thomas' in filedata
115115
# 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(
117117
'VCard', contact_file.file_name, contact_file.folder_path, contact_file.storage)) # type: models.HierarchicalObject
118118
assert len(contact_properties.internal_properties) >= 3
119119

120120
@pytest.mark.ai
121-
def test_ai_bcr_parse(test_data: TestData):
121+
def test_ai_bcr_parse(td: TestData):
122122
image_file = os.path.join(os.path.dirname(__file__), '..', 'data', 'test_single_0001.png')
123123
image_data = None
124124
with open(image_file, 'rb') as f:
125125
filedata = f.read()
126126
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(
128128
models.AiBcrBase64Rq(images=[models.AiBcrBase64Image(True, image_data)]))) # type: models.ListResponseOfHierarchicalObject
129129
assert len(result.value) == 1
130130
display_name = list(filter(
@@ -133,52 +133,52 @@ def test_ai_bcr_parse(test_data: TestData):
133133
assert 'Thomas' in display_name.value
134134

135135
@pytest.mark.ai
136-
def test_ai_name_genderize(test_data: TestData):
136+
def test_ai_name_genderize(td: TestData):
137137
""" Test name gender detection """
138-
result = test_data.email.ai_name_genderize(
138+
result = td.email.ai_name_genderize(
139139
requests.AiNameGenderizeRequest('John Cane')) # type: models.ListResponseOfAiNameGenderHypothesis
140140
assert len(result.value) >= 1
141141
assert result.value[0].gender == 'Male'
142142

143143
@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(
146146
requests.AiNameFormatRequest(
147147
'Mr. John Michael Cane',
148148
format='%t%L%f%m')) # type: models.AiNameFormatted
149149
assert result.name == 'Mr. Cane J. M.'
150150

151151
@pytest.mark.ai
152-
def test_ai_name_match(test_data: TestData):
152+
def test_ai_name_match(td: TestData):
153153
first = 'John Michael Cane'
154154
second = 'Cane J.'
155-
result = test_data.email.ai_name_match(
155+
result = td.email.ai_name_match(
156156
requests.AiNameMatchRequest(first, second)) # type: models.AiNameMatchResult
157157
assert result.similarity >= 0.5
158158

159159
@pytest.mark.ai
160-
def test_ai_name_expand(test_data: TestData):
160+
def test_ai_name_expand(td: TestData):
161161
name = 'Smith Bobby'
162-
result = test_data.email.ai_name_expand(
162+
result = td.email.ai_name_expand(
163163
requests.AiNameExpandRequest(name)) # type: models.AiNameWeightedVariants
164164
expandedNames = list(weighted.name for weighted in result.names)
165165
assert 'Mr. Smith' in expandedNames
166166
assert 'B. Smith' in expandedNames
167167

168168
@pytest.mark.ai
169-
def test_ai_name_complete(test_data: TestData):
169+
def test_ai_name_complete(td: TestData):
170170
prefix = 'Dav'
171-
result = test_data.email.ai_name_complete(
171+
result = td.email.ai_name_complete(
172172
requests.AiNameCompleteRequest(prefix)) # type: models.AiNameWeightedVariants
173173
names = list(prefix + weighted.name for weighted in result.names)
174174
assert 'David' in names
175175
assert 'Dave' in names
176176
assert 'Davis' in names
177177

178178
@pytest.mark.ai
179-
def test_ai_name_parse_email_address(test_data: TestData):
179+
def test_ai_name_parse_email_address(td: TestData):
180180
address = '[email protected]'
181-
result = test_data.email.ai_name_parse_email_address(
181+
result = td.email.ai_name_parse_email_address(
182182
requests.AiNameParseEmailAddressRequest(address))
183183
names = (extracted.name for extracted in result.value)
184184
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):
187187
assert given_name.value == 'John'
188188
assert surname.value == 'Cane'
189189

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):
192192
calendar = models.CalendarDto()
193193
calendar.attendees = [models.MailAddress('Attendee Name', '[email protected]', 'Accepted')]
194194
calendar.description = 'Some description'
@@ -198,48 +198,87 @@ def test_create_calendar_email(test_data: TestData):
198198
calendar.end_date = calendar.start_date + timedelta(hours=1)
199199
calendar.location = 'Some location'
200200

201-
folder_location = models.StorageFolderLocation(test_data.storage, test_data.folder)
201+
folder_location = models.StorageFolderLocation(td.storage, td.folder)
202202
calendar_file = str(uuid.uuid4()) + '.ics'
203-
test_data.email.save_calendar_model(
203+
td.email.save_calendar_model(
204204
requests.SaveCalendarModelRequest(
205205
calendar_file,
206206
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))
209209
assert exist_result.exists
210210

211-
alternate = test_data.email.convert_calendar_model_to_alternate(
211+
alternate = td.email.convert_calendar_model_to_alternate(
212212
requests.ConvertCalendarModelToAlternateRequest(
213213
models.CalendarDtoAlternateRq(calendar, 'Create')))
214214

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')
222222

223223
email_file = str(uuid.uuid4())+ '.eml'
224-
test_data.email.save_email_model(
224+
td.email.save_email_model(
225225
requests.SaveEmailModelRequest(
226226
'Eml', email_file,
227227
models.StorageModelRqOfEmailDto(email, folder_location)))
228228

229-
downloaded = test_data.email.download_file(
229+
downloaded = td.email.download_file(
230230
requests.DownloadFileRequest(
231-
test_data.folder + '/' + email_file,
232-
test_data.storage))
231+
td.folder + '/' + email_file,
232+
td.storage))
233233
with open(downloaded, 'r') as f:
234234
file_data = f.read()
235235
assert '[email protected]' in file_data
236236

237237

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):
239278
name = str(uuid.uuid4())+ '.ics'
240-
start_date =
279+
start_date = (
241280
start_date_param if start_date_param is not None
242-
else datetime.today() + timedelta(days=1)
281+
else datetime.today() + timedelta(days=1))
243282
end_date = start_date + timedelta(hours=1)
244283
request = requests.CreateCalendarRequest(
245284
name,
@@ -259,7 +298,7 @@ def _create_calendar(test_data, start_date_param=None):
259298
])
260299
])
261300
]),
262-
models.StorageFolderLocation(test_data.storage, test_data.folder)
301+
models.StorageFolderLocation(td.storage, td.folder)
263302
))
264-
test_data.email.create_calendar(request)
303+
td.email.create_calendar(request)
265304
return name

0 commit comments

Comments
 (0)