Skip to content

Commit

Permalink
added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nofalx committed Sep 11, 2023
1 parent 1b5d3fe commit efb7311
Showing 1 changed file with 80 additions and 1 deletion.
81 changes: 80 additions & 1 deletion tests/test_request.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
from typing import Optional

import pytest
from pydantic import ConfigDict

from ninja import Cookie, Header, Router
from ninja import Body, Cookie, Header, Router, Schema
from ninja.testing import TestClient


class OptionalEmptySchema(Schema):
model_config = ConfigDict(extra="forbid")
name: Optional[str] = None


class ExtraForbidSchema(Schema):
model_config = ConfigDict(extra="forbid")
name: str
metadata: Optional[OptionalEmptySchema] = None


router = Router()


Expand Down Expand Up @@ -41,6 +56,11 @@ def cookies2(request, wpn: str = Cookie(..., alias="weapon")):
return wpn


@router.post("/test-schema")
def test_schema(request, payload: ExtraForbidSchema = Body(...)):
return "ok"


client = TestClient(router)


Expand Down Expand Up @@ -77,3 +97,62 @@ def test_headers(path, expected_status, expected_response):
assert response.status_code == expected_status, response.content
print(response.json())
assert response.json() == expected_response


@pytest.mark.parametrize(
"path,json,expected_status,expected_response",
[
(
"/test-schema",
{"name": "test", "extra_name": "test2"},
422,
{
"detail": [
{
"type": "extra_forbidden",
"loc": ["body", "payload", "extra_name"],
"msg": "Extra inputs are not permitted",
}
]
},
),
(
"/test-schema",
{"name": "test", "metadata": {"extra_name": "xxx"}},
422,
{
"detail": [
{
"loc": ["body", "payload", "metadata", "extra_name"],
"msg": "Extra inputs are not permitted",
"type": "extra_forbidden",
}
]
},
),
(
"/test-schema",
{"name": "test", "metadata": "test2"},
422,
{
"detail": [
{
"type": "model_attributes_type",
"loc": ["body", "payload", "metadata"],
"msg": "Input should be a valid dictionary or object to extract fields from",
}
]
},
),
],
)
def test_pydantic_config(path, json, expected_status, expected_response):
# test extra forbid
response = client.post(path, json=json)
assert response.json() == expected_response
assert response.status_code == expected_status

# test extra forbid on nested schema
response = client.post(
path, json={"name": "test", "metadata": {"extra_name": "xxx"}}
)

0 comments on commit efb7311

Please sign in to comment.