4
4
import tempfile
5
5
from stage0_mongodb_api .managers .config_manager import ConfigManager
6
6
from stage0_py_utils import Config
7
- from unittest .mock import patch
7
+ from unittest .mock import patch , MagicMock
8
8
9
9
class TestConfigManager (unittest .TestCase ):
10
+ @classmethod
11
+ def setUpClass (cls ):
12
+ cls .mongoio_patcher = patch ('stage0_py_utils.mongo_utils.mongo_io.MongoIO.get_instance' )
13
+ cls .mock_mongoio_get_instance = cls .mongoio_patcher .start ()
14
+ cls .mock_mongoio_get_instance .return_value = MagicMock ()
15
+
16
+ @classmethod
17
+ def tearDownClass (cls ):
18
+ cls .mongoio_patcher .stop ()
19
+
10
20
def setUp (self ):
11
21
self .test_cases_dir = os .path .join (os .path .dirname (__file__ ), '..' , 'test_cases' )
12
22
self .config = Config .get_instance ()
@@ -16,38 +26,26 @@ def tearDown(self):
16
26
17
27
def test_load_minimum_valid (self ):
18
28
"""Test loading empty Collections directory structure."""
19
-
20
- # Initialize SchemaManager
21
29
test_case_dir = os .path .join (self .test_cases_dir , "minimum_valid" )
22
30
self .config .INPUT_FOLDER = test_case_dir
23
31
manager = ConfigManager ()
24
-
25
- # Verify no load errors
26
32
self .assertEqual (manager .load_errors , [], "Unexpected load errors in simple_valid test case" )
27
33
self .assertEqual (len (manager .collection_configs ), 0 , f"Unexpected number of collection configs { len (manager .collection_configs )} " )
28
34
29
35
def test_load_small_sample (self ):
30
36
"""Test loading Small configuration."""
31
-
32
- # Initialize SchemaManager
33
37
test_case_dir = os .path .join (self .test_cases_dir , "small_sample" )
34
38
self .config .INPUT_FOLDER = test_case_dir
35
39
manager = ConfigManager ()
36
-
37
- # Verify no load errors
38
40
self .assertEqual (manager .load_errors , [], "Unexpected load errors in simple_valid test case" )
39
41
self .assertEqual (len (manager .collection_configs ), 1 , f"Unexpected number of collection configs { len (manager .collection_configs )} " )
40
42
self .assertIn ("simple" , manager .collection_configs )
41
43
42
44
def test_load_large_sample (self ):
43
45
"""Test loading large config"""
44
-
45
- # Initialize SchemaManager
46
46
test_case_dir = os .path .join (self .test_cases_dir , "large_sample" )
47
47
self .config .INPUT_FOLDER = test_case_dir
48
48
manager = ConfigManager ()
49
-
50
- # Verify no load errors
51
49
self .assertEqual (manager .load_errors , [], "Unexpected load errors in simple_valid test case" )
52
50
self .assertEqual (len (manager .collection_configs ), 4 , f"Unexpected number of collection configs { len (manager .collection_configs )} " )
53
51
self .assertIn ("media" , manager .collection_configs )
@@ -57,75 +55,56 @@ def test_load_large_sample(self):
57
55
58
56
def test_non_parsable (self ):
59
57
"""Test loading with non-parsable files"""
60
-
61
- # Initialize SchemaManager
62
58
test_case_dir = os .path .join (self .test_cases_dir , "non_parsable" )
63
59
self .config .INPUT_FOLDER = test_case_dir
64
60
manager = ConfigManager ()
65
-
66
- # Verify no load errors
67
61
self .assertEqual (len (manager .load_errors ), 1 , f"Unexpected load errors { manager .load_errors } " )
68
62
69
63
def test_validation_errors (self ):
70
64
"""Test loading with validation errors"""
71
-
72
- # Initialize SchemaManager
73
65
test_case_dir = os .path .join (self .test_cases_dir , "validation_errors" )
74
66
self .config .INPUT_FOLDER = test_case_dir
75
67
manager = ConfigManager ()
76
68
errors = manager .validate_configs ()
77
-
78
- # Verify no load errors
79
69
self .assertEqual (len (manager .load_errors ), 0 , f"Unexpected load errors { manager .load_errors } " )
80
70
self .assertEqual (len (errors ), 6 , f"Unexpected number of validation errors { errors } " )
81
71
82
- @patch ('stage0_mongodb_api.managers.config_manager.MongoIO' )
83
- def test_load_test_data_bulk_write_error (self , mock_mongo ):
72
+ def test_load_test_data_bulk_write_error (self ):
84
73
"""Test that _load_test_data properly handles bulk write errors."""
85
- # Arrange
86
74
from stage0_py_utils .mongo_utils .mongo_io import TestDataLoadError
87
75
mock_details = {'writeErrors' : [{'index' : 0 , 'code' : 121 , 'errmsg' : 'Document failed validation' }]}
88
- mock_mongo .return_value .load_test_data .side_effect = TestDataLoadError ("Schema validation failed during test data load" , details = mock_details )
89
-
76
+ self .mock_mongoio_get_instance .return_value .load_test_data .side_effect = TestDataLoadError (
77
+ "Schema validation failed during test data load" , details = mock_details
78
+ )
90
79
config_manager = ConfigManager ()
91
80
collection_name = "test_collection"
92
81
test_data_file = "test.json"
93
-
94
- # Act
95
82
result = config_manager ._load_test_data (collection_name , test_data_file )
96
-
97
- # Assert
98
83
self .assertEqual (result ["status" ], "error" )
99
84
self .assertEqual (result ["operation" ], "load_test_data" )
100
85
self .assertEqual (result ["collection" ], collection_name )
101
86
self .assertIn ("test.json" , result ["test_data" ])
102
87
self .assertEqual (result ["error" ], "Schema validation failed during test data load" )
103
88
self .assertEqual (result ["details" ], mock_details )
104
89
105
- @patch ('stage0_mongodb_api.managers.config_manager.MongoIO' )
106
- def test_load_test_data_generic_error (self , mock_mongo ):
90
+ def test_load_test_data_generic_error (self ):
107
91
"""Test that _load_test_data properly handles generic errors."""
108
- # Arrange
109
- mock_mongo .return_value .load_test_data .side_effect = Exception ("File not found" )
110
-
92
+ self .mock_mongoio_get_instance .return_value .load_test_data .side_effect = Exception ("File not found" )
111
93
config_manager = ConfigManager ()
112
94
collection_name = "test_collection"
113
95
test_data_file = "test.json"
114
-
115
- # Act
116
96
result = config_manager ._load_test_data (collection_name , test_data_file )
117
-
118
- # Assert
119
97
self .assertEqual (result ["status" ], "error" )
120
98
self .assertEqual (result ["operation" ], "load_test_data" )
121
99
self .assertEqual (result ["collection" ], collection_name )
122
100
self .assertIn ("test.json" , result ["test_data" ])
123
101
self .assertEqual (result ["error" ], "File not found" )
124
102
125
- @patch ('stage0_mongodb_api.managers.config_manager.MongoIO' )
126
- def test_load_test_data_success (self , mock_mongo ):
103
+ def test_load_test_data_success (self ):
127
104
"""Test that _load_test_data properly handles successful loads."""
128
- # Arrange
105
+ # Reset any previous side effects or return values
106
+ self .mock_mongoio_get_instance .return_value .load_test_data .reset_mock ()
107
+ self .mock_mongoio_get_instance .return_value .load_test_data .side_effect = None
129
108
mock_results = {
130
109
"status" : "success" ,
131
110
"operation" : "load_test_data" ,
@@ -134,16 +113,11 @@ def test_load_test_data_success(self, mock_mongo):
134
113
"inserted_ids" : ["id1" , "id2" , "id3" , "id4" , "id5" ],
135
114
"acknowledged" : True
136
115
}
137
- mock_mongo .return_value .load_test_data .return_value = mock_results
138
-
116
+ self .mock_mongoio_get_instance .return_value .load_test_data .return_value = mock_results
139
117
config_manager = ConfigManager ()
140
118
collection_name = "test_collection"
141
119
test_data_file = "test.json"
142
-
143
- # Act
144
120
result = config_manager ._load_test_data (collection_name , test_data_file )
145
-
146
- # Assert
147
121
self .assertEqual (result ["status" ], "success" )
148
122
self .assertEqual (result ["operation" ], "load_test_data" )
149
123
self .assertEqual (result ["collection" ], collection_name )
0 commit comments