mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-12-16 22:54:01 +03:00
Add tests for .data access before .is_valid() in relation serializers
- Add test_data_cannot_be_accessed_prior_to_is_valid to HyperlinkedManyToManyTests - Add test_data_cannot_be_accessed_prior_to_is_valid to PKManyToManyTests - Remove TODO comments that were addressed - Ensures AssertionError is raised when accessing .data before validation Fixes TODO items in: - tests/test_relations_hyperlink.py (line 71) - tests/test_relations_pk.py (line 97)
This commit is contained in:
parent
cfc067be30
commit
da05872414
|
|
@ -1,3 +1,4 @@
|
||||||
|
import pytest
|
||||||
from django.test import TestCase, override_settings
|
from django.test import TestCase, override_settings
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
|
|
@ -68,7 +69,6 @@ class NullableOneToOneTargetSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
fields = ('url', 'name', 'nullable_source')
|
fields = ('url', 'name', 'nullable_source')
|
||||||
|
|
||||||
|
|
||||||
# TODO: Add test that .data cannot be accessed prior to .is_valid
|
|
||||||
@override_settings(ROOT_URLCONF='tests.test_relations_hyperlink')
|
@override_settings(ROOT_URLCONF='tests.test_relations_hyperlink')
|
||||||
class HyperlinkedManyToManyTests(TestCase):
|
class HyperlinkedManyToManyTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
@ -193,6 +193,15 @@ class HyperlinkedManyToManyTests(TestCase):
|
||||||
]
|
]
|
||||||
assert serializer.data == expected
|
assert serializer.data == expected
|
||||||
|
|
||||||
|
def test_data_cannot_be_accessed_prior_to_is_valid(self):
|
||||||
|
"""Test that .data cannot be accessed prior to .is_valid for hyperlinked serializers."""
|
||||||
|
serializer = ManyToManySourceSerializer(
|
||||||
|
data={'name': 'test-source', 'targets': ['http://testserver/manytomanytarget/1/']},
|
||||||
|
context={'request': request}
|
||||||
|
)
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
serializer.data
|
||||||
|
|
||||||
|
|
||||||
@override_settings(ROOT_URLCONF='tests.test_relations_hyperlink')
|
@override_settings(ROOT_URLCONF='tests.test_relations_hyperlink')
|
||||||
class HyperlinkedForeignKeyTests(TestCase):
|
class HyperlinkedForeignKeyTests(TestCase):
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import pytest
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
@ -94,8 +95,6 @@ class OneToOnePKSourceSerializer(serializers.ModelSerializer):
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
# TODO: Add test that .data cannot be accessed prior to .is_valid
|
|
||||||
|
|
||||||
class PKManyToManyTests(TestCase):
|
class PKManyToManyTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
for idx in range(1, 4):
|
for idx in range(1, 4):
|
||||||
|
|
@ -218,6 +217,14 @@ class PKManyToManyTests(TestCase):
|
||||||
]
|
]
|
||||||
assert serializer.data == expected
|
assert serializer.data == expected
|
||||||
|
|
||||||
|
def test_data_cannot_be_accessed_prior_to_is_valid(self):
|
||||||
|
"""Test that .data cannot be accessed prior to .is_valid for primary key serializers."""
|
||||||
|
serializer = ManyToManySourceSerializer(
|
||||||
|
data={'name': 'test-source', 'targets': [1]}
|
||||||
|
)
|
||||||
|
with pytest.raises(AssertionError):
|
||||||
|
serializer.data
|
||||||
|
|
||||||
|
|
||||||
class PKForeignKeyTests(TestCase):
|
class PKForeignKeyTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user