Debian Codegraph @

tests/test_test_client.py

raw symbols SCIP
1 from datetime import datetime
2 from http import HTTPStatus
3 from unittest import mock
4
5 import pytest
6 from django.utils import timezone
7
8 from ninja import Router
9 from ninja.schema import Schema
10 from ninja.testing import TestClient
11
12 router = Router()
13
14
15 @router.get("/request/build_absolute_uri")
16 def request_build_absolute_uri(request):
17 return request.build_absolute_uri()
18
19
20 @router.get("/request/build_absolute_uri/location")
21 def request_build_absolute_uri_location(request):
22 return request.build_absolute_uri("location")
23
24
25 @router.get("/test")
26 def simple_get(request):
27 return "test"
28
29
30 @router.get("/test-headers")
31 def get_headers(request):
32 return dict(request.headers)
33
34
35 @router.get("/test-cookies")
36 def get_cookies(request):
37 return dict(request.COOKIES)
38
39
40 client = TestClient(router)
41
42
43 @pytest.mark.parametrize(
44 "path,expected_status,expected_response",
45 [
46 ("/request/build_absolute_uri", HTTPStatus.OK, "http://testlocation/"),
47 (
48 "/request/build_absolute_uri/location",
49 HTTPStatus.OK,
50 "http://testlocation/location",
51 ),
52 ],
53 )
54 def test_sync_build_absolute_uri(path, expected_status, expected_response):
55 response = client.get(path)
56
57 assert response.status_code == expected_status
58 assert response.json() == expected_response
59
60
61 class ClientTestSchema(Schema):
62 time: datetime
63
64
65 def test_schema_as_data():
66 schema_instance = ClientTestSchema(time=timezone.now().replace(microsecond=0))
67
68 with mock.patch.object(client, "_call") as call:
69 client.post("/test", json=schema_instance)
70 request = call.call_args[0][1]
71 assert (
72 ClientTestSchema.model_validate_json(request.body).model_dump_json()
73 == schema_instance.model_dump_json()
74 )
75
76
77 def test_json_as_body():
78 schema_instance = ClientTestSchema(time=timezone.now().replace(microsecond=0))
79
80 with mock.patch.object(client, "_call") as call:
81 client.post(
82 "/test",
83 data=schema_instance.model_dump_json(),
84 content_type="application/json",
85 )
86 request = call.call_args[0][1]
87 assert (
88 ClientTestSchema.model_validate_json(request.body).model_dump_json()
89 == schema_instance.model_dump_json()
90 )
91
92
93 headered_client = TestClient(router, headers={"A": "a", "B": "b"})
94
95
96 def test_client_request_only_header():
97 r = client.get("/test-headers", headers={"A": "na"})
98 assert r.json() == {"A": "na"}
99
100
101 def test_headered_client_request_with_default_headers():
102 r = headered_client.get("/test-headers")
103 assert r.json() == {"A": "a", "B": "b"}
104
105
106 def test_headered_client_request_with_overwritten_and_additional_headers():
107 r = headered_client.get("/test-headers", headers={"A": "na", "C": "nc"})
108 assert r.json() == {"A": "na", "B": "b", "C": "nc"}
109
110
111 cookied_client = TestClient(router, COOKIES={"A": "a", "B": "b"})
112
113
114 def test_client_request_only_cookies():
115 r = client.get("/test-cookies", COOKIES={"A": "na"})
116 assert r.json() == {"A": "na"}
117
118
119 def test_headered_client_request_with_default_cookies():
120 r = cookied_client.get("/test-cookies")
121 assert r.json() == {"A": "a", "B": "b"}
122
123
124 def test_headered_client_request_with_overwritten_and_additional_cookies():
125 r = cookied_client.get("/test-cookies", COOKIES={"A": "na", "C": "nc"})
126 assert r.json() == {"A": "na", "B": "b", "C": "nc"}