| 1 |
from typing import List |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from ninja import NinjaAPI, Schema |
| 6 |
from ninja.pagination import PageNumberPagination, RouterPaginated, paginate |
| 7 |
from ninja.testing import TestAsyncClient, TestClient |
| 8 |
|
| 9 |
api = NinjaAPI(default_router=RouterPaginated()) |
| 10 |
|
| 11 |
|
| 12 |
class ItemSchema(Schema): |
| 13 |
id: int |
| 14 |
|
| 15 |
|
| 16 |
@api.get("/items", response=List[ItemSchema]) |
| 17 |
def items(request): |
| 18 |
return [{"id": i} for i in range(1, 51)] |
| 19 |
|
| 20 |
|
| 21 |
@api.get("/items_nolist", response=ItemSchema) |
| 22 |
def items_nolist(request): |
| 23 |
return {"id": 1} |
| 24 |
|
| 25 |
|
| 26 |
@api.get("/items_extra_layer", response=List[ItemSchema]) |
| 27 |
@paginate |
| 28 |
def items_extra_layer(request): |
| 29 |
return [{"id": i} for i in range(1, 51)] |
| 30 |
|
| 31 |
|
| 32 |
@api.get("/items_overridden", response=List[ItemSchema]) |
| 33 |
@paginate(PageNumberPagination, page_size=3) |
| 34 |
def items_overridden_pagination(request): |
| 35 |
return [{"id": i} for i in range(1, 51)] |
| 36 |
|
| 37 |
|
| 38 |
client = TestClient(api) |
| 39 |
|
| 40 |
|
| 41 |
def test_for_list_reponse(): |
| 42 |
parameters = api.get_openapi_schema()["paths"]["/api/items"]["get"]["parameters"] |
| 43 |
assert parameters == [ |
| 44 |
{ |
| 45 |
"in": "query", |
| 46 |
"name": "limit", |
| 47 |
"schema": { |
| 48 |
"title": "Limit", |
| 49 |
"default": 100, |
| 50 |
"minimum": 1, |
| 51 |
"type": "integer", |
| 52 |
}, |
| 53 |
"required": False, |
| 54 |
}, |
| 55 |
{ |
| 56 |
"in": "query", |
| 57 |
"name": "offset", |
| 58 |
"schema": { |
| 59 |
"title": "Offset", |
| 60 |
"default": 0, |
| 61 |
"minimum": 0, |
| 62 |
"type": "integer", |
| 63 |
}, |
| 64 |
"required": False, |
| 65 |
}, |
| 66 |
] |
| 67 |
|
| 68 |
response = client.get("/items?offset=5&limit=1").json() |
| 69 |
|
| 70 |
assert response == {"items": [{"id": 6}], "count": 50} |
| 71 |
|
| 72 |
|
| 73 |
def test_for_NON_list_reponse(): |
| 74 |
parameters = api.get_openapi_schema()["paths"]["/api/items_nolist"]["get"][ |
| 75 |
"parameters" |
| 76 |
] |
| 77 |
|
| 78 |
assert parameters == [] |
| 79 |
|
| 80 |
|
| 81 |
def test_extra_pagination_layer_does_not_crash(): |
| 82 |
parameters = api.get_openapi_schema()["paths"]["/api/items_extra_layer"]["get"][ |
| 83 |
"parameters" |
| 84 |
] |
| 85 |
assert parameters == [ |
| 86 |
{ |
| 87 |
"in": "query", |
| 88 |
"name": "limit", |
| 89 |
"schema": { |
| 90 |
"title": "Limit", |
| 91 |
"default": 100, |
| 92 |
"minimum": 1, |
| 93 |
"type": "integer", |
| 94 |
}, |
| 95 |
"required": False, |
| 96 |
}, |
| 97 |
{ |
| 98 |
"in": "query", |
| 99 |
"name": "offset", |
| 100 |
"schema": { |
| 101 |
"title": "Offset", |
| 102 |
"default": 0, |
| 103 |
"minimum": 0, |
| 104 |
"type": "integer", |
| 105 |
}, |
| 106 |
"required": False, |
| 107 |
}, |
| 108 |
] |
| 109 |
|
| 110 |
response = client.get("/items_extra_layer?offset=5&limit=1").json() |
| 111 |
assert response == {"items": [{"id": 6}], "count": 50} |
| 112 |
|
| 113 |
|
| 114 |
def test_for_list_with_overridden_pagination_reponse(): |
| 115 |
parameters = api.get_openapi_schema()["paths"]["/api/items_overridden"]["get"][ |
| 116 |
"parameters" |
| 117 |
] |
| 118 |
assert parameters == [ |
| 119 |
{ |
| 120 |
"in": "query", |
| 121 |
"name": "page", |
| 122 |
"schema": { |
| 123 |
"title": "Page", |
| 124 |
"default": 1, |
| 125 |
"minimum": 1, |
| 126 |
"type": "integer", |
| 127 |
}, |
| 128 |
"required": False, |
| 129 |
}, |
| 130 |
{ |
| 131 |
"in": "query", |
| 132 |
"name": "page_size", |
| 133 |
"schema": { |
| 134 |
"anyOf": [{"minimum": 1, "type": "integer"}, {"type": "null"}], |
| 135 |
"title": "Page Size", |
| 136 |
}, |
| 137 |
"required": False, |
| 138 |
}, |
| 139 |
] |
| 140 |
|
| 141 |
response = client.get("/items_overridden?page=5").json() |
| 142 |
assert response == {"items": [{"id": 13}, {"id": 14}, {"id": 15}], "count": 50} |
| 143 |
|
| 144 |
|
| 145 |
@pytest.mark.asyncio |
| 146 |
async def test_async_pagination(): |
| 147 |
|
| 148 |
async_api = NinjaAPI(default_router=RouterPaginated(), urls_namespace="async_test") |
| 149 |
|
| 150 |
@async_api.get("/items_async", response=List[ItemSchema]) |
| 151 |
async def items_async(request): |
| 152 |
return [{"id": i} for i in range(1, 51)] |
| 153 |
|
| 154 |
client = TestAsyncClient(async_api) |
| 155 |
|
| 156 |
response = await client.get("/items_async?offset=5&limit=1") |
| 157 |
assert response.json() == {"items": [{"id": 6}], "count": 50} |