From 1bb3669d0864599fd2ede15d262c5d02f68ca288 Mon Sep 17 00:00:00 2001 From: Ryan P Kilby Date: Thu, 21 Dec 2017 16:16:45 -0500 Subject: [PATCH] Add nested ListField/DictField tests --- tests/test_fields.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/test_fields.py b/tests/test_fields.py index 3febb3315..bc11cd133 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -1794,6 +1794,25 @@ class TestListField(FieldValues): assert exc_info.value.detail == ['Expected a list of items but got type "dict".'] +class TestNestedListField(FieldValues): + """ + Values for nested `ListField` with IntegerField as child. + """ + valid_inputs = [ + ([[1, 2], [3]], [[1, 2], [3]]), + ([[]], [[]]) + ] + invalid_inputs = [ + (['not a list'], {0: ['Expected a list of items but got type "str".']}), + ([[1, 2, 'error'], ['error']], {0: {2: ['A valid integer is required.']}, 1: {0: ['A valid integer is required.']}}), + ([{'one': 'two'}], {0: ['Expected a list of items but got type "dict".']}) + ] + outputs = [ + ([[1, 2], [3]], [[1, 2], [3]]), + ] + field = serializers.ListField(child=serializers.ListField(child=serializers.IntegerField())) + + class TestEmptyListField(FieldValues): """ Values for `ListField` with allow_empty=False flag. @@ -1866,6 +1885,23 @@ class TestDictField(FieldValues): assert output is None +class TestNestedDictField(FieldValues): + """ + Values for nested `DictField` with CharField as child. + """ + valid_inputs = [ + ({0: {'a': 1, 'b': '2'}, 1: {3: 3}}, {'0': {'a': '1', 'b': '2'}, '1': {'3': '3'}}), + ] + invalid_inputs = [ + ({0: {'a': 1, 'b': None}, 1: {'c': None}}, {'0': {'b': ['This field may not be null.']}, '1': {'c': ['This field may not be null.']}}), + ({0: 'not a dict'}, {'0': ['Expected a dictionary of items but got type "str".']}), + ] + outputs = [ + ({0: {'a': 1, 'b': '2'}, 1: {3: 3}}, {'0': {'a': '1', 'b': '2'}, '1': {'3': '3'}}), + ] + field = serializers.DictField(child=serializers.DictField(child=serializers.CharField())) + + class TestDictFieldWithNullChild(FieldValues): """ Values for `DictField` with allow_null CharField as child.