| 1 |
from unittest.mock import patch |
| 2 |
|
| 3 |
from ninja import NinjaAPI |
| 4 |
from ninja.testing import TestClient |
| 5 |
|
| 6 |
|
| 7 |
def test_examples(): |
| 8 |
api = NinjaAPI() |
| 9 |
|
| 10 |
with patch("builtins.api", api, create=True): |
| 11 |
import docs.src.tutorial.query.code01 |
| 12 |
import docs.src.tutorial.query.code010 |
| 13 |
import docs.src.tutorial.query.code02 |
| 14 |
import docs.src.tutorial.query.code03 |
| 15 |
|
| 16 |
client = TestClient(api) |
| 17 |
|
| 18 |
|
| 19 |
assert client.get("/weapons").json() == [ |
| 20 |
"Ninjato", |
| 21 |
"Shuriken", |
| 22 |
"Katana", |
| 23 |
"Kama", |
| 24 |
"Kunai", |
| 25 |
"Naginata", |
| 26 |
"Yari", |
| 27 |
] |
| 28 |
|
| 29 |
assert client.get("/weapons?offset=0&limit=3").json() == [ |
| 30 |
"Ninjato", |
| 31 |
"Shuriken", |
| 32 |
"Katana", |
| 33 |
] |
| 34 |
|
| 35 |
assert client.get("/weapons?offset=2&limit=2").json() == [ |
| 36 |
"Katana", |
| 37 |
"Kama", |
| 38 |
] |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
assert client.get("/weapons/search?offset=1&q=k").json() == [ |
| 43 |
"Katana", |
| 44 |
"Kama", |
| 45 |
"Kunai", |
| 46 |
] |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
assert client.get("/example?b=1").json() == [None, True, None, None] |
| 52 |
assert client.get("/example?b=True").json() == [None, True, None, None] |
| 53 |
assert client.get("/example?b=true").json() == [None, True, None, None] |
| 54 |
assert client.get("/example?b=on").json() == [None, True, None, None] |
| 55 |
assert client.get("/example?b=yes").json() == [None, True, None, None] |
| 56 |
assert client.get("/example?b=0").json() == [None, False, None, None] |
| 57 |
assert client.get("/example?b=no").json() == [None, False, None, None] |
| 58 |
assert client.get("/example?b=false").json() == [None, False, None, None] |
| 59 |
assert client.get("/example?d=1577836800").json() == [None, None, "2020-01-01", None] |
| 60 |
assert client.get("/example?d=2020-01-01").json() == [None, None, "2020-01-01", None] |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
assert client.get("/filter").json() == { |
| 66 |
"filters": { |
| 67 |
"limit": 100, |
| 68 |
"offset": None, |
| 69 |
"query": None, |
| 70 |
"category__in": None, |
| 71 |
} |
| 72 |
} |
| 73 |
assert client.get("/filter?limit=10").json() == { |
| 74 |
"filters": { |
| 75 |
"limit": 10, |
| 76 |
"offset": None, |
| 77 |
"query": None, |
| 78 |
"category__in": None, |
| 79 |
} |
| 80 |
} |
| 81 |
assert client.get("/filter?offset=10").json() == { |
| 82 |
"filters": {"limit": 100, "offset": 10, "query": None, "category__in": None} |
| 83 |
} |
| 84 |
assert client.get("/filter?query=10").json() == { |
| 85 |
"filters": { |
| 86 |
"limit": 100, |
| 87 |
"offset": None, |
| 88 |
"query": "10", |
| 89 |
"category__in": None, |
| 90 |
} |
| 91 |
} |
| 92 |
assert client.get("/filter?categories=a&categories=b").json() == { |
| 93 |
"filters": { |
| 94 |
"limit": 100, |
| 95 |
"offset": None, |
| 96 |
"query": None, |
| 97 |
"category__in": ["a", "b"], |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
schema = api.get_openapi_schema(path_prefix="") |
| 102 |
params = schema["paths"]["/filter"]["get"]["parameters"] |
| 103 |
|
| 104 |
assert params == [ |
| 105 |
{ |
| 106 |
"in": "query", |
| 107 |
"name": "limit", |
| 108 |
"schema": {"default": 100, "title": "Limit", "type": "integer"}, |
| 109 |
"required": False, |
| 110 |
}, |
| 111 |
{ |
| 112 |
"in": "query", |
| 113 |
"name": "offset", |
| 114 |
"schema": {"title": "Offset", "type": "integer"}, |
| 115 |
"required": False, |
| 116 |
}, |
| 117 |
{ |
| 118 |
"in": "query", |
| 119 |
"name": "query", |
| 120 |
"schema": {"title": "Query", "type": "string"}, |
| 121 |
"required": False, |
| 122 |
}, |
| 123 |
{ |
| 124 |
"in": "query", |
| 125 |
"name": "categories", |
| 126 |
"schema": { |
| 127 |
"items": {"type": "string"}, |
| 128 |
"title": "Categories", |
| 129 |
"type": "array", |
| 130 |
}, |
| 131 |
"required": False, |
| 132 |
}, |
| 133 |
] |