"""Tests for ows-carveouts Model.""" from unittest.mock import MagicMock from unittest.mock import Mock from owsrequest import request as requests import sentry_sdk from ows_product_physical import api from ows_product_physical.models import ows_carveouts def test_set_carveins_for_physical_product_success( monkeypatch, valid_get_header, context): """Test setting carveins for physical product success.""" with context, api.app.test_request_context(headers=valid_get_header): mock_response = Mock() mock_response.status_code = 200 monkeypatch.setattr(requests, 'post', MagicMock( return_value=mock_response)) carvein_response = \ ows_carveouts.set_carveins_for_physical_product(234567, 8888) assert requests.post.called assert requests.post.call_args[1].get('data').get('vendor_id') == 8888 assert requests.post.call_args[0][1] == \ '/carveout/product/234567/carveout-default-phys-dms' assert carvein_response.status == 200 def test_set_carveins_for_physical_product_fail( monkeypatch, valid_get_header, context): """Test setting carveins for physical product fail.""" with context, api.app.test_request_context(headers=valid_get_header): mock_response = Mock() mock_response.status_code = 401 monkeypatch.setattr(requests, 'post', value=MagicMock( return_value=mock_response)) carvein_response = \ ows_carveouts.set_carveins_for_physical_product(234567, 8888) assert requests.post.called assert requests.post.call_args[1].get('data').get('vendor_id') == 8888 assert requests.post.call_args[0][1] == \ '/carveout/product/234567/carveout-default-phys-dms' assert carvein_response.status == 401 def test_set_carveins_for_physical_product_exception( monkeypatch, valid_get_header, context): """Test setting carveins for physical product exception.""" with context, api.app.test_request_context(headers=valid_get_header): monkeypatch.delattr(requests, 'post') monkeypatch.setattr( sentry_sdk, 'capture_exception', value=MagicMock()) carvein_response = \ ows_carveouts.set_carveins_for_physical_product(234567, 8888) assert sentry_sdk.capture_exception.called assert carvein_response.status != 200 def test_get_release_carveouts_for_upc_success( monkeypatch, valid_get_header, context): """Test setting carveins for physical product success.""" with context, api.app.test_request_context(headers=valid_get_header): mock_response = Mock() mock_response.status_code = 200 monkeypatch.setattr(requests, 'get', MagicMock( return_value=mock_response)) carvein_response = \ ows_carveouts.get_release_carveouts_for_upc(234567) assert requests.get.called assert carvein_response.status == 200 def test_get_release_carveouts_for_upc_fail( monkeypatch, valid_get_header, context): """Test setting carveins for physical product fail.""" with context, api.app.test_request_context(headers=valid_get_header): mock_response = Mock() mock_response.status_code = 401 monkeypatch.setattr(requests, 'get', value=MagicMock( return_value=mock_response)) carvein_response = \ ows_carveouts.get_release_carveouts_for_upc(234567) assert requests.get.called assert carvein_response.status == 401 def test_get_release_carveouts_for_upc_exception( monkeypatch, valid_get_header, context): """Test setting carveins for physical product exception.""" with context, api.app.test_request_context(headers=valid_get_header): monkeypatch.delattr(requests, 'get') monkeypatch.setattr( sentry_sdk, 'capture_exception', value=MagicMock()) carvein_response = \ ows_carveouts.get_release_carveouts_for_upc(234567) assert sentry_sdk.capture_exception.called assert carvein_response.status != 200