File: test_router_path_params.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 (83 lines) | stat: -rw-r--r-- 2,791 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import pytest

from ninja import NinjaAPI, Path, Router
from ninja.testing import TestClient

api = NinjaAPI()
router_with_path_type = Router()
router_without_path_type = Router()
router_with_multiple = Router()


@router_with_path_type.get("/metadata")
def get_item_metadata(request, item_id: int = Path(None)) -> int:
    return item_id


@router_without_path_type.get("/")
def get_item_metadata_2(request, item_id: str = Path(None)) -> str:
    return item_id


@router_without_path_type.get("/metadata")
def get_item_metadata_3(request, item_id: str = Path(None)) -> str:
    return item_id


@router_without_path_type.get("/")
def get_item_metadata_4(request, item_id: str = Path(None)) -> str:
    return item_id


@router_with_multiple.get("/metadata/{kind}")
def get_item_metadata_5(
    request, item_id: int = Path(None), name: str = Path(None), kind: str = Path(None)
) -> str:
    return f"{item_id} {name} {kind}"


api.add_router("/with_type/{int:item_id}", router_with_path_type)
api.add_router("/without_type/{item_id}", router_without_path_type)
api.add_router("/with_multiple/{int:item_id}/name/{name}", router_with_multiple)

client = TestClient(api)


@pytest.mark.parametrize(
    "path,expected_status,expected_response",
    [
        ("/with_type/1/metadata", 200, 1),
        ("/without_type/1/metadata", 200, "1"),
        ("/without_type/abc/metadata", 200, "abc"),
        ("/with_multiple/99/name/foo/metadata/timestamp", 200, "99 foo timestamp"),
    ],
)
def test_router_with_path_params(path, expected_status, expected_response):
    response = client.get(path)
    assert response.status_code == expected_status
    assert response.json() == expected_response


@pytest.mark.parametrize(
    "path,expected_exception,expect_exception_contains",
    [
        ("/with_type/abc/metadata", Exception, "Cannot resolve"),
        ("/with_type//metadata", Exception, "Cannot resolve"),
        ("/with_type/null/metadata", Exception, "Cannot resolve"),
        ("/with_type", Exception, "Cannot resolve"),
        ("/with_type/", Exception, "Cannot resolve"),
        ("/with_type//", Exception, "Cannot resolve"),
        ("/with_type/null", Exception, "Cannot resolve"),
        ("/with_type/null/", Exception, "Cannot resolve"),
        ("/without_type", Exception, "Cannot resolve"),
        ("/without_type/", Exception, "Cannot resolve"),
        ("/without_type//", Exception, "Cannot resolve"),
        ("/with_multiple/abc/name/foo/metadata/timestamp", Exception, "Cannot resolve"),
        ("/with_multiple/99", Exception, "Cannot resolve"),
    ],
)
def test_router_with_path_params_nomatch(
    path, expected_exception, expect_exception_contains
):
    with pytest.raises(expected_exception, match=expect_exception_contains):
        client.get(path)