"""Tests for ows-abacus-legacy-sync related requests.""" from unittest.mock import patch import httpx from owsclient.test import OwsClientMock import config from sync_contract.ows_abacus_legacy_sync import ( create_legacy_contract, create_vendor_contract_distribution_types, ) @patch('sync_contract.ows_abacus_legacy_sync.app_logger') @patch('sync_contract.ows_abacus_legacy_sync.raise_service_error') def test_create_legacy_contract( mock_raise_service_error, mock_logger, mock_legacy_contract_body, mock_legacy_contract_response, ows_client_mock: OwsClientMock, ): """Test to successfully create legacy contract.""" request_url = '/vendor-contract/' ows_client_mock.post('ows-abacus-legacy-sync', request_url).mock( return_value=httpx.Response(201, json=mock_legacy_contract_response) ) result = create_legacy_contract(mock_legacy_contract_body) assert result == mock_legacy_contract_response mock_logger.info.assert_called_once_with( config.MESSAGE_CREATION_STARTED.format('Legacy Contract') ) mock_raise_service_error.assert_not_called() @patch('sync_contract.ows_abacus_legacy_sync.app_logger') @patch('sync_contract.ows_abacus_legacy_sync.raise_service_error') def test_create_legacy_contract_error( mock_raise_service_error, mock_logger, mock_legacy_contract_body, ows_client_mock: OwsClientMock, ): """Test failure to create legacy contract.""" request_url = '/vendor-contract/' ows_client_mock.post('ows-abacus-legacy-sync', request_url).mock( return_value=httpx.Response(400, json={'error': 'Error'}) ) result = create_legacy_contract(mock_legacy_contract_body) assert result is None mock_logger.info.assert_called_once_with( config.MESSAGE_CREATION_STARTED.format('Legacy Contract') ) mock_raise_service_error.assert_called_once_with( 'ERROR in POST /vendor-contract/', 'ows-abacus-legacy-sync' ) @patch('sync_contract.ows_abacus_legacy_sync.app_logger') @patch('sync_contract.ows_abacus_legacy_sync.raise_service_error') def test_create_vendor_contract_distribution_types( mock_raise_service_error, mock_logger, mock_vendor_contract_distribution_type_body, mock_vendor_contract_distribution_type_response, ows_client_mock: OwsClientMock, ): """Test to successfully create vendor_contract_distribution_types.""" request_url = '/vendor-contract-distribution-types/' ows_client_mock.post('ows-abacus-legacy-sync', request_url).mock( return_value=httpx.Response( 201, json=[mock_vendor_contract_distribution_type_response] ) ) result = create_vendor_contract_distribution_types( mock_vendor_contract_distribution_type_body ) assert result == [mock_vendor_contract_distribution_type_response] mock_logger.info.assert_called_once_with( config.MESSAGE_CREATION_STARTED.format('Vendor Contract Distribution Type') ) mock_raise_service_error.assert_not_called() @patch('sync_contract.ows_abacus_legacy_sync.app_logger') @patch('sync_contract.ows_abacus_legacy_sync.raise_service_error') def test_create_vendor_contract_distribution_types_error( mock_raise_service_error, mock_logger, mock_vendor_contract_distribution_type_body, ows_client_mock: OwsClientMock, ): """Test failure creating vendor_contract_distribution_types.""" request_url = '/vendor-contract-distribution-types/' ows_client_mock.post('ows-abacus-legacy-sync', request_url).mock( return_value=httpx.Response(400, json={'error': 'Error'}) ) result = create_vendor_contract_distribution_types( mock_vendor_contract_distribution_type_body ) assert result is None mock_logger.info.assert_called_once_with( config.MESSAGE_CREATION_STARTED.format('Vendor Contract Distribution Type') ) mock_raise_service_error.assert_called_once_with( 'ERROR in POST /vendor-contract-distribution-types/', 'ows-abacus-legacy-sync' )