-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathTestImages.py
More file actions
597 lines (457 loc) · 17.6 KB
/
TestImages.py
File metadata and controls
597 lines (457 loc) · 17.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#
# The MIT License
#
# @copyright Copyright (c) 2017 Intel Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import unittest
import TestCommand
from os import path
class TestImages(TestCommand.TestCommand):
def create_image(
self,
command_str,
ref=None,
format=None,
props=None,
ops=None,
constraints=None,
unique=False,
results=None,
link=None,
collections=None,
):
entity = {}
if ref is not None:
entity["_ref"] = ref
if format is not None and command_str == "AddImage":
entity["format"] = format
if unique and command_str == "FindImage":
entity["unique"] = unique
if results is not None and command_str == "FindImage":
entity["results"] = results
if constraints is not None:
entity["constraints"] = constraints
if link is not None:
entity["link"] = link
if collections is not None:
entity["collections"] = collections
if ops not in [None, {}, []]:
entity["operations"] = ops
if props not in [None, {}, []] and command_str in ["AddImage", "UpdateImage"]:
entity["properties"] = props
query = {command_str: entity}
return query
# Method to insert one image
def insertImage(self, db, props=None, collections=None, format="png"):
imgs_arr = []
all_queries = []
fd = open(path.join(self.find_tests_dir(), "test_images/brain.png"), "rb")
imgs_arr.append(fd.read())
fd.close()
# adds some prop
if props is not None:
props["test_case"] = "test_case_prop"
op_params_resize = {}
op_params_resize["height"] = 512
op_params_resize["width"] = 512
op_params_resize["type"] = "resize"
query = self.create_image(
"AddImage",
ref=12,
format=format,
ops=[op_params_resize],
props=props,
collections=collections,
)
all_queries.append(query)
response, _ = db.query(all_queries, [imgs_arr])
# Check success
self.assertEqual(response[0]["AddImage"]["status"], 0)
def test_JPG_addImage_Without_operations(self):
db = self.create_connection()
all_queries = []
imgs_arr = []
number_of_inserts = 2
for i in range(0, number_of_inserts):
# Read Brain Image
fd = open(path.join(self.find_tests_dir(), "test_images/large1.jpg"), "rb")
imgs_arr.append(fd.read())
fd.close()
props = {}
props["name"] = "brain_" + str(i)
props["doctor"] = "Dr. Strange Love"
query = self.create_image(
"AddImage",
format="jpg",
props=props,
)
all_queries.append(query)
response, img_array = db.query(all_queries, [imgs_arr])
self.assertEqual(len(response), number_of_inserts)
for i in range(0, number_of_inserts):
self.assertEqual(response[i]["AddImage"]["status"], 0)
def test_PNG_addImage_Without_operations(self):
db = self.create_connection()
all_queries = []
imgs_arr = []
number_of_inserts = 2
for i in range(0, number_of_inserts):
# Read Brain Image
fd = open(path.join(self.find_tests_dir(), "test_images/brain.png"), "rb")
imgs_arr.append(fd.read())
fd.close()
props = {}
props["name"] = "brain_" + str(i)
props["doctor"] = "Dr. Strange Love"
query = self.create_image(
"AddImage",
format="png",
props=props,
)
all_queries.append(query)
response, img_array = db.query(all_queries, [imgs_arr])
self.assertEqual(len(response), number_of_inserts)
for i in range(0, number_of_inserts):
self.assertEqual(response[i]["AddImage"]["status"], 0)
for img in img_array:
self.verify_png_signature(img)
def test_addImage(self):
# Setup
db = self.create_connection()
all_insert_queries = []
all_find_queries = []
imgs_arr = []
number_of_inserts = 2
for i in range(0, number_of_inserts):
# Read Brain Image
fd = open(path.join(self.find_tests_dir(), "test_images/brain.png"), "rb")
imgs_arr.append(fd.read())
fd.close()
op_params_resize = {}
op_params_resize["height"] = 512
op_params_resize["width"] = 512
op_params_resize["type"] = "resize"
props = {}
props["name"] = "test_brain_" + str(i)
props["doctor"] = "Dr. Strange Love"
query = self.create_image(
"AddImage",
format="png",
props=props,
ops=[op_params_resize],
)
all_insert_queries.append(query)
# Execute the test
response_from_insert, _ = db.query(all_insert_queries, [imgs_arr])
# Call to function in charge of checking the images were found
for i in range(0, number_of_inserts):
constraints = {}
constraints["name"] = ["==", "test_brain_" + str(i)]
query = self.create_image(
"FindImage",
constraints=constraints,
)
all_find_queries.append(query)
response_from_find, img_found_array = db.query(all_find_queries)
# Disconnect the connection to avoid unused connections in case of any
# assert fails
self.disconnect(db)
# Verify the results
self.assertEqual(len(response_from_insert), number_of_inserts)
for i in range(0, number_of_inserts):
self.assertEqual(response_from_insert[i]["AddImage"]["status"], 0)
# Verify the images were inserted earlier, now they could be found
for i in range(0, number_of_inserts):
self.assertEqual(response_from_find[i]["FindImage"]["status"], 0)
self.assertEqual(len(img_found_array), number_of_inserts)
# Verify the blob data returned is an array of PNG data
for img in img_found_array:
self.verify_png_signature(img)
def test_findEntityImage(self):
db = self.create_connection()
prefix_name = "fent_brain_"
number_of_iterations = 2
for i in range(0, number_of_iterations):
props = {}
props["name"] = prefix_name + str(i)
self.insertImage(db, props=props)
all_queries = []
for i in range(0, number_of_iterations):
constraints = {}
constraints["name"] = ["==", prefix_name + str(i)]
results = {}
results["list"] = ["name"]
query = self.create_entity(
"FindEntity",
class_str="VD:IMG",
results=results,
constraints=constraints,
)
all_queries.append(query)
response, _ = db.query(all_queries)
for index in range(0, number_of_iterations):
self.assertEqual(response[index]["FindEntity"]["status"], 0)
self.assertEqual(
response[index]["FindEntity"]["entities"][0]["name"],
prefix_name + str(index),
)
def test_findImage(self):
# Setup
db = self.create_connection()
prefix_name = "fimg_brain_"
num_images = 2
filenames = []
for i in range(0, num_images):
filenames.append(prefix_name + str(i))
for i in range(0, num_images):
props = {}
props["name"] = filenames[i]
self.insertImage(db, props=props)
# Execute the tests
all_queries = []
for i in range(0, num_images):
constraints = {}
constraints["name"] = ["==", filenames[i]]
query = self.create_image(
"FindImage",
constraints=constraints,
)
all_queries.append(query)
response, img_array = db.query(all_queries)
self.disconnect(db)
# Verify the results
for i in range(0, num_images):
self.assertEqual(response[i]["FindImage"]["status"], 0)
self.assertEqual(len(img_array), num_images)
# Verify the returned blob data is an array of PNG data
for img in img_array:
self.verify_png_signature(img)
def test_findImageResults(self):
# Setup
db = self.create_connection()
prefix_name = "fimg_results_"
number_of_iterations = 2
for i in range(0, number_of_iterations):
props = {}
props["name"] = prefix_name + str(i)
self.insertImage(db, props=props)
# Execute the tests
all_queries = []
for i in range(0, number_of_iterations):
constraints = {}
constraints["name"] = ["==", prefix_name + str(i)]
results = {}
results["list"] = ["name"]
query = self.create_image(
"FindImage", constraints=constraints, results=results
)
all_queries.append(query)
response, img_array = db.query(all_queries)
self.disconnect(db)
# Verify the results
for index in range(0, number_of_iterations):
self.assertEqual(response[index]["FindImage"]["status"], 0)
self.assertEqual(
response[index]["FindImage"]["entities"][0]["name"],
prefix_name + str(index),
)
self.assertEqual(len(img_array), number_of_iterations)
# Verify the returned blob data is an array of PNG data
for img in img_array:
self.verify_png_signature(img)
def test_addImageWithLink(self):
# Setup
db = self.create_connection()
all_queries = []
props = {}
props["name"] = "Luis"
props["lastname"] = "Ferro"
props["age"] = 27
query = self.create_entity(
"AddEntity", class_str="AwesomePeople", ref=32, props=props
)
all_queries.append(query)
props = {}
props["name"] = "Luis"
props["lastname"] = "Malo"
props["age"] = 27
link = {}
link["ref"] = 32
link["direction"] = "in"
link["class"] = "Friends"
imgs_arr = []
fd = open(path.join(self.find_tests_dir(), "test_images/brain.png"), "rb")
imgs_arr.append(fd.read())
fd.close()
query = self.create_image(
"AddImage",
format="png",
link=link,
props=props,
)
all_queries.append(query)
# Execute the test
response, _ = db.query(all_queries, [imgs_arr])
self.disconnect(db)
# Verify the results
self.assertEqual(response[0]["AddEntity"]["status"], 0)
self.assertEqual(response[1]["AddImage"]["status"], 0)
def test_findImage_multiple_results(self):
# Setup
db = self.create_connection()
prefix_name = "fimg_brain_multiple"
number_of_inserts = 4
for i in range(0, number_of_inserts):
props = {}
props["name"] = prefix_name
self.insertImage(db, props=props)
constraints = {}
constraints["name"] = ["==", prefix_name]
results = {}
results["list"] = ["name"]
query = self.create_image("FindImage", constraints=constraints, results=results)
all_queries = []
all_queries.append(query)
# Execute the tests
response, img_array = db.query(all_queries)
self.disconnect(db)
# Verify the results
self.assertEqual(len(img_array), number_of_inserts)
self.assertEqual(response[0]["FindImage"]["status"], 0)
self.assertEqual(response[0]["FindImage"]["returned"], number_of_inserts)
# Verify the blob data returned is an array of PNG data
for img in img_array:
self.verify_png_signature(img)
def test_findImageNoBlob(self):
# Setup
db = self.create_connection()
prefix_name = "fimg_no_blob_"
number_of_images = 2
for i in range(0, number_of_images):
props = {}
props["name"] = prefix_name + str(i)
self.insertImage(db, props=props)
all_queries = []
for i in range(0, number_of_images):
constraints = {}
constraints["name"] = ["==", prefix_name + str(i)]
results = {}
results["blob"] = False
results["list"] = ["name"]
query = self.create_image(
"FindImage", constraints=constraints, results=results
)
all_queries.append(query)
# Execute the tests
response, img_array = db.query(all_queries)
self.disconnect(db)
# Verify the results
for index in range(0, number_of_images):
self.assertEqual(response[index]["FindImage"]["status"], 0)
self.assertEqual(
response[index]["FindImage"]["entities"][0]["name"],
prefix_name + str(index),
)
self.assertEqual(len(img_array), 0)
def test_findImageRefNoBlobNoPropsResults(self):
# Setup
db = self.create_connection()
prefix_name = "fimg_no_blob_no_res"
expected_info = "No entities found"
number_of_images = 2
for i in range(0, number_of_images):
props = {}
props["name"] = prefix_name + str(i)
props["id"] = i
self.insertImage(db, props=props)
all_queries = []
for i in range(0, number_of_images):
constraints = {}
constraints["name"] = ["==", prefix_name + str(i)]
results = {}
results["blob"] = False
# results["list"] = ["name", "id"]
query = self.create_image(
"FindImage", ref=22 + i, constraints=constraints, results=results
)
all_queries.append(query)
# Execute the tests
response, img_array = db.query(all_queries)
self.disconnect(db)
# Verify the results
for index in range(0, number_of_images):
self.assertEqual(response[index]["FindImage"]["status"], 0)
self.assertEqual(response[index]["FindImage"]["info"], expected_info)
self.assertEqual(len(img_array), 0)
def test_updateImage(self):
# Setup
db = self.create_connection()
prefix_name = "fimg_update_"
number_of_images = 2
for i in range(0, number_of_images):
props = {}
props["name"] = prefix_name + str(i)
self.insertImage(db, props=props)
all_queries = []
constraints = {}
constraints["name"] = ["==", prefix_name + str(0)]
props = {}
props["name"] = "simg_update_0"
query = self.create_image("UpdateImage", constraints=constraints, props=props)
all_queries.append(query)
# Execute the tests
response, img_array = db.query(all_queries)
self.disconnect(db)
# Verify the results
self.assertEqual(response[0]["UpdateImage"]["count"], 1)
self.assertEqual(response[0]["UpdateImage"]["status"], 0)
self.assertEqual(len(img_array), 0)
# The following test fails:
# Error: "Object contains a property that could not be validated using
# 'properties' or 'additionalProperties' constraints: 'AddImage'"
@unittest.skip("Skipping the test until it is fixed")
def test_zFindImageWithCollection(self):
# Setup
db = self.create_connection()
prefix_name = "fimg_brain_collection_"
number_of_inserts = 4
colls = {}
colls = ["brainScans"]
for i in range(0, number_of_inserts):
props = {}
props["name"] = prefix_name + str(i)
self.insertImage(db, props=props, collections=colls)
all_queries = []
for i in range(0, 1):
results = {}
results["list"] = ["name"]
query = self.create_image(
"FindImage", collections=["brainScans"], results=results
)
all_queries.append(query)
# Execute the tests
response, img_array = db.query(all_queries)
self.disconnect(db)
# Verify the results
self.assertEqual(response[0]["FindImage"]["status"], 0)
self.assertEqual(len(img_array), number_of_inserts)