File: test_utils.py

package info (click to toggle)
django-ninja 1.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,280 kB
  • sloc: python: 16,041; javascript: 1,689; makefile: 40; sh: 25
file content (47 lines) | stat: -rw-r--r-- 1,223 bytes parent folder | download | duplicates (2)
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
import pytest

from ninja import NinjaAPI, Query
from ninja.utils import contribute_operation_args, replace_path_param_notation


@pytest.mark.parametrize(
    "input,expected_output",
    [
        ("abc/{def}", "abc/<def>"),
        ("abc/<def>", "abc/<def>"),
        ("abc", "abc"),
        ("<abc>", "<abc>"),
        ("{abc}", "<abc>"),
        ("{abc}/{def}", "<abc>/<def>"),
    ],
)
def test_replace_path_param_notation(input, expected_output):
    assert replace_path_param_notation(input) == expected_output


def test_contribute_operation_args():
    def some_func():
        pass

    contribute_operation_args(some_func, "arg1", str, Query(...))
    contribute_operation_args(some_func, "arg2", int, Query(...))

    api = NinjaAPI()

    api.get("/test")(some_func)

    schema = api.get_openapi_schema()
    assert schema["paths"]["/api/test"]["get"]["parameters"] == [
        {
            "in": "query",
            "name": "arg1",
            "schema": {"title": "Arg1", "type": "string"},
            "required": True,
        },
        {
            "in": "query",
            "name": "arg2",
            "schema": {"title": "Arg2", "type": "integer"},
            "required": True,
        },
    ]