2023-09-09 05:02:44 -04:00
|
|
|
import json
|
2026-08-08 12:07:30 -04:00
|
|
|
from types import SimpleNamespace
|
|
|
|
|
from typing import Any, Callable, Iterator
|
2023-06-27 19:21:33 -04:00
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
2025-03-27 15:49:09 -04:00
|
|
|
from immich_ml.config import log
|
|
|
|
|
from immich_ml.main import app
|
2023-06-27 19:21:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def pil_image() -> Image.Image:
|
|
|
|
|
return Image.new("RGB", (600, 800))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def mock_get_model() -> Iterator[mock.Mock]:
|
2025-03-27 15:49:09 -04:00
|
|
|
with mock.patch("immich_ml.models.cache.from_model_type", autospec=True) as mocked:
|
2023-06-27 19:21:33 -04:00
|
|
|
yield mocked
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
2023-12-14 14:51:24 -05:00
|
|
|
def deployed_app() -> Iterator[TestClient]:
|
|
|
|
|
with TestClient(app) as client:
|
|
|
|
|
yield client
|
2023-09-09 05:02:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
|
def responses() -> dict[str, Any]:
|
2023-11-13 11:18:46 -05:00
|
|
|
responses: dict[str, Any] = json.load(open("responses.json", "r"))
|
|
|
|
|
return responses
|
2023-10-31 06:02:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
|
def clip_model_cfg() -> dict[str, Any]:
|
|
|
|
|
return {
|
|
|
|
|
"embed_dim": 512,
|
|
|
|
|
"vision_cfg": {"image_size": 224, "layers": 12, "width": 768, "patch_size": 32},
|
|
|
|
|
"text_cfg": {"context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
|
def clip_preprocess_cfg() -> dict[str, Any]:
|
|
|
|
|
return {
|
|
|
|
|
"size": [224, 224],
|
|
|
|
|
"mode": "RGB",
|
|
|
|
|
"mean": [0.48145466, 0.4578275, 0.40821073],
|
|
|
|
|
"std": [0.26862954, 0.26130258, 0.27577711],
|
|
|
|
|
"interpolation": "bicubic",
|
|
|
|
|
"resize_mode": "shortest",
|
|
|
|
|
"fill_color": 0,
|
|
|
|
|
}
|
2023-12-20 20:47:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
|
def clip_tokenizer_cfg() -> dict[str, Any]:
|
|
|
|
|
return {
|
|
|
|
|
"add_prefix_space": False,
|
|
|
|
|
"added_tokens_decoder": {
|
|
|
|
|
"49406": {
|
|
|
|
|
"content": "<|startoftext|>",
|
|
|
|
|
"lstrip": False,
|
|
|
|
|
"normalized": True,
|
|
|
|
|
"rstrip": False,
|
|
|
|
|
"single_word": False,
|
|
|
|
|
"special": True,
|
|
|
|
|
},
|
|
|
|
|
"49407": {
|
|
|
|
|
"content": "<|endoftext|>",
|
|
|
|
|
"lstrip": False,
|
|
|
|
|
"normalized": True,
|
|
|
|
|
"rstrip": False,
|
|
|
|
|
"single_word": False,
|
|
|
|
|
"special": True,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"bos_token": "<|startoftext|>",
|
|
|
|
|
"clean_up_tokenization_spaces": True,
|
|
|
|
|
"do_lower_case": True,
|
|
|
|
|
"eos_token": "<|endoftext|>",
|
|
|
|
|
"errors": "replace",
|
|
|
|
|
"model_max_length": 77,
|
|
|
|
|
"pad_token": "<|endoftext|>",
|
|
|
|
|
"tokenizer_class": "CLIPTokenizer",
|
|
|
|
|
"unk_token": "<|endoftext|>",
|
|
|
|
|
}
|
2024-01-21 18:22:39 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
2024-06-25 12:00:24 -04:00
|
|
|
def providers(request: pytest.FixtureRequest) -> Iterator[mock.Mock]:
|
2024-01-21 18:22:39 -05:00
|
|
|
marker = request.node.get_closest_marker("providers")
|
|
|
|
|
if marker is None:
|
|
|
|
|
raise ValueError("Missing marker 'providers'")
|
|
|
|
|
|
|
|
|
|
providers = marker.args[0]
|
2025-03-27 15:49:09 -04:00
|
|
|
with mock.patch("immich_ml.sessions.ort.ort.get_available_providers") as mocked:
|
2024-01-21 18:22:39 -05:00
|
|
|
mocked.return_value = providers
|
|
|
|
|
yield providers
|
2024-06-25 12:00:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
def ort_pybind() -> Iterator[mock.Mock]:
|
2025-03-27 15:49:09 -04:00
|
|
|
with mock.patch("immich_ml.sessions.ort.ort.capi._pybind_state") as mocked:
|
2024-06-25 12:00:24 -04:00
|
|
|
yield mocked
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
def ov_device_ids(request: pytest.FixtureRequest, ort_pybind: mock.Mock) -> Iterator[mock.Mock]:
|
|
|
|
|
marker = request.node.get_closest_marker("ov_device_ids")
|
|
|
|
|
if marker is None:
|
|
|
|
|
raise ValueError("Missing marker 'ov_device_ids'")
|
|
|
|
|
ort_pybind.get_available_openvino_device_ids.return_value = marker.args[0]
|
|
|
|
|
return ort_pybind
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
def ort_session() -> Iterator[mock.Mock]:
|
2025-03-27 15:49:09 -04:00
|
|
|
with mock.patch("immich_ml.sessions.ort.ort.InferenceSession") as mocked:
|
2024-06-25 12:00:24 -04:00
|
|
|
yield mocked
|
|
|
|
|
|
|
|
|
|
|
2026-08-08 12:07:30 -04:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
def stub_session() -> Callable[..., mock.Mock]:
|
|
|
|
|
def _make(shape: tuple[Any, ...], outputs: Any = None, name: str = "input.1") -> mock.Mock:
|
|
|
|
|
session = mock.Mock()
|
|
|
|
|
session.get_inputs.return_value = [SimpleNamespace(name=name, shape=shape)]
|
|
|
|
|
if outputs is not None:
|
|
|
|
|
session.run.return_value = outputs
|
|
|
|
|
return session
|
|
|
|
|
|
|
|
|
|
return _make
|
|
|
|
|
|
|
|
|
|
|
2024-06-25 12:00:24 -04:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
def ann_session() -> Iterator[mock.Mock]:
|
2025-03-27 15:49:09 -04:00
|
|
|
with mock.patch("immich_ml.sessions.ann.Ann") as mocked:
|
2024-06-25 12:00:24 -04:00
|
|
|
yield mocked
|
|
|
|
|
|
|
|
|
|
|
2025-03-18 00:04:08 +08:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
def rknn_session() -> Iterator[mock.Mock]:
|
2025-03-27 15:49:09 -04:00
|
|
|
with mock.patch("immich_ml.sessions.rknn.RknnPoolExecutor") as mocked:
|
2025-03-18 00:04:08 +08:00
|
|
|
yield mocked
|
|
|
|
|
|
|
|
|
|
|
2024-06-25 12:00:24 -04:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
def rmtree() -> Iterator[mock.Mock]:
|
2025-03-27 15:49:09 -04:00
|
|
|
with mock.patch("immich_ml.models.base.rmtree", autospec=True) as mocked:
|
2024-06-25 12:00:24 -04:00
|
|
|
mocked.avoids_symlink_attacks = True
|
|
|
|
|
yield mocked
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
def path() -> Iterator[mock.Mock]:
|
|
|
|
|
path = mock.MagicMock()
|
|
|
|
|
path.exists.return_value = True
|
|
|
|
|
path.is_dir.return_value = True
|
|
|
|
|
path.is_file.return_value = True
|
|
|
|
|
path.with_suffix.return_value = path
|
|
|
|
|
path.return_value = path
|
|
|
|
|
|
2025-03-27 15:49:09 -04:00
|
|
|
with mock.patch("immich_ml.models.base.Path", return_value=path) as mocked:
|
2024-06-25 12:00:24 -04:00
|
|
|
yield mocked
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
def info() -> Iterator[mock.Mock]:
|
|
|
|
|
with mock.patch.object(log, "info") as mocked:
|
|
|
|
|
yield mocked
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
def warning() -> Iterator[mock.Mock]:
|
|
|
|
|
with mock.patch.object(log, "warning") as mocked:
|
|
|
|
|
yield mocked
|
|
|
|
|
|
|
|
|
|
|
2024-07-10 10:20:43 -04:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
def exception() -> Iterator[mock.Mock]:
|
|
|
|
|
with mock.patch.object(log, "exception") as mocked:
|
|
|
|
|
yield mocked
|
|
|
|
|
|
|
|
|
|
|
2024-06-25 12:00:24 -04:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
|
def snapshot_download() -> Iterator[mock.Mock]:
|
2025-03-27 15:49:09 -04:00
|
|
|
with mock.patch("immich_ml.models.base.snapshot_download") as mocked:
|
2024-06-25 12:00:24 -04:00
|
|
|
yield mocked
|