def test_get_for_object(self): c1 = DataContext() c1.save() r1 = Revision.objects.create_revision(c1, fields=['name']) c2 = DataContext() c2.save() r2 = Revision.objects.create_revision(c2, fields=['name']) # Standard, model extracted from instance self.assertEqual(Revision.objects.get_for_object(c1)[0], r1) # Explicity pass model self.assertEqual( Revision.objects.get_for_object(c1, model=DataContext)[0], r1) # Pass queryset containing target object queryset = DataContext.objects.filter(pk=c1.pk) self.assertEqual( Revision.objects.get_for_object(c1, model=queryset)[0], r1) # Pass queryset *not* containing target object queryset = DataContext.objects.filter(pk=c2.pk) self.assertEqual( list(Revision.objects.get_for_object(c1, model=queryset)), [])
def get_context(self, request): params = self.get_params(request) context = params.get('context') # Explicit request to not use a context if context != 'null': kwargs = { 'archived': False, } if hasattr(request, 'user') and request.user.is_authenticated(): kwargs['user'] = request.user else: if request.session.session_key is None: return DataContext() kwargs['session_key'] = request.session.session_key # Assume it is a primary key and fallback to the sesssion try: kwargs['pk'] = int(context) except (ValueError, TypeError): kwargs['session'] = True try: return DataContext.objects.get(**kwargs) except DataContext.DoesNotExist: pass return DataContext()
def test_delete(self): ctx1 = DataContext(user=self.user, name='Context 1') ctx1.save() ctx2 = DataContext(user=self.user, name='Context 2') ctx2.save() ctx3 = DataContext(user=self.user, name='Context 3', session=True) ctx3.save() response = self.client.get('/api/contexts/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertEqual(len(json.loads(response.content)), 3) response = self.client.delete('/api/contexts/{0}/'.format(ctx1.pk)) self.assertEqual(response.status_code, codes.no_content) response = self.client.get('/api/contexts/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertEqual(len(json.loads(response.content)), 2) # Cannot delete the session response = self.client.delete('/api/contexts/{0}/'.format(ctx3.pk)) self.assertEqual(response.status_code, codes.bad_request) response = self.client.get('/api/contexts/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertEqual(len(json.loads(response.content)), 2)
def test_multiple_contexts(self): cxt1 = DataContext(session=True, user=self.user) cxt1.save() cxt2 = DataContext(user=self.user, session=True) cxt2.save() response = self.client.get('/api/data/preview/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok)
def setUp(self): c1 = DataContext({'field': 1, 'operator': 'exact', 'value': 30}) c1.save() Revision.objects.create_revision(c1, fields=['json']) c2 = DataContext({'field': 2, 'operator': 'in', 'value': [1, 2]}) c2.save() Revision.objects.create_revision(c2, fields=['json']) self.c1 = c1 self.c2 = c2
def test_clean(self): # Save a default template cxt = DataContext(template=True, default=True) cxt.save() # Save new template (not default) cxt2 = DataContext(template=True) cxt2.save() # Try changing it to default cxt2.default = True self.assertRaises(ValidationError, cxt2.save) cxt.save()
def test_multiple_context_and_view(self): # Create two Contexts and views, an illegal action that # Our base resource should handle cxt3 = DataContext(session=True, user=self.user) cxt3.save() cxt4 = DataContext(user=self.user, session=True) cxt4.save() dv3 = DataView(session=True, user=self.user) dv3.save() dv4 = DataView(session=True, user=self.user) dv4.save() response = self.client.get('/api/data/preview/', HTTP_ACCEPT='application/json') self.assertTrue(response.content) self.assertEqual(response.status_code, codes.ok)
def test_put(self): # Add a context so we can try to update it later. ctx = DataContext(user=self.user, name='Context 1') ctx.save() response = self.client.get('/api/contexts/{0}/'.format(ctx.pk), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) # Attempt to update the name via a PUT request. response = self.client.put('/api/contexts/{0}/'.format(ctx.pk), data=u'{"name":"New Name"}', content_type='application/json') self.assertEqual(response.status_code, codes.ok) # Make sure our changes from the PUT request are persisted. response = self.client.get('/api/contexts/{0}/'.format(ctx.pk), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) self.assertEqual(json.loads(response.content)['name'], 'New Name') # Make a PUT request with invalid JSON and make sure we get an # unprocessable status code back. response = self.client.put('/api/contexts/{0}/'.format(ctx.pk), data=u'{"json":"]]]"}', content_type='application/json') self.assertEqual(response.status_code, codes.unprocessable_entity)
def test_create_revision(self): c = DataContext(name='Test', json={}) c.save() revision = Revision.objects.create_revision( c, fields=['name', 'description', 'json']) self.assertEqual(revision.data, { 'name': 'Test', 'description': None, 'json': {} }) revisions = Revision.objects.get_for_object(c) self.assertEqual(revisions.count(), 1) self.assertFalse(Revision.objects.object_has_changed(c)) self.assertEqual( revisions[0].changes, { 'name': { 'new_value': 'Test' }, 'description': { 'new_value': None }, 'json': { 'new_value': {} } })
def test_change(self): c = DataContext() c.save() revisions = Revision.objects.get_for_object(c) self.assertEqual(revisions.count(), 1) latest_revision = Revision.objects.latest_for_object(c) self.assertFalse('old_value' in latest_revision.changes) self.assertEqual( latest_revision.changes, { 'json': { 'new_value': {} }, 'name': { 'new_value': None }, 'description': { 'new_value': None } }) c.name = "New Name" c.save() revisions = Revision.objects.get_for_object(c) self.assertEqual(revisions.count(), 2) latest_revision = Revision.objects.latest_for_object(c) self.assertEqual( latest_revision.changes, {'name': { 'old_value': None, 'new_value': 'New Name' }})
def test_init(self): json = { 'field': 'tests.title.salary', 'operator': 'gt', 'value': '1000' } cxt = DataContext(json) self.assertEqual(cxt.json, json)
def test_session(self): cxt = DataContext(session=True, user=self.user) cxt.save() response = self.client.get('/api/contexts/session/stats/', HTTP_ACCEPT='application/json') self.assertEqual(json.loads(response.content)['count'], 6)
def test_get(self): ctx = DataContext(user=self.user) ctx.save() response = self.client.get('/api/contexts/revisions/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertEqual(len(json.loads(response.content)), 1)
def __init__(self, *args, **kwargs): kwargs['context'] = DataContext(json={ 'field': 'tests.title.salary', 'operator': 'lt', 'value': 20000, }) super(UnderTwentyThousandQueryProcessor, self)\ .__init__(*args, **kwargs)
def __init__(self, *args, **kwargs): kwargs['context'] = DataContext( json={ 'field': 'tests.employee.is_manager', 'operator': 'exact', 'value': True, }) super(ManagerQueryProcessor, self).__init__(*args, **kwargs)
def test_nochange(self): c = DataContext() c.save() revisions = Revision.objects.get_for_object(c) self.assertEqual(revisions.count(), 1) # No changes, no new revision c.save() self.assertEqual(revisions.count(), 1)
def test_count(self): json = { 'field': 'tests.title.salary', 'operator': 'gt', 'value': '10000' } ctx = DataContext(json) self.assertEqual(ctx.count(), 6) self.assertEqual(ctx.count(tree='office'), 1)
def test_deleted_revision(self): c = DataContext(name='Test', json={}) c.save() fields = ['name', 'description', 'json'] revision = Revision.objects.create_revision( c, fields=fields, deleted=True) self.assertEqual(revision.data, None) self.assertEqual(revision.changes, None)
def test_export_type(self): context = DataContext() view = DataView() export_type = 'json' query_options = { 'export_type': export_type, } result = utils.get_result_rows(context, view, query_options) self.assertEqual(len(list(result['rows'])), Employee.objects.count()) self.assertEqual(result['export_type'], export_type)
def test_processor(self): context = DataContext() view = DataView() processor = 'manager' query_options = { 'processor': processor, } result = utils.get_result_rows(context, view, query_options) self.assertEqual(len(list(result['rows'])), Employee.objects.filter(is_manager=True).count()) self.assertTrue(isinstance(result['processor'], ManagerQueryProcessor))
def test_limit(self): context = DataContext() view = DataView() limit = 2 query_options = { 'limit': limit, 'page': 1, } result = utils.get_result_rows(context, view, query_options) self.assertEqual(len(list(result['rows'])), limit) self.assertEqual(result['limit'], limit)
def test_apply(self): c = DataContext({'field': 1, 'operator': 'exact', 'value': 30}) c.save() revision = Revision.objects.create_revision(c, fields=['json']) c.json['value'] = 50 c.save() revision.apply(c) self.assertEqual(c.json['value'], 30)
def test_get_rows(self): context = DataContext() view = DataView() # Unless we tell the function to evaluate the rows, it should return # rows as a generator so we need to exclicitly evaluate it here. result = utils.get_result_rows(context, view, {}) self.assertEqual(len(list(result['rows'])), Employee.objects.count()) # Now, have the method evaluate the rows. result = utils.get_result_rows(context, view, {}, evaluate_rows=True) self.assertEqual(len(result['rows']), Employee.objects.count())
def test_processor(self): cxt = DataContext(session=True, user=self.user) cxt.save() response = self.client.get('/api/contexts/{0}/stats/'.format(cxt.pk), HTTP_ACCEPT='application/json') self.assertEqual(json.loads(response.content)['count'], 6) response = self.client.get( '/api/contexts/{0}/stats/?processor=manager'.format(cxt.pk), HTTP_ACCEPT='application/json') self.assertEqual(json.loads(response.content)['count'], 1)
def test_get_order_only(self): field = DataField.objects.get(field_name='salary') concept = field.concepts.all()[0] context = DataContext() view = DataView(json=[{ 'concept': concept.pk, 'visible': False, 'sort': 'desc', }]) result = utils.get_result_rows(context, view, {}) self.assertEqual(len(list(result['rows'])), Employee.objects.count())
def test_invalid_job_result(self): context = DataContext() view = DataView() query_options = { 'page': 0, } job_id = utils.async_get_result_rows(context, view, query_options) self.assertTrue(async_utils.get_job_count(), 1) async_utils.run_jobs() time.sleep(1) self.assertEqual(async_utils.get_job_result(job_id), None) self.assertEqual(async_utils.get_job(job_id).status, JobStatus.FAILED)
def test_get_object_data(self): c = DataContext() fields = history.utils.get_model_fields(DataContext) data = history.utils.get_object_data(c, fields=fields) self.assertEqual(data, { 'default': False, 'description': None, 'json': {}, 'keywords': None, 'name': None, 'session': False, 'session_key': None, 'template': False, })
def test_pages(self): context = DataContext() view = DataView() query_options = { 'page': 1, 'stop_page': 10, } result = utils.get_result_rows(context, view, query_options) self.assertEqual(len(list(result['rows'])), Employee.objects.count()) query_options = { 'page': 1, 'stop_page': 1, } result = utils.get_result_rows(context, view, query_options) self.assertEqual(len(list(result['rows'])), Employee.objects.count())
def test_get_session(self): context = DataContext(user=self.user, name='Session Context', session=True) context.save() response = self.client.get('/api/contexts/session/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) context.session = False context.save() response = self.client.get('/api/contexts/session/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found)
def test_get(self): ctx = DataContext(user=self.user) ctx.save() response = self.client.get('/api/contexts/{0}/'.format(ctx.pk), HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.ok) self.assertTrue(response.content) content = json.loads(response.content) self.assertEqual(content['object_name'], 'employee') self.assertLess(ctx.accessed, DataContext.objects.get(pk=ctx.pk).accessed) # Make sure that accessing a non-existent context returns a 404 error # indicating that it wasn't found. response = self.client.get('/api/contexts/999/', HTTP_ACCEPT='application/json') self.assertEqual(response.status_code, codes.not_found)