Skip to content

Commit 1537c1d

Browse files
committed
✨Added Datamate vector knowledge base core v2
2 parents ac695b0 + 5b669f1 commit 1537c1d

34 files changed

+1901
-1628
lines changed

test/backend/agents/test_create_agent_info.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from pathlib import Path
66
from unittest.mock import AsyncMock, MagicMock, patch, Mock, PropertyMock
77

8-
from test.common.env_test_utils import bootstrap_env
8+
from test.common.test_mocks import bootstrap_test_env
99

10-
env_state = bootstrap_env()
10+
env_state = bootstrap_test_env()
1111
consts_const = env_state["mock_const"]
1212
TEST_ROOT = Path(__file__).resolve().parents[2]
1313
PROJECT_ROOT = TEST_ROOT.parent

test/backend/app/test_image_app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
if str(TEST_ROOT) not in sys.path:
99
sys.path.append(str(TEST_ROOT))
1010

11-
from test.common.env_test_utils import bootstrap_env
11+
from test.common.test_mocks import bootstrap_test_env
1212

13-
helpers_env = bootstrap_env()
13+
helpers_env = bootstrap_test_env()
1414

1515

1616
helpers_env["mock_const"].DATA_PROCESS_SERVICE = "http://mock-data-process-service"

test/backend/app/test_knowledge_summary_app.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@
1212
if path not in sys.path:
1313
sys.path.insert(0, path)
1414

15-
# Patch environment variables before any imports that might use them
16-
os.environ.setdefault('MINIO_ENDPOINT', 'http://localhost:9000')
17-
os.environ.setdefault('MINIO_ACCESS_KEY', 'minioadmin')
18-
os.environ.setdefault('MINIO_SECRET_KEY', 'minioadmin')
19-
os.environ.setdefault('MINIO_REGION', 'us-east-1')
20-
os.environ.setdefault('MINIO_DEFAULT_BUCKET', 'test-bucket')
15+
# Environment variables are now configured in conftest.py
2116

2217
# Mock external dependencies
2318
sys.modules['boto3'] = MagicMock()

test/backend/app/test_mock_user_management_app.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
current_dir = os.path.dirname(os.path.abspath(__file__))
88
sys.path.insert(0, os.path.join(current_dir, "../../../backend"))
99

10-
# Patch environment variables before any imports that might use them
11-
os.environ.setdefault('MINIO_ENDPOINT', 'http://localhost:9000')
12-
os.environ.setdefault('MINIO_ACCESS_KEY', 'minioadmin')
13-
os.environ.setdefault('MINIO_SECRET_KEY', 'minioadmin')
14-
os.environ.setdefault('MINIO_REGION', 'us-east-1')
15-
os.environ.setdefault('MINIO_DEFAULT_BUCKET', 'test-bucket')
10+
# Environment variables are now configured in conftest.py
1611

1712
boto3_mock = MagicMock()
1813
minio_client_mock = MagicMock()

test/backend/app/test_model_managment_app.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
sys.path.insert(0, BACKEND_ROOT)
1818

1919
# Patch environment variables before any imports that might use them
20-
os.environ.setdefault('MINIO_ENDPOINT', 'http://localhost:9000')
21-
os.environ.setdefault('MINIO_ACCESS_KEY', 'minioadmin')
22-
os.environ.setdefault('MINIO_SECRET_KEY', 'minioadmin')
23-
os.environ.setdefault('MINIO_REGION', 'us-east-1')
24-
os.environ.setdefault('MINIO_DEFAULT_BUCKET', 'test-bucket')
20+
# Environment variables are now configured in conftest.py
2521

2622
# Patch storage factory and MinIO config validation to avoid errors during initialization
2723
# These patches must be started before any imports that use MinioClient

test/backend/app/test_vectordatabase_app.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@
1818
backend_dir = os.path.abspath(os.path.join(current_dir, "../../../backend"))
1919
sys.path.insert(0, backend_dir)
2020

21-
# Patch environment variables before any imports that might use them
22-
os.environ.setdefault('MINIO_ENDPOINT', 'http://localhost:9000')
23-
os.environ.setdefault('MINIO_ACCESS_KEY', 'minioadmin')
24-
os.environ.setdefault('MINIO_SECRET_KEY', 'minioadmin')
25-
os.environ.setdefault('MINIO_REGION', 'us-east-1')
26-
os.environ.setdefault('MINIO_DEFAULT_BUCKET', 'test-bucket')
21+
# Environment variables are now configured in conftest.py
2722

2823
boto3_mock = MagicMock()
2924
minio_client_mock = MagicMock()

test/backend/database/test_attachment_db.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,10 @@
1414
sys.path.insert(0, os.path.abspath(os.path.join(
1515
os.path.dirname(__file__), '..', '..', '..')))
1616

17-
# Mock environment variables before imports
18-
os.environ.setdefault('MINIO_ENDPOINT', 'http://localhost:9000')
19-
os.environ.setdefault('MINIO_ACCESS_KEY', 'minioadmin')
20-
os.environ.setdefault('MINIO_SECRET_KEY', 'minioadmin')
21-
os.environ.setdefault('MINIO_REGION', 'us-east-1')
22-
os.environ.setdefault('MINIO_DEFAULT_BUCKET', 'test-bucket')
23-
2417
# Mock consts module
2518
consts_mock = MagicMock()
2619
consts_mock.const = MagicMock()
27-
consts_mock.const.MINIO_ENDPOINT = os.environ.get('MINIO_ENDPOINT', 'http://localhost:9000')
28-
consts_mock.const.MINIO_ACCESS_KEY = os.environ.get('MINIO_ACCESS_KEY', 'minioadmin')
29-
consts_mock.const.MINIO_SECRET_KEY = os.environ.get('MINIO_SECRET_KEY', 'minioadmin')
30-
consts_mock.const.MINIO_REGION = os.environ.get('MINIO_REGION', 'us-east-1')
31-
consts_mock.const.MINIO_DEFAULT_BUCKET = os.environ.get('MINIO_DEFAULT_BUCKET', 'test-bucket')
20+
# Environment variables are now configured in conftest.py
3221

3322
sys.modules['consts'] = consts_mock
3423
sys.modules['consts.const'] = consts_mock.const

test/backend/database/test_client.py

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,10 @@
1313
sys.path.insert(0, os.path.abspath(os.path.join(
1414
os.path.dirname(__file__), '..', '..', '..')))
1515

16-
# Mock environment variables before imports
17-
os.environ.setdefault('MINIO_ENDPOINT', 'http://localhost:9000')
18-
os.environ.setdefault('MINIO_ACCESS_KEY', 'minioadmin')
19-
os.environ.setdefault('MINIO_SECRET_KEY', 'minioadmin')
20-
os.environ.setdefault('MINIO_REGION', 'us-east-1')
21-
os.environ.setdefault('MINIO_DEFAULT_BUCKET', 'test-bucket')
22-
os.environ.setdefault('POSTGRES_HOST', 'localhost')
23-
os.environ.setdefault('POSTGRES_USER', 'test_user')
24-
os.environ.setdefault('NEXENT_POSTGRES_PASSWORD', 'test_password')
25-
os.environ.setdefault('POSTGRES_DB', 'test_db')
26-
os.environ.setdefault('POSTGRES_PORT', '5432')
27-
2816
# Mock consts module
2917
consts_mock = MagicMock()
3018
consts_mock.const = MagicMock()
31-
consts_mock.const.MINIO_ENDPOINT = os.environ.get('MINIO_ENDPOINT', 'http://localhost:9000')
32-
consts_mock.const.MINIO_ACCESS_KEY = os.environ.get('MINIO_ACCESS_KEY', 'minioadmin')
33-
consts_mock.const.MINIO_SECRET_KEY = os.environ.get('MINIO_SECRET_KEY', 'minioadmin')
34-
consts_mock.const.MINIO_REGION = os.environ.get('MINIO_REGION', 'us-east-1')
35-
consts_mock.const.MINIO_DEFAULT_BUCKET = os.environ.get('MINIO_DEFAULT_BUCKET', 'test-bucket')
36-
consts_mock.const.POSTGRES_HOST = os.environ.get('POSTGRES_HOST', 'localhost')
37-
consts_mock.const.POSTGRES_USER = os.environ.get('POSTGRES_USER', 'test_user')
38-
consts_mock.const.NEXENT_POSTGRES_PASSWORD = os.environ.get('NEXENT_POSTGRES_PASSWORD', 'test_password')
39-
consts_mock.const.POSTGRES_DB = os.environ.get('POSTGRES_DB', 'test_db')
40-
consts_mock.const.POSTGRES_PORT = int(os.environ.get('POSTGRES_PORT', '5432'))
19+
# Environment variables are now configured in conftest.py
4120

4221
sys.modules['consts'] = consts_mock
4322
sys.modules['consts.const'] = consts_mock.const
@@ -51,7 +30,8 @@
5130
nexent_storage_mock = MagicMock()
5231
nexent_storage_factory_mock = MagicMock()
5332
storage_client_mock = MagicMock()
54-
nexent_storage_factory_mock.create_storage_client_from_config = MagicMock(return_value=storage_client_mock)
33+
nexent_storage_factory_mock.create_storage_client_from_config = MagicMock(
34+
return_value=storage_client_mock)
5535
nexent_storage_factory_mock.MinIOStorageConfig = MagicMock()
5636
nexent_storage_mock.storage_client_factory = nexent_storage_factory_mock
5737
nexent_mock.storage = nexent_storage_mock
@@ -79,7 +59,7 @@
7959

8060
# Patch storage factory before importing
8161
with patch('nexent.storage.storage_client_factory.create_storage_client_from_config', return_value=storage_client_mock), \
82-
patch('nexent.storage.storage_client_factory.MinIOStorageConfig'):
62+
patch('nexent.storage.storage_client_factory.MinIOStorageConfig'):
8363
from backend.database.client import (
8464
PostgresClient,
8565
MinioClient,
@@ -94,17 +74,26 @@
9474
class TestPostgresClient:
9575
"""Test cases for PostgresClient class"""
9676

97-
@patch('backend.database.client.create_engine')
98-
@patch('backend.database.client.sessionmaker')
99-
def test_postgres_client_init(self, mock_sessionmaker, mock_create_engine):
77+
def test_postgres_client_init(self, mocker):
10078
"""Test PostgresClient initialization"""
10179
# Reset singleton instance
10280
PostgresClient._instance = None
10381

82+
# Patch the constants
83+
mocker.patch('backend.database.client.POSTGRES_HOST', 'localhost')
84+
mocker.patch('backend.database.client.POSTGRES_USER', 'test_user')
85+
mocker.patch(
86+
'backend.database.client.NEXENT_POSTGRES_PASSWORD', 'test_password')
87+
mocker.patch('backend.database.client.POSTGRES_DB', 'test_db')
88+
mocker.patch('backend.database.client.POSTGRES_PORT', 5432)
89+
90+
# Mock the SQLAlchemy functions
10491
mock_engine = MagicMock()
105-
mock_create_engine.return_value = mock_engine
92+
mock_create_engine = mocker.patch(
93+
'backend.database.client.create_engine', return_value=mock_engine)
10694
mock_session = MagicMock()
107-
mock_sessionmaker.return_value = mock_session
95+
mock_sessionmaker = mocker.patch(
96+
'backend.database.client.sessionmaker', return_value=mock_session)
10897

10998
client = PostgresClient()
11099

@@ -186,7 +175,7 @@ def test_minio_client_singleton(self):
186175
MinioClient._instance = None
187176

188177
with patch('backend.database.client.create_storage_client_from_config'), \
189-
patch('backend.database.client.MinIOStorageConfig'):
178+
patch('backend.database.client.MinIOStorageConfig'):
190179
client1 = MinioClient()
191180
client2 = MinioClient()
192181

@@ -199,16 +188,19 @@ def test_minio_client_upload_file(self, mock_config_class, mock_create_client):
199188
MinioClient._instance = None
200189

201190
mock_storage_client = MagicMock()
202-
mock_storage_client.upload_file.return_value = (True, '/bucket/file.txt')
191+
mock_storage_client.upload_file.return_value = (
192+
True, '/bucket/file.txt')
203193
mock_create_client.return_value = mock_storage_client
204194
mock_config_class.return_value = MagicMock()
205195

206196
client = MinioClient()
207-
success, result = client.upload_file('/path/to/file.txt', 'file.txt', 'bucket')
197+
success, result = client.upload_file(
198+
'/path/to/file.txt', 'file.txt', 'bucket')
208199

209200
assert success is True
210201
assert result == '/bucket/file.txt'
211-
mock_storage_client.upload_file.assert_called_once_with('/path/to/file.txt', 'file.txt', 'bucket')
202+
mock_storage_client.upload_file.assert_called_once_with(
203+
'/path/to/file.txt', 'file.txt', 'bucket')
212204

213205
@patch('backend.database.client.create_storage_client_from_config')
214206
@patch('backend.database.client.MinIOStorageConfig')
@@ -218,7 +210,8 @@ def test_minio_client_upload_fileobj(self, mock_config_class, mock_create_client
218210

219211
from io import BytesIO
220212
mock_storage_client = MagicMock()
221-
mock_storage_client.upload_fileobj.return_value = (True, '/bucket/file.txt')
213+
mock_storage_client.upload_fileobj.return_value = (
214+
True, '/bucket/file.txt')
222215
mock_create_client.return_value = mock_storage_client
223216
mock_config_class.return_value = MagicMock()
224217

@@ -228,7 +221,8 @@ def test_minio_client_upload_fileobj(self, mock_config_class, mock_create_client
228221

229222
assert success is True
230223
assert result == '/bucket/file.txt'
231-
mock_storage_client.upload_fileobj.assert_called_once_with(file_obj, 'file.txt', 'bucket')
224+
mock_storage_client.upload_fileobj.assert_called_once_with(
225+
file_obj, 'file.txt', 'bucket')
232226

233227
@patch('backend.database.client.create_storage_client_from_config')
234228
@patch('backend.database.client.MinIOStorageConfig')
@@ -237,16 +231,19 @@ def test_minio_client_download_file(self, mock_config_class, mock_create_client)
237231
MinioClient._instance = None
238232

239233
mock_storage_client = MagicMock()
240-
mock_storage_client.download_file.return_value = (True, 'Downloaded successfully')
234+
mock_storage_client.download_file.return_value = (
235+
True, 'Downloaded successfully')
241236
mock_create_client.return_value = mock_storage_client
242237
mock_config_class.return_value = MagicMock()
243238

244239
client = MinioClient()
245-
success, result = client.download_file('file.txt', '/path/to/download.txt', 'bucket')
240+
success, result = client.download_file(
241+
'file.txt', '/path/to/download.txt', 'bucket')
246242

247243
assert success is True
248244
assert result == 'Downloaded successfully'
249-
mock_storage_client.download_file.assert_called_once_with('file.txt', '/path/to/download.txt', 'bucket')
245+
mock_storage_client.download_file.assert_called_once_with(
246+
'file.txt', '/path/to/download.txt', 'bucket')
250247

251248
@patch('backend.database.client.create_storage_client_from_config')
252249
@patch('backend.database.client.MinIOStorageConfig')
@@ -255,7 +252,8 @@ def test_minio_client_get_file_url(self, mock_config_class, mock_create_client):
255252
MinioClient._instance = None
256253

257254
mock_storage_client = MagicMock()
258-
mock_storage_client.get_file_url.return_value = (True, 'http://example.com/file.txt')
255+
mock_storage_client.get_file_url.return_value = (
256+
True, 'http://example.com/file.txt')
259257
mock_create_client.return_value = mock_storage_client
260258
mock_config_class.return_value = MagicMock()
261259

@@ -264,7 +262,8 @@ def test_minio_client_get_file_url(self, mock_config_class, mock_create_client):
264262

265263
assert success is True
266264
assert result == 'http://example.com/file.txt'
267-
mock_storage_client.get_file_url.assert_called_once_with('file.txt', 'bucket', 7200)
265+
mock_storage_client.get_file_url.assert_called_once_with(
266+
'file.txt', 'bucket', 7200)
268267

269268
@patch('backend.database.client.create_storage_client_from_config')
270269
@patch('backend.database.client.MinIOStorageConfig')
@@ -281,7 +280,8 @@ def test_minio_client_get_file_size(self, mock_config_class, mock_create_client)
281280
size = client.get_file_size('file.txt', 'bucket')
282281

283282
assert size == 1024
284-
mock_storage_client.get_file_size.assert_called_once_with('file.txt', 'bucket')
283+
mock_storage_client.get_file_size.assert_called_once_with(
284+
'file.txt', 'bucket')
285285

286286
@patch('backend.database.client.create_storage_client_from_config')
287287
@patch('backend.database.client.MinIOStorageConfig')
@@ -302,7 +302,8 @@ def test_minio_client_list_files(self, mock_config_class, mock_create_client):
302302

303303
assert len(files) == 2
304304
assert files[0]['key'] == 'file1.txt'
305-
mock_storage_client.list_files.assert_called_once_with('prefix/', 'bucket')
305+
mock_storage_client.list_files.assert_called_once_with(
306+
'prefix/', 'bucket')
306307

307308
@patch('backend.database.client.create_storage_client_from_config')
308309
@patch('backend.database.client.MinIOStorageConfig')
@@ -311,7 +312,8 @@ def test_minio_client_delete_file(self, mock_config_class, mock_create_client):
311312
MinioClient._instance = None
312313

313314
mock_storage_client = MagicMock()
314-
mock_storage_client.delete_file.return_value = (True, 'Deleted successfully')
315+
mock_storage_client.delete_file.return_value = (
316+
True, 'Deleted successfully')
315317
mock_create_client.return_value = mock_storage_client
316318
mock_config_class.return_value = MagicMock()
317319

@@ -320,7 +322,8 @@ def test_minio_client_delete_file(self, mock_config_class, mock_create_client):
320322

321323
assert success is True
322324
assert result == 'Deleted successfully'
323-
mock_storage_client.delete_file.assert_called_once_with('file.txt', 'bucket')
325+
mock_storage_client.delete_file.assert_called_once_with(
326+
'file.txt', 'bucket')
324327

325328
@patch('backend.database.client.create_storage_client_from_config')
326329
@patch('backend.database.client.MinIOStorageConfig')
@@ -340,7 +343,8 @@ def test_minio_client_get_file_stream(self, mock_config_class, mock_create_clien
340343

341344
assert success is True
342345
assert result == mock_stream
343-
mock_storage_client.get_file_stream.assert_called_once_with('file.txt', 'bucket')
346+
mock_storage_client.get_file_stream.assert_called_once_with(
347+
'file.txt', 'bucket')
344348

345349

346350
class TestGetDbSession:
@@ -410,7 +414,8 @@ def test_filter_property_filters_correctly(self):
410414
mock_model = MagicMock()
411415
mock_model.__table__ = MagicMock()
412416
mock_model.__table__.columns = MagicMock()
413-
mock_model.__table__.columns.keys.return_value = ['id', 'name', 'email']
417+
mock_model.__table__.columns.keys.return_value = [
418+
'id', 'name', 'email']
414419

415420
data = {
416421
'id': 1,
@@ -454,4 +459,3 @@ def test_filter_property_no_matching_fields(self):
454459
result = filter_property(data, mock_model)
455460

456461
assert result == {}
457-

test/backend/services/test_agent_service.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919

2020

2121
# Patch environment variables before any imports that might use them
22-
os.environ.setdefault('MINIO_ENDPOINT', 'http://localhost:9000')
23-
os.environ.setdefault('MINIO_ACCESS_KEY', 'minioadmin')
24-
os.environ.setdefault('MINIO_SECRET_KEY', 'minioadmin')
25-
os.environ.setdefault('MINIO_REGION', 'us-east-1')
26-
os.environ.setdefault('MINIO_DEFAULT_BUCKET', 'test-bucket')
22+
# Environment variables are now configured in conftest.py
2723

2824
# Mock boto3 before importing the module under test
2925
boto3_mock = MagicMock()

0 commit comments

Comments
 (0)