Skip to content

Commit de5c73b

Browse files
author
jiangpeiling
committed
♻️ Reconstruct the app layer and service layer to comply with the MVC three-layer architecture.
1 parent 2bd7dcc commit de5c73b

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

test/backend/app/test_elasticsearch_app.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,24 @@
44
All external services and dependencies are mocked to isolate the tests.
55
"""
66

7-
from nexent.vector_database.elasticsearch_core import ElasticSearchCore
8-
from backend.apps.elasticsearch_app import router
9-
from fastapi import HTTPException, FastAPI
107
from fastapi.testclient import TestClient
11-
from typing import List, Optional, Union, Dict, Any
12-
from pydantic import BaseModel
8+
from fastapi import HTTPException, FastAPI
9+
from backend.apps.elasticsearch_app import router
10+
from nexent.vector_database.elasticsearch_core import ElasticSearchCore
1311
import os
1412
import sys
1513
import pytest
1614
from unittest.mock import patch, MagicMock, AsyncMock, ANY
15+
from typing import List, Optional, Union, Dict, Any
16+
from pydantic import BaseModel
1717

18-
# Dynamically determine the backend path
18+
# Dynamically determine the backend path and add it to sys.path
1919
current_dir = os.path.dirname(os.path.abspath(__file__))
2020
backend_dir = os.path.abspath(os.path.join(current_dir, "../../../backend"))
21-
sys.path.append(backend_dir)
21+
sys.path.insert(0, backend_dir)
2222

2323
# Define necessary Pydantic models before importing any backend code
2424

25-
# Define custom Pydantic models to ensure they exist before backend code imports
26-
2725

2826
class SearchRequest(BaseModel):
2927
index_names: List[str]
@@ -53,19 +51,17 @@ class IndexingResponse(BaseModel):
5351
# Mock Elasticsearch to prevent connection errors
5452
patch('elasticsearch.Elasticsearch', return_value=MagicMock()).start()
5553

56-
# Create a mock for consts.model
54+
# Create a mock for consts.model and patch it before any imports
5755
consts_model_mock = MagicMock()
5856
consts_model_mock.SearchRequest = SearchRequest
5957
consts_model_mock.HybridSearchRequest = HybridSearchRequest
6058
consts_model_mock.IndexingResponse = IndexingResponse
6159

62-
# Patch the module import
60+
# Patch the module import before importing backend modules
6361
sys.modules['consts.model'] = consts_model_mock
6462

6563
# Now import the modules that depend on consts.model
6664

67-
# Import routes and services
68-
6965
# Create mocks for these services if they can't be imported
7066
ElasticSearchService = MagicMock()
7167
RedisService = MagicMock()

0 commit comments

Comments
 (0)