| 1 |
import copy |
| 2 |
import uuid |
| 3 |
|
| 4 |
import pytest |
| 5 |
from pydantic import BaseModel |
| 6 |
|
| 7 |
from ninja import NinjaAPI |
| 8 |
from ninja.constants import NOT_SET |
| 9 |
from ninja.signature.details import is_pydantic_model |
| 10 |
from ninja.signature.utils import UUIDStrConverter |
| 11 |
from ninja.testing import TestClient |
| 12 |
|
| 13 |
|
| 14 |
def test_is_pydantic_model(): |
| 15 |
class Model(BaseModel): |
| 16 |
x: int |
| 17 |
|
| 18 |
assert is_pydantic_model(Model) |
| 19 |
assert is_pydantic_model("instance") is False |
| 20 |
|
| 21 |
|
| 22 |
def test_client(): |
| 23 |
"covering everything in testclient (including invalid paths)" |
| 24 |
api = NinjaAPI() |
| 25 |
client = TestClient(api) |
| 26 |
with pytest.raises(Exception): |
| 27 |
client.get("/404") |
| 28 |
|
| 29 |
|
| 30 |
def test_kwargs(): |
| 31 |
api = NinjaAPI() |
| 32 |
|
| 33 |
@api.get("/") |
| 34 |
def operation(request, a: str, *args, **kwargs): |
| 35 |
pass |
| 36 |
|
| 37 |
schema = api.get_openapi_schema() |
| 38 |
params = schema["paths"]["/api/"]["get"]["parameters"] |
| 39 |
print(params) |
| 40 |
assert params == [ |
| 41 |
{ |
| 42 |
"in": "query", |
| 43 |
"name": "a", |
| 44 |
"schema": {"title": "A", "type": "string"}, |
| 45 |
"required": True, |
| 46 |
} |
| 47 |
] |
| 48 |
|
| 49 |
|
| 50 |
def test_uuid_converter(): |
| 51 |
conv = UUIDStrConverter() |
| 52 |
assert isinstance(conv.to_url(uuid.uuid4()), str) |
| 53 |
|
| 54 |
|
| 55 |
def test_copy_not_set(): |
| 56 |
assert id(NOT_SET) == id(copy.copy(NOT_SET)) |
| 57 |
assert id(NOT_SET) == id(copy.deepcopy(NOT_SET)) |