|
| 1 | +from typing import Annotated, Generic, Optional, Union |
| 2 | + |
| 3 | +from client import ClientTestBase |
| 4 | +from httpx import AsyncClient |
| 5 | +from typing_extensions import Self, TypeVar |
| 6 | + |
| 7 | +from lapidary.runtime import Body, ModelBase, Responses, get |
| 8 | +from lapidary.runtime.model.op import process_operation_method |
| 9 | + |
| 10 | + |
| 11 | +class BodyModel(ModelBase): |
| 12 | + a: Optional[str] |
| 13 | + |
| 14 | + |
| 15 | +def test_serialize_str(): |
| 16 | + class Client(ClientTestBase): |
| 17 | + def op(self: Self, body: Annotated[str, Body({'application/json': str})]) -> Annotated[None, Responses({})]: |
| 18 | + pass |
| 19 | + |
| 20 | + client = ClientTestBase(AsyncClient()) |
| 21 | + |
| 22 | + adapter, response = process_operation_method(Client.op, get('/path')) |
| 23 | + request, auth = adapter.build_request(client, dict(body='a')) |
| 24 | + |
| 25 | + assert request.content == b'"a"' |
| 26 | + |
| 27 | + |
| 28 | +def test_serialize_obj(): |
| 29 | + class Client(ClientTestBase): |
| 30 | + def op(self: Self, body: Annotated[BodyModel, Body({'application/json': BodyModel})]) -> Annotated[None, Responses({})]: |
| 31 | + pass |
| 32 | + |
| 33 | + client = ClientTestBase(AsyncClient()) |
| 34 | + |
| 35 | + adapter, response = process_operation_method(Client.op, get('/path')) |
| 36 | + request, auth = adapter.build_request(client, dict(body=BodyModel(a='a'))) |
| 37 | + |
| 38 | + assert request.content == b'{"a":"a"}' |
| 39 | + |
| 40 | + |
| 41 | +def test_serialize_list(): |
| 42 | + class Client(ClientTestBase): |
| 43 | + def op(self: Self, body: Annotated[list[BodyModel], Body({'application/json': list[BodyModel]})]) -> Annotated[None, Responses({})]: |
| 44 | + pass |
| 45 | + |
| 46 | + client = ClientTestBase(AsyncClient()) |
| 47 | + |
| 48 | + adapter, response = process_operation_method(Client.op, get('/path')) |
| 49 | + request, auth = adapter.build_request(client, dict(body=[BodyModel(a='a')])) |
| 50 | + |
| 51 | + assert request.content == b'[{"a":"a"}]' |
| 52 | + |
| 53 | + |
| 54 | +T = TypeVar('T') |
| 55 | + |
| 56 | + |
| 57 | +class GenericBodyModel(ModelBase, Generic[T]): |
| 58 | + a: T |
| 59 | + |
| 60 | + |
| 61 | +def test_serialize_generic_str(): |
| 62 | + class Client(ClientTestBase): |
| 63 | + def op( |
| 64 | + self: Self, body: Annotated[list[GenericBodyModel[str]], Body({'application/json': list[GenericBodyModel[str]]})] |
| 65 | + ) -> Annotated[None, Responses({})]: |
| 66 | + pass |
| 67 | + |
| 68 | + client = ClientTestBase(AsyncClient()) |
| 69 | + |
| 70 | + adapter, response = process_operation_method(Client.op, get('/path')) |
| 71 | + request, auth = adapter.build_request(client, dict(body=[GenericBodyModel(a='a')])) |
| 72 | + |
| 73 | + assert request.content == b'[{"a":"a"}]' |
| 74 | + |
| 75 | + |
| 76 | +def test_serialize_generic_int(): |
| 77 | + class Client(ClientTestBase): |
| 78 | + def op( |
| 79 | + self: Self, body: Annotated[list[GenericBodyModel[int]], Body({'application/json': list[GenericBodyModel[int]]})] |
| 80 | + ) -> Annotated[None, Responses({})]: |
| 81 | + pass |
| 82 | + |
| 83 | + client = ClientTestBase(AsyncClient()) |
| 84 | + |
| 85 | + adapter, response = process_operation_method(Client.op, get('/path')) |
| 86 | + request, auth = adapter.build_request(client, dict(body=[GenericBodyModel(a=1)])) |
| 87 | + |
| 88 | + assert request.content == b'[{"a":1}]' |
| 89 | + |
| 90 | + |
| 91 | +def test_serialize_generic_obj(): |
| 92 | + class Client(ClientTestBase): |
| 93 | + def op( |
| 94 | + self: Self, body: Annotated[list[GenericBodyModel[BodyModel]], Body({'application/json': list[GenericBodyModel[BodyModel]]})] |
| 95 | + ) -> Annotated[None, Responses({})]: |
| 96 | + pass |
| 97 | + |
| 98 | + client = ClientTestBase(AsyncClient()) |
| 99 | + |
| 100 | + adapter, response = process_operation_method(Client.op, get('/path')) |
| 101 | + request, auth = adapter.build_request(client, dict(body=[GenericBodyModel(a=BodyModel(a='a'))])) |
| 102 | + |
| 103 | + assert request.content == b'[{"a":{"a":"a"}}]' |
| 104 | + |
| 105 | + |
| 106 | +def test_serialize_generic_union(): |
| 107 | + class Client(ClientTestBase): |
| 108 | + def op( |
| 109 | + self: Self, |
| 110 | + body: Annotated[ |
| 111 | + Union[list[GenericBodyModel[BodyModel]], GenericBodyModel[BodyModel], BodyModel], |
| 112 | + Body({'application/json': Union[list[GenericBodyModel[BodyModel]], GenericBodyModel[BodyModel], BodyModel]}), |
| 113 | + ], |
| 114 | + ) -> Annotated[None, Responses({})]: |
| 115 | + pass |
| 116 | + |
| 117 | + client = ClientTestBase(AsyncClient()) |
| 118 | + |
| 119 | + adapter, _ = process_operation_method(Client.op, get('/path')) |
| 120 | + |
| 121 | + request, _ = adapter.build_request(client, dict(body=[GenericBodyModel(a=BodyModel(a='a'))])) |
| 122 | + assert request.content == b'[{"a":{"a":"a"}}]' |
| 123 | + |
| 124 | + request, _ = adapter.build_request(client, dict(body=GenericBodyModel(a=BodyModel(a='a')))) |
| 125 | + assert request.content == b'{"a":{"a":"a"}}' |
| 126 | + |
| 127 | + request, _ = adapter.build_request(client, dict(body=BodyModel(a='a'))) |
| 128 | + assert request.content == b'{"a":"a"}' |
0 commit comments