def test_unique_operation_ids(): api = NinjaAPI() @api.get("/1") def same_name(request): pass @api.get("/2") # noqa: F811 def same_name(request): # noqa: F811 pass match = 'operation_id "test_openapi_schema_same_name" is already used' with pytest.warns(UserWarning, match=match): api.get_openapi_schema()
def test_examples(): api = NinjaAPI() with patch("builtins.api", api, create=True): import docs.src.tutorial.path.code010 client = NinjaClient(api) response = client.get("/events/2020/1/1") assert response.json() == {"date": "2020-01-01"} schema = api.get_openapi_schema("") events_params = schema["paths"]["/events/{year}/{month}/{day}"]["get"][ "parameters"] assert events_params == [ { "in": "path", "name": "year", "required": True }, { "in": "path", "name": "month", "required": True }, { "in": "path", "name": "day", "required": True }, ]
def test_duplicate_schema_names(): from django.db import models from ninja import NinjaAPI, Schema from ninja.orm import create_schema class TestModelDuplicate(models.Model): field1 = models.CharField() field2 = models.CharField() class Meta: app_label = "tests" class TestSchema(Schema): data1: create_schema(TestModelDuplicate, fields=["field1"]) # noqa: F821 data2: create_schema(TestModelDuplicate, fields=["field2"]) # noqa: F821 api = NinjaAPI() @api.get("/test", response=TestSchema) def a_test_method(request): return [] match = r"Looks like you may have created multiple orm schemas with the same name:" with pytest.raises(ConfigError, match=match): assert api.get_openapi_schema()
def test_examples(): api = NinjaAPI() with patch("builtins.api", api, create=True): import docs.src.tutorial.path.code01 # noqa: F401 client = TestClient(api) response = client.get("/items/123") assert response.json() == {"item_id": "123"} api = NinjaAPI() with patch("builtins.api", api, create=True): import docs.src.tutorial.path.code02 # noqa: F401 import docs.src.tutorial.path.code010 # noqa: F401 client = TestClient(api) response = client.get("/items/123") assert response.json() == {"item_id": 123} response = client.get("/events/2020/1/1") assert response.json() == {"date": "2020-01-01"} schema = api.get_openapi_schema("") events_params = schema["paths"]["/events/{year}/{month}/{day}"]["get"][ "parameters"] # print(events_params, "!!") assert events_params == [ { "in": "path", "name": "year", "required": True, "schema": { "title": "Year", "type": "integer" }, }, { "in": "path", "name": "month", "required": True, "schema": { "title": "Month", "type": "integer" }, }, { "in": "path", "name": "day", "required": True, "schema": { "title": "Day", "type": "integer" }, }, ]
def test_unique_operation_ids(): api = NinjaAPI() @api.get("/1") def same_name(request): pass @api.get("/2") def same_name(request): pass with pytest.warns(UserWarning): schema = api.get_openapi_schema()
def test_kwargs(): api = NinjaAPI() @api.get("/") def operation(request, a: str, *args, **kwargs): pass schema = api.get_openapi_schema() params = schema["paths"]["/api/"]["get"]["parameters"] print(params) assert params == [ # Only `a` should be here, not kwargs { "in": "query", "name": "a", "schema": { "title": "A", "type": "string" }, "required": True, } ]
def test_examples(): api = NinjaAPI() with patch("builtins.api", api, create=True): import docs.src.tutorial.query.code01 # noqa: F401 import docs.src.tutorial.query.code02 # noqa: F401 import docs.src.tutorial.query.code03 # noqa: F401 import docs.src.tutorial.query.code010 # noqa: F401 client = TestClient(api) # Defaults assert client.get("/weapons").json() == [ "Ninjato", "Shuriken", "Katana", "Kama", "Kunai", "Naginata", "Yari", ] assert client.get("/weapons?offset=0&limit=3").json() == [ "Ninjato", "Shuriken", "Katana", ] assert client.get("/weapons?offset=2&limit=2").json() == [ "Katana", "Kama", ] # Required/Optional assert client.get("/weapons/search?offset=1&q=k").json() == [ "Katana", "Kama", "Kunai", ] # Coversion # fmt: off assert client.get("/example?b=1").json() == [None, True, None, None] assert client.get("/example?b=True").json() == [None, True, None, None] assert client.get("/example?b=true").json() == [None, True, None, None] assert client.get("/example?b=on").json() == [None, True, None, None] assert client.get("/example?b=yes").json() == [None, True, None, None] assert client.get("/example?b=0").json() == [None, False, None, None] assert client.get("/example?b=no").json() == [None, False, None, None] assert client.get("/example?b=false").json() == [None, False, None, None] assert client.get("/example?d=1577836800").json() == [None, None, "2020-01-01", None] assert client.get("/example?d=2020-01-01").json() == [None, None, "2020-01-01", None] # fmt: on # Schema assert client.get("/filter").json() == { "filters": { "limit": 100, "offset": None, "query": None, "category__in": None, } } assert client.get("/filter?limit=10").json() == { "filters": { "limit": 10, "offset": None, "query": None, "category__in": None, } } assert client.get("/filter?offset=10").json() == { "filters": {"limit": 100, "offset": 10, "query": None, "category__in": None} } assert client.get("/filter?query=10").json() == { "filters": { "limit": 100, "offset": None, "query": "10", "category__in": None, } } assert client.get("/filter?categories=a&categories=b").json() == { "filters": { "limit": 100, "offset": None, "query": None, "category__in": ["a", "b"], } } schema = api.get_openapi_schema("") params = schema["paths"]["/filter"]["get"]["parameters"] assert params == [ { "in": "query", "name": "limit", "required": False, "schema": {"title": "Limit", "default": 100, "type": "integer"}, }, { "in": "query", "name": "offset", "required": False, "schema": {"title": "Offset", "type": "integer"}, }, { "in": "query", "name": "query", "required": False, "schema": {"title": "Query", "type": "string"}, }, { "in": "query", "name": "categories", "required": False, "schema": { "title": "Categories", "type": "array", "items": {"type": "string"}, }, }, ]
def test_examples(): api = NinjaAPI() with patch("builtins.api", api, create=True): import docs.src.tutorial.query.code01 import docs.src.tutorial.query.code02 import docs.src.tutorial.query.code03 import docs.src.tutorial.query.code010 client = NinjaClient(api) # Defaults assert client.get("/weapons").json() == [ "Ninjato", "Shuriken", "Katana", "Kama", "Kunai", "Naginata", "Yari", ] assert client.get("/weapons?offset=0&limit=3").json() == [ "Ninjato", "Shuriken", "Katana", ] assert client.get("/weapons?offset=2&limit=2").json() == [ "Katana", "Kama", ] # Required/Optional assert client.get("/weapons/search?offset=1&q=k").json() == [ "Katana", "Kama", "Kunai", ] # Coversion # fmt: off assert client.get("/example?b=1").json() == [None, True, None, None] assert client.get("/example?b=True").json() == [None, True, None, None] assert client.get("/example?b=true").json() == [None, True, None, None] assert client.get("/example?b=on").json() == [None, True, None, None] assert client.get("/example?b=yes").json() == [None, True, None, None] assert client.get("/example?b=0").json() == [None, False, None, None] assert client.get("/example?b=no").json() == [None, False, None, None] assert client.get("/example?b=false").json() == [None, False, None, None] assert client.get("/example?d=1577836800").json() == [None, None, "2020-01-01", None] assert client.get("/example?d=2020-01-01").json() == [None, None, "2020-01-01", None] # fmt: on # Schema assert client.get("/filter").json() == { "filters": {"limit": 100, "offset": None, "query": None} } assert client.get("/filter?limit=10").json() == { "filters": {"limit": 10, "offset": None, "query": None} } assert client.get("/filter?offset=10").json() == { "filters": {"limit": 100, "offset": 10, "query": None} } assert client.get("/filter?query=10").json() == { "filters": {"limit": 100, "offset": None, "query": "10"} } schema = api.get_openapi_schema("") events_params = schema["paths"]["/filter"]["get"]["parameters"] assert events_params == [ {"in": "query", "name": "limit", "required": False}, {"in": "query", "name": "offset", "required": False}, {"in": "query", "name": "query", "required": False}, ]
def operation3(request): "This one also has docstring description" return {"summary": True, "description": "multiple"} @api.get("/operation4", tags=["tag1", "tag2"]) def operation4(request): return {"tags": True} @api.get("/not-included", include_in_schema=False) def not_included(request): return True schema = api.get_openapi_schema() def test_schema(): op1 = schema["paths"]["/api/operation1"]["get"] op2 = schema["paths"]["/api/operation2"]["get"] op3 = schema["paths"]["/api/operation3"]["get"] op4 = schema["paths"]["/api/operation4"]["get"] assert op1["operationId"] == "my_id" assert op2["operationId"] == "test_openapi_params_operation2" assert op3["operationId"] == "test_openapi_params_operation3" assert op4["operationId"] == "test_openapi_params_operation4" assert op1["summary"] == "Operation 1" assert op2["summary"] == "Operation2"