| 1 |
import sys |
| 2 |
from unittest.mock import patch |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from ninja import NinjaAPI |
| 7 |
from ninja.testing import TestClient |
| 8 |
|
| 9 |
|
| 10 |
def test_examples(): |
| 11 |
api = NinjaAPI() |
| 12 |
|
| 13 |
with patch("builtins.api", api, create=True): |
| 14 |
import docs.src.tutorial.form.code01 |
| 15 |
import docs.src.tutorial.form.code02 |
| 16 |
|
| 17 |
client = TestClient(api) |
| 18 |
|
| 19 |
assert client.post( |
| 20 |
"/items", data={"name": "Katana", "price": 299.00, "quantity": 10} |
| 21 |
).json() == { |
| 22 |
"name": "Katana", |
| 23 |
"description": None, |
| 24 |
"price": 299.0, |
| 25 |
"quantity": 10, |
| 26 |
} |
| 27 |
|
| 28 |
assert client.post( |
| 29 |
"/items/1?q=test", data={"name": "Katana", "price": 299.00, "quantity": 10} |
| 30 |
).json() == { |
| 31 |
"item_id": 1, |
| 32 |
"q": "test", |
| 33 |
"item": { |
| 34 |
"name": "Katana", |
| 35 |
"description": None, |
| 36 |
"price": 299.0, |
| 37 |
"quantity": 10, |
| 38 |
}, |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.skipif(sys.version_info[:2] < (3, 9), reason="requires py3.9+") |
| 43 |
def test_examples_extra(): |
| 44 |
api = NinjaAPI() |
| 45 |
|
| 46 |
with patch("builtins.api", api, create=True): |
| 47 |
import docs.src.tutorial.form.code03 |
| 48 |
|
| 49 |
client = TestClient(api) |
| 50 |
|
| 51 |
assert client.post( |
| 52 |
"/items-blank-default", |
| 53 |
data={"name": "Katana", "price": "", "quantity": "", "in_stock": ""}, |
| 54 |
).json() == { |
| 55 |
"name": "Katana", |
| 56 |
"description": None, |
| 57 |
"in_stock": True, |
| 58 |
"price": 0.0, |
| 59 |
"quantity": 0, |
| 60 |
} |