"""Test ows-sales-goals model.""" from unittest.mock import patch from owsrequest import request import pytest from requests import Response from requests.exceptions import ConnectionError from salessheets.models import ows_sales_goals def throw(e): """Raise exception.""" raise e @pytest.mark.parametrize( ('sg_status', 'sg_message', 'gsgfp_status', 'gsgfp_message', 'gsgfp_errors'), [ (200, b'{"applesauce": "bananas"}', 200, {'applesauce': 'bananas'}, None), (200, b'', 500, None, { 'code': 'internal_error', 'message': ( 'ows-sales-goals GET /marketing_highlights/123456' ' returned status: 200 message: ""')}), (200, b'{}', 500, None, { 'code': 'internal_error', 'message': ( 'ows-sales-goals GET /marketing_highlights/123456' ' returned status: 200 message: "{}"')}), (200, b'applesauce bananas', 500, None, { 'code': 'internal_error', 'message': ( 'ows-sales-goals GET /marketing_highlights/123456 ' 'returned status: 200 message: "applesauce bananas"')}), (404, b'{"applesauce": "bananas"}', 404, {'applesauce': 'bananas'}, None), (500, b'{"applesauce": "bananas"}', 500, None, {'code': 'internal_error', 'message': ( 'ows-sales-goals GET /marketing_highlights/123456 returned status' ': 500 message: {"applesauce": "bananas"}')}) ]) def test_get_marketing_highlights_for_project( mocker, sg_status, sg_message, gsgfp_status, gsgfp_message, gsgfp_errors): """Test get_marketing_highlights_for_project.""" sales_goals_response = Response() sales_goals_response.status_code = sg_status sales_goals_response._content = sg_message mocker.patch.object(request, 'process', return_value=sales_goals_response) gsgfp_response = ows_sales_goals\ .get_marketing_highlights_for_project(123456) assert gsgfp_response.status == gsgfp_status assert gsgfp_response.message == gsgfp_message assert gsgfp_response.errors == gsgfp_errors @patch('salessheets.models.ows_sales_goals.sentry_capture_exception') def test_get_marketing_highlights_for_project_connection_error( mock_capture_exception, mocker): """Test get_marketing_highlights_for_project connection error.""" connection_exception = ConnectionError('Oops!') mocker.patch.object( request, 'process', lambda **kw: throw(connection_exception)) gsgfp_response = ows_sales_goals\ .get_marketing_highlights_for_project(123456) assert mock_capture_exception.call_count == 1 assert gsgfp_response.status == 500 assert gsgfp_response.errors == { 'code': 'internal_error', 'message': 'Oops!'} @patch('salessheets.models.ows_sales_goals.sentry_capture_message') def test_get_marketing_highlights_for_project_unexpected_status( mock_capture_message, mocker): """Test get_marketing_highlights_for_project unexpected status.""" sales_goals_response = Response() sales_goals_response.status_code = 503 sales_goals_response._content = b'{"error": "Oopsies!"}' mocker.patch.object(request, 'process', return_value=sales_goals_response) gsgfp_response = ows_sales_goals\ .get_marketing_highlights_for_project(123456) message = ( 'ows-sales-goals GET /marketing_highlights/123456 returned status: 503' ' message: {"error": "Oopsies!"}') mock_capture_message.assert_called_once_with(message) assert gsgfp_response.status == 500 assert gsgfp_response.errors == { 'code': 'internal_error', 'message': message }