-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split v2.rest_framework package tests.
- Loading branch information
Showing
10 changed files
with
378 additions
and
279 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
Empty file.
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,26 @@ | ||
import sys | ||
|
||
import pytest | ||
from rest_framework import schemas | ||
from rest_framework.request import Request | ||
|
||
from .view_fixtures import create_views_urlconf | ||
|
||
coreapi = pytest.importorskip("django_pydantic_field.v2.rest_framework.coreapi") | ||
|
||
@pytest.mark.skipif(sys.version_info >= (3, 12), reason="CoreAPI is not compatible with 3.12") | ||
@pytest.mark.parametrize( | ||
"method, path", | ||
[ | ||
("GET", "/func"), | ||
("POST", "/func"), | ||
("GET", "/class"), | ||
("PUT", "/class"), | ||
], | ||
) | ||
def test_coreapi_schema_generators(request_factory, method, path): | ||
urlconf = create_views_urlconf(coreapi.AutoSchema) | ||
generator = schemas.SchemaGenerator(urlconf=urlconf) | ||
request = Request(request_factory.generic(method, path)) | ||
coreapi_schema = generator.get_schema(request) | ||
assert coreapi_schema |
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,56 @@ | ||
from datetime import date | ||
|
||
import pytest | ||
|
||
from tests.conftest import InnerSchema | ||
|
||
from .view_fixtures import ( | ||
ClassBasedView, | ||
ClassBasedViewWithModel, | ||
ClassBasedViewWithSchemaContext, | ||
sample_view, | ||
) | ||
|
||
rest_framework = pytest.importorskip("django_pydantic_field.v2.rest_framework") | ||
coreapi = pytest.importorskip("django_pydantic_field.v2.rest_framework.coreapi") | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"view", | ||
[ | ||
sample_view, | ||
ClassBasedView.as_view(), | ||
ClassBasedViewWithSchemaContext.as_view(), | ||
], | ||
) | ||
def test_end_to_end_api_view(view, request_factory): | ||
expected_instance = InnerSchema(stub_str="abc", stub_list=[date(2022, 7, 1)]) | ||
existing_encoded = b'{"stub_str":"abc","stub_int":1,"stub_list":["2022-07-01"]}' | ||
|
||
request = request_factory.post("/", existing_encoded, content_type="application/json") | ||
response = view(request) | ||
|
||
assert response.data == [expected_instance] | ||
assert response.data[0] is not expected_instance | ||
|
||
assert response.rendered_content == b"[%s]" % existing_encoded | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_end_to_end_list_create_api_view(request_factory): | ||
field_data = InnerSchema(stub_str="abc", stub_list=[date(2022, 7, 1)]).json() | ||
expected_result = { | ||
"sample_field": {"stub_str": "abc", "stub_list": [date(2022, 7, 1)], "stub_int": 1}, | ||
"sample_list": [{"stub_str": "abc", "stub_list": [date(2022, 7, 1)], "stub_int": 1}], | ||
"sample_seq": [], | ||
} | ||
|
||
payload = '{"sample_field": %s, "sample_list": [%s], "sample_seq": []}' % ((field_data,) * 2) | ||
request = request_factory.post("/", payload.encode(), content_type="application/json") | ||
response = ClassBasedViewWithModel.as_view()(request) | ||
|
||
assert response.data == expected_result | ||
|
||
request = request_factory.get("/", content_type="application/json") | ||
response = ClassBasedViewWithModel.as_view()(request) | ||
assert response.data == [expected_result] |
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,108 @@ | ||
import typing as ty | ||
from datetime import date | ||
|
||
import pytest | ||
from rest_framework import exceptions, serializers | ||
|
||
from tests.conftest import InnerSchema | ||
from tests.test_app.models import SampleModel | ||
|
||
rest_framework = pytest.importorskip("django_pydantic_field.v2.rest_framework") | ||
|
||
|
||
class SampleSerializer(serializers.Serializer): | ||
field = rest_framework.SchemaField(schema=ty.List[InnerSchema]) | ||
|
||
|
||
class SampleModelSerializer(serializers.ModelSerializer): | ||
sample_field = rest_framework.SchemaField(schema=InnerSchema) | ||
sample_list = rest_framework.SchemaField(schema=ty.List[InnerSchema]) | ||
sample_seq = rest_framework.SchemaField(schema=ty.List[InnerSchema], default=list) | ||
|
||
class Meta: | ||
model = SampleModel | ||
fields = "sample_field", "sample_list", "sample_seq" | ||
|
||
|
||
def test_schema_field(): | ||
field = rest_framework.SchemaField(InnerSchema) | ||
existing_instance = InnerSchema(stub_str="abc", stub_list=[date(2022, 7, 1)]) | ||
expected_encoded = { | ||
"stub_str": "abc", | ||
"stub_int": 1, | ||
"stub_list": [date(2022, 7, 1)], | ||
} | ||
|
||
assert field.to_representation(existing_instance) == expected_encoded | ||
assert field.to_internal_value(expected_encoded) == existing_instance | ||
|
||
with pytest.raises(serializers.ValidationError): | ||
field.to_internal_value(None) | ||
|
||
with pytest.raises(serializers.ValidationError): | ||
field.to_internal_value("null") | ||
|
||
|
||
def test_field_schema_with_custom_config(): | ||
field = rest_framework.SchemaField(InnerSchema, allow_null=True, exclude={"stub_int"}) | ||
existing_instance = InnerSchema(stub_str="abc", stub_list=[date(2022, 7, 1)]) | ||
expected_encoded = {"stub_str": "abc", "stub_list": [date(2022, 7, 1)]} | ||
|
||
assert field.to_representation(existing_instance) == expected_encoded | ||
assert field.to_internal_value(expected_encoded) == existing_instance | ||
assert field.to_internal_value(None) is None | ||
assert field.to_internal_value("null") is None | ||
|
||
|
||
def test_serializer_marshalling_with_schema_field(): | ||
existing_instance = {"field": [InnerSchema(stub_str="abc", stub_list=[date(2022, 7, 1)])]} | ||
expected_data = {"field": [{"stub_str": "abc", "stub_int": 1, "stub_list": [date(2022, 7, 1)]}]} | ||
|
||
serializer = SampleSerializer(instance=existing_instance) | ||
assert serializer.data == expected_data | ||
|
||
serializer = SampleSerializer(data=expected_data) | ||
serializer.is_valid(raise_exception=True) | ||
assert serializer.validated_data == existing_instance | ||
|
||
|
||
def test_model_serializer_marshalling_with_schema_field(): | ||
instance = SampleModel( | ||
sample_field=InnerSchema(stub_str="abc", stub_list=[date(2022, 7, 1)]), | ||
sample_list=[InnerSchema(stub_str="abc", stub_int=2, stub_list=[date(2022, 7, 1)])] * 2, | ||
sample_seq=[InnerSchema(stub_str="abc", stub_int=3, stub_list=[date(2022, 7, 1)])] * 3, | ||
) | ||
serializer = SampleModelSerializer(instance) | ||
|
||
expected_data = { | ||
"sample_field": {"stub_str": "abc", "stub_int": 1, "stub_list": [date(2022, 7, 1)]}, | ||
"sample_list": [{"stub_str": "abc", "stub_int": 2, "stub_list": [date(2022, 7, 1)]}] * 2, | ||
"sample_seq": [{"stub_str": "abc", "stub_int": 3, "stub_list": [date(2022, 7, 1)]}] * 3, | ||
} | ||
assert serializer.data == expected_data | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"export_kwargs", | ||
[ | ||
{"include": {"stub_str", "stub_int"}}, | ||
{"exclude": {"stub_list"}}, | ||
{"exclude_unset": True}, | ||
{"exclude_defaults": True}, | ||
{"exclude_none": True}, | ||
{"by_alias": True}, | ||
], | ||
) | ||
def test_field_export_kwargs(export_kwargs): | ||
field = rest_framework.SchemaField(InnerSchema, **export_kwargs) | ||
assert field.to_representation(InnerSchema(stub_str="abc", stub_list=[date(2022, 7, 1)])) | ||
|
||
|
||
def test_invalid_data_serialization(): | ||
invalid_data = {"field": [{"stub_int": "abc", "stub_list": ["abc"]}]} | ||
serializer = SampleSerializer(data=invalid_data) | ||
|
||
with pytest.raises(exceptions.ValidationError) as e: | ||
serializer.is_valid(raise_exception=True) | ||
|
||
assert e.match(r".*stub_str.*stub_int.*stub_list.*") |
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,23 @@ | ||
import pytest | ||
from rest_framework.schemas.openapi import SchemaGenerator | ||
from rest_framework.request import Request | ||
|
||
from .view_fixtures import create_views_urlconf | ||
|
||
openapi = pytest.importorskip("django_pydantic_field.v2.rest_framework.openapi") | ||
|
||
@pytest.mark.parametrize( | ||
"method, path", | ||
[ | ||
("GET", "/func"), | ||
("POST", "/func"), | ||
("GET", "/class"), | ||
("PUT", "/class"), | ||
], | ||
) | ||
def test_coreapi_schema_generators(request_factory, method, path): | ||
urlconf = create_views_urlconf(openapi.AutoSchema) | ||
generator = SchemaGenerator(urlconf=urlconf) | ||
request = Request(request_factory.generic(method, path)) | ||
openapi_schema = generator.get_schema(request) | ||
assert openapi_schema |
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,23 @@ | ||
import io | ||
from datetime import date | ||
|
||
import pytest | ||
|
||
from tests.conftest import InnerSchema | ||
|
||
rest_framework = pytest.importorskip("django_pydantic_field.v2.rest_framework") | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"schema_type, existing_encoded, expected_decoded", | ||
[ | ||
( | ||
InnerSchema, | ||
'{"stub_str": "abc", "stub_int": 1, "stub_list": ["2022-07-01"]}', | ||
InnerSchema(stub_str="abc", stub_list=[date(2022, 7, 1)]), | ||
) | ||
], | ||
) | ||
def test_schema_parser(schema_type, existing_encoded, expected_decoded): | ||
parser = rest_framework.SchemaParser[schema_type]() | ||
assert parser.parse(io.StringIO(existing_encoded)) == expected_decoded |
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,23 @@ | ||
from datetime import date | ||
|
||
import pytest | ||
|
||
from tests.conftest import InnerSchema | ||
|
||
rest_framework = pytest.importorskip("django_pydantic_field.v2.rest_framework") | ||
|
||
|
||
def test_schema_renderer(): | ||
renderer = rest_framework.SchemaRenderer() | ||
existing_instance = InnerSchema(stub_str="abc", stub_list=[date(2022, 7, 1)]) | ||
expected_encoded = b'{"stub_str":"abc","stub_int":1,"stub_list":["2022-07-01"]}' | ||
|
||
assert renderer.render(existing_instance) == expected_encoded | ||
|
||
|
||
def test_typed_schema_renderer(): | ||
renderer = rest_framework.SchemaRenderer[InnerSchema]() | ||
existing_data = {"stub_str": "abc", "stub_list": [date(2022, 7, 1)]} | ||
expected_encoded = b'{"stub_str":"abc","stub_int":1,"stub_list":["2022-07-01"]}' | ||
|
||
assert renderer.render(existing_data) == expected_encoded |
Oops, something went wrong.