-
-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add pyenv-virtualenv .python-version to ignore * added Router operation options default dict * moved statements to a new test and added override checks * remove typeddict * optional = None, default False in Operation__init__ * added more testing * revert tests added to existing file
- Loading branch information
Showing
5 changed files
with
144 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ docs/site | |
|
||
.DS_Store | ||
.idea | ||
.python-version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from typing import Optional | ||
|
||
import pytest | ||
from pydantic import Field | ||
|
||
from ninja import NinjaAPI, Router, Schema | ||
from ninja.testing import TestClient | ||
|
||
|
||
class SomeResponse(Schema): | ||
field1: Optional[int] = 1 | ||
field2: Optional[str] = "default value" | ||
field3: Optional[int] = Field(None, alias="aliased") | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"oparg,retdict,assertone,asserttwo", | ||
[ | ||
( | ||
"exclude_defaults", | ||
{"field1": 3}, | ||
{"field1": 3}, | ||
{"field1": 3, "field2": "default value", "field3": None}, | ||
), | ||
( | ||
"exclude_unset", | ||
{"field2": "test"}, | ||
{"field2": "test"}, | ||
{"field1": 1, "field2": "test", "field3": None}, | ||
), | ||
( | ||
"exclude_none", | ||
{"field1": None, "field2": None, "aliased": 10}, | ||
{"field3": 10}, | ||
{"field1": None, "field2": None, "field3": 10}, | ||
), | ||
( | ||
"by_alias", | ||
{"aliased": 10}, | ||
{"field1": 1, "field2": "default value", "aliased": 10}, | ||
{"field1": 1, "field2": "default value", "field3": 10}, | ||
), | ||
], | ||
) | ||
def test_router_defaults(oparg, retdict, assertone, asserttwo): | ||
"""Test that the router level settings work and can be overriden at the op level""" | ||
api = NinjaAPI() | ||
router = Router(**{oparg: True}) | ||
api.add_router("/", router) | ||
|
||
func1 = router.get("/test1", response=SomeResponse)(lambda request: retdict) | ||
func2 = router.get("/test2", response=SomeResponse, **{oparg: False})( | ||
lambda request: retdict | ||
) | ||
|
||
client = TestClient(api) | ||
|
||
assert getattr(func1._ninja_operation, oparg) is True | ||
assert getattr(func2._ninja_operation, oparg) is False | ||
|
||
assert client.get("/test1").json() == assertone | ||
assert client.get("/test2").json() == asserttwo |