|
12 | 12 |
|
13 | 13 | # --------------------------------------------------------------------------- |
14 | 14 | # build_query tests |
| 15 | +# |
| 16 | +# build_query is responsible for access control, annotate/order, document filter |
| 17 | +# and slicing and only constructs a lazy Django QuerySet without evaluating it |
| 18 | +# |
| 19 | +# We can test build_query by patching Embeddings.objects and inspecting which |
| 20 | +# methods and arguments were called on Embeddings.objects |
15 | 21 | # --------------------------------------------------------------------------- |
16 | 22 |
|
17 | | -# All assertions inspect which methods and arguments were called on Embeddings.objects |
18 | | - |
19 | 23 | # Only forwarded to L2Distance |
20 | 24 | EMBEDDING_VECTOR = [0.1, 0.2, 0.3] |
21 | 25 |
|
@@ -165,6 +169,13 @@ def test_build_query_returns_unevaluated_queryset(mock_objects): |
165 | 169 |
|
166 | 170 | # --------------------------------------------------------------------------- |
167 | 171 | # evaluate_query tests |
| 172 | +# |
| 173 | +# evaluate_query is responsible for iterating the queryset and mapping each |
| 174 | +# Embeddings object's attributes to a result dict, including the rename |
| 175 | +# page_num -> page_number and the None-safe file_id lookup |
| 176 | +# |
| 177 | +# We can test evaluate_query by passing plain MagicMock objects directly as |
| 178 | +# the iterable and asserting on the shape and values of the returned list |
168 | 179 | # --------------------------------------------------------------------------- |
169 | 180 |
|
170 | 181 | def test_evaluate_query_empty_queryset(): |
@@ -214,6 +225,13 @@ def test_evaluate_query_none_upload_file(): |
214 | 225 |
|
215 | 226 | # --------------------------------------------------------------------------- |
216 | 227 | # log_usage tests |
| 228 | +# |
| 229 | +# log_usage is responsible for computing distance stats, storing the correct |
| 230 | +# user (None for unauthenticated), handling empty results, and swallowing |
| 231 | +# exceptions so search is never interrupted |
| 232 | +# |
| 233 | +# We can test log_usage by patching SemanticSearchUsage.objects.create and |
| 234 | +# inspecting the keyword arguments it was called with |
217 | 235 | # --------------------------------------------------------------------------- |
218 | 236 |
|
219 | 237 | @patch("api.services.embedding_services.SemanticSearchUsage.objects.create") |
@@ -333,6 +351,12 @@ def test_log_usage_swallows_exceptions(mock_create): |
333 | 351 |
|
334 | 352 | # --------------------------------------------------------------------------- |
335 | 353 | # get_closest_embeddings tests |
| 354 | +# |
| 355 | +# get_closest_embeddings is responsible for wiring together encode, |
| 356 | +# build_query, evaluate_query, and log_usage and returning the results |
| 357 | +# |
| 358 | +# We can test get_closest_embeddings by patching all four collaborators and |
| 359 | +# asserting that each is called with the correct arguments in the correct order |
336 | 360 | # --------------------------------------------------------------------------- |
337 | 361 |
|
338 | 362 | @patch("api.services.embedding_services.log_usage") |
|
0 commit comments