@@ -38,14 +38,13 @@ def test_build_query_authenticated_uses_or_filter(mock_objects):
3838@patch ("api.services.embedding_services.Embeddings.objects" )
3939def test_build_query_unauthenticated_uses_superuser_only_filter (mock_objects ):
4040 # An unauthenticated user may only see files uploaded by superusers.
41- # The OR branch for the user's own files must NOT be present.
41+ # The source uses a plain kwarg here (not a positional Q object), so the
42+ # value lives in call_args.kwargs, not call_args.args.
4243 user = MagicMock (is_authenticated = False )
4344
4445 build_query (user , EMBEDDING_VECTOR )
4546
46- expected_q = Q (upload_file__uploaded_by__is_superuser = True )
47- actual_q = mock_objects .filter .call_args .args [0 ]
48- assert actual_q == expected_q
47+ assert mock_objects .filter .call_args .kwargs == {"upload_file__uploaded_by__is_superuser" : True }
4948
5049# Test application of annotate and order_by
5150
@@ -86,16 +85,18 @@ def test_build_query_no_document_filter_when_both_none(mock_objects):
8685@patch ("api.services.embedding_services.Embeddings.objects" )
8786def test_build_query_guid_takes_precedence_over_document_name (mock_objects ):
8887 # When both guid and document_name are provided, the guid branch runs and
89- # the document_name branch is skipped entirely (only two filter calls total) .
88+ # the document_name branch is skipped entirely.
9089 user = MagicMock (is_authenticated = True )
9190
9291 build_query (user , EMBEDDING_VECTOR , guid = "abc-123" , document_name = "study.pdf" )
9392
94- # Two calls: auth filter + guid filter. No third call for document_name.
95- assert mock_objects .filter .call_count == 2
93+ # The auth filter fires on mock_objects.filter (call_count == 1).
94+ # The document filter fires on the chained ordered_qs.filter — a different
95+ # mock object — so mock_objects.filter.call_count stays at 1.
96+ assert mock_objects .filter .call_count == 1
9697
97- # The second filter must use upload_file__guid, not name.
98- # We follow the mock chain to the queryset that .annotate().order_by() returned .
98+ # The document filter must use upload_file__guid, not name, and must be
99+ # called exactly once (confirming document_name branch was skipped) .
99100 ordered_qs = mock_objects .filter .return_value .annotate .return_value .order_by .return_value
100101 ordered_qs .filter .assert_called_once_with (upload_file__guid = "abc-123" )
101102
0 commit comments