"""Tests for the Reserve payout workflow.""" from unittest import mock import pytest from accounting.flows.reserve_payouts import flow def get_schedule(): """Build a set of mocks to replace schedule for testing the flow. Returns: tuple: schedule mock, schedule_result_object mock. """ schedule = mock.MagicMock() schedule_result_object = mock.MagicMock() schedule_result_object.result = {} schedule.return_value = schedule_result_object return schedule, schedule_result_object @mock.patch('garcon.activity.create') def test_flow_decider_exits_if_bootstrap_stop(mock_create): """Test flow decider task exits if bootstrap task has failed.""" reserve_payout_flow = flow.Flow() context = {} schedule, schedule_result_object = get_schedule() schedule_result_object.result['bootstrap.stop'] = True assert reserve_payout_flow.decider(schedule, context) is None schedule.assert_called_once_with( 'bootstrap', mock.ANY) @pytest.mark.parametrize('bootstrap_result, expected_calls', [ # full run ( { 'bootstrap.validate_only': False, 'bootstrap.skip_prepare_label_data': False }, [ mock.call('bootstrap', mock.ANY), mock.call( 'prepare_temp_table', mock.ANY, requires=mock.ANY), mock.call( 'prepare_label_data', mock.ANY, requires=mock.ANY), mock.call( 'calculate_reserve_payouts', mock.ANY, requires=mock.ANY), mock.call( 'validate_reserve_payout_calculation', mock.ANY, requires=mock.ANY) ] ), # validate only ( { 'bootstrap.validate_only': True, 'bootstrap.skip_prepare_label_data': False }, [ mock.call('bootstrap', mock.ANY), mock.call( 'validate_reserve_payout_calculation', mock.ANY, requires=mock.ANY) ] ), # skip prepare label data ( { 'bootstrap.validate_only': False, 'bootstrap.skip_prepare_label_data': True }, [ mock.call('bootstrap', mock.ANY), mock.call( 'calculate_reserve_payouts', mock.ANY, requires=mock.ANY), mock.call( 'validate_reserve_payout_calculation', mock.ANY, requires=mock.ANY) ] ), ]) @mock.patch('garcon.activity.create') def test_flow_decider_full_run(mock_create, bootstrap_result, expected_calls): """Test the decider method of the flow.""" reserve_payout_flow = flow.Flow() context = {} schedule, schedule_result_object = get_schedule() schedule_result_object.result.update(bootstrap_result) reserve_payout_flow.decider(schedule, context) schedule.assert_has_calls(expected_calls)