8000 Add nested ListField/DictField tests · encode/django-rest-framework@1bb3669 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1bb3669

Browse files
author
Ryan P Kilby
committed
Add nested ListField/DictField tests
1 parent 3fee0bf commit 1bb3669

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tests/test_fields.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,25 @@ def test_collection_types_are_invalid_input(self):
17941794
assert exc_info.value.detail == ['Expected a list of items but got type "dict".']
17951795

17961796

1797+
class TestNestedListField(FieldValues):
1798+
"""
1799+
Values for nested `ListField` with IntegerField as child.
1800+
"""
1801+
valid_inputs = [
1802+
([[1, 2], [3]], [[1, 2], [3]]),
1803+
([[]], [[]])
1804+
]
1805+
invalid_inputs = [
1806+
(['not a list'], {0: ['Expected a list of items but got type "str".']}),
1807+
([[1, 2, 'error'], ['error']], {0: {2: ['A valid integer is required.']}, 1: {0: ['A valid integer is required.']}}),
1808+
([{'one': 'two'}], {0: ['Expected a list of items but got type "dict".']})
1809+
]
1810+
outputs = [
1811+
([[1, 2], [3]], [[1, 2], [3]]),
1812+
]
1813+
field = serializers.ListField(child=serializers.ListField(child=serializers.IntegerField()))
1814+
1815+
17971816
class TestEmptyListField(FieldValues):
17981817
"""
17991818
Values for `ListField` with allow_empty=False flag.
@@ -1866,6 +1885,23 @@ def test_allow_null(self):
18661885
assert output is None
18671886

18681887

1888+
class TestNestedDictField(FieldValues):
1889+
"""
1890+
Values for nested `DictField` with CharField as child.
1891+
"""
1892+
valid_inputs = [
1893+
({0: {'a': 1, 'b': '2'}, 1: {3: 3}}, {'0': {'a': '1', 'b': '2'}, '1': {'3': '3'}}),
1894+
]
1895+
invalid_inputs = [
1896+
({0: {'a': 1, 'b': None}, 1: {'c': None}}, {'0': {'b': ['This field may not be null.']}, '1': {'c': ['This field may not be null.']}}),
1897+
({0: 'not a dict'}, {'0': ['Expected a dictionary of items but got type "str".']}),
1898+
]
1899+
outputs = [
1900+
({0: {'a': 1, 'b': '2'}, 1: {3: 3}}, {'0': {'a': '1', 'b': '2'}, '1': {'3': '3'}}),
1901+
]
1902+
field = serializers.DictField(child=serializers.DictField(child=serializers.CharField()))
1903+
1904+
18691905
class TestDictFieldWithNullChild(FieldValues):
18701906
"""
18711907
Values for `DictField` with allow_null CharField as child.

0 commit comments

Comments
 (0)
0