"""Tests for analytics_aggregation.util.common module.""" from unittest.mock import Mock from freezegun import freeze_time from garcon.contrib.dynamo_feed_status import feed_status_ingestion import pytest from analytics_aggregation.util import common def test_list_of_dates(): """Test of list_of_dates generator.""" expected_dates = ['2016-01-01', '2016-01-02', '2016-01-03'] dates = common.list_of_dates('2016-01-01', '2016-01-03') for expected_date, date in zip(expected_dates, dates): assert expected_date == date with pytest.raises(StopIteration): next(dates) def test_list_of_dates__reversed(): """Test of list_of_dates generator.""" expected_dates = ['2016-01-03', '2016-01-02', '2016-01-01'] dates = common.list_of_dates('2016-01-01', '2016-01-03', reversed=True) for expected_date, date in zip(expected_dates, dates): assert expected_date == date with pytest.raises(StopIteration): next(dates) def test_exit_message(): """Test of flow_exception function.""" message = 'message' res = common.exit_message(message) assert res['message'] == message assert res['stop'] @pytest.mark.parametrize( 'negative_flag', [False, 'false', 'False', 'FALSE', None]) def test_get_bool_from_negative_flag_returns_false(negative_flag): """Test get_bool_from_flag returns False.""" assert not common.get_bool_from_flag(negative_flag) @pytest.mark.parametrize( 'positive_flag', [True, 'true', 'True', 'TRUE']) def test_get_bool_from_positive_flag_returns_true(positive_flag): """Test get_bool_from_flag returns True.""" assert common.get_bool_from_flag(positive_flag) @pytest.mark.parametrize( 'value, result', [ ('1, 2, 3', [1, 2, 3]), ([1, 2, 3], [1, 2, 3]), ([1], [1]), (0, [0]), (None, []), ]) def test_format_id_list_happy(value, result): """Test format_id_list function with good values.""" assert common.format_id_list(value) == result @pytest.mark.parametrize( 'value', ['1, 2, c', [1, 2, 'c'], 'a']) def test_format_id_list_unhappy(value): """Test format_id_list function with bad values.""" with pytest.raises(common.UnprocessableFlowParamsException): common.format_id_list(value) @pytest.mark.parametrize( 'labelids, table_alias, result', [ ([1, 2], None, ('AND labelid in %(labelids)s', tuple([1, 2]))), ([1, 2], 'sos', ('AND sos.labelid in %(labelids)s', tuple([1, 2]))), (None, None, ('', None)) ]) def test_sos_labelid_filter(labelids, table_alias, result): """Test sos_labelid_filter function.""" assert common.sos_labelid_filter(labelids, table_alias) == result @pytest.mark.parametrize( 'context_date_range, reload', [ ('', True), ('test_range', False) ]) def test_validate_date_range_reload_unprocessable(context_date_range, reload): """Test validate_date_range_reload.""" with pytest.raises(common.UnprocessableFlowParamsException): common.validate_date_range_reload(context_date_range, reload) @pytest.mark.parametrize( 'context_date_range, reload', [ ('test_range', True), ('', False) ]) def test_validate_date_range_reload_positive(context_date_range, reload): """Test validate_date_range_reload.""" result = common.validate_date_range_reload(context_date_range, reload) assert result is True def test_find_flow_date_range_with_date_range(): """Test find_flow_date_range_with_value with date range.""" start_date, end_date = 'start', 'end' date_range = '{}-{}'.format(start_date, end_date) result = common.find_flow_date_range( '', '', date_range, days_back=0) assert date_range.split('_') == result def test_find_flow_date_range_unprocessable(monkeypatch): """Test find_flow_date_range without date range.""" # Mocking find_range_to_process_mock = Mock( return_value={'found_days': False, 'week_range': 'week_range'}) monkeypatch.setattr( common, '_find_range_to_process', find_range_to_process_mock) feed_name = 'feed_name' source_feed_name = 'source_feed_name' days_back = 1 # Tested function call with pytest.raises(common.UnprocessableFlowParamsException): common.find_flow_date_range( feed_name, source_feed_name, '', days_back=days_back) # Checks find_range_to_process_mock.assert_called_once_with( feed_name, source_feed_name, days_back) def test_find_flow_date_range(monkeypatch): """Test find_flow_date_range without date range.""" # Mocking start_date = 'start_date' end_date = 'end_date' find_range_to_process_mock = Mock( return_value={ 'found_days': True, 'week_range': 'week_range', 'start_date': start_date, 'end_date': end_date }) monkeypatch.setattr( common, '_find_range_to_process', find_range_to_process_mock) feed_name = 'feed_name' source_feed_name = 'source_feed_name' days_back = 1 # Tested function call result = common.find_flow_date_range( feed_name, source_feed_name, '', days_back=days_back) # Checks find_range_to_process_mock.assert_called_once_with( feed_name, source_feed_name, days_back) assert result == (start_date, end_date) @freeze_time('2018-01-10') @pytest.mark.parametrize( 'list_of_dates, feed_ingested, days_back, expected_result', [ (['2018-01-09', '2018-01-10'], [True, False, True, False], 1, {'found_days': True, 'week_range': '2018-01-09_2018-01-10', 'start_date': '2018-01-09', 'end_date': '2018-01-10'}), (['2018-01-09', '2018-01-10'], [True, True, True, False], 1, {'found_days': True, 'week_range': '2018-01-09_2018-01-10', 'start_date': '2018-01-10', 'end_date': '2018-01-10'}), (['2018-01-09', '2018-01-10'], [True, True, True, True], 1, {'found_days': False, 'week_range': '2018-01-09_2018-01-10'}), ]) def test_find_range_to_process( monkeypatch, list_of_dates, feed_ingested, days_back, expected_result): """Test find_range_to_process.""" # Mocking feed_name = 'feed_name' source_feed_name = 'source_feed_name' list_of_dates_mock = Mock(return_value=list_of_dates) monkeypatch.setattr( common, 'list_of_dates', list_of_dates_mock) is_feed_ingested_mock = Mock() is_feed_ingested_mock.side_effect = feed_ingested monkeypatch.setattr( common, 'is_feed_ingested', is_feed_ingested_mock) # Tested function call result = common._find_range_to_process( feed_name, source_feed_name, days_back) # Checks assert result == expected_result @pytest.mark.parametrize( 'feed_name, date, status, expected_result', [ ('feed_name', 'date', 'INGESTED', True), ('feed_name', 'date', 'NOT INGESTED', False), ]) def test_is_feed_ingested( monkeypatch, feed_name, date, status, expected_result): """Test is_feed_ingested function.""" # Mocking get_overall_status_mock = Mock(return_value=status) monkeypatch.setattr( feed_status_ingestion, 'get_overall_status', get_overall_status_mock) # Tested function call result = common.is_feed_ingested(feed_name, date) # Checks assert result == expected_result get_overall_status_mock.assert_called_once_with(feed_name, date) @pytest.mark.parametrize( 'labelids, alias, expected_result', [ ([], None, ''), ([1, 2], None, 'AND labelid in (%(labelids)s)'), ([1, 2], 'alias', 'AND alias.labelid in (%(labelids)s)'), ]) def test_sos_labelid_clause(labelids, alias, expected_result): """Test sos_labelid_clause.""" result = common.sos_labelid_clause(labelids, alias) assert result == expected_result