conftest.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import json
  2. from pathlib import Path
  3. from typing import Any, Iterator
  4. from unittest import mock
  5. import numpy as np
  6. import pytest
  7. from fastapi.testclient import TestClient
  8. from PIL import Image
  9. from .main import app, init_state
  10. from .schemas import ndarray_f32
  11. @pytest.fixture
  12. def pil_image() -> Image.Image:
  13. return Image.new("RGB", (600, 800))
  14. @pytest.fixture
  15. def cv_image(pil_image: Image.Image) -> ndarray_f32:
  16. return np.asarray(pil_image)[:, :, ::-1] # PIL uses RGB while cv2 uses BGR
  17. @pytest.fixture
  18. def mock_get_model() -> Iterator[mock.Mock]:
  19. with mock.patch("app.models.cache.from_model_type", autospec=True) as mocked:
  20. yield mocked
  21. @pytest.fixture(scope="session")
  22. def deployed_app() -> TestClient:
  23. init_state()
  24. return TestClient(app)
  25. @pytest.fixture(scope="session")
  26. def responses() -> dict[str, Any]:
  27. return json.load(open("responses.json", "r"))
  28. @pytest.fixture(scope="session")
  29. def clip_model_cfg() -> dict[str, Any]:
  30. return {
  31. "embed_dim": 512,
  32. "vision_cfg": {"image_size": 224, "layers": 12, "width": 768, "patch_size": 32},
  33. "text_cfg": {"context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12},
  34. }
  35. @pytest.fixture(scope="session")
  36. def clip_preprocess_cfg() -> dict[str, Any]:
  37. return {
  38. "size": [224, 224],
  39. "mode": "RGB",
  40. "mean": [0.48145466, 0.4578275, 0.40821073],
  41. "std": [0.26862954, 0.26130258, 0.27577711],
  42. "interpolation": "bicubic",
  43. "resize_mode": "shortest",
  44. "fill_color": 0,
  45. }