|
13 | 13 |
|
14 | 14 | import pytest |
15 | 15 | import requests |
| 16 | +from PIL import Image |
16 | 17 | from requests import codes |
17 | 18 | from requests_mock import POST |
18 | 19 | from urllib3.filepost import encode_multipart_formdata |
19 | 20 |
|
20 | | -from mock_vws._constants import TargetStatuses |
| 21 | +from mock_vws._constants import ResultCodes, TargetStatuses |
21 | 22 | from tests.mock_vws.utils import ( |
22 | 23 | add_target_to_vws, |
23 | 24 | delete_target, |
|
28 | 29 | ) |
29 | 30 | from tests.mock_vws.utils.assertions import ( |
30 | 31 | assert_query_success, |
| 32 | + assert_valid_date_header, |
| 33 | + assert_valid_transaction_id, |
31 | 34 | assert_vwq_failure, |
32 | 35 | ) |
33 | 36 | from tests.mock_vws.utils.authorization import ( |
@@ -939,7 +942,6 @@ def test_valid( |
939 | 942 | date=date, |
940 | 943 | request_path=request_path, |
941 | 944 | ) |
942 | | - |
943 | 945 | headers = { |
944 | 946 | 'Authorization': authorization_string, |
945 | 947 | 'Date': date, |
@@ -1077,17 +1079,71 @@ class TestImageFormats: |
1077 | 1079 | Tests for various image formats. |
1078 | 1080 | """ |
1079 | 1081 |
|
1080 | | - def test_supported(self) -> None: |
| 1082 | + @pytest.mark.parametrize('file_format', ['png', 'jpeg']) |
| 1083 | + def test_supported( |
| 1084 | + self, |
| 1085 | + high_quality_image: io.BytesIO, |
| 1086 | + vuforia_database_keys: VuforiaDatabaseKeys, |
| 1087 | + file_format: str, |
| 1088 | + ) -> None: |
1081 | 1089 | """ |
1082 | | - See https://github.com/adamtheturtle/vws-python/issues/357 for |
1083 | | - implementing this test. |
| 1090 | + PNG and JPEG formats are supported. |
1084 | 1091 | """ |
| 1092 | + image_buffer = io.BytesIO() |
| 1093 | + pil_image = Image.open(high_quality_image) |
| 1094 | + pil_image.save(image_buffer, file_format) |
| 1095 | + image_content = image_buffer.getvalue() |
1085 | 1096 |
|
1086 | | - def test_unsupported(self) -> None: |
| 1097 | + body = {'image': ('image.jpeg', image_content, 'image/jpeg')} |
| 1098 | + |
| 1099 | + response = query( |
| 1100 | + vuforia_database_keys=vuforia_database_keys, |
| 1101 | + body=body, |
| 1102 | + ) |
| 1103 | + |
| 1104 | + assert_query_success(response=response) |
| 1105 | + assert response.json()['results'] == [] |
| 1106 | + |
| 1107 | + def test_unsupported( |
| 1108 | + self, |
| 1109 | + high_quality_image: io.BytesIO, |
| 1110 | + vuforia_database_keys: VuforiaDatabaseKeys, |
| 1111 | + ) -> None: |
1087 | 1112 | """ |
1088 | | - See https://github.com/adamtheturtle/vws-python/issues/357 for |
1089 | | - implementing this test. |
| 1113 | + File formats which are not PNG or JPEG are not supported. |
1090 | 1114 | """ |
| 1115 | + file_format = 'tiff' |
| 1116 | + image_buffer = io.BytesIO() |
| 1117 | + pil_image = Image.open(high_quality_image) |
| 1118 | + pil_image.save(image_buffer, file_format) |
| 1119 | + image_content = image_buffer.getvalue() |
| 1120 | + |
| 1121 | + body = {'image': ('image.jpeg', image_content, 'image/jpeg')} |
| 1122 | + |
| 1123 | + response = query( |
| 1124 | + vuforia_database_keys=vuforia_database_keys, |
| 1125 | + body=body, |
| 1126 | + ) |
| 1127 | + |
| 1128 | + assert_vwq_failure( |
| 1129 | + response=response, |
| 1130 | + status_code=codes.UNPROCESSABLE_ENTITY, |
| 1131 | + content_type='application/json', |
| 1132 | + ) |
| 1133 | + assert response.json().keys() == {'transaction_id', 'result_code'} |
| 1134 | + assert_valid_transaction_id(response=response) |
| 1135 | + assert_valid_date_header(response=response) |
| 1136 | + result_code = response.json()['result_code'] |
| 1137 | + transaction_id = response.json()['transaction_id'] |
| 1138 | + assert result_code == ResultCodes.BAD_IMAGE.value |
| 1139 | + # The separators are inconsistent and we test this. |
| 1140 | + expected_text = ( |
| 1141 | + '{"transaction_id": ' |
| 1142 | + f'"{transaction_id}",' |
| 1143 | + f'"result_code":"{result_code}"' |
| 1144 | + '}' |
| 1145 | + ) |
| 1146 | + assert response.text == expected_text |
1091 | 1147 |
|
1092 | 1148 |
|
1093 | 1149 | @pytest.mark.usefixtures('verify_mock_vuforia') |
|
0 commit comments