@@ -252,8 +252,8 @@ def test_get_image_by_id_index_out_of_range(client):
252252 with (
253253 patch ("transformerlab.routers.experiment.diffusion.find_image_by_id" ) as mock_find_image ,
254254 patch ("transformerlab.routers.experiment.diffusion.get_images_dir" , return_value = "/fake/images" ),
255- patch ("os.path .exists" , return_value = True ),
256- patch ("os.path .isdir" , return_value = True ),
255+ patch ("transformerlab.routers.experiment.diffusion.storage .exists" , return_value = True ),
256+ patch ("transformerlab.routers.experiment.diffusion.storage .isdir" , return_value = True ),
257257 ):
258258 # Create mock image with folder-format
259259 mock_image = MagicMock ()
@@ -272,9 +272,9 @@ def test_get_image_info_by_id_success(client):
272272 """Test getting image metadata by ID"""
273273 with (
274274 patch ("transformerlab.routers.experiment.diffusion.find_image_by_id" ) as mock_find_image ,
275- patch ("os.path .exists" , return_value = True ),
276- patch ("os.path .isdir" , return_value = True ),
277- patch ("os.listdir " , return_value = ["0.png" , "1.png" , "2.png" ]),
275+ patch ("transformerlab.routers.experiment.diffusion.storage .exists" , return_value = True ),
276+ patch ("transformerlab.routers.experiment.diffusion.storage .isdir" , return_value = True ),
277+ patch ("transformerlab.routers.experiment.diffusion.storage.ls " , return_value = ["0.png" , "1.png" , "2.png" ]),
278278 ):
279279 # Create mock image
280280 mock_image = MagicMock ()
@@ -295,9 +295,9 @@ def test_get_image_count_success(client):
295295 """Test getting image count for an image set"""
296296 with (
297297 patch ("transformerlab.routers.experiment.diffusion.find_image_by_id" ) as mock_find_image ,
298- patch ("os.path .exists" , return_value = True ),
299- patch ("os.path .isdir" , return_value = True ),
300- patch ("os.listdir " , return_value = ["0.png" , "1.png" ]),
298+ patch ("transformerlab.routers.experiment.diffusion.storage .exists" , return_value = True ),
299+ patch ("transformerlab.routers.experiment.diffusion.storage .isdir" , return_value = True ),
300+ patch ("transformerlab.routers.experiment.diffusion.storage.ls " , return_value = ["0.png" , "1.png" ]),
301301 ):
302302 # Create mock image
303303 mock_image = MagicMock ()
@@ -317,8 +317,8 @@ def test_delete_image_from_history_not_found(client):
317317 """Test deleting a non-existent image from history"""
318318 with (
319319 patch ("transformerlab.routers.experiment.diffusion.get_history_file_path" , return_value = "/fake/history.json" ),
320- patch ("os.path .exists" , return_value = True ),
321- patch ("builtins .open" , mock_open (read_data = '[{"id": "other-id", "image_path": "/fake/path.png"}]' )),
320+ patch ("transformerlab.routers.experiment.diffusion.storage .exists" , return_value = True ),
321+ patch ("transformerlab.routers.experiment.diffusion.storage .open" , mock_open (read_data = '[{"id": "other-id", "image_path": "/fake/path.png"}]' )),
322322 ):
323323 resp = client .delete ("/experiment/test-exp-name/diffusion/history/non-existent-id" )
324324 assert resp .status_code == 500
@@ -331,12 +331,12 @@ def test_create_dataset_from_history_success(client):
331331 patch ("transformerlab.routers.experiment.diffusion.find_image_by_id" ) as mock_find_image ,
332332 patch ("transformerlab.routers.experiment.diffusion.Dataset.get" ) as mock_dataset_get ,
333333 patch ("transformerlab.routers.experiment.diffusion.create_local_dataset" ) as mock_create_dataset ,
334- patch ("os .makedirs" ),
335- patch ("os.path .exists" , return_value = True ),
336- patch ("os.path .isdir" , return_value = True ),
337- patch ("os.listdir " , return_value = ["0.png" , "1.png" ]),
338- patch ("shutil.copy2 " ),
339- patch ("builtins .open" , mock_open ()),
334+ patch ("transformerlab.routers.experiment.diffusion.storage .makedirs" ),
335+ patch ("transformerlab.routers.experiment.diffusion.storage .exists" , return_value = True ),
336+ patch ("transformerlab.routers.experiment.diffusion.storage .isdir" , return_value = True ),
337+ patch ("transformerlab.routers.experiment.diffusion.storage.ls " , return_value = ["/fake/path/folder/ 0.png" , "/fake/path/folder/ 1.png" ]),
338+ patch ("transformerlab.routers.experiment.diffusion.storage.copy_file " ),
339+ patch ("transformerlab.routers.experiment.diffusion.storage .open" , mock_open ()),
340340 ):
341341 # Mock Dataset.get to raise FileNotFoundError for non-existent dataset (new behavior)
342342 mock_dataset_get .side_effect = FileNotFoundError ("Directory for Dataset with id 'test-dataset' not found" )
@@ -610,9 +610,9 @@ def test_load_history_success():
610610 """Test loading history with valid data"""
611611 with (
612612 patch ("transformerlab.routers.experiment.diffusion.get_history_file_path" , return_value = "/fake/history.json" ),
613- patch ("os.path .exists" , return_value = True ),
613+ patch ("transformerlab.routers.experiment.diffusion.storage .exists" , return_value = True ),
614614 patch (
615- "builtins .open" ,
615+ "transformerlab.routers.experiment.diffusion.storage .open" ,
616616 mock_open (
617617 read_data = '[{"id": "test-id", "model": "test-model", "prompt": "test prompt", "adaptor": "", "adaptor_scale": 1.0, "num_inference_steps": 20, "guidance_scale": 7.5, "seed": 42, "image_path": "/fake/path.png", "timestamp": "2023-01-01T00:00:00", "upscaled": false, "upscale_factor": 1, "negative_prompt": "", "eta": 0.0, "clip_skip": 0, "guidance_rescale": 0.0, "height": 512, "width": 512, "generation_time": 5.0, "num_images": 1, "input_image_path": "", "strength": 0.8, "is_img2img": false, "mask_image_path": "", "is_inpainting": false}]'
618618 ),
@@ -665,8 +665,8 @@ def test_load_history_with_pagination():
665665
666666 with (
667667 patch ("transformerlab.routers.experiment.diffusion.get_history_file_path" , return_value = "/fake/history.json" ),
668- patch ("os.path .exists" , return_value = True ),
669- patch ("builtins .open" , mock_open (read_data = json .dumps (history_data ))),
668+ patch ("transformerlab.routers.experiment.diffusion.storage .exists" , return_value = True ),
669+ patch ("transformerlab.routers.experiment.diffusion.storage .open" , mock_open (read_data = json .dumps (history_data ))),
670670 ):
671671 from transformerlab .routers .experiment .diffusion import load_history
672672
@@ -683,7 +683,7 @@ def test_load_history_no_file():
683683 """Test loading history when history file doesn't exist"""
684684 with (
685685 patch ("transformerlab.routers.experiment.diffusion.get_history_file_path" , return_value = "/fake/history.json" ),
686- patch ("os.path .exists" , return_value = False ),
686+ patch ("transformerlab.routers.experiment.diffusion.storage .exists" , return_value = False ),
687687 ):
688688 from transformerlab .routers .experiment .diffusion import load_history
689689
@@ -697,8 +697,8 @@ def test_load_history_invalid_json():
697697 """Test loading history with corrupted JSON file"""
698698 with (
699699 patch ("transformerlab.routers.experiment.diffusion.get_history_file_path" , return_value = "/fake/history.json" ),
700- patch ("os.path .exists" , return_value = True ),
701- patch ("builtins .open" , mock_open (read_data = "invalid json" )),
700+ patch ("lab.storage .exists" , return_value = True ),
701+ patch ("transformerlab.routers.experiment.diffusion.storage .open" , mock_open (read_data = "invalid json" )),
702702 ):
703703 from transformerlab .routers .experiment .diffusion import load_history
704704
@@ -769,8 +769,8 @@ def test_find_image_by_id_success():
769769
770770 with (
771771 patch ("transformerlab.routers.experiment.diffusion.get_history_file_path" , return_value = "/fake/history.json" ),
772- patch ("os.path .exists" , return_value = True ),
773- patch ("builtins .open" , mock_open (read_data = json .dumps (history_data ))),
772+ patch ("transformerlab.routers.experiment.diffusion.storage .exists" , return_value = True ),
773+ patch ("transformerlab.routers.experiment.diffusion.storage .open" , mock_open (read_data = json .dumps (history_data ))),
774774 ):
775775 from transformerlab .routers .experiment .diffusion import find_image_by_id
776776
@@ -816,8 +816,8 @@ def test_find_image_by_id_not_found(client):
816816
817817 with (
818818 patch ("transformerlab.routers.experiment.diffusion.get_history_file_path" , return_value = "/fake/history.json" ),
819- patch ("os.path .exists" , return_value = True ),
820- patch ("builtins .open" , mock_open (read_data = json .dumps (history_data ))),
819+ patch ("transformerlab.routers.experiment.diffusion.storage .exists" , return_value = True ),
820+ patch ("transformerlab.routers.experiment.diffusion.storage .open" , mock_open (read_data = json .dumps (history_data ))),
821821 ):
822822 from transformerlab .routers .experiment .diffusion import find_image_by_id
823823
@@ -830,7 +830,7 @@ def test_find_image_by_id_no_file():
830830 """Test finding an image by ID when history file doesn't exist"""
831831 with (
832832 patch ("transformerlab.routers.experiment.diffusion.get_history_file_path" , return_value = "/fake/history.json" ),
833- patch ("os.path .exists" , return_value = False ),
833+ patch ("transformerlab.routers.experiment.diffusion.storage .exists" , return_value = False ),
834834 ):
835835 from transformerlab .routers .experiment .diffusion import find_image_by_id
836836
@@ -843,8 +843,8 @@ def test_find_image_by_id_invalid_json():
843843 """Test finding an image by ID with corrupted JSON file"""
844844 with (
845845 patch ("transformerlab.routers.experiment.diffusion.get_history_file_path" , return_value = "/fake/history.json" ),
846- patch ("os.path .exists" , return_value = True ),
847- patch ("builtins .open" , mock_open (read_data = "invalid json" )),
846+ patch ("lab.storage .exists" , return_value = True ),
847+ patch ("transformerlab.routers.experiment.diffusion.storage .open" , mock_open (read_data = "invalid json" )),
848848 ):
849849 from transformerlab .routers .experiment .diffusion import find_image_by_id
850850
0 commit comments