| 1 | import sys |
| 2 | from datetime import datetime |
| 3 | from enum import IntEnum |
| 4 | from typing import Optional |
| 5 | |
| 6 | import pytest |
| 7 | from pydantic import BaseModel, Field |
| 8 | |
| 9 | from ninja import NinjaAPI, Query, Schema |
| 10 | from ninja.testing.client import TestClient |
| 11 | |
| 12 | PY_310 = sys.version_info >= (3, 10) |
| 13 | |
| 14 | |
| 15 | class Range(IntEnum): |
| 16 | TWENTY = 20 |
| 17 | FIFTY = 50 |
| 18 | TWO_HUNDRED = 200 |
| 19 | |
| 20 | |
| 21 | class Filter(BaseModel): |
| 22 | to_datetime: datetime = Field(alias="to") |
| 23 | from_datetime: datetime = Field(alias="from") |
| 24 | range: Range = Range.TWENTY |
| 25 | |
| 26 | |
| 27 | class Data(Schema): |
| 28 | an_int: int = Field(alias="int", default=0) |
| 29 | a_float: float = Field(alias="float", default=1.5) |
| 30 | |
| 31 | |
| 32 | api = NinjaAPI() |
| 33 | |
| 34 | |
| 35 | @api.get("/test") |
| 36 | def query_params_schema(request, filters: Filter = Query(...)): |
| 37 | return filters.model_dump() |
| 38 | |
| 39 | |
| 40 | @api.get("/test-mixed") |
| 41 | def query_params_mixed_schema( |
| 42 | request, |
| 43 | query1: int, |
| 44 | query2: int = 5, |
| 45 | filters: Filter = Query(...), |
| 46 | data: Data = Query(...), |
| 47 | ): |
| 48 | return dict( |
| 49 | query1=query1, |
| 50 | query2=query2, |
| 51 | filters=filters.model_dump(), |
| 52 | data=data.model_dump(), |
| 53 | ) |
| 54 | |
| 55 | |
| 56 | def test_request(): |
| 57 | client = TestClient(api) |
| 58 | response = client.get("/test?from=1&to=2&range=20&foo=1&range2=50") |
| 59 | print("!", response.json()) |
| 60 | assert response.json() == { |
| 61 | "to_datetime": "1970-01-01T00:00:02Z", |
| 62 | "from_datetime": "1970-01-01T00:00:01Z", |
| 63 | "range": 20, |
| 64 | } |
| 65 | |
| 66 | response = client.get("/test?from=1&to=2&range=21") |
| 67 | assert response.status_code == 422 |
| 68 | |
| 69 | |
| 70 | def test_request_mixed(): |
| 71 | client = TestClient(api) |
| 72 | response = client.get( |
| 73 | "/test-mixed?from=1&to=2&range=20&foo=1&range2=50&query1=2&int=3&float=1.6" |
| 74 | ) |
| 75 | print(response.json()) |
| 76 | assert response.json() == { |
| 77 | "data": {"a_float": 1.6, "an_int": 3}, |
| 78 | "filters": { |
| 79 | "from_datetime": "1970-01-01T00:00:01Z", |
| 80 | "range": 20, |
| 81 | "to_datetime": "1970-01-01T00:00:02Z", |
| 82 | }, |
| 83 | "query1": 2, |
| 84 | "query2": 5, |
| 85 | } |
| 86 | |
| 87 | response = client.get( |
| 88 | "/test-mixed?from=1&to=2&range=20&foo=1&range2=50&query1=2&query2=10" |
| 89 | ) |
| 90 | print(response.json()) |
| 91 | assert response.json() == { |
| 92 | "data": {"a_float": 1.5, "an_int": 0}, |
| 93 | "filters": { |
| 94 | "from_datetime": "1970-01-01T00:00:01Z", |
| 95 | "range": 20, |
| 96 | "to_datetime": "1970-01-01T00:00:02Z", |
| 97 | }, |
| 98 | "query1": 2, |
| 99 | "query2": 10, |
| 100 | } |
| 101 | |
| 102 | response = client.get("/test-mixed?from=1&to=2") |
| 103 | assert response.status_code == 422 |
| 104 | |
| 105 | |
| 106 | def test_request_query_params_using_basemodel(): |
| 107 | class Foo(BaseModel): |
| 108 | start: int |
| 109 | optional: int = 42 |
| 110 | |
| 111 | temp_api = NinjaAPI() |
| 112 | |
| 113 | @temp_api.get("/foo") |
| 114 | def view(request, foo: Foo = Query(...)): |
| 115 | return foo.model_dump() |
| 116 | |
| 117 | client = TestClient(temp_api) |
| 118 | resp = client.get("/foo?start=1") |
| 119 | |
| 120 | assert resp.status_code == 200 |
| 121 | assert resp.json() == {"start": 1, "optional": 42} |
| 122 | |
| 123 | |
| 124 | def test_schema(): |
| 125 | schema = api.get_openapi_schema() |
| 126 | params = schema["paths"]["/api/test"]["get"]["parameters"] |
| 127 | print(params) |
| 128 | assert params == [ |
| 129 | { |
| 130 | "in": "query", |
| 131 | "name": "to", |
| 132 | "schema": {"format": "date-time", "title": "To", "type": "string"}, |
| 133 | "required": True, |
| 134 | }, |
| 135 | { |
| 136 | "in": "query", |
| 137 | "name": "from", |
| 138 | "schema": {"format": "date-time", "title": "From", "type": "string"}, |
| 139 | "required": True, |
| 140 | }, |
| 141 | { |
| 142 | "in": "query", |
| 143 | "name": "range", |
| 144 | "schema": { |
| 145 | "allOf": [{"enum": [20, 50, 200], "title": "Range", "type": "integer"}], |
| 146 | "default": 20, |
| 147 | }, |
| 148 | "required": False, |
| 149 | }, |
| 150 | ] |
| 151 | |
| 152 | |
| 153 | def test_schema_all_of_no_ref(): |
| 154 | details = { |
| 155 | "default": 1, |
| 156 | "allOf": [ |
| 157 | {"$ref": "#/components/schemas/Type"}, |
| 158 | {"no-ref-here": "xyzzy"}, |
| 159 | ], |
| 160 | } |
| 161 | definitions = {"Type": {"title": "Best Type Ever!"}} |
| 162 | |
| 163 | from ninja.openapi.schema import resolve_allOf |
| 164 | |
| 165 | resolve_allOf(details, definitions) |
| 166 | |
| 167 | assert details == { |
| 168 | "default": 1, |
| 169 | "allOf": [{"title": "Best Type Ever!"}, {"no-ref-here": "xyzzy"}], |
| 170 | } |
| 171 | |
| 172 | |
| 173 | @pytest.mark.skipif(not PY_310, reason="requires Python 3.10+ pipe syntax") |
| 174 | def test_unwrap_union_model_no_model(): |
| 175 | """_unwrap_union_model returns input as-is when union has no pydantic model.""" |
| 176 | from ninja.signature.details import _unwrap_union_model |
| 177 | |
| 178 | annotation = int | None |
| 179 | assert _unwrap_union_model(annotation) is annotation |
| 180 | assert _unwrap_union_model(str) is str |
| 181 | |
| 182 | |
| 183 | def test_optional_query_schema(): |
| 184 | """Optional[Model] in Query should not crash (issue #1634).""" |
| 185 | |
| 186 | class MyFilter(Schema): |
| 187 | name: str = "default" |
| 188 | |
| 189 | temp_api = NinjaAPI() |
| 190 | |
| 191 | @temp_api.get("/opt") |
| 192 | def view(request, f: Optional[MyFilter] = Query(None)): |
| 193 | if f: |
| 194 | return f.model_dump() |
| 195 | return {} |
| 196 | |
| 197 | client = TestClient(temp_api) |
| 198 | |
| 199 | resp = client.get("/opt?name=hello") |
| 200 | assert resp.status_code == 200 |
| 201 | assert resp.json() == {"name": "hello"} |
| 202 | |
| 203 | resp = client.get("/opt") |
| 204 | assert resp.status_code == 200 |
| 205 | |
| 206 | |
| 207 | @pytest.mark.skipif(not PY_310, reason="requires Python 3.10+ pipe syntax") |
| 208 | def test_union_pipe_syntax_query_schema(): |
| 209 | """Model | None in Query should not crash (issue #1634, Python 3.10+ pipe syntax).""" |
| 210 | |
| 211 | class MyFilter(Schema): |
| 212 | name: str = "default" |
| 213 | |
| 214 | temp_api = NinjaAPI() |
| 215 | |
| 216 | @temp_api.get("/pipe") |
| 217 | def view(request, f: MyFilter | None = Query(None)): |
| 218 | if f: |
| 219 | return f.model_dump() |
| 220 | return {} |
| 221 | |
| 222 | client = TestClient(temp_api) |
| 223 | |
| 224 | resp = client.get("/pipe?name=world") |
| 225 | assert resp.status_code == 200 |
| 226 | assert resp.json() == {"name": "world"} |
| 227 | |
| 228 | resp = client.get("/pipe") |
| 229 | assert resp.status_code == 200 |
| 230 | |
| 231 | |
| 232 | @pytest.mark.skipif(not PY_310, reason="requires Python 3.10+ pipe syntax") |
| 233 | def test_nested_optional_query_schema(): |
| 234 | """Nested optional model fields should also work.""" |
| 235 | |
| 236 | class Inner(Schema): |
| 237 | value: Optional[int] = 0 |
| 238 | items: list[str] = [] |
| 239 | |
| 240 | class Outer(Schema): |
| 241 | inner: Inner | None = None |
| 242 | label: str = "x" |
| 243 | |
| 244 | temp_api = NinjaAPI() |
| 245 | |
| 246 | @temp_api.get("/nested") |
| 247 | def view(request, f: Outer = Query(...)): |
| 248 | return f.model_dump() |
| 249 | |
| 250 | client = TestClient(temp_api) |
| 251 | |
| 252 | resp = client.get("/nested?label=test") |
| 253 | assert resp.status_code == 200 |
| 254 | assert resp.json()["label"] == "test" |