File: test_misc.py

package info (click to toggle)
django-ninja 1.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,280 kB
  • sloc: python: 16,041; javascript: 1,689; makefile: 40; sh: 25
file content (57 lines) | stat: -rw-r--r-- 1,362 bytes parent folder | download
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
55
56
57
import copy
import uuid

import pytest
from pydantic import BaseModel

from ninja import NinjaAPI
from ninja.constants import NOT_SET
from ninja.signature.details import is_pydantic_model
from ninja.signature.utils import UUIDStrConverter
from ninja.testing import TestClient


def test_is_pydantic_model():
    class Model(BaseModel):
        x: int

    assert is_pydantic_model(Model)
    assert is_pydantic_model("instance") is False


def test_client():
    "covering everything in testclient (including invalid paths)"
    api = NinjaAPI()
    client = TestClient(api)
    with pytest.raises(Exception):  # noqa: B017
        client.get("/404")


def test_kwargs():
    api = NinjaAPI()

    @api.get("/")
    def operation(request, a: str, *args, **kwargs):
        pass

    schema = api.get_openapi_schema()
    params = schema["paths"]["/api/"]["get"]["parameters"]
    print(params)
    assert params == [  # Only `a` should be here, not kwargs
        {
            "in": "query",
            "name": "a",
            "schema": {"title": "A", "type": "string"},
            "required": True,
        }
    ]


def test_uuid_converter():
    conv = UUIDStrConverter()
    assert isinstance(conv.to_url(uuid.uuid4()), str)


def test_copy_not_set():
    assert id(NOT_SET) == id(copy.copy(NOT_SET))
    assert id(NOT_SET) == id(copy.deepcopy(NOT_SET))