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