Skip to content

Commit bbd44ae

Browse files
committed
Updated the test cases based on the CR comments
1 parent 085c3e8 commit bbd44ae

File tree

1 file changed

+26
-55
lines changed

1 file changed

+26
-55
lines changed

tests/test_serializer_nested.py

Lines changed: 26 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -73,84 +73,55 @@ def test_multipart_validate(self):
7373

7474
class TestNestedSerializerWithMany:
7575
def setup(self):
76-
class PaymentInfoSerializer(serializers.Serializer):
77-
url = serializers.URLField()
78-
amount = serializers.DecimalField(max_digits=6, decimal_places=2)
79-
8076
class NestedSerializer(serializers.Serializer):
81-
foo = serializers.CharField()
82-
83-
class AcceptRequestSerializer(serializers.Serializer):
84-
ERROR_MESSAGE = '`payment_info` is required under this condition'
85-
86-
nested = NestedSerializer(many=True)
87-
payment_info = PaymentInfoSerializer(many=True, allow_null=True)
77+
example = serializers.IntegerField(max_value=10)
8878

89-
def validate_payment_info(self, value):
90-
if value is None and not self.context['condition_to_allow_null']:
91-
raise serializers.ValidationError(self.ERROR_MESSAGE)
92-
93-
return value
94-
95-
self.Serializer = AcceptRequestSerializer
79+
class TestSerializer(serializers.Serializer):
80+
allow_null = NestedSerializer(many=True, allow_null=True)
81+
not_allow_null = NestedSerializer(many=True)
9682

97-
def get_serializer(self, data, condition_to_allow_null=True):
98-
return self.Serializer(
99-
data=data,
100-
context={'condition_to_allow_null': condition_to_allow_null})
83+
self.Serializer = TestSerializer
10184

10285
def test_null_allowed_if_allow_null_is_set(self):
10386
input_data = {
104-
'nested': [{'foo': 'bar'}],
105-
'payment_info': None
87+
'allow_null': None,
88+
'not_allow_null': [{'example': '2'}, {'example': '3'}]
10689
}
10790
expected_data = {
108-
'nested': [{'foo': 'bar'}],
109-
'payment_info': None
91+
'allow_null': None,
92+
'not_allow_null': [{'example': 2}, {'example': 3}]
11093
}
111-
serializer = self.get_serializer(input_data)
94+
serializer = self.Serializer(data=input_data)
11295

11396
assert serializer.is_valid(), serializer.errors
11497
assert serializer.validated_data == expected_data
11598

11699
def test_null_is_not_allowed_if_allow_null_is_not_set(self):
117100
input_data = {
118-
'nested': None,
119-
'payment_info': None
101+
'allow_null': None,
102+
'not_allow_null': None
120103
}
121-
serializer = self.get_serializer(input_data)
104+
serializer = self.Serializer(data=input_data)
122105

123106
assert not serializer.is_valid()
124-
assert set(serializer.errors) == set(['nested'])
125-
assert serializer.errors['nested'][0] == serializer.error_messages['null']
107+
108+
expected_errors = {'not_allow_null': [serializer.error_messages['null']]}
109+
assert serializer.errors == expected_errors
126110

127111
def test_run_the_field_validation_even_if_the_field_is_null(self):
128-
input_data = {
129-
'nested': [{'foo': 'bar'}],
130-
'payment_info': None,
131-
}
132-
serializer = self.get_serializer(input_data, condition_to_allow_null=False)
112+
class TestSerializer(self.Serializer):
113+
validation_was_run = False
133114

134-
assert not serializer.is_valid()
135-
assert set(serializer.errors) == set(['payment_info'])
136-
assert serializer.errors['payment_info'][0] == serializer.ERROR_MESSAGE
115+
def validate_allow_null(self, value):
116+
TestSerializer.validation_was_run = True
117+
return value
137118

138-
def test_expected_results_if_not_null(self):
139119
input_data = {
140-
'nested': [{'foo': 'bar'}],
141-
'payment_info': [
142-
{'url': 'https://domain.org/api/payment-method/1/', 'amount': '22'},
143-
{'url': 'https://domain.org/api/payment-method/2/', 'amount': '33'},
144-
]
145-
}
146-
expected_data = {
147-
'nested': [{'foo': 'bar'}],
148-
'payment_info': [
149-
{'url': 'https://domain.org/api/payment-method/1/', 'amount': 22},
150-
{'url': 'https://domain.org/api/payment-method/2/', 'amount': 33},
151-
]
120+
'allow_null': None,
121+
'not_allow_null': [{'example': 2}]
152122
}
153-
serializer = self.get_serializer(input_data)
123+
serializer = TestSerializer(data=input_data)
154124

155125
assert serializer.is_valid()
156-
assert serializer.validated_data == expected_data
126+
assert serializer.validated_data == input_data
127+
assert TestSerializer.validation_was_run

0 commit comments

Comments
 (0)