| 531 | |
| 532 | |
| 533 | async def test_base64_file_input(send: Send, monkeypatch: pytest.MonkeyPatch) -> None: |
| 534 | monkeypatch.setattr(MARKERS_ATTR, MARKERS) |
| 535 | |
| 536 | assert await send(b64_obj("bar")) == b64_obj("bar") |
| 537 | assert await send(b64_obj(SAMPLE_FILE_PATH)) == b64_obj(SAMPLE_FILE_BASE64) |
| 538 | assert await send(b64_obj(io.StringIO("Hello, world!"))) == b64_obj("SGVsbG8sIHdvcmxkIQ==") |
| 539 | assert await send(b64_obj(io.BytesIO(b"Hello, world!"))) == b64_obj("SGVsbG8sIHdvcmxkIQ==") |
| 540 | |
| 541 | pdf = {"type": "base64", "media_type": "application/pdf", "data": SAMPLE_FILE_PATH} |
| 542 | assert await send({"type": "document", "source": pdf}) == { |
| 543 | "type": "document", |
| 544 | "source": {"type": "base64", "media_type": "application/pdf", "data": SAMPLE_FILE_BASE64}, |
| 545 | } |
| 546 | |
| 547 | assert await send({"type": "base64"}) == {"type": "base64"} |
| 548 | assert await send({"type": "base64", "data": None}) == {"type": "base64", "data": None} |
| 549 | assert await send({"type": "base64", "data": [1]}) == {"type": "base64", "data": [1]} |
| 550 | |
| 551 | items = (b64_obj(SAMPLE_FILE_PATH), "given", MappingProxyType(b64_obj(io.BytesIO(b"Hello, world!")))) |
| 552 | assert await send({"items": items}) == { |
| 553 | "items": [b64_obj(SAMPLE_FILE_BASE64), "given", b64_obj("SGVsbG8sIHdvcmxkIQ==")] |
| 554 | } |
| 555 | assert await send({"items": (b for b in [b64_obj(SAMPLE_FILE_PATH)])}) == {"items": [b64_obj(SAMPLE_FILE_BASE64)]} |
| 556 | assert await send({"x": {"a": b64_obj(SAMPLE_FILE_PATH), "b": "given"}}) == { |
| 557 | "x": {"a": b64_obj(SAMPLE_FILE_BASE64), "b": "given"} |
| 558 | } |
| 559 | assert await send( |
| 560 | {"a": 1}, extra_body={"doc": b64_obj(SAMPLE_FILE_PATH), "docs": [b64_obj(io.BytesIO(b"Hello, world!"))]} |
| 561 | ) == {"a": 1, "doc": b64_obj(SAMPLE_FILE_BASE64), "docs": [b64_obj("SGVsbG8sIHdvcmxkIQ==")]} |
| 562 | |
| 563 | monkeypatch.setattr(MARKERS_ATTR, ()) |
| 564 | with pytest.raises(TypeError, match="is not JSON serializable"): |
| 565 | await send(b64_obj(SAMPLE_FILE_PATH)) |
| 566 | |
| 567 | |
| 568 | async def test_get_does_not_prepare_a_body(send: Send, monkeypatch: pytest.MonkeyPatch) -> None: |