alessandro trinca tornidor commited on
Commit ·
b680bf1
1
Parent(s): abeae27
test: fix env leak in test_resolve_model_folder, add MODEL_FOLDER tests
Browse filesWrap test_default_variant_when_no_env_vars with @patch .dict to
auto-restore os.environ on failure. Add _models_available() support
for MODEL_FOLDER env override with two new test cases.
Files changed (2)
2 unmodified lines34567869 unmodified lines78798081828398 unmodified lines1821831841851861872 unmodified linesimport osimport timeimport unittestfrom unittest.mock import patchimport pytest69 unmodified linesdef _models_available() -> bool:"""Check if SAM2 model files are downloaded."""try:from samgis_core.prediction_api.model_registry import verify_downloadvariant = os.getenv("MODEL_VARIANT", "sam2.1_hiera_base_plus_uint8")98 unmodified lines"Less than 2 geometries within the Shapely geometry from the geojson")@patch.object(time, "time")@patch.object(app, "samexporter_predict")def test_infer_samgis_mocked_200(self, samexporter_predict_mocked, time_mocked):2 unmodified lines345678969 unmodified lines79808182838485868788899098 unmodified lines1891901911921931941951961971981992002012022032042052062072082092102112 unmodified linesimport osimport timeimport unittestfrom pathlib import Pathfrom unittest.mock import patchimport pytest69 unmodified linesdef _models_available() -> bool:"""Check if SAM2 model files are downloaded."""try:model_folder = os.getenv("MODEL_FOLDER")if model_folder is not None:folder = Path(model_folder)return (folder / "encoder.onnx").exists() and (folder / "decoder.onnx").exists()from samgis_core.prediction_api.model_registry import verify_downloadvariant = os.getenv("MODEL_VARIANT", "sam2.1_hiera_base_plus_uint8")98 unmodified lines"Less than 2 geometries within the Shapely geometry from the geojson")@patch.dict(os.environ, {"MODEL_FOLDER": ""})def test_models_available_with_model_folder_valid(self):import tempfilewith tempfile.TemporaryDirectory() as tmp:(Path(tmp) / "encoder.onnx").touch()(Path(tmp) / "decoder.onnx").touch()with patch.dict(os.environ, {"MODEL_FOLDER": tmp}):self.assertTrue(_models_available())def test_models_available_with_model_folder_empty_dir(self):import tempfilewith tempfile.TemporaryDirectory() as tmp:with patch.dict(os.environ, {"MODEL_FOLDER": tmp}):self.assertFalse(_models_available())@patch.object(time, "time")@patch.object(app, "samexporter_predict")def test_infer_samgis_mocked_200(self, samexporter_predict_mocked, time_mocked):
34 unmodified lines35363738394034 unmodified linesget_model_dir_mocked.assert_called_once_with("sam2.1_hiera_tiny_uint8")self.assertEqual(result, Path("/mock/tiny"))@patch("samgis_core.prediction_api.model_registry.get_model_dir")def test_default_variant_when_no_env_vars(self, get_model_dir_mocked):get_model_dir_mocked.return_value = Path("/mock/default")34 unmodified lines3536373839404134 unmodified linesget_model_dir_mocked.assert_called_once_with("sam2.1_hiera_tiny_uint8")self.assertEqual(result, Path("/mock/tiny"))@patch.dict(os.environ, {}, clear=False)@patch("samgis_core.prediction_api.model_registry.get_model_dir")def test_default_variant_when_no_env_vars(self, get_model_dir_mocked):get_model_dir_mocked.return_value = Path("/mock/default")
tests/test_app.py+24
tests/test_resolve_model_folder.py+1