|
4 | 4 | All external services and dependencies are mocked to isolate the tests. |
5 | 5 | """ |
6 | 6 |
|
7 | | -from nexent.vector_database.elasticsearch_core import ElasticSearchCore |
8 | | -from backend.apps.elasticsearch_app import router |
9 | | -from fastapi import HTTPException, FastAPI |
10 | 7 | 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 |
13 | 11 | import os |
14 | 12 | import sys |
15 | 13 | import pytest |
16 | 14 | from unittest.mock import patch, MagicMock, AsyncMock, ANY |
| 15 | +from typing import List, Optional, Union, Dict, Any |
| 16 | +from pydantic import BaseModel |
17 | 17 |
|
18 | | -# Dynamically determine the backend path |
| 18 | +# Dynamically determine the backend path and add it to sys.path |
19 | 19 | current_dir = os.path.dirname(os.path.abspath(__file__)) |
20 | 20 | backend_dir = os.path.abspath(os.path.join(current_dir, "../../../backend")) |
21 | | -sys.path.append(backend_dir) |
| 21 | +sys.path.insert(0, backend_dir) |
22 | 22 |
|
23 | 23 | # Define necessary Pydantic models before importing any backend code |
24 | 24 |
|
25 | | -# Define custom Pydantic models to ensure they exist before backend code imports |
26 | | - |
27 | 25 |
|
28 | 26 | class SearchRequest(BaseModel): |
29 | 27 | index_names: List[str] |
@@ -53,19 +51,17 @@ class IndexingResponse(BaseModel): |
53 | 51 | # Mock Elasticsearch to prevent connection errors |
54 | 52 | patch('elasticsearch.Elasticsearch', return_value=MagicMock()).start() |
55 | 53 |
|
56 | | -# Create a mock for consts.model |
| 54 | +# Create a mock for consts.model and patch it before any imports |
57 | 55 | consts_model_mock = MagicMock() |
58 | 56 | consts_model_mock.SearchRequest = SearchRequest |
59 | 57 | consts_model_mock.HybridSearchRequest = HybridSearchRequest |
60 | 58 | consts_model_mock.IndexingResponse = IndexingResponse |
61 | 59 |
|
62 | | -# Patch the module import |
| 60 | +# Patch the module import before importing backend modules |
63 | 61 | sys.modules['consts.model'] = consts_model_mock |
64 | 62 |
|
65 | 63 | # Now import the modules that depend on consts.model |
66 | 64 |
|
67 | | -# Import routes and services |
68 | | - |
69 | 65 | # Create mocks for these services if they can't be imported |
70 | 66 | ElasticSearchService = MagicMock() |
71 | 67 | RedisService = MagicMock() |
|
0 commit comments