|
1 | | -"""Tests for ``MockVWS`` intercepting ``httpx`` via asynchronous ``vws`` |
| 1 | +"""Tests for ``MockVWS`` intercepting ``httpx`` via synchronous ``vws`` |
2 | 2 | clients. |
3 | 3 | """ |
4 | 4 |
|
5 | | -import asyncio |
6 | 5 | import io |
7 | 6 | import uuid |
8 | 7 |
|
9 | 8 | import httpx |
10 | 9 | import pytest |
11 | | -from vws import AsyncCloudRecoService, AsyncVuMarkService, AsyncVWS |
| 10 | +from vws import VWS, CloudRecoService, VuMarkService |
12 | 11 | from vws.exceptions.vws_exceptions import UnknownTargetError |
13 | 12 | from vws.reports import TargetStatuses |
| 13 | +from vws.transports import HTTPXTransport |
14 | 14 | from vws.vumark_accept import VuMarkAccept |
15 | 15 |
|
16 | 16 | from mock_vws import MockVWS |
|
19 | 19 | from mock_vws.target import VuMarkTarget |
20 | 20 |
|
21 | 21 |
|
22 | | -class TestAsyncVWS: |
23 | | - """Asynchronous ``vws-python`` client usage through the mock.""" |
| 22 | +class TestVWS: |
| 23 | + """Synchronous ``vws-python`` client usage through the mock via |
| 24 | + ``httpx``. |
| 25 | + """ |
24 | 26 |
|
25 | 27 | @staticmethod |
26 | 28 | def test_response_delay_causes_httpx_timeout() -> None: |
27 | | - """``httpx`` timeouts are surfaced through ``AsyncVWS``.""" |
| 29 | + """``httpx`` timeouts are surfaced through ``VWS``.""" |
28 | 30 | database = CloudDatabase() |
29 | 31 | calls: list[float] = [] |
30 | 32 |
|
31 | | - async def run_test() -> None: |
32 | | - """Trigger a timed request through the client.""" |
33 | | - async with AsyncVWS( |
34 | | - server_access_key=database.server_access_key, |
35 | | - server_secret_key=database.server_secret_key, |
36 | | - request_timeout_seconds=0.1, |
37 | | - ) as client: |
38 | | - await client.get_database_summary_report() |
39 | | - |
40 | 33 | with MockVWS( |
41 | 34 | response_delay_seconds=5.0, |
42 | 35 | sleep_fn=calls.append, |
43 | 36 | processing_time_seconds=0, |
44 | 37 | ) as mock: |
45 | 38 | mock.add_cloud_database(cloud_database=database) |
| 39 | + client = VWS( |
| 40 | + server_access_key=database.server_access_key, |
| 41 | + server_secret_key=database.server_secret_key, |
| 42 | + request_timeout_seconds=0.1, |
| 43 | + transport=HTTPXTransport(), |
| 44 | + ) |
46 | 45 | with pytest.raises(expected_exception=httpx.ReadTimeout): |
47 | | - asyncio.run(run_test()) |
| 46 | + client.get_database_summary_report() |
48 | 47 |
|
49 | 48 | assert calls == [0.1] |
50 | 49 |
|
51 | 50 | @staticmethod |
52 | 51 | def test_custom_base_vws_url_with_path_prefix() -> None: |
53 | | - """``AsyncVWS`` works with a custom VWS base URL path prefix.""" |
| 52 | + """``VWS`` works with a custom VWS base URL path prefix.""" |
54 | 53 | database = CloudDatabase() |
55 | 54 | base_vws_url = "https://vuforia.vws.example.com/prefix" |
56 | 55 |
|
57 | | - async def run_test() -> str: |
58 | | - """Return the database name via the custom base URL.""" |
59 | | - async with AsyncVWS( |
| 56 | + with MockVWS(base_vws_url=base_vws_url) as mock: |
| 57 | + mock.add_cloud_database(cloud_database=database) |
| 58 | + client = VWS( |
60 | 59 | server_access_key=database.server_access_key, |
61 | 60 | server_secret_key=database.server_secret_key, |
62 | 61 | base_vws_url=base_vws_url, |
63 | | - ) as client: |
64 | | - report = await client.get_database_summary_report() |
65 | | - return report.name |
66 | | - |
67 | | - with MockVWS(base_vws_url=base_vws_url) as mock: |
68 | | - mock.add_cloud_database(cloud_database=database) |
69 | | - database_name = asyncio.run(run_test()) |
| 62 | + transport=HTTPXTransport(), |
| 63 | + ) |
| 64 | + report = client.get_database_summary_report() |
| 65 | + database_name = report.name |
70 | 66 |
|
71 | 67 | assert database_name == database.database_name |
72 | 68 |
|
73 | 69 | @staticmethod |
74 | 70 | def test_add_get_and_delete_target( |
75 | 71 | image_file_success_state_low_rating: io.BytesIO, |
76 | 72 | ) -> None: |
77 | | - """A target life cycle works through ``AsyncVWS``.""" |
| 73 | + """A target life cycle works through ``VWS``.""" |
78 | 74 | database = CloudDatabase() |
79 | 75 | target_name = "async-target" |
80 | 76 |
|
81 | | - async def run_test() -> None: |
82 | | - """Exercise the target life cycle.""" |
83 | | - async with AsyncVWS( |
| 77 | + with MockVWS(processing_time_seconds=0) as mock: |
| 78 | + mock.add_cloud_database(cloud_database=database) |
| 79 | + client = VWS( |
84 | 80 | server_access_key=database.server_access_key, |
85 | 81 | server_secret_key=database.server_secret_key, |
86 | | - ) as client: |
87 | | - target_id = await client.add_target( |
88 | | - name=target_name, |
89 | | - width=1, |
90 | | - image=image_file_success_state_low_rating, |
91 | | - application_metadata=None, |
92 | | - active_flag=True, |
93 | | - ) |
94 | | - await client.wait_for_target_processed(target_id=target_id) |
95 | | - target_record = await client.get_target_record( |
96 | | - target_id=target_id, |
97 | | - ) |
98 | | - assert target_record.status == TargetStatuses.SUCCESS |
99 | | - assert target_record.target_record.name == target_name |
100 | | - |
101 | | - await client.delete_target(target_id=target_id) |
102 | | - |
103 | | - with pytest.raises(expected_exception=UnknownTargetError): |
104 | | - await client.get_target_record(target_id=target_id) |
| 82 | + transport=HTTPXTransport(), |
| 83 | + ) |
| 84 | + target_id = client.add_target( |
| 85 | + name=target_name, |
| 86 | + width=1, |
| 87 | + image=image_file_success_state_low_rating, |
| 88 | + application_metadata=None, |
| 89 | + active_flag=True, |
| 90 | + ) |
| 91 | + client.wait_for_target_processed(target_id=target_id) |
| 92 | + target_record = client.get_target_record(target_id=target_id) |
| 93 | + assert target_record.status == TargetStatuses.SUCCESS |
| 94 | + assert target_record.target_record.name == target_name |
105 | 95 |
|
106 | | - with MockVWS(processing_time_seconds=0) as mock: |
107 | | - mock.add_cloud_database(cloud_database=database) |
108 | | - asyncio.run(run_test()) |
| 96 | + client.delete_target(target_id=target_id) |
109 | 97 |
|
| 98 | + with pytest.raises(expected_exception=UnknownTargetError): |
| 99 | + client.get_target_record(target_id=target_id) |
110 | 100 |
|
111 | | -class TestAsyncCloudRecoService: |
112 | | - """Asynchronous cloud query usage through the mock.""" |
| 101 | + |
| 102 | +class TestCloudRecoService: |
| 103 | + """Synchronous cloud query usage through the mock via ``httpx``.""" |
113 | 104 |
|
114 | 105 | @staticmethod |
115 | 106 | def test_query_returns_match(high_quality_image: io.BytesIO) -> None: |
116 | | - """``AsyncCloudRecoService`` returns a match via the mock.""" |
| 107 | + """``CloudRecoService`` returns a match via the mock.""" |
117 | 108 | database = CloudDatabase() |
118 | 109 |
|
119 | | - async def run_test() -> None: |
120 | | - """Add a target and query it using the clients.""" |
121 | | - async with ( |
122 | | - AsyncVWS( |
123 | | - server_access_key=database.server_access_key, |
124 | | - server_secret_key=database.server_secret_key, |
125 | | - ) as vws_client, |
126 | | - AsyncCloudRecoService( |
127 | | - client_access_key=database.client_access_key, |
128 | | - client_secret_key=database.client_secret_key, |
129 | | - ) as query_client, |
130 | | - ): |
131 | | - target_id = await vws_client.add_target( |
132 | | - name="query-target", |
133 | | - width=1, |
134 | | - image=high_quality_image, |
135 | | - application_metadata=None, |
136 | | - active_flag=True, |
137 | | - ) |
138 | | - await vws_client.wait_for_target_processed(target_id=target_id) |
139 | | - results = await query_client.query(image=high_quality_image) |
140 | | - assert [result.target_id for result in results] == [target_id] |
141 | | - |
142 | 110 | with MockVWS( |
143 | 111 | processing_time_seconds=0, |
144 | 112 | query_match_checker=ExactMatcher(), |
145 | 113 | ) as mock: |
146 | 114 | mock.add_cloud_database(cloud_database=database) |
147 | | - asyncio.run(run_test()) |
148 | | - |
149 | | - |
150 | | -class TestAsyncVuMarkService: |
151 | | - """Asynchronous VuMark generation usage through the mock.""" |
| 115 | + vws_client = VWS( |
| 116 | + server_access_key=database.server_access_key, |
| 117 | + server_secret_key=database.server_secret_key, |
| 118 | + transport=HTTPXTransport(), |
| 119 | + ) |
| 120 | + query_client = CloudRecoService( |
| 121 | + client_access_key=database.client_access_key, |
| 122 | + client_secret_key=database.client_secret_key, |
| 123 | + transport=HTTPXTransport(), |
| 124 | + ) |
| 125 | + target_id = vws_client.add_target( |
| 126 | + name="query-target", |
| 127 | + width=1, |
| 128 | + image=high_quality_image, |
| 129 | + application_metadata=None, |
| 130 | + active_flag=True, |
| 131 | + ) |
| 132 | + vws_client.wait_for_target_processed(target_id=target_id) |
| 133 | + results = query_client.query(image=high_quality_image) |
| 134 | + assert [result.target_id for result in results] == [target_id] |
| 135 | + |
| 136 | + |
| 137 | +class TestVuMarkService: |
| 138 | + """Synchronous VuMark generation usage through the mock via |
| 139 | + ``httpx``. |
| 140 | + """ |
152 | 141 |
|
153 | 142 | @staticmethod |
154 | 143 | def test_generate_vumark_instance_returns_png_bytes() -> None: |
155 | | - """``AsyncVuMarkService`` returns VuMark image bytes.""" |
| 144 | + """``VuMarkService`` returns VuMark image bytes.""" |
156 | 145 | vumark_target = VuMarkTarget(name="test-target") |
157 | 146 | database = VuMarkDatabase(vumark_targets={vumark_target}) |
158 | 147 |
|
159 | | - async def run_test() -> bytes: |
160 | | - """Generate a VuMark instance image and return its bytes.""" |
161 | | - async with AsyncVuMarkService( |
162 | | - server_access_key=database.server_access_key, |
163 | | - server_secret_key=database.server_secret_key, |
164 | | - ) as client: |
165 | | - return await client.generate_vumark_instance( |
166 | | - target_id=vumark_target.target_id, |
167 | | - instance_id=uuid.uuid4().hex, |
168 | | - accept=VuMarkAccept.PNG, |
169 | | - ) |
170 | | - |
171 | 148 | with MockVWS() as mock: |
172 | 149 | mock.add_vumark_database(vumark_database=database) |
173 | | - response_content = asyncio.run(run_test()) |
| 150 | + client = VuMarkService( |
| 151 | + server_access_key=database.server_access_key, |
| 152 | + server_secret_key=database.server_secret_key, |
| 153 | + transport=HTTPXTransport(), |
| 154 | + ) |
| 155 | + response_content = client.generate_vumark_instance( |
| 156 | + target_id=vumark_target.target_id, |
| 157 | + instance_id=uuid.uuid4().hex, |
| 158 | + accept=VuMarkAccept.PNG, |
| 159 | + ) |
174 | 160 |
|
175 | 161 | assert response_content.startswith(b"\x89PNG") |
0 commit comments