adds test for missing but required many-to-many relationship value

This commit is contained in:
Anton Martyniuk 2014-06-29 16:31:25 +02:00
parent 2f73e3f704
commit 9cb4e4e00e

View File

@ -3,6 +3,7 @@ General tests for relational fields.
"""
from __future__ import unicode_literals
from django import get_version
from django.core.exceptions import ValidationError
from django.db import models
from django.test import TestCase
from django.utils import unittest
@ -43,12 +44,28 @@ class TestManyRelatedMixin(TestCase):
https://github.com/tomchristie/django-rest-framework/pull/632
'''
field = serializers.RelatedField(many=True, read_only=False)
field = serializers.RelatedField(many=True, read_only=False, required=False)
into = {}
field.field_from_native({}, None, 'field_name', into)
self.assertEqual(into['field_name'], [])
def test_missing_required_many_to_many_related_field(self):
'''
Missing but required many-to-many relationship should cause
ValidationError
'''
field = serializers.RelatedField(many=True, read_only=False)
into = {}
self.assertRaisesMessage(
ValidationError,
"This field is required.",
field.field_from_native,
{}, None, 'field_name', into
)
self.assertDictEqual(into, {})
# Regression tests for #694 (`source` attribute on related fields)