""" Tests for bin/seat.py # flake8: noqa """ from datetime import date, datetime, timedelta from unittest.mock import patch, call from feed_ingestion.bin import seat def test_get_tasks_aggregated_durations(): tasks = [ { 'name': 'task1', 'duration': timedelta(seconds=3) }, { 'name': 'task2', 'duration': timedelta(seconds=4) }, { 'name': 'task2', 'duration': timedelta(seconds=6) }, ] tasks_totals = seat.get_tasks_aggregated_durations(tasks) expected_totals = { 'task1': 3.0, 'task2': 10.0 } assert tasks_totals == expected_totals @patch.object(seat.swf, 'list_closed_swf_executions') @patch.object(seat.swf, 'load_all_execution_events') @patch.object(seat.swf, 'tasks_from_events') @patch.object(seat, 'get_tasks_aggregated_durations') def test_calculate_aggregated_totals( mock_get_tasks_aggregated_durations, mock_tasks_from_events, mock_load_all_execution_events, mock_load_executions ): # Mock the responses mock_load_executions.return_value = [ {'execution': {'workflowId': 'workflow_id_1', 'runId': 'run_id_1'}}, {'execution': {'workflowId': 'workflow_id_2', 'runId': 'run_id_2'}} ] mock_load_all_execution_events.return_value = [ {'eventId': 1, 'eventType': 'WorkflowExecutionStarted'}, {'eventId': 2, 'eventType': 'DecisionTaskScheduled'} ] mock_tasks_from_events.return_value = [ {'name': 'task1', 'duration': timedelta(seconds=3)}, {'name': 'task2', 'duration': timedelta(seconds=4)} ] mock_get_tasks_aggregated_durations.return_value = { 'task1': 3.0, 'task2': 4.0 } from_date = datetime(2021, 1, 1) to_date = datetime(2021, 1, 2) swf_domain = 'test_domain' execution_type = 'test_execution_type' result = seat.calculate_aggregated_totals( from_date=from_date, to_date=to_date, swf_domain=swf_domain, execution_type=execution_type, aggregate_function='sum', aggregate_by=timedelta(days=1) ) expected_result = [ { 'from': datetime(2021, 1, 1), 'to': datetime(2021, 1, 2), 'num_executions': 2, 'totals': { 'task1': 6.0, 'task2': 8.0 } }, { 'from': datetime(2021, 1, 2), 'to': datetime(2021, 1, 3), 'num_executions': 2, 'totals': { 'task1': 6.0, 'task2': 8.0 } }, ] assert result == expected_result def test_aggregated_totals_as_table(): aggregated_totals = [ { 'from': datetime(2021, 1, 1), 'to': datetime(2021, 1, 2), 'num_executions': 2, 'totals': { 'task1': 3.0, 'task2': 4.0, } }, { 'from': datetime(2021, 1, 2), 'to': datetime(2021, 1, 3), 'num_executions': 3, 'totals': { 'task1': 5.0, 'task2': 6.0, } } ] table = seat.aggregated_totals_as_table(aggregated_totals) expected_table = [ ['task_name', date(2021, 1, 1), date(2021, 1, 2)], ['task1', 3, 5], ['task2', 4, 6], ['EXECUTIONS', 2, 3] ] assert table == expected_table