32 lines
854 B
Python
32 lines
854 B
Python
from pathlib import Path
|
|
from typing import TypeVar
|
|
|
|
from gallery.sketch.api import Api, S
|
|
from gallery.sketch.source import ApiSource
|
|
|
|
|
|
class MockSource(ApiSource):
|
|
|
|
def __init__(self, path: Path, mapping: dict[str, str]):
|
|
super().__init__("")
|
|
self._path = path
|
|
self._mapping = mapping
|
|
|
|
# pylint:disable=arguments-differ,unused-argument
|
|
async def request(self, endpoint: str, /, **kwargs) -> str:
|
|
for pattern, filename in self._mapping.items():
|
|
if pattern in endpoint:
|
|
return (self._path / filename).read_text()
|
|
raise ValueError(endpoint)
|
|
|
|
|
|
API = Api[S]
|
|
A = TypeVar("A", bound=API)
|
|
|
|
|
|
def create_mock_api(api_type: type[A], mock_source: MockSource) -> A:
|
|
api = api_type()
|
|
# pylint: disable=protected-access
|
|
api._build_source = lambda: mock_source
|
|
return api
|