|
1 | 1 | # Copyright (C) 2025 Intel Corporation |
2 | 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | +from pathlib import Path |
3 | 4 | from uuid import UUID, uuid4 |
4 | 5 |
|
5 | 6 | import pytest |
@@ -28,9 +29,9 @@ def setup_project_with_models( |
28 | 29 |
|
29 | 30 |
|
30 | 31 | @pytest.fixture |
31 | | -def fxt_model_service(db_session: Session) -> ModelService: |
| 32 | +def fxt_model_service(tmp_path: Path, db_session: Session) -> ModelService: |
32 | 33 | """Fixture to create a ModelService instance.""" |
33 | | - return ModelService(db_session=db_session) |
| 34 | + return ModelService(data_dir=tmp_path, db_session=db_session) |
34 | 35 |
|
35 | 36 |
|
36 | 37 | class TestModelServiceIntegration: |
@@ -65,10 +66,63 @@ def test_non_existent_model(self, model_operation, fxt_project_id, fxt_db_projec |
65 | 66 | assert excinfo.value.resource_type == ResourceType.MODEL |
66 | 67 | assert excinfo.value.resource_id == str(model_id) |
67 | 68 |
|
68 | | - def test_delete_model( |
69 | | - self, fxt_project_id: UUID, fxt_model_id: UUID, fxt_model_service: ModelService, db_session: Session |
| 69 | + def test_delete_model_no_files( |
| 70 | + self, |
| 71 | + tmp_path: Path, |
| 72 | + fxt_project_id: UUID, |
| 73 | + fxt_model_id: UUID, |
| 74 | + fxt_model_service: ModelService, |
| 75 | + db_session: Session, |
70 | 76 | ): |
71 | | - """Test deleting a model by ID.""" |
| 77 | + """Test that deleting a model removes its database record when no filesystem artifacts exist.""" |
| 78 | + model_rev_path = tmp_path / "projects" / str(fxt_project_id) / "models" / str(fxt_model_id) |
| 79 | + |
72 | 80 | fxt_model_service.delete_model(project_id=fxt_project_id, model_id=fxt_model_id) |
73 | 81 |
|
74 | 82 | assert db_session.get(ModelRevisionDB, str(fxt_model_id)) is None |
| 83 | + assert not model_rev_path.exists() |
| 84 | + |
| 85 | + def test_delete_model_with_files( |
| 86 | + self, |
| 87 | + tmp_path: Path, |
| 88 | + fxt_project_id: UUID, |
| 89 | + fxt_model_id: UUID, |
| 90 | + fxt_model_service: ModelService, |
| 91 | + db_session: Session, |
| 92 | + ): |
| 93 | + """Test that deleting a model removes both its database record and filesystem artifacts.""" |
| 94 | + model_rev_path = tmp_path / "projects" / str(fxt_project_id) / "models" / str(fxt_model_id) |
| 95 | + model_rev_path.mkdir(parents=True, exist_ok=True) |
| 96 | + (model_rev_path / "config.yaml").touch() |
| 97 | + (model_rev_path / "training.log").touch() |
| 98 | + |
| 99 | + fxt_model_service.delete_model(project_id=fxt_project_id, model_id=fxt_model_id) |
| 100 | + |
| 101 | + assert db_session.get(ModelRevisionDB, str(fxt_model_id)) is None |
| 102 | + assert not model_rev_path.exists() |
| 103 | + |
| 104 | + def test_delete_model_with_files_no_permission( |
| 105 | + self, |
| 106 | + tmp_path: Path, |
| 107 | + fxt_project_id: UUID, |
| 108 | + fxt_model_id: UUID, |
| 109 | + fxt_model_service: ModelService, |
| 110 | + db_session: Session, |
| 111 | + ): |
| 112 | + """Test that deleting a model removes both its database record and filesystem artifacts.""" |
| 113 | + model_rev_path = tmp_path / "projects" / str(fxt_project_id) / "models" / str(fxt_model_id) |
| 114 | + model_rev_path.mkdir(parents=True, exist_ok=True) |
| 115 | + (model_rev_path / "config.yaml").touch() |
| 116 | + |
| 117 | + # Make directory read-only to simulate permission error |
| 118 | + model_rev_path.chmod(0o444) |
| 119 | + |
| 120 | + try: |
| 121 | + with pytest.raises(OSError): |
| 122 | + fxt_model_service.delete_model(project_id=fxt_project_id, model_id=fxt_model_id) |
| 123 | + |
| 124 | + assert db_session.get(ModelRevisionDB, str(fxt_model_id)) is not None |
| 125 | + assert model_rev_path.exists() |
| 126 | + finally: |
| 127 | + # Cleanup: restore permissions so pytest can clean up temp directory |
| 128 | + model_rev_path.chmod(0o755) |
0 commit comments