| 1 |
from unittest.mock import patch |
| 2 |
|
| 3 |
from ninja import NinjaAPI |
| 4 |
from ninja.testing import TestClient |
| 5 |
|
| 6 |
|
| 7 |
def test_examples(): |
| 8 |
api = NinjaAPI() |
| 9 |
|
| 10 |
with patch("builtins.api", api, create=True): |
| 11 |
import docs.src.tutorial.path.code01 |
| 12 |
|
| 13 |
client = TestClient(api) |
| 14 |
|
| 15 |
response = client.get("/items/123") |
| 16 |
assert response.json() == {"item_id": "123"} |
| 17 |
|
| 18 |
api = NinjaAPI() |
| 19 |
|
| 20 |
with patch("builtins.api", api, create=True): |
| 21 |
import docs.src.tutorial.path.code010 |
| 22 |
import docs.src.tutorial.path.code02 |
| 23 |
|
| 24 |
client = TestClient(api) |
| 25 |
|
| 26 |
response = client.get("/items/123") |
| 27 |
assert response.json() == {"item_id": 123} |
| 28 |
|
| 29 |
response = client.get("/events/2020/1/1") |
| 30 |
assert response.json() == {"date": "2020-01-01"} |
| 31 |
schema = api.get_openapi_schema(path_prefix="") |
| 32 |
events_params = schema["paths"]["/events/{year}/{month}/{day}"]["get"][ |
| 33 |
"parameters" |
| 34 |
] |
| 35 |
assert events_params == [ |
| 36 |
{ |
| 37 |
"in": "path", |
| 38 |
"name": "year", |
| 39 |
"schema": {"title": "Year", "type": "integer"}, |
| 40 |
"required": True, |
| 41 |
}, |
| 42 |
{ |
| 43 |
"in": "path", |
| 44 |
"name": "month", |
| 45 |
"schema": {"title": "Month", "type": "integer"}, |
| 46 |
"required": True, |
| 47 |
}, |
| 48 |
{ |
| 49 |
"in": "path", |
| 50 |
"name": "day", |
| 51 |
"schema": {"title": "Day", "type": "integer"}, |
| 52 |
"required": True, |
| 53 |
}, |
| 54 |
] |