-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_async_query.py
More file actions
225 lines (204 loc) · 6.7 KB
/
Copy pathtest_async_query.py
File metadata and controls
225 lines (204 loc) · 6.7 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
"""Tests for the ``AsyncCloudRecoService`` querying functionality."""
import io # noqa: TC003
import uuid
from typing import BinaryIO
import pytest
from mock_vws import MockVWS
from mock_vws.database import CloudDatabase
from vws import AsyncCloudRecoService, AsyncVWS
from vws.include_target_data import CloudRecoIncludeTargetData
class TestQuery:
"""Tests for making async image queries."""
@staticmethod
@pytest.mark.asyncio
async def test_no_matches(
*,
async_cloud_reco_client: AsyncCloudRecoService,
image: io.BytesIO | BinaryIO,
) -> None:
"""An empty list is returned if there are no matches."""
result = await async_cloud_reco_client.query(
image=image,
)
assert result == []
@staticmethod
@pytest.mark.asyncio
async def test_match(
*,
async_vws_client: AsyncVWS,
async_cloud_reco_client: AsyncCloudRecoService,
image: io.BytesIO | BinaryIO,
) -> None:
"""Details of matching targets are returned."""
target_id = await async_vws_client.add_target(
name="x",
width=1,
image=image,
active_flag=True,
application_metadata=None,
)
await async_vws_client.wait_for_target_processed(
target_id=target_id,
)
[matching_target] = await async_cloud_reco_client.query(
image=image,
)
assert matching_target.target_id == target_id
class TestCustomBaseVWQURL:
"""Tests for using a custom base VWQ URL."""
@staticmethod
@pytest.mark.asyncio
async def test_custom_base_url(
image: io.BytesIO | BinaryIO,
) -> None:
"""It is possible to query a target in a database under
a custom VWQ URL.
"""
base_vwq_url = "http://example.com"
with MockVWS(base_vwq_url=base_vwq_url) as mock:
database = CloudDatabase()
mock.add_cloud_database(cloud_database=database)
async_vws_client = AsyncVWS(
server_access_key=database.server_access_key,
server_secret_key=database.server_secret_key,
)
target_id = await async_vws_client.add_target(
name="x",
width=1,
image=image,
active_flag=True,
application_metadata=None,
)
await async_vws_client.wait_for_target_processed(
target_id=target_id,
)
async_cloud_reco_client = AsyncCloudRecoService(
client_access_key=database.client_access_key,
client_secret_key=database.client_secret_key,
base_vwq_url=base_vwq_url,
)
matches = await async_cloud_reco_client.query(
image=image,
)
assert len(matches) == 1
match = matches[0]
assert match.target_id == target_id
class TestMaxNumResults:
"""Tests for the ``max_num_results`` parameter of
``query``.
"""
@staticmethod
@pytest.mark.asyncio
async def test_custom(
*,
async_vws_client: AsyncVWS,
async_cloud_reco_client: AsyncCloudRecoService,
image: io.BytesIO | BinaryIO,
) -> None:
"""It is possible to set a custom
``max_num_results``.
"""
target_id = await async_vws_client.add_target(
name=uuid.uuid4().hex,
width=1,
image=image,
active_flag=True,
application_metadata=None,
)
target_id_2 = await async_vws_client.add_target(
name=uuid.uuid4().hex,
width=1,
image=image,
active_flag=True,
application_metadata=None,
)
target_id_3 = await async_vws_client.add_target(
name=uuid.uuid4().hex,
width=1,
image=image,
active_flag=True,
application_metadata=None,
)
await async_vws_client.wait_for_target_processed(
target_id=target_id,
)
await async_vws_client.wait_for_target_processed(
target_id=target_id_2,
)
await async_vws_client.wait_for_target_processed(
target_id=target_id_3,
)
max_num_results = 2
matches = await async_cloud_reco_client.query(
image=image,
max_num_results=max_num_results,
)
assert len(matches) == max_num_results
class TestIncludeTargetData:
"""Tests for the ``include_target_data`` parameter of
``query``.
"""
@staticmethod
@pytest.mark.asyncio
async def test_none(
*,
async_vws_client: AsyncVWS,
async_cloud_reco_client: AsyncCloudRecoService,
image: io.BytesIO | BinaryIO,
) -> None:
"""When ``CloudRecoIncludeTargetData.NONE`` is given,
target data is not returned in any match.
"""
target_id = await async_vws_client.add_target(
name=uuid.uuid4().hex,
width=1,
image=image,
active_flag=True,
application_metadata=None,
)
await async_vws_client.wait_for_target_processed(
target_id=target_id,
)
[match] = await async_cloud_reco_client.query(
image=image,
include_target_data=(CloudRecoIncludeTargetData.NONE),
)
assert match.target_data is None
@staticmethod
@pytest.mark.asyncio
async def test_all(
*,
async_vws_client: AsyncVWS,
async_cloud_reco_client: AsyncCloudRecoService,
image: io.BytesIO | BinaryIO,
) -> None:
"""When ``CloudRecoIncludeTargetData.ALL`` is given,
target data is returned in all matches.
"""
target_id = await async_vws_client.add_target(
name=uuid.uuid4().hex,
width=1,
image=image,
active_flag=True,
application_metadata=None,
)
target_id_2 = await async_vws_client.add_target(
name=uuid.uuid4().hex,
width=1,
image=image,
active_flag=True,
application_metadata=None,
)
await async_vws_client.wait_for_target_processed(
target_id=target_id,
)
await async_vws_client.wait_for_target_processed(
target_id=target_id_2,
)
top_match, second_match = await async_cloud_reco_client.query(
image=image,
max_num_results=2,
include_target_data=(CloudRecoIncludeTargetData.ALL),
)
assert top_match.target_data is not None
assert second_match.target_data is not None