|
1 | 1 | from .utils import mock_reverse, fail_reverse, BadType, MockObject, MockQueryset
|
2 | 2 | from django.core.exceptions import ImproperlyConfigured
|
| 3 | +from django.utils.datastructures import MultiValueDict |
3 | 4 | from rest_framework import serializers
|
| 5 | +from rest_framework.fields import empty |
4 | 6 | from rest_framework.test import APISimpleTestCase
|
5 | 7 | import pytest
|
6 | 8 |
|
@@ -134,3 +136,34 @@ def test_slug_related_lookup_invalid_type(self):
|
134 | 136 | def test_representation(self):
|
135 | 137 | representation = self.field.to_representation(self.instance)
|
136 | 138 | assert representation == self.instance.name
|
| 139 | + |
| 140 | + |
| 141 | +class TestManyRelatedField(APISimpleTestCase): |
| 142 | + def setUp(self): |
| 143 | + self.instance = MockObject(pk=1, name='foo') |
| 144 | + self.field = serializers.StringRelatedField(many=True) |
| 145 | + self.field.field_name = 'foo' |
| 146 | + |
| 147 | + def test_get_value_regular_dictionary_full(self): |
| 148 | + assert 'bar' == self.field.get_value({'foo': 'bar'}) |
| 149 | + assert empty == self.field.get_value({'baz': 'bar'}) |
| 150 | + |
| 151 | + def test_get_value_regular_dictionary_partial(self): |
| 152 | + setattr(self.field.root, 'partial', True) |
| 153 | + assert 'bar' == self.field.get_value({'foo': 'bar'}) |
| 154 | + assert empty == self.field.get_value({'baz': 'bar'}) |
| 155 | + |
| 156 | + def test_get_value_multi_dictionary_full(self): |
| 157 | + mvd = MultiValueDict({'foo': ['bar1', 'bar2']}) |
| 158 | + assert ['bar1', 'bar2'] == self.field.get_value(mvd) |
| 159 | + |
| 160 | + mvd = MultiValueDict({'baz': ['bar1', 'bar2']}) |
| 161 | + assert [] == self.field.get_value(mvd) |
| 162 | + |
| 163 | + def test_get_value_multi_dictionary_partial(self): |
| 164 | + setattr(self.field.root, 'partial', True) |
| 165 | + mvd = MultiValueDict({'foo': ['bar1', 'bar2']}) |
| 166 | + assert ['bar1', 'bar2'] == self.field.get_value(mvd) |
| 167 | + |
| 168 | + mvd = MultiValueDict({'baz': ['bar1', 'bar2']}) |
| 169 | + assert empty == self.field.get_value(mvd) |
0 commit comments