def test_get_all_fields(self): s = GroupSerializer() all_keys1 = six.iterkeys(s.get_all_fields()) f2 = s.fields all_keys2 = six.iterkeys(s.get_all_fields()) expected = ['id', 'name'] self.assertEqual(list(six.iterkeys(f2)), expected) self.assertEqual(list(all_keys1), list(all_keys2))
def test_data_with_included_has_many(self): request_fields = {'groups': {}} expected = { 'users': [{ 'id': 1, 'name': '0', 'groups': [1, 2], 'location': 1 }, { 'id': 2, 'name': '1', 'groups': [1, 2], 'location': 1 }, { 'id': 3, 'name': '2', 'groups': [1, 2], 'location': 2 }, { 'id': 4, 'name': '3', 'groups': [1, 2], 'location': 3 }], 'groups': [{ 'id': 1, 'name': '0' }, { 'id': 2, 'name': '1' }] } serializer = UserSerializer( self.fixture.users, many=True, envelope=True, request_fields=request_fields, ) self.assertEqual(serializer.data, expected) request_fields = {'members': {}} expected = { 'users': [{ 'id': 1, 'name': '0', 'location': 1 }, { 'id': 2, 'name': '1', 'location': 1 }, { 'id': 3, 'name': '2', 'location': 2 }, { 'id': 4, 'name': '3', 'location': 3 }], 'groups': [{ 'id': 1, 'name': '0', 'members': [1, 2, 3, 4] }, { 'id': 2, 'name': '1', 'members': [1, 2, 3, 4] }] } serializer = GroupSerializer( self.fixture.groups, many=True, envelope=True, request_fields=request_fields, ) self.assertEqual(serializer.data, expected)
def test_nested_filter_rewrite(self): node = FilterNode(['members', 'id'], 'in', [1]) gs = GroupSerializer(include_fields='*') filter_key, field = node.generate_query_key(gs) self.assertEqual(filter_key, 'users__id__in')