def test_list_usage_exists_no_custom_filters(self): fake_request = self.mox.CreateMockAnything() fake_request.GET = {} self.mox.StubOutWithMock(dbapi, 'get_db_objects') objects = self.mox.CreateMockAnything() dbapi.get_db_objects(models.InstanceExists, fake_request, 'id', custom_filters={}).AndReturn(objects) self.mox.StubOutWithMock(dbapi, '_convert_model_list') dbapi._convert_model_list(objects, dbapi._exists_extra_values) self.mox.ReplayAll() resp = dbapi.list_usage_exists(fake_request) self.assertEqual(resp.status_code, 200) self.mox.VerifyAll()
def test_get_db_objects_with_custom_filter(self): fake_model = self.make_fake_model() fake_request = self.mox.CreateMockAnything() filters = {'instance': INSTANCE_ID_1} custom_filters = {'raw': {'raw__id': 1}} fake_request.GET = filters self.mox.StubOutWithMock(dbapi, '_get_filter_args') dbapi._get_filter_args( fake_model, fake_request, custom_filters=custom_filters).AndReturn(filters) self.mox.StubOutWithMock(dbapi, '_check_has_field') dbapi._check_has_field(fake_model, 'id') result = self.mox.CreateMockAnything() all_filters = {} all_filters.update(filters) all_filters.update(custom_filters['raw']) fake_model.objects.filter(**all_filters).AndReturn(result) result.order_by('-id').AndReturn(result) s = slice(None, dbapi.DEFAULT_LIMIT, None) result.__getitem__(s).AndReturn(result) self.mox.ReplayAll() query_result = dbapi.get_db_objects(fake_model, fake_request, 'id', custom_filters=custom_filters) self.assertEquals(query_result, result) self.mox.VerifyAll()
def test_list_usage_exists_with_received_max(self): fake_request = self.mox.CreateMockAnything() date = str(datetime.datetime.utcnow()) fake_request.GET = {'received_max': date} self.mox.StubOutWithMock(dbapi, 'get_db_objects') unix_date = stacktach_utils.str_time_to_unix(date) custom_filters = {'received_max': {'raw__when__lte': unix_date}} objects = self.mox.CreateMockAnything() dbapi.get_db_objects(models.InstanceExists, fake_request, 'id', custom_filters=custom_filters).AndReturn(objects) self.mox.StubOutWithMock(dbapi, '_convert_model_list') dbapi._convert_model_list(objects, dbapi._exists_extra_values) self.mox.ReplayAll() resp = dbapi.list_usage_exists(fake_request) self.assertEqual(resp.status_code, 200) self.mox.VerifyAll()
def test_get_db_objects_with_custom_filter(self): fake_model = self.make_fake_model() fake_request = self.mox.CreateMockAnything() filters = {'instance': INSTANCE_ID_1} custom_filters = {'raw': {'raw__id': 1}} fake_request.GET = filters self.mox.StubOutWithMock(dbapi, '_get_filter_args') dbapi._get_filter_args(fake_model, fake_request, custom_filters=custom_filters).AndReturn(filters) self.mox.StubOutWithMock(dbapi, '_check_has_field') dbapi._check_has_field(fake_model, 'id') result = self.mox.CreateMockAnything() all_filters = {} all_filters.update(filters) all_filters.update(custom_filters['raw']) fake_model.objects.filter(**all_filters).AndReturn(result) result.order_by('-id').AndReturn(result) s = slice(None, dbapi.DEFAULT_LIMIT, None) result.__getitem__(s).AndReturn(result) self.mox.ReplayAll() query_result = dbapi.get_db_objects(fake_model, fake_request, 'id', custom_filters=custom_filters) self.assertEquals(query_result, result) self.mox.VerifyAll()
def test_list_usage_deletes_for_glance(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'GET' fake_request.GET = {} self.mox.StubOutWithMock(dbapi, 'get_db_objects') mock_objects = self.mox.CreateMockAnything() deletes = {'a': 1} self.mox.StubOutWithMock(dbapi, '_convert_model_list') dbapi._convert_model_list(mock_objects).AndReturn(deletes) dbapi.get_db_objects(models.ImageDeletes, fake_request, 'deleted_at').AndReturn(mock_objects) self.mox.ReplayAll() resp = dbapi.list_usage_deletes_glance(fake_request) self.assertEqual(resp.status_code, 200) self.assertEqual(json.loads(resp.content), {'deletes': deletes}) self.mox.VerifyAll()
def test_list_usage_launches_without_service(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'GET' fake_request.GET = {} self.mox.StubOutWithMock(dbapi, 'get_db_objects') mock_objects = self.mox.CreateMockAnything() launches = {'a': 1} self.mox.StubOutWithMock(dbapi, '_convert_model_list') dbapi._convert_model_list(mock_objects).AndReturn(launches) dbapi.get_db_objects(models.InstanceUsage, fake_request, 'launched_at').AndReturn(mock_objects) self.mox.ReplayAll() resp = dbapi.list_usage_launches(fake_request) self.assertEqual(resp.status_code, 200) self.assertEqual(json.loads(resp.content), {'launches': launches}) self.mox.VerifyAll()
def test_get_db_objects_offset_and_limit(self): fake_model = self.make_fake_model() fake_request = self.mox.CreateMockAnything() fake_request.GET = {'offset': '2', 'limit': '2'} self.mox.StubOutWithMock(dbapi, '_get_filter_args') dbapi._get_filter_args(fake_model, fake_request, custom_filters=None).AndReturn({}) self.mox.StubOutWithMock(dbapi, '_check_has_field') dbapi._check_has_field(fake_model, 'id') result = self.mox.CreateMockAnything() fake_model.objects.all().AndReturn(result) result.order_by('-id').AndReturn(result) result.__getslice__(2, 4).AndReturn(result) self.mox.ReplayAll() query_result = dbapi.get_db_objects(fake_model, fake_request, 'id') self.assertEquals(query_result, result) self.mox.VerifyAll()
def test_get_db_objects_offset_and_limit(self): fake_model = self.make_fake_model() fake_request = self.mox.CreateMockAnything() fake_request.GET = {'offset': 2, 'limit': 2} self.mox.StubOutWithMock(dbapi, '_get_filter_args') dbapi._get_filter_args(fake_model, fake_request, custom_filters=None).AndReturn({}) self.mox.StubOutWithMock(dbapi, '_check_has_field') dbapi._check_has_field(fake_model, 'id') result = self.mox.CreateMockAnything() fake_model.objects.all().AndReturn(result) result.order_by('-id').AndReturn(result) result.__getslice__(2, 4).AndReturn(result) self.mox.ReplayAll() query_result = dbapi.get_db_objects(fake_model, fake_request, 'id') self.assertEquals(query_result, result) self.mox.VerifyAll()
def test_get_db_objects_desc(self): fake_model = self.make_fake_model() fake_request = self.mox.CreateMockAnything() fake_request.GET = {'direction': 'desc'} self.mox.StubOutWithMock(dbapi, '_get_filter_args') dbapi._get_filter_args(fake_model, fake_request, custom_filters=None).AndReturn({}) self.mox.StubOutWithMock(dbapi, '_check_has_field') dbapi._check_has_field(fake_model, 'id') result = self.mox.CreateMockAnything() fake_model.objects.all().AndReturn(result) result.order_by('-id').AndReturn(result) result.__getitem__(slice(None, None, None)).AndReturn(result) self.mox.ReplayAll() query_result = dbapi.get_db_objects(fake_model, fake_request, 'id') self.assertEquals(query_result, result) self.mox.VerifyAll()
def test_get_db_objects_hard_limit(self): fake_model = self.make_fake_model() fake_request = self.mox.CreateMockAnything() fake_request.GET = {'limit': str(dbapi.HARD_LIMIT + 1)} self.mox.StubOutWithMock(dbapi, '_get_filter_args') dbapi._get_filter_args(fake_model, fake_request, custom_filters=None).AndReturn({}) self.mox.StubOutWithMock(dbapi, '_check_has_field') dbapi._check_has_field(fake_model, 'id') result = self.mox.CreateMockAnything() fake_model.objects.all().AndReturn(result) result.order_by('-id').AndReturn(result) s = slice(None, dbapi.HARD_LIMIT, None) result.__getitem__(s).AndReturn(result) self.mox.ReplayAll() query_result = dbapi.get_db_objects(fake_model, fake_request, 'id') self.assertEquals(query_result, result) self.mox.VerifyAll()
def test_get_db_objects_with_filter(self): fake_model = self.make_fake_model() fake_request = self.mox.CreateMockAnything() filters = {'instance': INSTANCE_ID_1} fake_request.GET = filters self.mox.StubOutWithMock(dbapi, '_get_filter_args') dbapi._get_filter_args(fake_model, fake_request, custom_filters=None).AndReturn(filters) self.mox.StubOutWithMock(dbapi, '_check_has_field') dbapi._check_has_field(fake_model, 'id') result = self.mox.CreateMockAnything() fake_model.objects.filter(**filters).AndReturn(result) result.order_by('id').AndReturn(result) result.__getitem__(slice(None, None, None)).AndReturn(result) self.mox.ReplayAll() query_result = dbapi.get_db_objects(fake_model, fake_request, 'id') self.assertEquals(query_result, result) self.mox.VerifyAll()