"""Tests for general purpose helper's methods.""" from unittest.mock import patch from dags.lib import constants from dags.lib.utils import helpers import pytest def test_chunk_collection_by_size(): """Test for chunk_collection_by_size utility.""" list_items = [1, 2, 3, 'number', 'string', 'tuple', True, False] tuple_items = tuple(range(1, 11)) assert list(helpers.chunk_collection_by_size(list_items, 3)) == [ [1, 2, 3], ['number', 'string', 'tuple'], [True, False] ] chunk_1, chunk_2 = helpers.chunk_collection_by_size(tuple_items, 5) assert chunk_1 == (1, 2, 3, 4, 5) assert chunk_2 == (6, 7, 8, 9, 10) def test_get_country_by_code_error(): """Test get_country_by_code for an invalid country.""" with pytest.raises( Exception, match=constants.ERROR_UNKNOWN_COUNTRY.format(code='test') ): helpers.get_country_by_code('test') @patch('dags.lib.utils.helpers.ows') def test_get_accounting_period_state_success(mock_ows): """Test successfully getting a specified accounting_period abacus_state.""" accounting_period_id = 1 mock_abacus_states = [ { 'abacus_state_id': 7, 'action_status': 'complete', 'action_name': 'deliver_sales_files', 'parent_table_id': accounting_period_id, 'parent_table_name': 'accounting_period' }, { 'abacus_state_id': 8, 'action_status': 'complete', 'action_name': 'upload_exchange_rates', 'parent_table_id': accounting_period_id, 'parent_table_name': 'accounting_period' }, { 'abacus_state_id': 9, 'action_status': 'complete', 'action_name': 'prep_mechanical_deductions', 'parent_table_id': accounting_period_id, 'parent_table_name': 'accounting_period' }, { 'abacus_state_id': 10, 'action_status': 'init', 'action_name': 'close_period', 'parent_table_id': accounting_period_id, 'parent_table_name': 'accounting_period' } ] mock_ows.get_accounting_period_state.return_value = mock_abacus_states close_period = helpers.get_accounting_period_state( accounting_period_id, constants.ACCOUNTING_PERIOD_ACTIONS.CLOSE_PERIOD ) assert close_period['abacus_state_id'] == mock_abacus_states[-1]['abacus_state_id'] mock_ows.get_accounting_period_state.assert_called_once_with(accounting_period_id) @patch('dags.lib.utils.helpers.ows') def test_get_accounting_period_state_no_states(mock_ows): """Test error is raised when no abacus states are returned for specified period.""" accounting_period_id = 1 mock_abacus_states = '' mock_ows.get_accounting_period_state.return_value = mock_abacus_states with pytest.raises(Exception) as e: helpers.get_accounting_period_state( accounting_period_id, constants.ACCOUNTING_PERIOD_ACTIONS.CLOSE_PERIOD ) assert str(e.value) == 'Accounting Period states not found' mock_ows.get_accounting_period_state.assert_called_once_with(accounting_period_id) @patch('dags.lib.utils.helpers.ows') def test_get_accounting_period_state_no_action_name(mock_ows): """Test error is raised when the specified action_name is not in returned states.""" accounting_period_id = 1 mock_abacus_states = [{ 'abacus_state_id': 7, 'action_status': 'complete', 'action_name': 'deliver_sales_files', 'parent_table_id': accounting_period_id, 'parent_table_name': 'accounting_period' }] mock_ows.get_accounting_period_state.return_value = mock_abacus_states with pytest.raises(Exception) as e: helpers.get_accounting_period_state( accounting_period_id, constants.ACCOUNTING_PERIOD_ACTIONS.CLOSE_PERIOD ) assert str(e.value) == 'Accounting Period {} state not found'.format( constants.ACCOUNTING_PERIOD_ACTIONS.CLOSE_PERIOD ) mock_ows.get_accounting_period_state.assert_called_once_with(accounting_period_id) @patch('dags.lib.utils.helpers.ows') def test_get_sales_file_ids(mock_ows): """Test method to get sales files in parent accounting_period.""" accounting_period_id = 1 mock_sales_files = [ { 'sales_file_id': 1, 'sales_file_name': 'Sales 1' }, { 'sales_file_id': 2, 'sales_file_name': 'Sales 2' }, { 'sales_file_id': 3, 'sales_file_name': 'Sales 3' } ] mock_ows.get_accounting_period_sales_files.return_value = mock_sales_files res = helpers.get_sales_file_ids(accounting_period_id) assert res == [1, 2, 3] mock_ows.get_accounting_period_sales_files.assert_called_once_with( accounting_period_id )