| 1 |
import json |
| 2 |
from enum import Enum |
| 3 |
from ipaddress import IPv4Address, IPv6Address |
| 4 |
from typing import List, Union |
| 5 |
|
| 6 |
import pytest |
| 7 |
from django.http import HttpResponse |
| 8 |
from pydantic import BaseModel, HttpUrl, ValidationError |
| 9 |
from pydantic_core import Url |
| 10 |
|
| 11 |
from ninja import Router |
| 12 |
from ninja.responses import Response |
| 13 |
from ninja.testing import TestClient |
| 14 |
|
| 15 |
router = Router() |
| 16 |
|
| 17 |
|
| 18 |
@router.get("/check_int", response=int) |
| 19 |
def check_int(request): |
| 20 |
return "1" |
| 21 |
|
| 22 |
|
| 23 |
@router.get("/check_int2", response=int) |
| 24 |
def check_int2(request): |
| 25 |
return "str" |
| 26 |
|
| 27 |
|
| 28 |
class User: |
| 29 |
def __init__(self, id, user_name, password): |
| 30 |
self.id = id |
| 31 |
self.user_name = user_name |
| 32 |
self.password = password |
| 33 |
|
| 34 |
|
| 35 |
class MyEnum(Enum): |
| 36 |
first = "first" |
| 37 |
second = "second" |
| 38 |
|
| 39 |
|
| 40 |
def to_camel(string: str) -> str: |
| 41 |
words = string.split("_") |
| 42 |
return words[0].lower() + "".join(word.capitalize() for word in words[1:]) |
| 43 |
|
| 44 |
|
| 45 |
class UserModel(BaseModel): |
| 46 |
id: int |
| 47 |
user_name: str |
| 48 |
|
| 49 |
|
| 50 |
model_config = dict( |
| 51 |
from_attributes=True, |
| 52 |
alias_generator=to_camel, |
| 53 |
populate_by_name=True, |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
@router.get("/check_model", response=UserModel) |
| 58 |
def check_model(request): |
| 59 |
return User(1, "John", "Password") |
| 60 |
|
| 61 |
|
| 62 |
@router.get("/check_list_model", response=List[UserModel]) |
| 63 |
def check_list_model(request): |
| 64 |
return [User(1, "John", "Password")] |
| 65 |
|
| 66 |
|
| 67 |
@router.get("/check_model_alias", response=UserModel, by_alias=True) |
| 68 |
def check_model_alias(request): |
| 69 |
return User(1, "John", "Password") |
| 70 |
|
| 71 |
|
| 72 |
@router.get("/check_union", response=Union[int, UserModel]) |
| 73 |
def check_union(request, q: int): |
| 74 |
if q == 0: |
| 75 |
return 1 |
| 76 |
if q == 1: |
| 77 |
return User(1, "John", "Password") |
| 78 |
return "invalid" |
| 79 |
|
| 80 |
|
| 81 |
@router.get("/check_set_header") |
| 82 |
def check_set_header(request, response: HttpResponse): |
| 83 |
response["Cache-Control"] = "no-cache" |
| 84 |
return 1 |
| 85 |
|
| 86 |
|
| 87 |
@router.get("/check_set_cookie") |
| 88 |
def check_set_cookie(request, set: bool, response: HttpResponse): |
| 89 |
if set: |
| 90 |
response.set_cookie("test", "me") |
| 91 |
return 1 |
| 92 |
|
| 93 |
|
| 94 |
@router.get("/check_del_cookie") |
| 95 |
def check_del_cookie(request, response: HttpResponse): |
| 96 |
response.delete_cookie("test") |
| 97 |
return 1 |
| 98 |
|
| 99 |
|
| 100 |
client = TestClient(router) |
| 101 |
|
| 102 |
|
| 103 |
@pytest.mark.parametrize( |
| 104 |
"path,expected_response", |
| 105 |
[ |
| 106 |
("/check_int", 1), |
| 107 |
("/check_model", {"id": 1, "user_name": "John"}), |
| 108 |
( |
| 109 |
"/check_list_model", |
| 110 |
[{"id": 1, "user_name": "John"}], |
| 111 |
), |
| 112 |
("/check_model", {"id": 1, "user_name": "John"}), |
| 113 |
("/check_model_alias", {"id": 1, "userName": "John"}), |
| 114 |
("/check_union?q=0", 1), |
| 115 |
("/check_union?q=1", {"id": 1, "user_name": "John"}), |
| 116 |
], |
| 117 |
) |
| 118 |
def test_responses(path, expected_response): |
| 119 |
response = client.get(path) |
| 120 |
assert response.status_code == 200, response.content |
| 121 |
assert response.json() == expected_response |
| 122 |
assert response.data == response.data == expected_response |
| 123 |
|
| 124 |
|
| 125 |
def test_validates(): |
| 126 |
with pytest.raises(ValidationError): |
| 127 |
client.get("/check_int2") |
| 128 |
|
| 129 |
with pytest.raises(ValidationError): |
| 130 |
client.get("/check_union?q=2") |
| 131 |
|
| 132 |
|
| 133 |
def test_set_header(): |
| 134 |
response = client.get("/check_set_header") |
| 135 |
assert response.status_code == 200 |
| 136 |
assert response.content == b"1" |
| 137 |
assert response["Cache-Control"] == "no-cache" |
| 138 |
|
| 139 |
|
| 140 |
def test_set_cookie(): |
| 141 |
response = client.get("/check_set_cookie?set=0") |
| 142 |
assert "test" not in response.cookies |
| 143 |
|
| 144 |
response = client.get("/check_set_cookie?set=1") |
| 145 |
cookie = response.cookies.get("test") |
| 146 |
assert cookie |
| 147 |
assert cookie.value == "me" |
| 148 |
|
| 149 |
|
| 150 |
def test_del_cookie(): |
| 151 |
response = client.get("/check_del_cookie") |
| 152 |
cookie = response.cookies.get("test") |
| 153 |
assert cookie |
| 154 |
assert cookie["expires"] == "Thu, 01 Jan 1970 00:00:00 GMT" |
| 155 |
assert cookie["max-age"] == 0 |
| 156 |
|
| 157 |
|
| 158 |
def test_ipv4address_encoding(): |
| 159 |
data = {"ipv4": IPv4Address("127.0.0.1")} |
| 160 |
response = Response(data) |
| 161 |
response_data = json.loads(response.content) |
| 162 |
assert response_data["ipv4"] == str(data["ipv4"]) |
| 163 |
|
| 164 |
|
| 165 |
def test_ipv6address_encoding(): |
| 166 |
data = {"ipv6": IPv6Address("::1")} |
| 167 |
response = Response(data) |
| 168 |
response_data = json.loads(response.content) |
| 169 |
assert response_data["ipv6"] == str(data["ipv6"]) |
| 170 |
|
| 171 |
|
| 172 |
def test_enum_encoding(): |
| 173 |
data = {"enum": MyEnum.first} |
| 174 |
response = Response(data) |
| 175 |
response_data = json.loads(response.content) |
| 176 |
assert response_data["enum"] == str(data["enum"]) |
| 177 |
|
| 178 |
|
| 179 |
def test_pydantic_url(): |
| 180 |
data = {"url": Url("https://django-ninja.dev/")} |
| 181 |
response = Response(data) |
| 182 |
response_data = json.loads(response.content) |
| 183 |
assert response_data == {"url": "https://django-ninja.dev/"} |
| 184 |
|
| 185 |
|
| 186 |
def test_pydantic_httpurl(): |
| 187 |
data = {"url": HttpUrl("https://django-ninja.dev/")} |
| 188 |
response = Response(data) |
| 189 |
response_data = json.loads(response.content) |
| 190 |
assert response_data == {"url": "https://django-ninja.dev/"} |
| 191 |
|
| 192 |
|
| 193 |
class HttpUrlSchema(BaseModel): |
| 194 |
url: HttpUrl |
| 195 |
|
| 196 |
|
| 197 |
@router.get("/check_httpurl", response=HttpUrlSchema) |
| 198 |
def check_httpurl(request): |
| 199 |
return HttpUrlSchema(url="https://django-ninja.dev/") |
| 200 |
|
| 201 |
|
| 202 |
def test_pydantic_httpurl_schema(): |
| 203 |
response = client.get("/check_httpurl") |
| 204 |
assert response.status_code == 200 |
| 205 |
assert response.json() == {"url": "https://django-ninja.dev/"} |