Skip to content
This repository was archived by the owner on Nov 19, 2023. It is now read-only.

Commit 0e37606

Browse files
committed
Add test for nested route resolve path at static schema loaders
1 parent c3a48c9 commit 0e37606

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

test_project/api/views/products.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from rest_framework.request import Request
2+
from rest_framework.response import Response
3+
from rest_framework.status import HTTP_200_OK
4+
from rest_framework.views import APIView
5+
6+
7+
class Products(APIView):
8+
def get(self, request: Request, version: int, category_pk: int, subcategory_pk: int) -> Response:
9+
products = {
10+
1: {
11+
1: {},
12+
2: {},
13+
3: {}
14+
},
15+
2: {
16+
1: {},
17+
2: {},
18+
3: {}
19+
},
20+
3: {
21+
1: {},
22+
2: {},
23+
3: {}
24+
},
25+
4: {
26+
1: {},
27+
2: {},
28+
3: {}
29+
}
30+
}
31+
return Response(products.get(category_pk, {}).get(subcategory_pk, {}), HTTP_200_OK)

test_project/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from test_project.api.views.snake_cased_response import SnakeCasedResponse
1515
from test_project.api.views.trucks import BadTrucks, GoodTrucks
1616
from test_project.api.views.vehicles import Vehicles
17+
from test_project.api.views.products import Products
1718

1819
router = routers.SimpleRouter()
1920
router.register(r"names", NameViewSet)
@@ -28,6 +29,7 @@
2829
path("api/<str:version>/items", Items.as_view()),
2930
path("api/<str:version>/exempt-endpoint", Exempt.as_view()),
3031
path("api/<str:version>/<str:pk>/names", NamesRetrieveView.as_view()),
32+
path("api/<str:version>/categories/<int:category_pk>/subcategories/<int:subcategory_pk>/", Products.as_view()),
3133
path("api/<str:version>/snake-case/", SnakeCasedResponse.as_view()),
3234
# ^trailing slash is here on purpose
3335
path("api/<str:version>/router_generated/", include(router.urls)),

tests/schemas/manual_reference_schema.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,37 @@
55
"version": "1.0.0"
66
},
77
"paths": {
8+
"/api/v1/categories/{category_pk}/subcategories/{subcategory_pk}": {
9+
"get": {
10+
"operationId": "getProducts",
11+
"description": "",
12+
"parameters": [
13+
{
14+
"in": "path",
15+
"name": "category_pk",
16+
"schema": {
17+
"type": "integer"
18+
},
19+
"description": "A unique value identifying this category.",
20+
"required": true
21+
},
22+
{
23+
"in": "path",
24+
"name": "subcategory_pk",
25+
"schema": {
26+
"type": "integer"
27+
},
28+
"description": "A unique value identifying this subcategory.",
29+
"required": true
30+
}
31+
],
32+
"responses": {
33+
"200": {
34+
"description": ""
35+
}
36+
}
37+
}
38+
},
839
"/api/v1/{pk}/names": {
940
"get": {
1041
"operationId": "api_v1_names_retrieve",

tests/schemas/manual_reference_schema.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,23 @@ paths:
243243
responses:
244244
'204':
245245
description: ''
246+
/api/v1/categories/{category_pk}/subcategories/{subcategory_pk}/:
247+
get:
248+
operationId: getProducts
249+
description: ''
250+
parameters:
251+
- in: path
252+
name: category_pk
253+
schema:
254+
type: integer
255+
description: A unique value identifying this category.
256+
required: true
257+
- in: path
258+
name: subcategory_pk
259+
schema:
260+
type: integer
261+
description: A unique value identifying this subcategory.
262+
required: true
263+
responses:
264+
'200':
265+
description: ''

tests/test_loaders.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
DrfYasgSchemaLoader(field_key_map={"language": "en"}),
1212
DrfSpectacularSchemaLoader(field_key_map={"language": "en"}),
1313
]
14+
static_schema_loaders = [
15+
StaticSchemaLoader(yaml_schema_path, field_key_map={"language": "en"}),
16+
StaticSchemaLoader(json_schema_path, field_key_map={"language": "en"}),
17+
]
1418

1519

1620
@pytest.mark.parametrize("loader", loaders)
@@ -40,3 +44,8 @@ def test_loader_resolve_path(loader):
4044
ValueError, match="Could not resolve path `/api/v1/blars/correct`.\n\nDid you mean one of these?"
4145
):
4246
loader.resolve_path("/api/v1/blars/correct", "get")
47+
48+
49+
@pytest.mark.parametrize("loader", static_schema_loaders)
50+
def test_static_loader_resolve_nested_route(loader):
51+
assert loader.resolve_path("/api/v1/categories/1/subcategories/1/", "get")[0] == "/api/{version}/categories/{category_pk}/subcategories/{subcategory_pk}/"

0 commit comments

Comments
 (0)