| 1 |
import json |
| 2 |
import re |
| 3 |
from pathlib import Path |
| 4 |
|
| 5 |
import pytest |
| 6 |
from django.core.files.uploadedfile import SimpleUploadedFile |
| 7 |
from django.test import Client as DjangoTestClient |
| 8 |
|
| 9 |
from ninja.testing import TestClient as NinjaTestClient |
| 10 |
from tests.demo_project.multi_param.api import router |
| 11 |
|
| 12 |
ninja_client = NinjaTestClient(router) |
| 13 |
|
| 14 |
test_file = SimpleUploadedFile("test.txt", b"data123") |
| 15 |
|
| 16 |
expected_response = { |
| 17 |
"i": 1, |
| 18 |
"s": "a-str", |
| 19 |
"data": {"foo4": 44, "bar4": "44bar"}, |
| 20 |
"nested-data": { |
| 21 |
"foo": 11, |
| 22 |
"bar": "11bar", |
| 23 |
"d2": {"foo2": 22, "bar2": "22bar", "d3": {"foo3": 33, "bar3": "33bar"}}, |
| 24 |
}, |
| 25 |
} |
| 26 |
|
| 27 |
test_client_args = { |
| 28 |
"/test-multi-query": { |
| 29 |
"query": "i=1&s=a-str&foo4=44&bar4=44bar&foo=11&bar=11bar&foo2=22&bar2=22bar&foo3=33&bar3=33bar", |
| 30 |
}, |
| 31 |
"/test-multi-path/{i}/{s}/{foo4}/{bar4}/{foo}/{bar}/{foo2}/{bar2}/{foo3}/{bar3}/": { |
| 32 |
"path": "/test-multi-path/1/a-str/44/44bar/11/11bar/22/22bar/33/33bar/" |
| 33 |
}, |
| 34 |
"/test-multi-header": {"headers": {"i": 1, "foo": 11, "bar2": "22bar", "foo3": 33}}, |
| 35 |
"/test-multi-cookie": {"COOKIES": {"i": 1, "foo": 11, "bar2": "22bar", "foo3": 33}}, |
| 36 |
"/test-multi-form": {"POST": {"i": 1, "foo": 11, "bar2": "22bar", "foo3": 33}}, |
| 37 |
"/test-multi-body": {"json": expected_response}, |
| 38 |
"/test-multi-body-file": { |
| 39 |
"FILES": {"file": test_file}, |
| 40 |
"POST": { |
| 41 |
k: json.dumps(v) if isinstance(v, dict) else str(v) |
| 42 |
for k, v in expected_response.items() |
| 43 |
}, |
| 44 |
}, |
| 45 |
"/test-multi-form-file": { |
| 46 |
"FILES": {"file": test_file}, |
| 47 |
"POST": {"i": 1, "foo": 11, "bar2": "22bar", "foo3": 33}, |
| 48 |
}, |
| 49 |
"/test-multi-body-form": { |
| 50 |
"POST": { |
| 51 |
"i": "1", |
| 52 |
"foo": 11, |
| 53 |
"bar2": "22bar", |
| 54 |
"foo3": 33, |
| 55 |
"data": '{"foo4": 44, "bar4": "44bar"}', |
| 56 |
} |
| 57 |
}, |
| 58 |
"/test-multi-form-body": { |
| 59 |
"POST": { |
| 60 |
"i": 1, |
| 61 |
"nested-data": '{"foo": 11, "d2": {"bar2": "22bar", "d3": {"foo3": 33}}}', |
| 62 |
} |
| 63 |
}, |
| 64 |
"/test-multi-body-form-file": { |
| 65 |
"FILES": {"file": test_file}, |
| 66 |
"POST": { |
| 67 |
"i": "1", |
| 68 |
"foo": 11, |
| 69 |
"bar2": "22bar", |
| 70 |
"foo3": 33, |
| 71 |
"data": '{"foo4": 44, "bar4": "44bar"}', |
| 72 |
}, |
| 73 |
}, |
| 74 |
"/test-multi-form-body-file": { |
| 75 |
"FILES": {"file": test_file}, |
| 76 |
"POST": { |
| 77 |
"i": 1, |
| 78 |
"nested-data": '{"foo": 11, "d2": {"bar2": "22bar", "d3": {"foo3": 33}}}', |
| 79 |
}, |
| 80 |
}, |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
def test_validate_test_data(): |
| 85 |
ops = router.path_operations |
| 86 |
schema = DjangoTestClient().get("/api/mp/openapi.json").json() |
| 87 |
|
| 88 |
for path in tuple(schema["paths"]): |
| 89 |
schema["paths"]["/" + path.split("/", 3)[3]] = schema["paths"].pop(path) |
| 90 |
assert set(ops) == set( |
| 91 |
schema["paths"] |
| 92 |
), "Expect a test case for each endpoint on the API" |
| 93 |
|
| 94 |
fixture_dir = Path(__file__).parent / "schema_fixtures" |
| 95 |
fixture_files = { |
| 96 |
path: (fixture_dir / f"{path.split('/', 2)[1]}.json") for path in ops |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
for path, filename in fixture_files.items(): |
| 102 |
if 0: |
| 103 |
|
| 104 |
|
| 105 |
with Path(filename).open("w") as f: |
| 106 |
json.dump(schema["paths"][path], f, indent=2) |
| 107 |
with Path(filename).open() as f: |
| 108 |
data = json.load(f) |
| 109 |
path_schema = json.dumps(schema["paths"][path]) |
| 110 |
if "allOf" in path_schema: |
| 111 |
|
| 112 |
path_schema = re.sub( |
| 113 |
r'"allOf": \[\{"\$ref": "(.*?)"\}\]', r'"$ref": "\1"', path_schema |
| 114 |
) |
| 115 |
assert json.loads(path_schema) == data |
| 116 |
|
| 117 |
|
| 118 |
@pytest.mark.parametrize("path, client_args", tuple(test_client_args.items())) |
| 119 |
def test_data_round_trip_with_ninja_client(path, client_args): |
| 120 |
client_args = test_client_args[path] |
| 121 |
kwargs = {"path": path} |
| 122 |
for k in ("path", "headers", "COOKIES", "POST", "json", "FILES"): |
| 123 |
if k in client_args: |
| 124 |
kwargs[k] = client_args[k] |
| 125 |
|
| 126 |
query = client_args.get("query") |
| 127 |
if query: |
| 128 |
kwargs["path"] += f"?{query}" |
| 129 |
|
| 130 |
response = ninja_client.post(**kwargs) |
| 131 |
assert response.json() == expected_response |
| 132 |
assert response.status_code == 200 |
| 133 |
|
| 134 |
|
| 135 |
@pytest.mark.parametrize("path, client_args", tuple(test_client_args.items())) |
| 136 |
def test_data_round_trip_with_django_client(path, client_args): |
| 137 |
django_client = DjangoTestClient() |
| 138 |
|
| 139 |
client_args = test_client_args[path] |
| 140 |
kwargs = {"path": client_args.get("path", path)} |
| 141 |
|
| 142 |
if "headers" in client_args: |
| 143 |
for k, v in client_args["headers"].items(): |
| 144 |
kwargs[f"HTTP_{k.upper()}"] = v |
| 145 |
|
| 146 |
if "COOKIES" in client_args: |
| 147 |
django_client.cookies.load(client_args["COOKIES"]) |
| 148 |
|
| 149 |
if "POST" in client_args: |
| 150 |
kwargs["data"] = client_args["POST"] |
| 151 |
|
| 152 |
if "FILES" in client_args: |
| 153 |
if "data" in kwargs: |
| 154 |
kwargs["data"].update(client_args["FILES"]) |
| 155 |
else: |
| 156 |
kwargs["data"] = client_args["FILES"] |
| 157 |
|
| 158 |
if "json" in client_args: |
| 159 |
assert "data" not in kwargs |
| 160 |
kwargs["data"] = json.dumps(client_args["json"]) |
| 161 |
kwargs["content_type"] = "application/json" |
| 162 |
|
| 163 |
query = client_args.get("query") |
| 164 |
if query: |
| 165 |
kwargs["path"] += f"?{query}" |
| 166 |
|
| 167 |
kwargs["path"] = "/api/mp" + kwargs["path"] |
| 168 |
|
| 169 |
response = django_client.post(**kwargs) |
| 170 |
assert response.json() == expected_response |
| 171 |
assert response.status_code == 200 |