Skip to content

Commit 99465b5

Browse files
Merge pull request #1046 from adamtheturtle/test-update-image
Test update image
2 parents 7542316 + 6ecfab6 commit 99465b5

File tree

5 files changed

+82
-48
lines changed

5 files changed

+82
-48
lines changed

spelling_private_dict.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
AuthenticationFailure
22
BadImage
33
ImageTooLarge
4+
MatchProcessing
45
MaxNumResultsOutOfRange
56
MetadataTooLarge
67
ProjectInactive
8+
RequestTimeTooSkewed
79
TargetNameExist
810
TargetProcessingTimeout
911
TargetStatusProcessing

src/vws/query.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def query(
6464
is not correct.
6565
~vws.exceptions.MaxNumResultsOutOfRange: `max_num_results`` is not
6666
within the range (1, 50).
67+
~vws.exceptions.MatchProcessing: The given image matches a target
68+
which was recently added, updated or deleted and Vuforia
69+
returns an error in this case.
6770
~vws.exceptions.ProjectInactive: The project is inactive.
6871
6972
Returns:

tests/fixtures/images.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,43 @@
33
"""
44

55
import io
6+
import random
67

78
import pytest
9+
from PIL import Image
10+
11+
12+
def _make_image_file(
13+
file_format: str,
14+
color_space: str,
15+
width: int,
16+
height: int,
17+
) -> io.BytesIO:
18+
"""
19+
Return an image file in the given format and color space.
20+
21+
The image file is filled with randomly colored pixels.
22+
23+
Args:
24+
file_format: See
25+
http://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html
26+
color_space: One of "L", "RGB", or "CMYK".
27+
width: The width, in pixels of the image.
28+
height: The width, in pixels of the image.
29+
30+
Returns:
31+
An image file in the given format and color space.
32+
"""
33+
image_buffer = io.BytesIO()
34+
reds = random.choices(population=range(0, 255), k=width * height)
35+
greens = random.choices(population=range(0, 255), k=width * height)
36+
blues = random.choices(population=range(0, 255), k=width * height)
37+
pixels = list(zip(reds, greens, blues))
38+
image = Image.new(color_space, (width, height))
39+
image.putdata(pixels)
40+
image.save(image_buffer, file_format)
41+
image_buffer.seek(0)
42+
return image_buffer
843

944

1045
@pytest.fixture()
@@ -18,3 +53,34 @@ def high_quality_image() -> io.BytesIO:
1853
path = 'tests/data/high_quality_image.jpg'
1954
with open(path, 'rb') as high_quality_image_file:
2055
return io.BytesIO(high_quality_image_file.read())
56+
57+
58+
@pytest.fixture
59+
def image_file_failed_state() -> io.BytesIO:
60+
"""
61+
Return an image file which is expected to be accepted by the add and
62+
update target endpoints, but get a "failed" status.
63+
"""
64+
# This image gets a "failed" status because it is so small.
65+
return _make_image_file(
66+
file_format='PNG',
67+
color_space='RGB',
68+
width=1,
69+
height=1,
70+
)
71+
72+
73+
@pytest.fixture
74+
def png_too_large() -> io.BytesIO:
75+
"""
76+
Return a PNG file which has dimensions which are too large to be added to
77+
a Vuforia database.
78+
"""
79+
width = height = 890
80+
81+
return _make_image_file(
82+
file_format='PNG',
83+
color_space='RGB',
84+
width=width,
85+
height=height,
86+
)

tests/test_exceptions.py

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
"""
44

55
import io
6-
import random
76

87
import pytest
98
from freezegun import freeze_time
109
from mock_vws import MockVWS
1110
from mock_vws.database import VuforiaDatabase
1211
from mock_vws.states import States
13-
from PIL import Image
1412
from requests import codes
1513

1614
from vws import VWS, CloudRecoService
@@ -30,53 +28,14 @@
3028
)
3129

3230

33-
def _make_image_file(
34-
file_format: str,
35-
color_space: str,
36-
width: int,
37-
height: int,
38-
) -> io.BytesIO:
39-
"""
40-
Return an image file in the given format and color space.
41-
42-
The image file is filled with randomly colored pixels.
43-
44-
Args:
45-
file_format: See
46-
http://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html
47-
color_space: One of "L", "RGB", or "CMYK".
48-
width: The width, in pixels of the image.
49-
height: The width, in pixels of the image.
50-
51-
Returns:
52-
An image file in the given format and color space.
53-
"""
54-
image_buffer = io.BytesIO()
55-
reds = random.choices(population=range(0, 255), k=width * height)
56-
greens = random.choices(population=range(0, 255), k=width * height)
57-
blues = random.choices(population=range(0, 255), k=width * height)
58-
pixels = list(zip(reds, greens, blues))
59-
image = Image.new(color_space, (width, height))
60-
image.putdata(pixels)
61-
image.save(image_buffer, file_format)
62-
image_buffer.seek(0)
63-
return image_buffer
64-
65-
66-
def test_image_too_large(vws_client: VWS) -> None:
31+
def test_image_too_large(
32+
vws_client: VWS,
33+
png_too_large: io.BytesIO,
34+
) -> None:
6735
"""
6836
When giving an image which is too large, an ``ImageTooLarge`` exception is
6937
raised.
7038
"""
71-
width = height = 890
72-
73-
png_too_large = _make_image_file(
74-
file_format='PNG',
75-
color_space='RGB',
76-
width=width,
77-
height=height,
78-
)
79-
8039
with pytest.raises(ImageTooLarge) as exc:
8140
vws_client.add_target(name='x', width=1, image=png_too_large)
8241

tests/test_vws.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ def test_update_target(
454454
self,
455455
vws_client: VWS,
456456
high_quality_image: io.BytesIO,
457+
image_file_failed_state: io.BytesIO,
457458
) -> None:
458459
"""
459460
It is possible to update a target.
@@ -465,21 +466,24 @@ def test_update_target(
465466
active_flag=True,
466467
)
467468
vws_client.wait_for_target_processed(target_id=target_id)
469+
report = vws_client.get_target_summary_report(target_id=target_id)
470+
assert report['status'] == 'success'
468471
vws_client.update_target(
469472
target_id=target_id,
470473
name='x2',
471474
width=2,
472475
active_flag=False,
473-
# These will be tested in
474-
# https://github.com/adamtheturtle/vws-python/issues/809.
475-
image=high_quality_image,
476+
image=image_file_failed_state,
476477
application_metadata=b'a',
477478
)
478479

480+
vws_client.wait_for_target_processed(target_id=target_id)
479481
target_details = vws_client.get_target_record(target_id=target_id)
480482
assert target_details['name'] == 'x2'
481483
assert target_details['width'] == 2
482484
assert not target_details['active_flag']
485+
report = vws_client.get_target_summary_report(target_id=target_id)
486+
assert report['status'] == 'failed'
483487

484488
def test_no_fields_given(
485489
self,

0 commit comments

Comments
 (0)