| 1 |
from functools import wraps |
| 2 |
from unittest import mock |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from ninja import Router |
| 7 |
from ninja.testing import TestClient |
| 8 |
|
| 9 |
router = Router() |
| 10 |
client = TestClient(router) |
| 11 |
|
| 12 |
|
| 13 |
def a_good_test_wrapper(f): |
| 14 |
"""Validate that decorators using functools.wraps(), work as expected""" |
| 15 |
|
| 16 |
@wraps(f) |
| 17 |
def wrapper(*args, **kwargs): |
| 18 |
return f(*args, **kwargs) |
| 19 |
|
| 20 |
return wrapper |
| 21 |
|
| 22 |
|
| 23 |
def a_bad_test_wrapper(f): |
| 24 |
"""Validate that decorators failing to using functools.wraps(), fail""" |
| 25 |
|
| 26 |
def wrapper(*args, **kwargs): |
| 27 |
return f(*args, **kwargs) |
| 28 |
|
| 29 |
return wrapper |
| 30 |
|
| 31 |
|
| 32 |
@router.get("/text") |
| 33 |
@a_good_test_wrapper |
| 34 |
def get_text( |
| 35 |
request, |
| 36 |
): |
| 37 |
return "Hello World" |
| 38 |
|
| 39 |
|
| 40 |
@router.get("/path/{item_id}") |
| 41 |
@a_good_test_wrapper |
| 42 |
def get_id(request, item_id): |
| 43 |
return item_id |
| 44 |
|
| 45 |
|
| 46 |
@router.get("/query") |
| 47 |
@a_good_test_wrapper |
| 48 |
def get_query_type(request, query: int): |
| 49 |
return f"foo bar {query}" |
| 50 |
|
| 51 |
|
| 52 |
@router.get("/path-query/{item_id}") |
| 53 |
@a_good_test_wrapper |
| 54 |
def get_query_id(request, item_id, query: int): |
| 55 |
return f"foo bar {item_id} {query}" |
| 56 |
|
| 57 |
|
| 58 |
@router.get("/text-bad") |
| 59 |
@a_bad_test_wrapper |
| 60 |
def get_text_bad(request): |
| 61 |
return "Hello World" |
| 62 |
|
| 63 |
|
| 64 |
with mock.patch("ninja.signature.details.warnings.warn_explicit"): |
| 65 |
|
| 66 |
@router.get("/path-bad/{item_id}") |
| 67 |
@a_bad_test_wrapper |
| 68 |
def get_id_bad(request, item_id): |
| 69 |
return item_id |
| 70 |
|
| 71 |
|
| 72 |
@router.get("/query-bad") |
| 73 |
@a_bad_test_wrapper |
| 74 |
def get_query_type_bad(request, query: int): |
| 75 |
return f"foo bar {query}" |
| 76 |
|
| 77 |
|
| 78 |
with mock.patch("ninja.signature.details.warnings.warn_explicit"): |
| 79 |
|
| 80 |
@router.get("/path-query-bad/{item_id}") |
| 81 |
@a_bad_test_wrapper |
| 82 |
def get_query_id_bad(request, item_id, query: int): |
| 83 |
return f"foo bar {item_id} {query}" |
| 84 |
|
| 85 |
|
| 86 |
@pytest.mark.parametrize( |
| 87 |
"path,expected_status,expected_response", |
| 88 |
[ |
| 89 |
("/text", 200, "Hello World"), |
| 90 |
("/path/id", 200, "id"), |
| 91 |
("/query?query=1", 200, "foo bar 1"), |
| 92 |
("/path-query/id?query=2", 200, "foo bar id 2"), |
| 93 |
("/text-bad", 200, "Hello World"), |
| 94 |
("/path-bad/id", None, TypeError), |
| 95 |
("/query-bad?query=1", None, TypeError), |
| 96 |
("/path-query-bad/id?query=2", None, TypeError), |
| 97 |
], |
| 98 |
) |
| 99 |
def test_get_path(path, expected_status, expected_response): |
| 100 |
if isinstance(expected_response, str): |
| 101 |
response = client.get(path) |
| 102 |
assert response.status_code == expected_status |
| 103 |
assert response.json() == expected_response |
| 104 |
else: |
| 105 |
match = r"Did you fail to use functools.wraps\(\) in a decorator\?" |
| 106 |
with pytest.raises(expected_response, match=match): |
| 107 |
client.get(path) |