mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-15 04:17:56 +00:00
Pg 4 (#2390)
* feat: postgres tests * feat: mongo cutoff * feat: mongo cutoff * feat: adjust docs and compose files * fix: mini code mongo removals * fix: tests and k8s mongo stuff * feat: test fixes * fix: ruff * fix: vale * Potential fix for pull request finding 'CodeQL / Clear-text logging of sensitive information' Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * fix: mini suggestions * vale lint fix 2 * fix: codeql columns thing * fix: test mongo * fix: tests coverage * feat: better tests 4 * feat: more tests * feat: decent coverage * fix: ruff fixes * fix: remove mongo mock * feat: enhance workflow engine and API routes; add document retrieval and source handling * feat: e2e tests * fix: mcp, mongo and more * fix: mini codeql warning * fix: agent chunk view * fix: mini issues * fix: more pg fixes * feat: postgres prep on start * feat: qa tests * fix: mini improvements * fix: tests --------- Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Co-authored-by: Siddhant Rai <siddhant.rai.5686@gmail.com>
This commit is contained in:
312 files changed
+763725
-25159
No files matched your search
@@ -4,7 +4,8 @@ Covers ContinuationService, ToolExecutor.check_pause, handler pause
|
||||
signaling, BaseAgent.gen_continuation, and request validation.
|
||||
"""
|
||||
|
||||
from unittest.mock import Mock
|
||||
import uuid
|
||||
from unittest.mock import Mock, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -12,15 +13,104 @@ from application.agents.tool_executor import ToolExecutor
|
||||
from application.llm.handlers.base import LLMHandler, LLMResponse, ToolCall
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# In-memory MongoDB collection mock (no mongomock / bson dependency)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class _InMemoryCollection:
|
||||
"""Minimal dict-backed collection supporting find_one, replace_one, delete_one."""
|
||||
|
||||
def __init__(self):
|
||||
self._docs = []
|
||||
|
||||
def _matches(self, doc, query):
|
||||
return all(doc.get(k) == v for k, v in query.items())
|
||||
|
||||
def find_one(self, query):
|
||||
for doc in self._docs:
|
||||
if self._matches(doc, query):
|
||||
import copy
|
||||
return copy.deepcopy(doc)
|
||||
return None
|
||||
|
||||
def replace_one(self, query, replacement, upsert=False):
|
||||
result = MagicMock()
|
||||
for i, doc in enumerate(self._docs):
|
||||
if self._matches(doc, query):
|
||||
self._docs[i] = dict(replacement)
|
||||
if "_id" not in self._docs[i]:
|
||||
self._docs[i]["_id"] = str(uuid.uuid4())
|
||||
result.upserted_id = None
|
||||
return result
|
||||
if upsert:
|
||||
new_doc = dict(replacement)
|
||||
new_doc["_id"] = str(uuid.uuid4())
|
||||
self._docs.append(new_doc)
|
||||
result.upserted_id = new_doc["_id"]
|
||||
else:
|
||||
result.upserted_id = None
|
||||
return result
|
||||
|
||||
def delete_one(self, query):
|
||||
result = MagicMock()
|
||||
for i, doc in enumerate(self._docs):
|
||||
if self._matches(doc, query):
|
||||
self._docs.pop(i)
|
||||
result.deleted_count = 1
|
||||
return result
|
||||
result.deleted_count = 0
|
||||
return result
|
||||
|
||||
def create_index(self, *args, **kwargs):
|
||||
pass # no-op
|
||||
|
||||
|
||||
class _InMemoryDB:
|
||||
def __init__(self):
|
||||
self._collections = {}
|
||||
|
||||
def __getitem__(self, name):
|
||||
if name not in self._collections:
|
||||
self._collections[name] = _InMemoryCollection()
|
||||
return self._collections[name]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_mongo_continuation(monkeypatch):
|
||||
"""Provide an in-memory MongoDB for ContinuationService (no bson/mongomock)."""
|
||||
db = _InMemoryDB()
|
||||
mock_client = {_get_mongo_db_name(): db}
|
||||
|
||||
def _get_client():
|
||||
return mock_client
|
||||
|
||||
monkeypatch.setattr(
|
||||
"application.api.answer.services.continuation_service.MongoDB.get_client",
|
||||
_get_client,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"application.storage.db.dual_write.dual_write",
|
||||
lambda repo_cls, fn: None,
|
||||
)
|
||||
return db
|
||||
|
||||
|
||||
def _get_mongo_db_name():
|
||||
from application.core.settings import settings
|
||||
return settings.MONGO_DB_NAME
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# ContinuationService
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.skip(reason="needs PG fixture rewrite — tracked as part of post-cutover test cleanup")
|
||||
class TestContinuationService:
|
||||
|
||||
def test_save_and_load(self, mock_mongo_db):
|
||||
def test_save_and_load(self, mock_mongo_continuation):
|
||||
from application.api.answer.services.continuation_service import (
|
||||
ContinuationService,
|
||||
)
|
||||
@@ -44,7 +134,7 @@ class TestContinuationService:
|
||||
assert len(state["pending_tool_calls"]) == 1
|
||||
assert state["agent_config"]["model_id"] == "gpt-4"
|
||||
|
||||
def test_load_returns_none_when_missing(self, mock_mongo_db):
|
||||
def test_load_returns_none_when_missing(self, mock_mongo_continuation):
|
||||
from application.api.answer.services.continuation_service import (
|
||||
ContinuationService,
|
||||
)
|
||||
@@ -52,7 +142,7 @@ class TestContinuationService:
|
||||
svc = ContinuationService()
|
||||
assert svc.load_state("nonexistent", "alice") is None
|
||||
|
||||
def test_delete_state(self, mock_mongo_db):
|
||||
def test_delete_state(self, mock_mongo_continuation):
|
||||
from application.api.answer.services.continuation_service import (
|
||||
ContinuationService,
|
||||
)
|
||||
@@ -70,7 +160,7 @@ class TestContinuationService:
|
||||
assert svc.delete_state("conv-2", "bob") is True
|
||||
assert svc.load_state("conv-2", "bob") is None
|
||||
|
||||
def test_delete_nonexistent(self, mock_mongo_db):
|
||||
def test_delete_nonexistent(self, mock_mongo_continuation):
|
||||
from application.api.answer.services.continuation_service import (
|
||||
ContinuationService,
|
||||
)
|
||||
@@ -78,7 +168,7 @@ class TestContinuationService:
|
||||
svc = ContinuationService()
|
||||
assert svc.delete_state("nope", "nope") is False
|
||||
|
||||
def test_upsert_replaces_existing(self, mock_mongo_db):
|
||||
def test_upsert_replaces_existing(self, mock_mongo_continuation):
|
||||
from application.api.answer.services.continuation_service import (
|
||||
ContinuationService,
|
||||
)
|
||||
@@ -637,7 +727,7 @@ class TestValidateRequest:
|
||||
with app.app_context():
|
||||
yield
|
||||
|
||||
def test_continuation_request_without_question(self, mock_mongo_db):
|
||||
def test_continuation_request_without_question(self):
|
||||
from application.api.answer.routes.base import BaseAnswerResource
|
||||
|
||||
base = BaseAnswerResource()
|
||||
@@ -648,7 +738,7 @@ class TestValidateRequest:
|
||||
result = base.validate_request(data)
|
||||
assert result is None # Valid
|
||||
|
||||
def test_continuation_request_missing_conversation_id(self, mock_mongo_db):
|
||||
def test_continuation_request_missing_conversation_id(self):
|
||||
from application.api.answer.routes.base import BaseAnswerResource
|
||||
|
||||
base = BaseAnswerResource()
|
||||
@@ -658,7 +748,7 @@ class TestValidateRequest:
|
||||
result = base.validate_request(data)
|
||||
assert result is not None # Error — missing conversation_id
|
||||
|
||||
def test_normal_request_still_requires_question(self, mock_mongo_db):
|
||||
def test_normal_request_still_requires_question(self):
|
||||
from application.api.answer.routes.base import BaseAnswerResource
|
||||
|
||||
base = BaseAnswerResource()
|
||||
|
||||
Reference in new issue
Block a user