-
Notifications
You must be signed in to change notification settings - Fork 756
/
test_base.py
84 lines (58 loc) · 2.17 KB
/
test_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from __future__ import annotations
import typing as t
from typing import TYPE_CHECKING
import pytest
from bentoml.io import IODescriptor
if TYPE_CHECKING:
from typing_extensions import Self
from bentoml._internal.context import InferenceApiContext as Context
class DummyDescriptor(IODescriptor[t.Any], descriptor_id="bentoml.io.Dummy"):
_mime_type = "application/vnd.bentoml.dummy"
def __init__(self, **kwargs: t.Any):
[object.__setattr__(self, k, v) for k, v in kwargs.items()]
def openapi_schema(self) -> t.Any:
raise NotImplementedError
def openapi_components(self) -> dict[str, t.Any] | None:
raise NotImplementedError
def openapi_example(self) -> t.Any | None:
raise NotImplementedError
def openapi_request_body(self) -> dict[str, t.Any]:
raise NotImplementedError
def openapi_responses(self) -> dict[str, t.Any]:
raise NotImplementedError
def to_spec(self) -> dict[str, t.Any]:
raise NotImplementedError
@classmethod
def from_spec(cls, spec: dict[str, t.Any]) -> Self:
return cls(**spec)
def input_type(self) -> t.Any:
return str
async def from_http_request(self, request: t.Any) -> t.Any:
return request
async def to_http_response(self, obj: t.Any, ctx: Context | None = None) -> t.Any:
return obj, ctx
async def from_proto(self, field: t.Any) -> t.Any:
return field
async def to_proto(self, obj: t.Any) -> t.Any:
return obj
@classmethod
def from_sample(cls, sample: t.Any, **kwargs: t.Any):
return super().from_sample(sample, **kwargs)
@pytest.mark.parametrize(
"fn",
[
f"openapi_{n}"
for n in ("schema", "components", "example", "request_body", "responses")
],
)
def test_raise_not_implemented_openapi(fn: str):
with pytest.raises(NotImplementedError):
getattr(DummyDescriptor(), fn)()
@pytest.mark.asyncio
async def test_lazy_init():
d = DummyDescriptor.from_sample("asdf", foo="bar", baz="qux")
assert not d._initialized
assert d.sample == "asdf"
assert d.foo == "bar"
r = await d.from_http_request("asdf")
assert r and d._initialized