@@ -51,7 +51,7 @@ def test_db_init_new_database(self, mock_config, mock_manager):
5151
5252 @patch ("ml_agents.core.database_manager.DatabaseManager" )
5353 @patch ("ml_agents.core.database_manager.DatabaseConfig" )
54- @patch ("pathlib .Path" )
54+ @patch ("ml_agents.cli.commands.db .Path" )
5555 def test_db_init_existing_database_without_force (
5656 self , mock_path , mock_config , mock_manager
5757 ):
@@ -71,7 +71,7 @@ def test_db_init_existing_database_without_force(
7171
7272 @patch ("ml_agents.core.database_manager.DatabaseManager" )
7373 @patch ("ml_agents.core.database_manager.DatabaseConfig" )
74- @patch ("pathlib .Path" )
74+ @patch ("ml_agents.cli.commands.db .Path" )
7575 def test_db_init_existing_database_with_force (
7676 self , mock_path , mock_config , mock_manager
7777 ):
@@ -98,8 +98,13 @@ def test_db_init_existing_database_with_force(
9898
9999 @patch ("ml_agents.core.database_manager.DatabaseManager" )
100100 @patch ("ml_agents.core.database_manager.DatabaseConfig" )
101- def test_db_init_default_path (self , mock_config , mock_manager ):
101+ @patch ("ml_agents.cli.commands.db.Path" )
102+ def test_db_init_default_path (self , mock_path , mock_config , mock_manager ):
102103 """Test db init with default database path."""
104+ mock_path_instance = Mock ()
105+ mock_path_instance .exists .return_value = False
106+ mock_path .return_value = mock_path_instance
107+
103108 mock_manager_instance = Mock ()
104109 mock_manager_instance .get_database_stats .return_value = {
105110 "schema_version" : "1.2.0" ,
@@ -142,35 +147,35 @@ def test_db_backup_command_help(self):
142147
143148 @patch ("ml_agents.core.database_manager.DatabaseManager" )
144149 @patch ("ml_agents.core.database_manager.DatabaseConfig" )
145- @patch ("pathlib .Path" )
150+ @patch ("ml_agents.cli.commands.db .Path" )
146151 def test_db_backup_success (self , mock_path , mock_config , mock_manager ):
147152 """Test successful database backup."""
148- # Mock source database exists
149- mock_source_path = Mock ()
150- mock_source_path .exists .return_value = True
151- mock_path .return_value = mock_source_path
152-
153- # Mock backup file stats
154- mock_backup_path = Mock ()
155- mock_backup_path .stat .return_value .st_size = 2048
156-
157- with patch (
158- "ml_agents.cli.commands.db.Path" ,
159- side_effect = lambda x : (
160- mock_source_path if "source" in str (x ) else mock_backup_path
161- ),
162- ):
163- mock_manager_instance = Mock ()
164- mock_manager .return_value = mock_manager_instance
165153
166- result = self .runner .invoke (app , ["db" , "backup" , "--source" , "source.db" ])
154+ # Mock source database exists and backup file stats
155+ def path_side_effect (path_str ):
156+ mock_path_instance = Mock ()
157+ if "source.db" == str (path_str ) and "backup" not in str (path_str ):
158+ mock_path_instance .exists .return_value = True
159+ else :
160+ # This is the backup file path - mock stat method
161+ mock_stat = Mock ()
162+ mock_stat .st_size = 2048
163+ mock_path_instance .stat .return_value = mock_stat
164+ return mock_path_instance
167165
168- assert result .exit_code == 0
169- assert "Database backup created successfully" in result .stdout
170- assert "Source: source.db" in result .stdout
171- assert "Size: 2048 bytes" in result .stdout
166+ mock_path .side_effect = path_side_effect
167+
168+ mock_manager_instance = Mock ()
169+ mock_manager .return_value = mock_manager_instance
170+
171+ result = self .runner .invoke (app , ["db" , "backup" , "--source" , "source.db" ])
172+
173+ assert result .exit_code == 0
174+ assert "Database backup created successfully" in result .stdout
175+ assert "Source: source.db" in result .stdout
176+ assert "Size: 2048 bytes" in result .stdout
172177
173- @patch ("pathlib .Path" )
178+ @patch ("ml_agents.cli.commands.db .Path" )
174179 def test_db_backup_source_not_exists (self , mock_path ):
175180 """Test backup when source database doesn't exist."""
176181 mock_path_instance = Mock ()
@@ -184,47 +189,60 @@ def test_db_backup_source_not_exists(self, mock_path):
184189
185190 @patch ("ml_agents.core.database_manager.DatabaseManager" )
186191 @patch ("ml_agents.core.database_manager.DatabaseConfig" )
187- @patch ("pathlib .Path" )
192+ @patch ("ml_agents.cli.commands.db .Path" )
188193 def test_db_backup_custom_path (self , mock_path , mock_config , mock_manager ):
189194 """Test backup with custom backup path."""
190- mock_source_path = Mock ()
191- mock_source_path .exists .return_value = True
192- mock_backup_path = Mock ()
193- mock_backup_path .stat .return_value .st_size = 1500
194195
195196 def path_side_effect (path ):
197+ mock_path_instance = Mock ()
196198 if "custom_backup" in str (path ):
197- return mock_backup_path
198- return mock_source_path
199+ mock_stat = Mock ()
200+ mock_stat .st_size = 1500
201+ mock_path_instance .stat .return_value = mock_stat
202+ else :
203+ mock_path_instance .exists .return_value = True
204+ return mock_path_instance
199205
200- with patch ("ml_agents.cli.commands.db.Path" , side_effect = path_side_effect ):
201- mock_manager_instance = Mock ()
202- mock_manager .return_value = mock_manager_instance
206+ mock_path .side_effect = path_side_effect
203207
204- result = self .runner .invoke (
205- app ,
206- [
207- "db" ,
208- "backup" ,
209- "--source" ,
210- "source.db" ,
211- "--backup-path" ,
212- "custom_backup.db" ,
213- ],
214- )
208+ mock_manager_instance = Mock ()
209+ mock_manager .return_value = mock_manager_instance
215210
216- assert result .exit_code == 0
217- assert "Backup: custom_backup.db" in result .stdout
211+ result = self .runner .invoke (
212+ app ,
213+ [
214+ "db" ,
215+ "backup" ,
216+ "--source" ,
217+ "source.db" ,
218+ "--backup-path" ,
219+ "custom_backup.db" ,
220+ ],
221+ )
222+
223+ assert result .exit_code == 0
224+ assert "Backup: custom_backup.db" in result .stdout
218225
219226 @patch ("ml_agents.core.database_manager.DatabaseManager" )
220227 @patch ("ml_agents.core.database_manager.DatabaseConfig" )
221- @patch ("pathlib .Path" )
228+ @patch ("ml_agents.cli.commands.db .Path" )
222229 def test_db_backup_default_source (self , mock_path , mock_config , mock_manager ):
223230 """Test backup with default source path."""
224- mock_path_instance = Mock ()
225- mock_path_instance .exists .return_value = True
226- mock_path_instance .stat .return_value .st_size = 1024
227- mock_path .return_value = mock_path_instance
231+
232+ def path_side_effect (path_str ):
233+ mock_path_instance = Mock ()
234+ if "ml_agents_results.db" in str (path_str ) and "backup" not in str (
235+ path_str
236+ ):
237+ mock_path_instance .exists .return_value = True
238+ else :
239+ # This is the backup file
240+ mock_stat = Mock ()
241+ mock_stat .st_size = 1024
242+ mock_path_instance .stat .return_value = mock_stat
243+ return mock_path_instance
244+
245+ mock_path .side_effect = path_side_effect
228246
229247 mock_manager_instance = Mock ()
230248 mock_manager .return_value = mock_manager_instance
@@ -251,7 +269,7 @@ def test_db_stats_command_help(self):
251269
252270 @patch ("ml_agents.core.database_manager.DatabaseManager" )
253271 @patch ("ml_agents.core.database_manager.DatabaseConfig" )
254- @patch ("pathlib .Path" )
272+ @patch ("ml_agents.cli.commands.db .Path" )
255273 def test_db_stats_display (self , mock_path , mock_config , mock_manager ):
256274 """Test database statistics display."""
257275 mock_path_instance = Mock ()
@@ -282,7 +300,7 @@ def test_db_stats_display(self, mock_path, mock_config, mock_manager):
282300 assert "5,120 bytes" in result .stdout
283301 assert "Database integrity check passed" in result .stdout
284302
285- @patch ("pathlib .Path" )
303+ @patch ("ml_agents.cli.commands.db .Path" )
286304 def test_db_stats_database_not_found (self , mock_path ):
287305 """Test stats when database doesn't exist."""
288306 mock_path_instance = Mock ()
@@ -296,7 +314,7 @@ def test_db_stats_database_not_found(self, mock_path):
296314
297315 @patch ("ml_agents.core.database_manager.DatabaseManager" )
298316 @patch ("ml_agents.core.database_manager.DatabaseConfig" )
299- @patch ("pathlib .Path" )
317+ @patch ("ml_agents.cli.commands.db .Path" )
300318 def test_db_stats_integrity_failed (self , mock_path , mock_config , mock_manager ):
301319 """Test stats with failed integrity check."""
302320 mock_path_instance = Mock ()
@@ -337,7 +355,7 @@ def test_db_migrate_command_help(self):
337355
338356 @patch ("ml_agents.core.database_manager.DatabaseManager" )
339357 @patch ("ml_agents.core.database_manager.DatabaseConfig" )
340- @patch ("pathlib .Path" )
358+ @patch ("ml_agents.cli.commands.db .Path" )
341359 def test_db_migrate_schema_update (self , mock_path , mock_config , mock_manager ):
342360 """Test schema migration process."""
343361 mock_path_instance = Mock ()
@@ -370,7 +388,7 @@ def test_db_migrate_schema_update(self, mock_path, mock_config, mock_manager):
370388
371389 @patch ("ml_agents.core.database_manager.DatabaseManager" )
372390 @patch ("ml_agents.core.database_manager.DatabaseConfig" )
373- @patch ("pathlib .Path" )
391+ @patch ("ml_agents.cli.commands.db .Path" )
374392 def test_db_migrate_already_up_to_date (self , mock_path , mock_config , mock_manager ):
375393 """Test migration when schema is already up to date."""
376394 mock_path_instance = Mock ()
@@ -395,7 +413,7 @@ def test_db_migrate_already_up_to_date(self, mock_path, mock_config, mock_manage
395413 assert result .exit_code == 0
396414 assert "Database schema is already up to date" in result .stdout
397415
398- @patch ("pathlib .Path" )
416+ @patch ("ml_agents.cli.commands.db .Path" )
399417 def test_db_migrate_database_not_found (self , mock_path ):
400418 """Test migration when database doesn't exist."""
401419 mock_path_instance = Mock ()
@@ -411,7 +429,7 @@ def test_db_migrate_database_not_found(self, mock_path):
411429
412430 @patch ("ml_agents.core.database_manager.DatabaseManager" )
413431 @patch ("ml_agents.core.database_manager.DatabaseConfig" )
414- @patch ("pathlib .Path" )
432+ @patch ("ml_agents.cli.commands.db .Path" )
415433 def test_db_migrate_user_cancellation (self , mock_path , mock_config , mock_manager ):
416434 """Test migration cancellation by user."""
417435 mock_path_instance = Mock ()
0 commit comments