Skip to content

Commit 78db4dd

Browse files
therealjozbertherealjozbertauvipy
authored
Add tests for .data access before .is_valid() in relation serializers (#9845)
- 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) Co-authored-by: therealjozbert <info@therealjozbert.com> Co-authored-by: Asif Saif Uddin {"Auvi":"অভি"} <auvipy@gmail.com>
1 parent 3a70eb2 commit 78db4dd

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

tests/test_relations_hyperlink.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from django.test import TestCase, override_settings
23
from django.urls import path
34

@@ -68,7 +69,6 @@ class Meta:
6869
fields = ('url', 'name', 'nullable_source')
6970

7071

71-
# TODO: Add test that .data cannot be accessed prior to .is_valid
7272
@override_settings(ROOT_URLCONF='tests.test_relations_hyperlink')
7373
class HyperlinkedManyToManyTests(TestCase):
7474
def setUp(self):
@@ -193,6 +193,15 @@ def test_reverse_many_to_many_create(self):
193193
]
194194
assert serializer.data == expected
195195

196+
def test_data_cannot_be_accessed_prior_to_is_valid(self):
197+
"""Test that .data cannot be accessed prior to .is_valid for hyperlinked serializers."""
198+
serializer = ManyToManySourceSerializer(
199+
data={'name': 'test-source', 'targets': ['http://testserver/manytomanytarget/1/']},
200+
context={'request': request}
201+
)
202+
with pytest.raises(AssertionError):
203+
serializer.data
204+
196205

197206
@override_settings(ROOT_URLCONF='tests.test_relations_hyperlink')
198207
class HyperlinkedForeignKeyTests(TestCase):

tests/test_relations_pk.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from django.test import TestCase
23

34
from rest_framework import serializers
@@ -94,8 +95,6 @@ class Meta:
9495
fields = '__all__'
9596

9697

97-
# TODO: Add test that .data cannot be accessed prior to .is_valid
98-
9998
class PKManyToManyTests(TestCase):
10099
def setUp(self):
101100
for idx in range(1, 4):
@@ -218,6 +217,14 @@ def test_reverse_many_to_many_create(self):
218217
]
219218
assert serializer.data == expected
220219

220+
def test_data_cannot_be_accessed_prior_to_is_valid(self):
221+
"""Test that .data cannot be accessed prior to .is_valid for primary key serializers."""
222+
serializer = ManyToManySourceSerializer(
223+
data={'name': 'test-source', 'targets': [1]}
224+
)
225+
with pytest.raises(AssertionError):
226+
serializer.data
227+
221228

222229
class PKForeignKeyTests(TestCase):
223230
def setUp(self):

0 commit comments

Comments
 (0)