Skip to content

Commit 9d654be

Browse files
committed
♻️ Add Unitest and fix node.js server env read
1 parent b3e78f3 commit 9d654be

File tree

1 file changed

+70
-25
lines changed

1 file changed

+70
-25
lines changed

test/backend/test_llm_integration.py

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,52 @@
55
import pytest
66
import sys
77
import os
8+
import types
89
from unittest.mock import patch, MagicMock
910

1011
# Add backend to path
1112
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', 'backend'))
1213

14+
# Mock database.client and MinioClient before any imports to avoid MinIO initialization
15+
class _MinioClient:
16+
pass
17+
18+
if "database.client" not in sys.modules:
19+
database_client_mod = types.ModuleType("database.client")
20+
database_client_mod.MinioClient = _MinioClient
21+
sys.modules["database.client"] = database_client_mod
22+
23+
# Mock backend.database.client as well
24+
if "backend.database.client" not in sys.modules:
25+
backend_db_client_mod = types.ModuleType("backend.database.client")
26+
backend_db_client_mod.MinioClient = _MinioClient
27+
sys.modules["backend.database.client"] = backend_db_client_mod
28+
29+
# Ensure database module exists as a package (needs __path__ attribute)
30+
if "database" not in sys.modules:
31+
database_mod = types.ModuleType("database")
32+
database_mod.__path__ = [] # Make it a package
33+
sys.modules["database"] = database_mod
34+
35+
# Mock database.model_management_db module to avoid MinIO initialization
36+
if "database.model_management_db" not in sys.modules:
37+
model_mgmt_db_mod = types.ModuleType("database.model_management_db")
38+
model_mgmt_db_mod.get_model_by_model_id = MagicMock(return_value=None)
39+
sys.modules["database.model_management_db"] = model_mgmt_db_mod
40+
setattr(sys.modules["database"], "model_management_db", model_mgmt_db_mod)
41+
42+
# Mock database.tenant_config_db to avoid import errors
43+
if "database.tenant_config_db" not in sys.modules:
44+
tenant_config_db_mod = types.ModuleType("database.tenant_config_db")
45+
# Mock all functions that config_utils imports
46+
tenant_config_db_mod.delete_config_by_tenant_config_id = MagicMock()
47+
tenant_config_db_mod.get_all_configs_by_tenant_id = MagicMock()
48+
tenant_config_db_mod.get_single_config_info = MagicMock()
49+
tenant_config_db_mod.insert_config = MagicMock()
50+
tenant_config_db_mod.update_config_by_tenant_config_id_and_data = MagicMock()
51+
sys.modules["database.tenant_config_db"] = tenant_config_db_mod
52+
setattr(sys.modules["database"], "tenant_config_db", tenant_config_db_mod)
53+
1354
from utils.document_vector_utils import summarize_document, summarize_cluster
1455

1556

@@ -33,19 +74,21 @@ def test_summarize_document_with_llm_params_no_config(self):
3374
content = "This is a test document with some content about machine learning and AI."
3475
filename = "test_doc.txt"
3576

36-
# Mock get_model_by_model_id to return None (no config found) to avoid MinIO initialization
37-
# Patch at the source module since it's imported inside the function
38-
with patch('database.model_management_db.get_model_by_model_id', return_value=None):
39-
# Test with model_id and tenant_id but no actual LLM call (will fallback due to missing config)
40-
result = summarize_document(
41-
content, filename, language="zh", max_words=50,
42-
model_id=1, tenant_id="test_tenant"
43-
)
44-
45-
# Should return placeholder summary when model config not found (fallback behavior)
46-
assert "[Document Summary: test_doc.txt]" in result
47-
assert "max 50 words" in result
48-
assert "Content:" in result
77+
# Mock get_model_by_model_id to return None (no config found)
78+
# Use the already mocked module and just ensure it returns None
79+
import database.model_management_db as model_mgmt_db
80+
model_mgmt_db.get_model_by_model_id = MagicMock(return_value=None)
81+
82+
# Test with model_id and tenant_id but no actual LLM call (will fallback due to missing config)
83+
result = summarize_document(
84+
content, filename, language="zh", max_words=50,
85+
model_id=1, tenant_id="test_tenant"
86+
)
87+
88+
# Should return placeholder summary when model config not found (fallback behavior)
89+
assert "[Document Summary: test_doc.txt]" in result
90+
assert "max 50 words" in result
91+
assert "Content:" in result
4992

5093
def test_summarize_cluster_without_llm(self):
5194
"""Test cluster summarization without LLM (fallback mode)"""
@@ -69,18 +112,20 @@ def test_summarize_cluster_with_llm_params_no_config(self):
69112
"Document 2 discusses neural networks and deep learning."
70113
]
71114

72-
# Mock get_model_by_model_id to return None (no config found) to avoid MinIO initialization
73-
# Patch at the source module since it's imported inside the function
74-
with patch('database.model_management_db.get_model_by_model_id', return_value=None):
75-
result = summarize_cluster(
76-
document_summaries, language="zh", max_words=100,
77-
model_id=1, tenant_id="test_tenant"
78-
)
79-
80-
# Should return placeholder summary when model config not found (fallback behavior)
81-
assert "[Cluster Summary]" in result
82-
assert "max 100 words" in result
83-
assert "Based on 2 documents" in result
115+
# Mock get_model_by_model_id to return None (no config found)
116+
# Use the already mocked module and just ensure it returns None
117+
import database.model_management_db as model_mgmt_db
118+
model_mgmt_db.get_model_by_model_id = MagicMock(return_value=None)
119+
120+
result = summarize_cluster(
121+
document_summaries, language="zh", max_words=100,
122+
model_id=1, tenant_id="test_tenant"
123+
)
124+
125+
# Should return placeholder summary when model config not found (fallback behavior)
126+
assert "[Cluster Summary]" in result
127+
assert "max 100 words" in result
128+
assert "Based on 2 documents" in result
84129

85130
def test_summarize_document_english(self):
86131
"""Test document summarization in English"""

0 commit comments

Comments
 (0)