| 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.body.code01 |
| 12 |
import docs.src.tutorial.body.code02 |
| 13 |
import docs.src.tutorial.body.code03 |
| 14 |
|
| 15 |
client = TestClient(api) |
| 16 |
|
| 17 |
assert client.post( |
| 18 |
"/items", json={"name": "Katana", "price": 299.00, "quantity": 10} |
| 19 |
).json() == { |
| 20 |
"name": "Katana", |
| 21 |
"description": None, |
| 22 |
"price": 299.0, |
| 23 |
"quantity": 10, |
| 24 |
} |
| 25 |
|
| 26 |
assert client.put( |
| 27 |
"/items/1", json={"name": "Katana", "price": 299.00, "quantity": 10} |
| 28 |
).json() == { |
| 29 |
"item_id": 1, |
| 30 |
"item": { |
| 31 |
"name": "Katana", |
| 32 |
"description": None, |
| 33 |
"price": 299.0, |
| 34 |
"quantity": 10, |
| 35 |
}, |
| 36 |
} |
| 37 |
|
| 38 |
assert client.post( |
| 39 |
"/items/1?q=test", json={"name": "Katana", "price": 299.00, "quantity": 10} |
| 40 |
).json() == { |
| 41 |
"item_id": 1, |
| 42 |
"q": "test", |
| 43 |
"item": { |
| 44 |
"name": "Katana", |
| 45 |
"description": None, |
| 46 |
"price": 299.0, |
| 47 |
"quantity": 10, |
| 48 |
}, |
| 49 |
} |