"""Tests for ows utilities.""" import json from typing import Any from unittest.mock import patch import httpx from owsclient.test import OwsClientMock import pytest from lib import constants from lib.utils import ows def test_create_abacus_event(ows_client_mock: OwsClientMock): """Test creating a new abacus_event.""" mock_response = { 'abacus_event_id': 1, 'event_date': '2022-08-02', 'event_name': 'test_event', 'statement_period_id': 280, 'target_id': 123, 'target_type': 'royalty_accounting_table_name', } ows_client_mock.post( constants.SERVICE_ABACUS_EVENT, '/abacus-event' ).mock( return_value=httpx.Response(201, json=mock_response) ) res = ows.create_abacus_event( mock_response.get('event_name'), mock_response.get('target_id'), mock_response.get('target-type') ) assert res == mock_response def test_get_accounting_period_details(ows_client_mock: OwsClientMock): """Test getting accounting period details.""" mock_response = { 'accounting_period_id': 12, 'accounting_period_status': 'closed' } ows_client_mock.get( constants.SERVICE_ROYALTIES, '/accounting-period/12' ).mock( return_value=httpx.Response(200, json=mock_response) ) actual = ows.get_accounting_period_details(12) assert actual == mock_response def test_get_accounting_period_sales_files(ows_client_mock: OwsClientMock): """Test getting sales files belonging to accounting period.""" accounting_period_id = 12 mock_response = [{ 'accounting_period_id': accounting_period_id, 'accounting_period_name': 'Period Name', 'amount': 12345.67, 'main_url': '', 'file_name': 'period-212', 'row_count': 20, 'sales_file_id': 3, 'staging_url': '' }] ows_client_mock.get( constants.SERVICE_ROYALTIES, f'/accounting-period/{accounting_period_id}/sales-files' ).mock( return_value=httpx.Response(200, json=mock_response) ) result = ows.get_accounting_period_sales_files(accounting_period_id) assert result == mock_response def test_get_accounting_run_details(ows_client_mock: OwsClientMock): """Test getting accounting run details.""" mock_response = { 'accounting_run_id': 12, 'run_status': 'Error' } ows_client_mock.get( constants.SERVICE_ROYALTIES, '/accounting-run/12' ).mock( return_value=httpx.Response(200, json=mock_response) ) actual = ows.get_accounting_run_details(12) assert actual == mock_response def test_get_exchange_rates_by_statement_period(ows_client_mock: OwsClientMock): """Test getting exchange_rates by statement_period_id.""" statement_period_id = 123 mock_response = [ { 'exchange_rate_id': 1, 'from_currency_code': 'USD', 'to_currency_code': 'GBP', 'rate': '0.82' }, { 'exchange_rate_id': 2, 'from_currency_code': 'GBP', 'to_currency_code': 'USD', 'rate': '1.23' } ] ows_client_mock.get( constants.SERVICE_ROYALTIES, f'/statement-period/{statement_period_id}/bulk-exchange-rates' ).mock( return_value=httpx.Response(200, json=mock_response) ) result = ows.get_exchange_rates_by_statement_period(statement_period_id) assert result == mock_response def test_get_contract_details_by_contract_ids(ows_client_mock: OwsClientMock): """Test getting details for one or more contracts by their contract_ids.""" mock_response = [ { 'contract_id': 10, 'contract_name': 'Best Contract', 'payee_id': 12, 'payee_name': 'Payee 12', 'term_end': '2020-06-01', 'term_start': '2019-06-01' }, { 'contract_id': 11, 'contract_name': 'Second Best Contract', 'payee_id': 13, 'term_end': '2020-06-01', 'term_start': '2019-06-01' } ] contract_ids = ', '.join(str(contract['contract_id']) for contract in mock_response) ows_client_mock.post( constants.SERVICE_ROYALTIES, '/contracts/snapshot' ).mock( return_value=httpx.Response(200, json=mock_response) ) result = ows.get_contract_details_by_contract_ids(contract_ids) assert json.loads(result) == mock_response def test_get_contract_term_details_by_contract_ids(ows_client_mock: OwsClientMock): """Test getting details for one or more contract terms by their contract_ids.""" mock_response = [{ 'attachments': [10, 18], 'conditions': { 'countries': ['MEX', 'USA'], 'stores': [22, 33], 'transaction_types': [44, 55] }, 'contract_id': 10, 'contract_term_condition_id': 123, 'contract_term_id': 456, 'is_base_term': True, 'priority': 1, 'term_rate': 20.20, 'term_type': 'label' }] contract_ids = ', '.join(str(term['contract_id']) for term in mock_response) ows_client_mock.post( constants.SERVICE_ROYALTIES, '/contract-terms/snapshot' ).mock( return_value=httpx.Response(200, json=mock_response) ) result = ows.get_contract_term_details_by_contract_ids(contract_ids) assert json.loads(result) == mock_response def test_get_contracts_by_accounting_run(ows_client_mock: OwsClientMock): """Test getting list of contract_ids that belong to accounting run.""" accounting_run_id = 12 mock_response = [10, 11] ows_client_mock.get( constants.SERVICE_ROYALTIES, f'/accounting-run/{accounting_run_id}/run-controller/contracts' ).mock( return_value=httpx.Response(200, json=mock_response) ) result = ows.get_contracts_by_accounting_run(accounting_run_id) assert result == mock_response def test_get_sales_file_details(ows_client_mock: OwsClientMock): """Test getting sales file details.""" mock_response = { 'sales_file_id': 12 } ows_client_mock.get( constants.SERVICE_ROYALTIES, '/sales-file/12' ).mock( return_value=httpx.Response(200, json=mock_response) ) actual = ows.get_sales_file_details(12) assert actual == mock_response @patch('lib.utils.ows.get_accounting_period_sales_files') def test_get_sales_file_urls(mock_get_sales_files): """Test return sales_file urls from sales_file JSON.""" accounting_period_id = 123 mock_sales_files = [ { 'sales_file_id': 1, 'main_url': 's3://bucket/period/eligible-sales/sales-1/results/' }, { 'sales_file_id': 2, 'main_url': 's3://bucket/period/eligible-sales/sales-2/results/' } ] mock_get_sales_files.return_value = mock_sales_files res = ows.get_sales_file_urls(accounting_period_id) assert len(res) == len(mock_sales_files) assert res[0] == 's3://bucket/period/eligible-sales/sales-1/results/' assert res[1] == 's3://bucket/period/eligible-sales/sales-2/results/' mock_get_sales_files.assert_called_once_with(accounting_period_id) def test_update_accounting_run(ows_client_mock: OwsClientMock): """Test the update to accounting_run.""" mock_response = { 'accounting_run_id': 12, 'run_status': 'error' } ows_client_mock.put( constants.SERVICE_ROYALTIES, '/accounting-run/12' ).mock( return_value=httpx.Response(200, json=mock_response) ) actual = ows.update_accounting_run(12) assert actual == mock_response def test_update_accounting_period(ows_client_mock: OwsClientMock): """Test the update to accounting_period.""" mock_response = { 'accounting_period_id': 1021, 'status': 'Calculating Mechanicals' } update_body = dict(accounting_period_status='Calculating Mechanicals') ows_client_mock.put( constants.SERVICE_ROYALTIES, '/accounting-period/1021', json=update_body, ).mock( return_value=httpx.Response(200, json=mock_response) ) actual = ows.update_accounting_period(1021, update_body) assert actual == mock_response def test_create_or_update_report_payment_group_payment( ows_client_mock: OwsClientMock, mock_report_payment_group_payment ): """Test upserting a report_payment_group_payment record.""" mock_report = mock_report_payment_group_payment[0] payment_group_payment_id = mock_report['payment_group_payment_id'] ows_client_mock.put( constants.SERVICE_PAYMENT, f'/payment-group-payment/{payment_group_payment_id}/report' ).mock( return_value=httpx.Response(200, json=mock_report) ) actual = ows.create_or_update_report_payment_group_payment( payment_group_payment_id, mock_report['report_export_url'], mock_report['report_type'] ) assert actual == mock_report def test_get_payment_group_payment( ows_client_mock: OwsClientMock, mock_payment_group_payment ): """Test getting payment_group_payment details.""" payment_group_payment_id = mock_payment_group_payment['payment_group_payment_id'] ows_client_mock.get( constants.SERVICE_PAYMENT, f'/payment-group-payment/{payment_group_payment_id}' ).mock( return_value=httpx.Response(200, json=mock_payment_group_payment) ) actual = ows.get_payment_group_payment_details(payment_group_payment_id) assert actual == mock_payment_group_payment def test_get_payment_group_payment_accounts( ows_client_mock: OwsClientMock, mock_payment_group_payment_accounts ): """Test getting a paginated list of payment_group_payment_accounts.""" mock_response = {'items': mock_payment_group_payment_accounts, 'total_count': 2} payment_group_payment_id = \ mock_payment_group_payment_accounts[0]['payment_group_payment_id'] ows_client_mock.get( constants.SERVICE_PAYMENT, f'/payment-group-payment/{payment_group_payment_id}/accounts' ).mock( return_value=httpx.Response(200, json=mock_response) ) actual = ows.get_payment_group_payment_accounts(payment_group_payment_id) assert actual == mock_response def test_get_payment_group_payment_reports( ows_client_mock: OwsClientMock, mock_report_payment_group_payment ): """Test getting list of report_payment_group_payments.""" payment_group_payment_id = \ mock_report_payment_group_payment[0]['payment_group_payment_id'] ows_client_mock.get( constants.SERVICE_PAYMENT, f'/payment-group-payment/{payment_group_payment_id}/reports' ).mock( return_value=httpx.Response(200, json=mock_report_payment_group_payment) ) actual = ows.get_payment_group_payment_reports(payment_group_payment_id) assert actual == mock_report_payment_group_payment def test_update_payment_group_payment( ows_client_mock: OwsClientMock, payments_generate_dag_config ): """Test the update to payment_group_payment.""" payment_group_payment_id = payments_generate_dag_config['target_id'] mock_response = {'payment_group_payment_id': payment_group_payment_id} ows_client_mock.put( constants.SERVICE_PAYMENT, f'/payment-group-payment/{payment_group_payment_id}' ).mock( return_value=httpx.Response(200, json=mock_response) ) assert ows.update_payment_group_payment(payment_group_payment_id) == { 'payment_group_payment_id': payment_group_payment_id } def test_update_sales_file(ows_client_mock: OwsClientMock): """Test updating a sales file via owsrequest.""" mock_response = { 'amount': '1234.90' } ows_client_mock.put( constants.SERVICE_ROYALTIES, '/sales-file/12' ).mock( return_value=httpx.Response(200, json=mock_response) ) actual = ows.update_sales_file(12) assert actual == mock_response def test_get_contract_mechanicals( mock_contract_mechanicals_response, ows_client_mock: OwsClientMock, ): """Test fetching contract mechanicals.""" ows_client_mock.get( constants.SERVICE_ROYALTIES, '/contract-mechanicals/snapshot' ).mock( return_value=httpx.Response(200, json=mock_contract_mechanicals_response) ) res = ows.get_contract_mechanicals() assert type(res) is bytes assert json.loads(res) == mock_contract_mechanicals_response def test_get_accounting_abacus_state( mock_accounting_period_state_response, ows_client_mock: OwsClientMock ): """Test fetching abacus state for the accounting period.""" ows_client_mock.get( constants.SERVICE_ABACUS_STATE, '/abacus-state/accounting-period/1010' ).mock( return_value=httpx.Response(200, json=mock_accounting_period_state_response) ) result = ows.get_accounting_period_state(1010) assert result == mock_accounting_period_state_response def test_get_abacus_states( mock_sales_file_state_response, ows_client_mock: OwsClientMock ): """Test fetching abacus state for the sales file.""" ows_client_mock.get( constants.SERVICE_ABACUS_STATE, '/abacus-state/sales_file/87' ).mock( return_value=httpx.Response(200, json=mock_sales_file_state_response) ) result = ows.get_abacus_states('sales_file', 87) assert result == mock_sales_file_state_response def test_update_abacus_state( mock_accounting_period_state_response, ows_client_mock: OwsClientMock ): """Test updating abacus state by abacus state id.""" update_body = dict(action_status='complete') ows_client_mock.put( constants.SERVICE_ABACUS_STATE, '/abacus-state/1010', json=update_body, ).mock( return_value=httpx.Response(200, json=mock_accounting_period_state_response) ) result = ows.update_abacus_state(1010, update_body) assert result == mock_accounting_period_state_response def test_update_abacus_state_body( mock_accounting_period_state_response, ows_client_mock: OwsClientMock ): """Test updating abacus state by abacus state id.""" update_body = dict(action_status='complete') ows_client_mock.put( constants.SERVICE_ABACUS_STATE, '/abacus-state/1010', json=update_body, ).mock( return_value=httpx.Response(200, json=mock_accounting_period_state_response) ) result = ows.update_abacus_state(1010, body=update_body) assert result == mock_accounting_period_state_response def test_create_abacus_state(ows_client_mock: OwsClientMock): """Test creating abacus state.""" action_name = 'some_action' parent_table_id = 123 parent_table_name = 'table_name' mock_response = [{ 'abacus_state_id': 1, 'action_name': action_name, 'action_status': 'init' }] ows_client_mock.post( constants.SERVICE_ABACUS_STATE, '/abacus-states' ).mock( return_value=httpx.Response(201, json=mock_response) ) result = ows.create_abacus_state(action_name, parent_table_name, parent_table_id) assert result == mock_response def test_debit_reserves_from_ledger_account_by_run( ows_client_mock: OwsClientMock ): """Test taking reserves by accounting run.""" mock_response = {'msg': 'well done'} accounting_run_id = 123 ows_client_mock.post( constants.SERVICE_LEDGER, f'/ledger-reserve-taken/accounting-run/{accounting_run_id}' ).mock( return_value=httpx.Response(200, json=mock_response) ) result = ows.debit_reserves_from_ledger_account_by_run( accounting_run_id) assert result == mock_response def test_get_statement_period_details( mock_statement_period, ows_client_mock: OwsClientMock ): """Test getting statement period details.""" ows_client_mock.get( constants.SERVICE_ROYALTIES, '/statement-period/1' ).mock( return_value=httpx.Response(200, json=mock_statement_period) ) actual = ows.get_statement_period_details(1) assert actual == mock_statement_period def test_get_statement_period_adjustment_file_details( mock_statement_period, ows_client_mock: OwsClientMock ): """Test getting statement period adjustment file details.""" ows_client_mock.get( constants.SERVICE_ROYALTIES, '/statement-period-adjustment-file/1' ).mock( return_value=httpx.Response(200, json=mock_statement_period) ) actual = ows.get_statement_period_adjustment_file_details(1) assert actual == mock_statement_period @pytest.mark.parametrize('params', [ pytest.param( {'body': {'error_type': 'format_error'}}, id='body is not passed to the json named argument' ), pytest.param( {'error_type': 'format_error'}, id='params are used for the json named argument' ), ]) def test_update_adjustment_file( params: dict[str, Any], ows_client_mock: OwsClientMock ) -> None: """Test update statement period adjustment file details.""" ows_client_mock.put( constants.SERVICE_ROYALTIES, '/statement-period-adjustment-file/1', json={'error_type': 'format_error'}, ).mock( return_value=httpx.Response(200, json={'status': 'ok'}) ) actual = ows.update_adjustment_file(1, **params) assert actual == {'status': 'ok'} def test_get_payment_group_details( mock_payment_group, ows_client_mock: OwsClientMock ): """Test getting payment group details.""" ows_client_mock.get( constants.SERVICE_PAYMENT, '/payment-group/1' ).mock( return_value=httpx.Response(200, json=mock_payment_group) ) actual = ows.get_payment_group_details(1) assert actual == mock_payment_group def test_get_account_payment_term_by_account_id( mock_account_payment_term, ows_client_mock: OwsClientMock ): """Test to get account payment term details for a specified account_id.""" account_id = 1012 ows_client_mock.get( constants.SERVICE_ABACUS_ACCOUNT, f'/account/{account_id}/account-payment-term/' ).mock( return_value=httpx.Response(200, json=mock_account_payment_term) ) actual = ows.get_account_payment_term_by_account_id(account_id) assert actual == mock_account_payment_term def test_get_signing_entities( mock_signing_entities, ows_client_mock: OwsClientMock ): """Test getting signing entities.""" ows_client_mock.get( constants.SERVICE_ABACUS_ACCOUNT, '/signing-entities' ).mock( return_value=httpx.Response(200, json=mock_signing_entities) ) actual = ows.get_signing_entities() assert actual == {2: 'AWAL Digital Limited (UK)'} def test_get_ref_payment_entities( mock_ref_payment_entities, ows_client_mock: OwsClientMock ): """Test getting reference payment entities.""" ows_client_mock.get( constants.SERVICE_ROYALTIES, '/reference-payment-entities' ).mock( return_value=httpx.Response(200, json=mock_ref_payment_entities) ) actual = ows.get_reference_payment_entities() assert actual == { 1: 'AWAL-UK', 2: 'AWAL-US' }