"""Unit tests for the flow.""" from unittest.mock import MagicMock from freezegun import freeze_time import pytest from activity_detector.flows.trending_tracks_discrete.flow import Flow class TestFlow(object): """Tests for flow.""" @pytest.fixture def flow(self): """Create flow.""" return Flow() @pytest.fixture def run_flow(self, flow, mock_schedule, mock_context): """Execute flow. Args: flow (activity_detector.flows.chartmetric_spike_detector.flow): flow to run # noqa:E501 mock_schedule (MagicMock): mock of schedule passed to flow mock_context (MagicMock): mock execution context """ flow.decider(mock_schedule, mock_context) @pytest.fixture def mock_schedule_do_not_run(self, mock_schedule): """Run flow with dynamo status mocked. Args: mock_schedule (MagicMock): mock of schedule passed to flow """ mock_schedule.return_value = MagicMock( result={'check_dynamo_status.should_run': False}) return mock_schedule @freeze_time('2019-06-15') def test_flow_id_default(self, flow): """Test generate flow id with no input.""" output = flow.workflow_id({}) assert output == 'trending_tracks_discrete-2019-06-13' def test_flow_id_specific(self, flow): """Test generate flow id with given date.""" output = flow.workflow_id({'context_date': '2019-06-10'}) assert output == 'trending_tracks_discrete-2019-06-10' def test_decider_not_run(self, mock_schedule_do_not_run, run_flow): """Flow short-circuit based on dynamo status. Args: mock_schedule_do_not_run (MagicMock): schedule with false dynamo status # noqa:E501 run_flow (None): fixture function that runs flow, returns nothing """ assert len(mock_schedule_do_not_run.call_args_list) == 2 def test_decider_schedule(self, mock_schedule, run_flow): """Test decider ordering and requirements. Args: mock_schedule (MagicMock): mock of schedule passed to flow run_flow (None): fixture function that runs flow, returns nothing """ def _get_activity(index): (name, activity), _ = mock_schedule.call_args_list[index] tasks = [ { 'requirements': x.__garcon__['requirements'], 'name': x.__name__ } for x in activity.runner.tasks ] return (name, tasks) assert len(mock_schedule.call_args_list) == 4 (name, tasks) = _get_activity(0) assert name == 'bootstrap' assert tasks == [ { 'name': 'bootstrap', 'requirements': ['context_date', 'execution.run_id'] } ] (name, tasks) = _get_activity(1) assert name == 'check_dynamo_status' assert tasks == [ { 'name': 'check_dynamo_status', 'requirements': ['bootstrap.target_date'] } ] (name, tasks) = _get_activity(2) assert name == 'detect_spikes' assert tasks == [ { 'name': 'detect_spikes', 'requirements': ['bootstrap.target_date'] } ] (name, tasks) = _get_activity(3) assert name == 'set_dynamo_status' assert tasks == [ { 'name': 'set_dynamo_status', 'requirements': [ 'bootstrap.target_date', 'detect_spikes.failures' ] } ]