"""Test commit helpers.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from lib import constants from lib.abacus_event import AbacusEvent from tasks.accounting_run_commit import helpers @patch('tasks.accounting_run_commit.helpers.event') def test_get_event_from_params(mock_event, mock_commit_dag_run): """Test getting abacus event validates event name.""" mock_event.get_abacus_event.return_value = 'accounting_run_commit event' mock_event.validate_event_for_handler.return_value = True res = helpers.get_event_from_params(mock_commit_dag_run) assert res mock_event.get_abacus_event.assert_called_once_with(mock_commit_dag_run) mock_event.validate_event_for_handler.assert_called_once_with( 'accounting_run_commit event', event_name=constants.DAG_ACCOUNTING_RUN_COMMIT_EVENT_NAME, target_type=constants.DAG_ACCOUNTING_RUN_COMMIT_TARGET_TYPE ) @patch('tasks.accounting_run_commit.helpers.event') def test_get_event_from_params_raises( mock_event, mock_commit_dag_run): """Test getting abacus event raises exception.""" mock_event.get_abacus_event.return_value = 'accounting_run_commit event' mock_event.validate_event_for_handler.side_effect = AssertionError('nope') with pytest.raises(AssertionError): helpers.get_event_from_params(mock_commit_dag_run) @patch('tasks.accounting_run_commit.helpers.paths') @patch('tasks.accounting_run_commit.helpers.aws') def test_build_mechanical_export_location(mock_aws, mock_paths): """Test method to build the aws s3 location of the mechanical export.""" accounting_period = { 'accounting_period_id': 456, 'accounting_period_name': 'accounting period name' } accounting_run = { 'accounting_run_id': 123, 'run_controller_name': 'run controller name' } period_slug = '456-accounting-period-name' run_slug = '123-run-controller-name' s3_key = f'{period_slug}/{run_slug}/{constants.FILE_NAME_MECHANICAL_EXPORT_TSV}' s3_url = f's3://qa-royalties-sales-files/{s3_key}' mock_s3_location = MagicMock(key=s3_key, url=s3_url) mock_paths.build_period_slug.return_value = period_slug mock_paths.build_accounting_run_slug.return_value = run_slug mock_aws.location.return_value = mock_s3_location mech_export_location = helpers.build_mechanical_export_location( accounting_period, accounting_run ) assert mech_export_location.key == s3_key assert mech_export_location.url == s3_url mock_paths.build_period_slug.assert_called_once_with( *accounting_period.values() ) mock_paths.build_accounting_run_slug.assert_called_once_with( *accounting_run.values() ) mock_aws.location.assert_called_once_with( period_slug, run_slug, constants.FILE_NAME_MECHANICAL_EXPORT_TSV ) @patch('tasks.accounting_run_commit.helpers.ows') def test_create_event(mock_ows, mock_commit_royalties_event): """Test creating a 'commit_royalties' abacus_event.""" accounting_run_id = mock_commit_royalties_event.get('target_id') mock_ows.create_abacus_event.return_value = mock_commit_royalties_event res = helpers.create_event(accounting_run_id, 'mock_event') assert isinstance(res, AbacusEvent) assert res.abacus_event_id == mock_commit_royalties_event.get('abacus_event_id') assert res.event_date == mock_commit_royalties_event.get('event_date') assert res.event_name == mock_commit_royalties_event.get('event_name') assert res.statement_period_id == \ mock_commit_royalties_event.get('statement_period_id') assert res.target_id == mock_commit_royalties_event.get('target_id') assert res.target_type == mock_commit_royalties_event.get('target_type') mock_ows.create_abacus_event.assert_called_once_with( event_name='mock_event', target_id=accounting_run_id, target_type=mock_commit_royalties_event.get('target_type') ) @patch('tasks.accounting_run_commit.helpers.ows') def test_get_event_records(mock_ows): """Test getting accounting_run and accounting_period from abacus_event's target.""" accounting_period = { 'accounting_period_id': 1, 'accounting_period_name': 'A Distribution Period', 'contract_type': constants.CONTRACT_TYPES.DISTRIBUTION } accounting_run = { 'accounting_run_id': 123, 'accounting_period_id': 1 } mock_ows.get_accounting_run_details.return_value = accounting_run mock_ows.get_accounting_period_details.return_value = accounting_period res = helpers.get_event_records(accounting_run.get('accounting_run_id')) assert res == (accounting_period, accounting_run) mock_ows.get_accounting_run_details.assert_called_once_with( accounting_run.get('accounting_run_id') ) mock_ows.get_accounting_period_details.assert_called_once_with( accounting_run.get('accounting_period_id') ) @patch('tasks.accounting_run_commit.helpers.ows') def test_get_abacus_state_success(mock_ows, mock_accounting_run_abacus_states): """Test getting an accounting_run's 'commit_royalties' abacus_state.""" action_name = constants.ACCOUNTING_RUN_ACTIONS.COMMIT_ROYALTIES mock_ows.get_abacus_states.return_value = mock_accounting_run_abacus_states res = helpers.get_abacus_state(123) assert res.action_name == action_name mock_ows.get_abacus_states.assert_called_once_with(helpers.PARENT_TABLE_NAME, 123) @patch('tasks.accounting_run_commit.helpers.ows') def test_get_abacus_states_failure(mock_ows, mock_accounting_run_abacus_states): """Test that an error is raised when abacus_state record does not exist.""" mock_ows.get_abacus_states.return_value = [mock_accounting_run_abacus_states[0]] with pytest.raises(ValueError): helpers.get_abacus_state(123) mock_ows.get_abacus_states.assert_called_once_with(helpers.PARENT_TABLE_NAME, 123)