Debian Codegraph @

tests/test_app.py

raw symbols SCIP
1 import contextlib
2 from pathlib import Path
3 from tempfile import NamedTemporaryFile
4
5 import pytest
6 from django.http import FileResponse, HttpResponse
7
8 from ninja import NinjaAPI
9 from ninja.testing import TestClient
10
11 api = NinjaAPI()
12
13 client = TestClient(api)
14
15 # TODO: check if you add operation to the same path - it should raise a ConfigError that this path already exist
16 # make sure to check how this will work with versioning
17 # and also check what will happen if you add same path in different routers
18 # api.add_router("", router1)
19 # api.add_router("", router2)
20 # and both routers have same path defined
21
22
23 @api.get("")
24 def emptypath(request):
25 return "/"
26
27
28 @api.get("/get")
29 def get(request):
30 return f"this is {request.method}"
31
32
33 @api.post("/post")
34 def post(request):
35 return f"this is {request.method}"
36
37
38 @api.put("/put")
39 def put(request):
40 return f"this is {request.method}"
41
42
43 @api.patch("/patch")
44 def patch(request):
45 return f"this is {request.method}"
46
47
48 @api.delete("/delete")
49 def delete(request):
50 return f"this is {request.method}"
51
52
53 @api.api_operation(["GET", "POST"], "/multi")
54 def multiple(request):
55 return f"this is {request.method}"
56
57
58 @api.get("/html")
59 def html(request):
60 return HttpResponse("html")
61
62
63 @api.get("/file")
64 def file_response(request):
65 tmp = NamedTemporaryFile(delete=False)
66 try:
67 p = Path(tmp.name)
68 p.write_bytes(b"this is a file")
69 return FileResponse(Path(tmp.name).open("rb"))
70 finally:
71 with contextlib.suppress(PermissionError):
72 Path(tmp.name).unlink()
73
74
75 @pytest.mark.parametrize(
76 "method,path,expected_status,expected_data,expected_streaming",
77 [
78 ("get", "/", 200, "/", False),
79 ("get", "/get", 200, "this is GET", False),
80 ("post", "/post", 200, "this is POST", False),
81 ("put", "/put", 200, "this is PUT", False),
82 ("patch", "/patch", 200, "this is PATCH", False),
83 ("delete", "/delete", 200, "this is DELETE", False),
84 ("get", "/multi", 200, "this is GET", False),
85 ("post", "/multi", 200, "this is POST", False),
86 ("patch", "/multi", 405, b"Method not allowed", False),
87 ("get", "/html", 200, b"html", False),
88 ("get", "/file", 200, b"this is a file", True),
89 ],
90 )
91 def test_method(method, path, expected_status, expected_data, expected_streaming):
92 func = getattr(client, method)
93 response = func(path)
94 assert response.status_code == expected_status
95 assert response.streaming == expected_streaming
96 try:
97 data = response.json()
98 except Exception:
99 data = response.content
100 assert data == expected_data
101
102
103 def test_validates():
104 # Registry check was removed - routers are now independent templates
105 # that can be reused across multiple APIs without conflicts
106 # This test now just verifies that creating an API and accessing urls works
107 api2 = NinjaAPI(urls_namespace="test-validates-api")
108 _ = api2.urls # Should not raise