"""Test ows-carveouts-python.""" import json from http.client import INTERNAL_SERVER_ERROR, OK from unittest.mock import MagicMock import pytest import requests from owsrequest import request from abacus_legacy_sync.config import Config from abacus_legacy_sync.constants import services from abacus_legacy_sync.models.ows import carveouts_python ows_carveouts_response_json = """{ 'account': { 'service': [ { 'service_id': 1, 'service_name': 'iTunes/Apple', 'distribution_types': [1, 3] }, { 'service_id': 11, 'service_name': 'TDC Play', 'distribution_types': [3] } ], 'service_country': [], 'country': [ 'RU' ], 'new_service_opt_outs': [] }, 'subaccount': { 'service': [], 'service_country': [], 'country': [] }, 'product': { 'service': [ { 'service_id': 1, 'service_name': 'iTunes/Apple', 'distribution_types': [3] } ], 'service_country': [], 'country': [], 'new_service_opt_outs': [] } } """ def test_get_carveouts_success(mocker): """Test get_carveouts_python success.""" ows_carveouts_response = requests.Response() ows_carveouts_response.status_code = OK ows_carveouts_response._content = json.dumps(ows_carveouts_response_json).encode() mocker.patch.object( request, 'process', return_value=ows_carveouts_response, autospec=True ) carveouts = carveouts_python.get_account_carveouts(123) assert carveouts == ows_carveouts_response_json def test_get_carveouts_error(mocker): """Test get_carveouts_python error.""" ows_carveouts_response = requests.Response() ows_carveouts_response.status_code = INTERNAL_SERVER_ERROR mocker.patch.object( request, 'process', return_value=ows_carveouts_response, autospec=True ) with pytest.raises(requests.exceptions.HTTPError) as excinfo: carveouts_python.get_account_carveouts(123456) assert '500 Server Error' in str(excinfo.value) def test_save_carveouts_success(mocker): """Test save_carveouts function success.""" mock_response = MagicMock() mock_response.raise_for_status.return_value = None # No error # Patch request.process to return the mock response mock_object = mocker.patch.object( request, 'process', return_value=mock_response, autospec=True ) post_data = { 'service': [ {'service_id': 1, 'distribution_types': [3]}, {'service_id': 1208, 'distribution_types': [3]}, ], } account_id = 123 carveouts_python.save_account_carveouts(account_id, post_data) # Verify that request.process was called with correct arguments mock_object.assert_called_once_with( application=Config.SERVICE_NAME, environment=Config.ENVIRONMENT, method='POST', service_name=services.OWS_CARVEOUTS_PYTHON, uwsgi_cache_enabled=True, path=f'/delivery-restrictions/account/{account_id}', json=post_data, ) mock_response.raise_for_status.assert_called_once() def test_save_carveouts_error(mocker): """Test save_carveouts function in error case.""" ows_carveouts_response = requests.Response() ows_carveouts_response.status_code = 500 mocker.patch.object( request, 'process', return_value=ows_carveouts_response, autospec=True ) post_data = {'dummy_data'} with pytest.raises(requests.exceptions.HTTPError) as excinfo: carveouts_python.save_account_carveouts(123456, post_data) assert '500 Server Error' in str(excinfo.value)