Skip to content

Commit

Permalink
Test new UUID convertor
Browse files Browse the repository at this point in the history
  • Loading branch information
edthrn committed Dec 16, 2024
1 parent a6d581e commit cbc7f87
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions tests/test_convertors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime
from typing import Iterator
from uuid import UUID

import pytest

Expand Down Expand Up @@ -58,15 +59,30 @@ def test_datetime_convertor(test_client_factory: TestClientFactory, app: Router)
)


@pytest.mark.parametrize("param, status_code", [("1.0", 200), ("1-0", 404)])
def test_default_float_convertor(test_client_factory: TestClientFactory, param: str, status_code: int) -> None:
def float_convertor(request: Request) -> JSONResponse:
@pytest.mark.parametrize(
"param_type, param_value, status_code",
[
("float", "1.0", 200),
("float", "1-0", 404),
("uuid", "00000000-aaaa-ffff-9999-000000000000", 200),
("uuid", "00000000aaaaffff9999000000000000", 200),
("uuid", "00000000-AAAA-FFFF-9999-000000000000", 200),
("uuid", "00000000AAAAFFFF9999000000000000", 200),
("uuid", "not-a-uuid", 404),
],
)
def test_default_convertor(
test_client_factory: TestClientFactory, param_type: str, param_value: str, status_code: int
) -> None:
expected_types = {"float": float, "uuid": UUID}

def convertor(request: Request) -> JSONResponse:
param = request.path_params["param"]
assert isinstance(param, float)
return JSONResponse({"float": param})
assert isinstance(param, expected_types[param_type])
return JSONResponse("ok")

app = Router(routes=[Route("/{param:float}", endpoint=float_convertor)])
app = Router(routes=[Route(f"/{{param:{param_type}}}", endpoint=convertor)])

client = test_client_factory(app)
response = client.get(f"/{param}")
response = client.get(f"/{param_value}")
assert response.status_code == status_code

0 comments on commit cbc7f87

Please sign in to comment.