mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-26 01:06:23 +02:00
fix: correct test fixtures and add missing summarizer tests
This commit is contained in:
parent
c5ae62140d
commit
1b4ed35de3
4 changed files with 82 additions and 25 deletions
|
|
@ -1,15 +1,33 @@
|
|||
import pytest
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patched_chunker_instance(mocker):
|
||||
mock = mocker.patch("app.indexing_pipeline.document_chunker.config.chunker_instance")
|
||||
mock.chunk.return_value = [mocker.Mock(text="prose chunk")]
|
||||
def patched_summarizer_chain(monkeypatch):
|
||||
chain = MagicMock()
|
||||
chain.ainvoke = AsyncMock(return_value=MagicMock(content="The summary."))
|
||||
|
||||
template = MagicMock()
|
||||
template.__or__ = MagicMock(return_value=chain)
|
||||
|
||||
monkeypatch.setattr(
|
||||
"app.indexing_pipeline.document_summarizer.SUMMARY_PROMPT_TEMPLATE",
|
||||
template,
|
||||
)
|
||||
return chain
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patched_chunker_instance(monkeypatch):
|
||||
mock = MagicMock()
|
||||
mock.chunk.return_value = [MagicMock(text="prose chunk")]
|
||||
monkeypatch.setattr("app.indexing_pipeline.document_chunker.config.chunker_instance", mock)
|
||||
return mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patched_code_chunker_instance(mocker):
|
||||
mock = mocker.patch("app.indexing_pipeline.document_chunker.config.code_chunker_instance")
|
||||
mock.chunk.return_value = [mocker.Mock(text="code chunk")]
|
||||
def patched_code_chunker_instance(monkeypatch):
|
||||
mock = MagicMock()
|
||||
mock.chunk.return_value = [MagicMock(text="code chunk")]
|
||||
monkeypatch.setattr("app.indexing_pipeline.document_chunker.config.code_chunker_instance", mock)
|
||||
return mock
|
||||
|
|
|
|||
|
|
@ -5,15 +5,15 @@ from app.indexing_pipeline.document_chunker import chunk_text
|
|||
pytestmark = pytest.mark.unit
|
||||
|
||||
|
||||
def test_uses_code_chunker_when_flag_is_true(patched_code_chunker_instance):
|
||||
@pytest.mark.usefixtures("patched_chunker_instance", "patched_code_chunker_instance")
|
||||
def test_uses_code_chunker_when_flag_is_true():
|
||||
result = chunk_text("def foo(): pass", use_code_chunker=True)
|
||||
|
||||
patched_code_chunker_instance.chunk.assert_called_once_with("def foo(): pass")
|
||||
assert result == ["code chunk"]
|
||||
|
||||
|
||||
def test_uses_default_chunker_when_flag_is_false(patched_chunker_instance):
|
||||
@pytest.mark.usefixtures("patched_chunker_instance", "patched_code_chunker_instance")
|
||||
def test_uses_default_chunker_when_flag_is_false():
|
||||
result = chunk_text("Some prose text.", use_code_chunker=False)
|
||||
|
||||
patched_chunker_instance.chunk.assert_called_once_with("Some prose text.")
|
||||
assert result == ["prose chunk"]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
import pytest
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from app.indexing_pipeline.document_summarizer import summarize_document
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("patched_summarizer_chain")
|
||||
async def test_without_metadata_returns_raw_summary():
|
||||
result = await summarize_document("# Content", llm=MagicMock(model="gpt-4"))
|
||||
|
||||
assert result == "The summary."
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("patched_summarizer_chain")
|
||||
async def test_with_metadata_includes_metadata_values_in_output():
|
||||
result = await summarize_document(
|
||||
"# Content",
|
||||
llm=MagicMock(model="gpt-4"),
|
||||
metadata={"author": "Alice", "source": "Notion"},
|
||||
)
|
||||
|
||||
assert "Alice" in result
|
||||
assert "Notion" in result
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("patched_summarizer_chain")
|
||||
async def test_with_metadata_omits_empty_fields_from_output():
|
||||
result = await summarize_document(
|
||||
"# Content",
|
||||
llm=MagicMock(model="gpt-4"),
|
||||
metadata={"author": "Alice", "description": ""},
|
||||
)
|
||||
|
||||
assert "Alice" in result
|
||||
assert "description" not in result.lower()
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue