conftest.py 926 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import json
  2. from typing import Any, Iterator, TypeAlias
  3. from unittest import mock
  4. import numpy as np
  5. import pytest
  6. from fastapi.testclient import TestClient
  7. from PIL import Image
  8. from .main import app, init_state
  9. ndarray: TypeAlias = np.ndarray[int, np.dtype[np.float32]]
  10. @pytest.fixture
  11. def pil_image() -> Image.Image:
  12. return Image.new("RGB", (600, 800))
  13. @pytest.fixture
  14. def cv_image(pil_image: Image.Image) -> ndarray:
  15. return np.asarray(pil_image)[:, :, ::-1] # PIL uses RGB while cv2 uses BGR
  16. @pytest.fixture
  17. def mock_get_model() -> Iterator[mock.Mock]:
  18. with mock.patch("app.models.cache.InferenceModel.from_model_type", autospec=True) as mocked:
  19. yield mocked
  20. @pytest.fixture(scope="session")
  21. def deployed_app() -> TestClient:
  22. init_state()
  23. return TestClient(app)
  24. @pytest.fixture(scope="session")
  25. def responses() -> dict[str, Any]:
  26. return json.load(open("responses.json", "r"))