def setUp(self): self.check_uuid = 'e2e1ec23-707a-4382-a956-f0cf3276a1b8' self.trigger_uuid = 'bd69fcab-5424-44a8-9ac8-24d2c46ec3b8' self.service_id = '999' now = datetime.now() one_minute_ago = now - timedelta(minutes=1) two_minutes_ago = now - timedelta(minutes=2) ok, fail, unknown = 0, 1, 2 results = zip( [two_minutes_ago, one_minute_ago, now], [unknown, fail, ok] ) for scheduled, result in results: kw = dict( check_uuid=self.check_uuid, trigger_uuid=self.trigger_uuid, alert_sent=False, status=result, meta={}, scheduled_time=scheduled, execution_time=scheduled, insertion_time=scheduled, message='', service_id=self.service_id, ) self.result = ServiceResult(**kw) self.check_result = CheckResults(**kw) self.check_result.save() self.result.save()
class CheckApiTestCase(APITestCase): def setUp(self): self.check_uuid = 'e2e1ec23-707a-4382-a956-f0cf3276a1b8' self.trigger_uuid = 'bd69fcab-5424-44a8-9ac8-24d2c46ec3b8' self.service_id = '999' now = datetime.now() one_minute_ago = now - timedelta(minutes=1) two_minutes_ago = now - timedelta(minutes=2) ok, fail, unknown = 0, 1, 2 results = zip( [two_minutes_ago, one_minute_ago, now], [unknown, fail, ok] ) for scheduled, result in results: kw = dict( check_uuid=self.check_uuid, trigger_uuid=self.trigger_uuid, alert_sent=False, status=result, meta={}, scheduled_time=scheduled, execution_time=scheduled, insertion_time=scheduled, message='', service_id=self.service_id, ) self.result = ServiceResult(**kw) self.check_result = CheckResults(**kw) self.check_result.save() self.result.save() def tearDown(self): ServiceResult.objects.filter(service_id=self.service_id, check_uuid=self.check_uuid).delete() CheckResults.objects.filter(check_uuid=self.check_uuid, trigger_uuid=self.trigger_uuid).delete() def test_service_result_count(self): count = ServiceResult.objects.count() self.assertEqual(count, 3) def test_check_result_count(self): count = CheckResults.objects.count() self.assertEqual(count, 3) def test_check_result_list_endpoint(self): detail_url = reverse('api-checkresults-list') response = self.client.get(detail_url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, {}) def test_service_result_endpoint(self): detail_url = reverse('api-checkresults-multiple') response = self.client.post(detail_url, data={ 'service_id': self.result.service_id, 'checks': [ str(self.result.check_uuid)]}, format='json') expected = { self.check_uuid: [ {'trigger_uuid': self.trigger_uuid, 'status': s} for s in [0, 1, 2]] } self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], 3) self.assertEqual(response.data['results'], expected) def test_check_detail_endpoint(self): fields = {'check_uuid', 'insertion_time', 'message', 'meta', 'scheduled_time', 'service_id', 'status', 'trigger_uuid'} detail_url = reverse( 'api-checkresults-detail', kwargs={'check_uuid': self.check_uuid} ) response = self.client.get( detail_url, data={'triggers': self.trigger_uuid}, format='json' ) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], 3) for result in response.data['results']: for field in fields: self.assertIn(field, result)