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:
therealjozbert 2025-12-09 14:09:09 +03:00
parent cfc067be30
commit da05872414
2 changed files with 19 additions and 3 deletions

View File

@ -1,3 +1,4 @@
import pytest
from django.test import TestCase, override_settings
from django.urls import path
@ -68,7 +69,6 @@ class NullableOneToOneTargetSerializer(serializers.HyperlinkedModelSerializer):
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')
class HyperlinkedManyToManyTests(TestCase):
def setUp(self):
@ -193,6 +193,15 @@ class HyperlinkedManyToManyTests(TestCase):
]
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')
class HyperlinkedForeignKeyTests(TestCase):

View File

@ -1,3 +1,4 @@
import pytest
from django.test import TestCase
from rest_framework import serializers
@ -94,8 +95,6 @@ class OneToOnePKSourceSerializer(serializers.ModelSerializer):
fields = '__all__'
# TODO: Add test that .data cannot be accessed prior to .is_valid
class PKManyToManyTests(TestCase):
def setUp(self):
for idx in range(1, 4):
@ -218,6 +217,14 @@ class PKManyToManyTests(TestCase):
]
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):
def setUp(self):