def test_delete_alert_policy(self): """ Check that the delete_alert_policy function behaves correctly if DELETE request unsuccessful. We expect the function *not* to raise an exception if the alert policy to delete can not be found (i.e., if the DELETE request comes back with a 404). In all other cases the function should raise an exception. """ policy_id = 1 client_errors = [status_code for status_code in requests.status_codes._codes if 400 <= status_code < 500] server_errors = [status_code for status_code in requests.status_codes._codes if 500 <= status_code < 600] for error in client_errors + server_errors: with responses.RequestsMock() as mock_responses: mock_responses.add( responses.DELETE, '{}/{}.json'.format(newrelic.ALERTS_POLICIES_API_URL, policy_id), status=error ) try: newrelic.delete_alert_policy(policy_id) except requests.exceptions.HTTPError: if error == requests.codes.not_found: self.fail('Should not raise an exception for {} response.'.format(error)) else: if not error == requests.codes.not_found: self.fail('Should raise an exception for {} response.'.format(error))
def delete(self, *args, **kwargs): # pylint: disable=arguments-differ """ Delete this alert policy. """ newrelic.delete_alert_policy(self.id) super().delete(*args, **kwargs)