"""Unit tests for sales_get_eligible helpers.""" from unittest.mock import patch import pytest from lib import constants from tasks.sales_get_eligible import helpers @patch('tasks.sales_get_eligible.helpers.event') def test_get_event_from_params(mock_event, mock_sales_get_eligible_dag_run): """Test getting abacus_event from dag run config.""" mock_event.get_abacus_event.return_value = 'an event' mock_event.validate_event_for_handler.return_value = True res = helpers.get_event_from_params(mock_sales_get_eligible_dag_run) assert res mock_event.get_abacus_event.assert_called_once_with(mock_sales_get_eligible_dag_run) mock_event.validate_event_for_handler.assert_called_once_with( 'an event', event_name=constants.DAG_SALES_GET_ELIGIBLE_EVENT_NAME, target_type=constants.DAG_SALES_GET_ELIGIBLE_TARGET_TYPE ) @patch('tasks.sales_get_eligible.helpers.ows') def test_get_abacus_state_success(mock_ows, mock_sales_file_abacus_states): """Test getting a sales_file's 'get_eligible_sales' abacus_state.""" action_name = constants.SALES_FILE_ACTIONS.GET_ELIGIBLE_SALES mock_ows.get_abacus_states.return_value = mock_sales_file_abacus_states res = helpers.get_abacus_state(123) assert res.get('action_name') == action_name mock_ows.get_abacus_states.assert_called_once_with(helpers.PARENT_TABLE_NAME, 123) @patch('tasks.sales_get_eligible.helpers.ows') def test_get_abacus_state_failure(mock_ows, mock_sales_file_abacus_states): """Test that an error is raised when abacus_state record does not exist.""" mock_ows.get_abacus_states.return_value = [mock_sales_file_abacus_states[1]] with pytest.raises(ValueError): helpers.get_abacus_state(123) mock_ows.get_abacus_states.assert_called_once_with(helpers.PARENT_TABLE_NAME, 123)