File: test_query.py

package info (click to toggle)
django-ninja 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 11,280 kB
  • sloc: python: 15,956; javascript: 1,689; makefile: 39; sh: 25
file content (133 lines) | stat: -rw-r--r-- 4,251 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from unittest.mock import patch

from ninja import NinjaAPI
from ninja.testing import TestClient


def test_examples():
    api = NinjaAPI()

    with patch("builtins.api", api, create=True):
        import docs.src.tutorial.query.code01  # noqa: F401
        import docs.src.tutorial.query.code010  # noqa: F401
        import docs.src.tutorial.query.code02  # noqa: F401
        import docs.src.tutorial.query.code03  # noqa: F401

        client = TestClient(api)

        # Defaults
        assert client.get("/weapons").json() == [
            "Ninjato",
            "Shuriken",
            "Katana",
            "Kama",
            "Kunai",
            "Naginata",
            "Yari",
        ]

        assert client.get("/weapons?offset=0&limit=3").json() == [
            "Ninjato",
            "Shuriken",
            "Katana",
        ]

        assert client.get("/weapons?offset=2&limit=2").json() == [
            "Katana",
            "Kama",
        ]

        # Required/Optional

        assert client.get("/weapons/search?offset=1&q=k").json() == [
            "Katana",
            "Kama",
            "Kunai",
        ]

        # Coversion

        # fmt: off
        assert client.get("/example?b=1").json() == [None, True, None, None]
        assert client.get("/example?b=True").json() == [None, True, None, None]
        assert client.get("/example?b=true").json() == [None, True, None, None]
        assert client.get("/example?b=on").json() == [None, True, None, None]
        assert client.get("/example?b=yes").json() == [None, True, None, None]
        assert client.get("/example?b=0").json() == [None, False, None, None]
        assert client.get("/example?b=no").json() == [None, False, None, None]
        assert client.get("/example?b=false").json() == [None, False, None, None]
        assert client.get("/example?d=1577836800").json() == [None, None, "2020-01-01", None]
        assert client.get("/example?d=2020-01-01").json() == [None, None, "2020-01-01", None]
        # fmt: on

        # Schema

        assert client.get("/filter").json() == {
            "filters": {
                "limit": 100,
                "offset": None,
                "query": None,
                "category__in": None,
            }
        }
        assert client.get("/filter?limit=10").json() == {
            "filters": {
                "limit": 10,
                "offset": None,
                "query": None,
                "category__in": None,
            }
        }
        assert client.get("/filter?offset=10").json() == {
            "filters": {"limit": 100, "offset": 10, "query": None, "category__in": None}
        }
        assert client.get("/filter?query=10").json() == {
            "filters": {
                "limit": 100,
                "offset": None,
                "query": "10",
                "category__in": None,
            }
        }
        assert client.get("/filter?categories=a&categories=b").json() == {
            "filters": {
                "limit": 100,
                "offset": None,
                "query": None,
                "category__in": ["a", "b"],
            }
        }

        schema = api.get_openapi_schema(path_prefix="")
        params = schema["paths"]["/filter"]["get"]["parameters"]
        # print(params)
        assert params == [
            {
                "in": "query",
                "name": "limit",
                "schema": {"default": 100, "title": "Limit", "type": "integer"},
                "required": False,
            },
            {
                "in": "query",
                "name": "offset",
                "schema": {"title": "Offset", "type": "integer"},
                "required": False,
            },
            {
                "in": "query",
                "name": "query",
                "schema": {"title": "Query", "type": "string"},
                "required": False,
            },
            {
                "in": "query",
                "name": "categories",
                "schema": {
                    "items": {"type": "string"},
                    "title": "Categories",
                    "type": "array",
                },
                "required": False,
            },
        ]