fix: more test fixes

This commit is contained in:
Alex committed 2026-08-08 12:28:20 +01:00
1 parent 132a2de3bf
commit c5d2329a61
3 files changed
+39 -8

No files matched your search

+3 -4
View File
@@ -383,10 +383,9 @@ class AnthropicLLM(BaseLLM):
return
try:
output = int(getattr(usage, "output_tokens", 0) or 0)
if output_only:
# Input counts come from the message_start record below.
base_input = created = cached = 0
else:
# On a message_delta the input bins are absent; the counts kept at
# message_start are reused below instead.
if not output_only:
base_input = int(getattr(usage, "input_tokens", 0) or 0)
created = int(getattr(usage, "cache_creation_input_tokens", 0) or 0)
cached = int(getattr(usage, "cache_read_input_tokens", 0) or 0)
@@ -9,6 +9,8 @@ from unittest.mock import patch
import pytest
import application.api.answer.services.stream_processor as sp_mod
@contextmanager
def _patch_db(conn):
@@ -350,6 +352,23 @@ class TestGetAgentKey:
with _patch_db(pg_conn), pytest.raises(Exception):
sp._get_agent_key(str(agent["id"]), "not-owner")
def _stub_db_readonly(monkeypatch, sp_mod):
"""``_load_request_sources`` opens a connection to run the access check.
Patch the name bound in ``stream_processor`` — it is imported at module
load, so rebinding the source module has no effect. Without this these
pass only where a database happens to be reachable.
"""
import contextlib
from unittest.mock import MagicMock
@contextlib.contextmanager
def _conn():
yield MagicMock()
monkeypatch.setattr(sp_mod, "db_readonly", _conn)
class TestConfigureSource:
def test_agent_data_with_sources_list(self):
@@ -385,9 +404,9 @@ class TestConfigureSource:
assert sp.source == {}
def test_request_active_docs_used(self, monkeypatch):
import application.api.answer.services.stream_processor as sp_mod
StreamProcessor = sp_mod.StreamProcessor
_stub_db_readonly(monkeypatch, sp_mod)
monkeypatch.setattr(sp_mod, "can_access", lambda *a, **k: True)
sp = StreamProcessor({"active_docs": "abc"}, {"sub": "u"})
sp._configure_source()
@@ -399,7 +418,6 @@ class TestConfigureSource:
An unchecked id read another tenant's documents straight into the
answer, while /api/sources/<id>/search correctly refused the same id.
"""
import application.api.answer.services.stream_processor as sp_mod
StreamProcessor = sp_mod.StreamProcessor
monkeypatch.setattr(sp_mod, "can_access", lambda *a, **k: False)
@@ -409,9 +427,9 @@ class TestConfigureSource:
assert sp.all_sources == []
def test_mixed_access_keeps_only_the_readable_ids(self, monkeypatch):
import application.api.answer.services.stream_processor as sp_mod
StreamProcessor = sp_mod.StreamProcessor
_stub_db_readonly(monkeypatch, sp_mod)
monkeypatch.setattr(
sp_mod, "can_access", lambda conn, kind, sid, user: sid == "mine"
)
@@ -420,7 +438,6 @@ class TestConfigureSource:
assert sp.source == {"active_docs": ["mine"]}
def test_access_check_failure_fails_closed(self, monkeypatch):
import application.api.answer.services.stream_processor as sp_mod
StreamProcessor = sp_mod.StreamProcessor
def _boom(*a, **k):
@@ -25,6 +25,21 @@ TEAM_SHARED = "src-team-shared"
FOREIGN = "src-foreign"
@pytest.fixture(autouse=True)
def _stub_db(monkeypatch):
"""``_load_request_sources`` opens a connection; it needn't be a real one."""
import contextlib
from unittest.mock import MagicMock
@contextlib.contextmanager
def _conn():
yield MagicMock()
# Patch the name bound in stream_processor, not the source module: it is
# imported at module load, so rebinding the origin has no effect.
monkeypatch.setattr(sp_mod, "db_readonly", _conn)
@pytest.fixture
def access_model(monkeypatch):
"""``can_access`` semantics: own it, or hold a direct team grant."""