@@ -1767,7 +1767,7 @@ class TestListField(FieldValues):
1767
1767
]
1768
1768
invalid_inputs = [
1769
1769
('not a list' , ['Expected a list of items but got type "str".' ]),
1770
- ([1 , 2 , 'error' ], ['A valid integer is required.' ]),
1770
+ ([1 , 2 , 'error' , 'error' ], { 2 : ['A valid integer is required.' ], 3 : [ 'A valid integer is required.' ]} ),
1771
1771
({'one' : 'two' }, ['Expected a list of items but got type "dict".' ])
1772
1772
]
1773
1773
outputs = [
@@ -1794,6 +1794,25 @@ def test_collection_types_are_invalid_input(self):
1794
1794
assert exc_info .value .detail == ['Expected a list of items but got type "dict".' ]
1795
1795
1796
1796
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
+
1797
1816
class TestEmptyListField (FieldValues ):
1798
1817
"""
1799
1818
Values for `ListField` with allow_empty=False flag.
@@ -1834,13 +1853,13 @@ class TestUnvalidatedListField(FieldValues):
1834
1853
1835
1854
class TestDictField (FieldValues ):
1836
1855
"""
1837
- Values for `ListField ` with CharField as child.
1856
+ Values for `DictField ` with CharField as child.
1838
1857
"""
1839
1858
valid_inputs = [
1840
1859
({'a' : 1 , 'b' : '2' , 3 : 3 }, {'a' : '1' , 'b' : '2' , '3' : '3' }),
1841
1860
]
1842
1861
invalid_inputs = [
1843
- ({'a' : 1 , 'b' : None }, ['This field may not be null.' ]),
1862
+ ({'a' : 1 , 'b' : None , 'c' : None }, { 'b' : ['This field may not be null.' ], 'c' : [ 'This field may not be null.' ]} ),
1844
1863
('not a dict' , ['Expected a dictionary of items but got type "str".' ]),
1845
1864
]
1846
1865
outputs = [
@@ -1866,9 +1885,26 @@ def test_allow_null(self):
1866
1885
assert output is None
1867
1886
1868
1887
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
+
1869
1905
class TestDictFieldWithNullChild (FieldValues ):
1870
1906
"""
1871
- Values for `ListField ` with allow_null CharField as child.
1907
+ Values for `DictField ` with allow_null CharField as child.
1872
1908
"""
1873
1909
valid_inputs = [
1874
1910
({'a' : None , 'b' : '2' , 3 : 3 }, {'a' : None , 'b' : '2' , '3' : '3' }),
@@ -1883,7 +1919,7 @@ class TestDictFieldWithNullChild(FieldValues):
1883
1919
1884
1920
class TestUnvalidatedDictField (FieldValues ):
1885
1921
"""
1886
- Values for `ListField ` with no `child` argument.
1922
+ Values for `DictField ` with no `child` argument.
1887
1923
"""
1888
1924
valid_inputs = [
1889
1925
({'a' : 1 , 'b' : [4 , 5 , 6 ], 1 : 123 }, {'a' : 1 , 'b' : [4 , 5 , 6 ], '1' : 123 }),
0 commit comments