Skip to content

Commit

Permalink
fix tests and remove unnessacry cls
Browse files Browse the repository at this point in the history
  • Loading branch information
john0isaac committed Jun 21, 2024
1 parent 4bbac78 commit 09b310e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
12 changes: 2 additions & 10 deletions src/quartapp/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
from collections.abc import AsyncGenerator
from dataclasses import asdict, is_dataclass
from json import JSONEncoder, dumps
from json import dumps
from pathlib import Path
from typing import Any

Expand All @@ -17,20 +16,13 @@
)


class CustomJSONEncoder(JSONEncoder):
def default(self, o: Any) -> Any:
if is_dataclass(o):
return asdict(o)
return super().default(o)


async def format_as_ndjson(r: AsyncGenerator[RetrievalResponse | Message, None]) -> AsyncGenerator[str, None]:
"""
Format the response as NDJSON
"""
try:
async for event in r:
yield dumps(event.to_dict(), ensure_ascii=False, cls=CustomJSONEncoder) + "\n"
yield dumps(event.to_dict(), ensure_ascii=False) + "\n"
except Exception as error:
logging.exception("Exception while generating response stream: %s", error)
yield dumps({"error": str(error)}, ensure_ascii=False) + "\n"
Expand Down
32 changes: 20 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import dataclass
from unittest.mock import AsyncMock, MagicMock, patch

import langchain_core
import mongomock
import pytest
import pytest_asyncio
Expand Down Expand Up @@ -47,25 +48,14 @@ def approaches_base_mock():

# Mock Vector Store
mock_vector_store = MagicMock()
mock_retriever = MagicMock()
mock_document = Document(
page_content='{"name": "test", "description": "test", "price": "5.0USD", "category": "test"}'
)
mock_retriever.ainvoke = AsyncMock(return_value=[mock_document]) # Assume there is always a response
mock_vector_store.as_retriever.return_value = mock_retriever
mock_vector_store.as_retriever.return_value.ainvoke = AsyncMock(return_value=[mock_document])

# Mock Chat
mock_chat = MagicMock()

@dataclass
class MockContent:
content: str

mock_content = MockContent(content="content")

mock_chat.__or__ = MagicMock()
mock_chat.__or__.return_value.ainvoke = AsyncMock(return_value=mock_content)

# Mock Data Collection
mock_mongo_document = {"textContent": mock_document.page_content, "source": "test"}
mock_data_collection = MagicMock()
Expand Down Expand Up @@ -167,6 +157,24 @@ def setup_data_collection_mock(monkeypatch):
return _mock


@pytest.fixture(autouse=True)
def mock_runnable_or(monkeypatch):
"""Mock langchain_core.runnables.base.Runnable.__or__."""

@dataclass
class MockContent:
content: str

or_return = MagicMock()
or_return.ainvoke = AsyncMock(return_value=MockContent(content="content"))
or_mock = MagicMock()
or_mock.return_value = or_return
monkeypatch.setattr(
langchain_core.runnables.base.Runnable, langchain_core.runnables.base.Runnable.__or__.__name__, or_mock
)
return or_mock


@pytest_asyncio.fixture
async def app_mock(app_config_mock):
"""Create a test app with the test config mock."""
Expand Down

0 comments on commit 09b310e

Please sign in to comment.