def setUp(self): self.view = UserViewSet() self.rf = RequestFactory()
class TestUserViewSet(TestCase): def setUp(self): self.view = UserViewSet() self.rf = RequestFactory() def test_get_request_fields(self): request = Request(self.rf.get('/users/', { 'include[]': ['name', 'groups.permissions'], 'exclude[]': ['groups.name'] })) self.view.request = request fields = self.view.get_request_fields() self.assertEqual({ 'groups': { 'name': False, 'permissions': True }, 'name': True }, fields) def test_get_request_fields_disabled(self): self.view.features = (self.view.INCLUDE) request = Request(self.rf.get('/users/', { 'include[]': ['name', 'groups'], 'exclude[]': ['groups.name'] })) self.view.request = request fields = self.view.get_request_fields() self.assertEqual({ 'groups': True, 'name': True }, fields) def test_get_request_fields_invalid(self): for invalid_field in ('groups..name', 'groups..'): request = Request( self.rf.get('/users/', {'include[]': [invalid_field]})) self.view.request = request self.assertRaises( exceptions.ParseError, self.view.get_request_fields) def test_filter_extraction(self): filters_map = { 'attr': ['bar'], 'attr2.eq': ['bar'], '-attr3': ['bar'], 'rel|attr1': ['val'], '-rel|attr2': ['val'], 'rel.attr': ['baz'], 'rel.bar|attr': ['val'], 'attr4.lt': ['val'], 'attr5.in': ['val1', 'val2', 'val3'], } backend = DynamicFilterBackend() out = backend._extract_filters(filters_map=filters_map) self.assertEqual(out['_include']['attr'].value, 'bar') self.assertEqual(out['_include']['attr2'].value, 'bar') self.assertEqual(out['_exclude']['attr3'].value, 'bar') self.assertEqual(out['rel']['_include']['attr1'].value, 'val') self.assertEqual(out['rel']['_exclude']['attr2'].value, 'val') self.assertEqual(out['_include']['rel__attr'].value, 'baz') self.assertEqual(out['rel']['bar']['_include']['attr'].value, 'val') self.assertEqual(out['_include']['attr4__lt'].value, 'val') self.assertEqual(len(out['_include']['attr5__in'].value), 3) def test_is_null_casting(self): filters_map = { 'f1.isnull': [True], 'f2.isnull': [['a']], 'f3.isnull': ['true'], 'f4.isnull': ['1'], 'f5.isnull': [1], 'f6.isnull': [False], 'f7.isnull': [[]], 'f8.isnull': ['false'], 'f9.isnull': ['0'], 'f10.isnull': [0], 'f11.isnull': [''], 'f12.isnull': [None], } backend = DynamicFilterBackend() out = backend._extract_filters(filters_map=filters_map) self.assertEqual(out['_include']['f1__isnull'].value, True) self.assertEqual(out['_include']['f2__isnull'].value, ['a']) self.assertEqual(out['_include']['f3__isnull'].value, True) self.assertEqual(out['_include']['f4__isnull'].value, True) self.assertEqual(out['_include']['f5__isnull'].value, 1) self.assertEqual(out['_include']['f6__isnull'].value, False) self.assertEqual(out['_include']['f7__isnull'].value, []) self.assertEqual(out['_include']['f8__isnull'].value, False) self.assertEqual(out['_include']['f9__isnull'].value, False) self.assertEqual(out['_include']['f10__isnull'].value, False) self.assertEqual(out['_include']['f11__isnull'].value, False) self.assertEqual(out['_include']['f12__isnull'].value, None) def test_nested_filter_rewrite(self): node = FilterNode(['members', 'id'], 'in', [1]) gs = GroupSerializer(include_fields='*') filter_key = node.generate_query_key(gs) self.assertEqual(filter_key, 'users__id__in')
class TestUserViewSet(TestCase): def setUp(self): self.view = UserViewSet() self.rf = RequestFactory() def test_get_request_fields(self): request = Request( self.rf.get( '/users/', { 'include[]': ['name', 'groups.permissions'], 'exclude[]': ['groups.name'] })) self.view.request = request fields = self.view.get_request_fields() self.assertEqual( { 'groups': { 'name': False, 'permissions': True }, 'name': True }, fields) def test_get_request_fields_disabled(self): self.view.features = (self.view.INCLUDE) request = Request( self.rf.get('/users/', { 'include[]': ['name', 'groups'], 'exclude[]': ['groups.name'] })) self.view.request = request fields = self.view.get_request_fields() self.assertEqual({'groups': True, 'name': True}, fields) def test_get_request_fields_invalid(self): for invalid_field in ('groups..name', 'groups..'): request = Request( self.rf.get('/users/', {'include[]': [invalid_field]})) self.view.request = request self.assertRaises(exceptions.ParseError, self.view.get_request_fields) def test_filter_extraction(self): filters_map = { 'attr': ['bar'], 'attr2.eq': ['bar'], '-attr3': ['bar'], 'rel|attr1': ['val'], '-rel|attr2': ['val'], 'rel.attr': ['baz'], 'rel.bar|attr': ['val'], 'attr4.lt': ['val'], 'attr5.in': ['val1', 'val2', 'val3'], } backend = DynamicFilterBackend() out = backend._get_requested_filters(filters_map=filters_map) self.assertEqual(out['_include']['attr'], 'bar') self.assertEqual(out['_include']['attr2'], 'bar') self.assertEqual(out['_exclude']['attr3'], 'bar') self.assertEqual(out['rel']['_include']['attr1'], 'val') self.assertEqual(out['rel']['_exclude']['attr2'], 'val') self.assertEqual(out['_include']['rel__attr'], 'baz') self.assertEqual(out['rel']['bar']['_include']['attr'], 'val') self.assertEqual(out['_include']['attr4__lt'], 'val') self.assertEqual(len(out['_include']['attr5__in']), 3) def test_is_null_casting(self): filters_map = { 'f1.isnull': [True], 'f2.isnull': [['a']], 'f3.isnull': ['true'], 'f4.isnull': ['1'], 'f5.isnull': [1], 'f6.isnull': [False], 'f7.isnull': [[]], 'f8.isnull': ['false'], 'f9.isnull': ['0'], 'f10.isnull': [0], 'f11.isnull': [''], 'f12.isnull': [None], } backend = DynamicFilterBackend() out = backend._get_requested_filters(filters_map=filters_map) self.assertEqual(out['_include']['f1__isnull'], True) self.assertEqual(out['_include']['f2__isnull'], ['a']) self.assertEqual(out['_include']['f3__isnull'], True) self.assertEqual(out['_include']['f4__isnull'], True) self.assertEqual(out['_include']['f5__isnull'], 1) self.assertEqual(out['_include']['f6__isnull'], False) self.assertEqual(out['_include']['f7__isnull'], []) self.assertEqual(out['_include']['f8__isnull'], False) self.assertEqual(out['_include']['f9__isnull'], False) self.assertEqual(out['_include']['f10__isnull'], False) self.assertEqual(out['_include']['f11__isnull'], False) self.assertEqual(out['_include']['f12__isnull'], None)