1313sys .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
2917consts_mock = MagicMock ()
3018consts_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
4221sys .modules ['consts' ] = consts_mock
4322sys .modules ['consts.const' ] = consts_mock .const
5130nexent_storage_mock = MagicMock ()
5231nexent_storage_factory_mock = MagicMock ()
5332storage_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 )
5535nexent_storage_factory_mock .MinIOStorageConfig = MagicMock ()
5636nexent_storage_mock .storage_client_factory = nexent_storage_factory_mock
5737nexent_mock .storage = nexent_storage_mock
7959
8060# Patch storage factory before importing
8161with 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 ,
9474class 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
346350class 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-
0 commit comments