| 1 | import pytest |
| 2 | |
| 3 | from ninja import NinjaAPI, Query |
| 4 | from ninja.utils import contribute_operation_args, replace_path_param_notation |
| 5 | |
| 6 | |
| 7 | @pytest.mark.parametrize( |
| 8 | "input,expected_output", |
| 9 | [ |
| 10 | ("abc/{def}", "abc/<def>"), |
| 11 | ("abc/<def>", "abc/<def>"), |
| 12 | ("abc", "abc"), |
| 13 | ("<abc>", "<abc>"), |
| 14 | ("{abc}", "<abc>"), |
| 15 | ("{abc}/{def}", "<abc>/<def>"), |
| 16 | ], |
| 17 | ) |
| 18 | def test_replace_path_param_notation(input, expected_output): |
| 19 | assert replace_path_param_notation(input) == expected_output |
| 20 | |
| 21 | |
| 22 | def test_contribute_operation_args(): |
| 23 | def some_func(): |
| 24 | pass |
| 25 | |
| 26 | contribute_operation_args(some_func, "arg1", str, Query(...)) |
| 27 | contribute_operation_args(some_func, "arg2", int, Query(...)) |
| 28 | |
| 29 | api = NinjaAPI() |
| 30 | |
| 31 | api.get("/test")(some_func) |
| 32 | |
| 33 | schema = api.get_openapi_schema() |
| 34 | assert schema["paths"]["/api/test"]["get"]["parameters"] == [ |
| 35 | { |
| 36 | "in": "query", |
| 37 | "name": "arg1", |
| 38 | "schema": {"title": "Arg1", "type": "string"}, |
| 39 | "required": True, |
| 40 | }, |
| 41 | { |
| 42 | "in": "query", |
| 43 | "name": "arg2", |
| 44 | "schema": {"title": "Arg2", "type": "integer"}, |
| 45 | "required": True, |
| 46 | }, |
| 47 | ] |