"""Tests for carveouts model.""" from collections import namedtuple from owsrequest import request as requests import sentry_sdk from product.constants import service_name from product.models import ows_carveouts def test_delete_carveouts_success(mocker, test_product): """Test success deletion.""" response = namedtuple('response', 'status_code') mocker.patch.object( requests, 'put', return_value=response(status_code=200), autospec=True) assert ows_carveouts.delete_carveout(test_product) def test_delete_carveouts_fail_exception( monkeypatch, mocker, test_product): """Test exception handler.""" monkeypatch.delattr(requests, 'put') mocker.patch.object( sentry_sdk, 'capture_exception', return_value='', autospec=True) result = ows_carveouts.delete_carveout(test_product) assert not result assert sentry_sdk.capture_exception.called def test_delete_carveouts_error_response(mocker, test_product): """Test success deletion.""" response = namedtuple('response', 'status_code') mocker.patch.object( requests, 'put', return_value=response(status_code=500), autospec=True) result = ows_carveouts.delete_carveout(test_product) assert not result def test_model_copy_carveouts_success(ows_request_mocker): """Test copy carveouts success.""" existing_product_id = 93 new_product_id = 98765 ows_request_mocker.add({ 'method': 'post', 'service': service_name.OWS_CARVEOUTS, 'path': '/carveout/product/{}/copy/{}'.format( existing_product_id, new_product_id), 'status': 201 }) ows_request_mocker.apply() result = ows_carveouts.copy( existing_product_id, new_product_id) assert result.status == 201 def test_model_copy_carveouts_exception(mocker): """Test copy carveouts when an exception occurs.""" existing_product_id = 93 new_product_id = 98765 mocker.patch.object(requests, 'post', side_effect=Exception('oops')) mocker.patch.object(sentry_sdk, 'capture_exception') result = ows_carveouts.copy(existing_product_id, new_product_id) assert result.status == 500 assert sentry_sdk.capture_exception.called def test_model_copy_product_opp_down(ows_request_mocker): """Test copy carveouts when ows-carveouts returns an error.""" existing_product_id = 93 new_product_id = 9292 ows_request_mocker.add({ 'method': 'post', 'service': service_name.OWS_CARVEOUTS, 'path': '/carveout/product/{}/copy/{}'.format( existing_product_id, new_product_id), 'status': 404 }) ows_request_mocker.apply() result = ows_carveouts.copy( existing_product_id, new_product_id) assert result.status == 404