"""Unit tests for geographics logic layer.""" from analytics.logic import geographics as geographics_logic from analytics.logic.geographics import TimePeriod as TimePeriod def test_get_geographic_insights_with_subaccount( geographics_params, mock_success_get_geographic_insights, mock_success_get_vendor_id_by_subaccount_id, default_geographics_subaccount_params, geographics_logic_expected_result): """Test logic with sub account returns response with data.""" response = geographics_logic.get_geographic_insights( **default_geographics_subaccount_params) assert response.message == geographics_logic_expected_result assert mock_success_get_geographic_insights.called assert mock_success_get_geographic_insights.call_count == 1 assert mock_success_get_vendor_id_by_subaccount_id.called def test_get_geographic_insights_with_subaccount_with_storeids( mock_success_get_geographic_insights, mock_success_get_vendor_id_by_subaccount_id, default_geographics_subaccount_params, geographics_logic_expected_result): """Test logic with sub account returns response with data.""" default_geographics_subaccount_params['store_ids'] = [286] response = geographics_logic.get_geographic_insights( **default_geographics_subaccount_params) assert response.message == geographics_logic_expected_result assert mock_success_get_geographic_insights.called assert mock_success_get_geographic_insights.call_count == 1 assert mock_success_get_vendor_id_by_subaccount_id.called def test_get_geographic_insights_with_growth( mock_success_get_geographic_insights, mock_success_get_vendor_id_by_subaccount_id, default_geographics_subaccount_one_month_params, geographics_logic_expected_result_with_growth, mock_execute_in_parallel): """Test logic with sub account returns response with data.""" response = geographics_logic.get_geographic_insights( **default_geographics_subaccount_one_month_params) assert response.message == geographics_logic_expected_result_with_growth assert 'listeners_growth' in response.message.get('items')[0] assert 'streams_growth' in response.message.get('items')[0] assert mock_success_get_geographic_insights.called assert mock_success_get_geographic_insights.call_count == 2 assert mock_success_get_vendor_id_by_subaccount_id.called def test_get_geographic_insights_with_subaccount_error( mock_success_get_geographic_insights, mock_error_get_vendor_id_by_subaccount_id, default_geographics_subaccount_params): """Test logic with subaccount error.""" response = geographics_logic.get_geographic_insights( **default_geographics_subaccount_params) assert response.status == 500 assert not mock_success_get_geographic_insights.called assert mock_error_get_vendor_id_by_subaccount_id.called def test_get_geographic_insights_with_vendor_account( mock_success_get_geographic_insights, mock_success_get_vendor_id_by_subaccount_id, default_geographics_vendor_params, geographics_logic_expected_result): """Test logic with vendor account returns response with data.""" response = geographics_logic.get_geographic_insights( **default_geographics_vendor_params) assert response.message == geographics_logic_expected_result assert mock_success_get_geographic_insights.called assert not mock_success_get_vendor_id_by_subaccount_id.called def test_get_geographic_insights_no_content( mock_no_content_get_geographic_insights, mock_success_get_vendor_id_by_subaccount_id, default_geographics_subaccount_params): """Test logic returns 204 response when no data is found.""" response = geographics_logic.get_geographic_insights( **default_geographics_subaccount_params) assert response.status == 204 assert mock_no_content_get_geographic_insights.called assert mock_success_get_vendor_id_by_subaccount_id.called def test_get_geographic_insights_by_region_with_subaccount( mock_success_get_geographic_insights_by_region, mock_success_get_vendor_id_by_subaccount_id, geographics_by_region_logic_expected_result, default_geographics_subaccount_params): """Test logic with sub account returns response with data.""" response = geographics_logic.get_geographic_insights_by_region( **default_geographics_subaccount_params) assert response.message == geographics_by_region_logic_expected_result assert mock_success_get_geographic_insights_by_region.called assert mock_success_get_geographic_insights_by_region.call_count == 1 assert mock_success_get_vendor_id_by_subaccount_id.called def test_get_geographic_insights_by_region_with_growth( mock_success_get_geographic_insights_by_region, mock_success_get_vendor_id_by_subaccount_id, default_geographics_subaccount_one_month_params, geographics_by_region_expected_logic_result_with_growth): """Test logic with sub account returns response with data.""" response = geographics_logic.get_geographic_insights_by_region( **default_geographics_subaccount_one_month_params) assert response.message == ( geographics_by_region_expected_logic_result_with_growth) assert mock_success_get_geographic_insights_by_region.called assert mock_success_get_geographic_insights_by_region.call_count == 2 assert 'listeners_growth' in response.message.get('items')[0] assert 'streams_growth' in response.message.get('items')[0] assert mock_success_get_vendor_id_by_subaccount_id.called def test_get_geographic_insights_by_region_with_subaccount_error( mock_success_get_geographic_insights_by_region, mock_error_get_vendor_id_by_subaccount_id, default_geographics_subaccount_params): """Test logic with subaccount error.""" response = geographics_logic.get_geographic_insights_by_region( **default_geographics_subaccount_params) assert response.status == 500 assert not mock_success_get_geographic_insights_by_region.called assert mock_error_get_vendor_id_by_subaccount_id.called def test_get_geographic_insights_by_region_with_vendor_account( mock_success_get_geographic_insights_by_region, mock_success_get_vendor_id_by_subaccount_id, default_geographics_vendor_params, geographics_by_region_logic_expected_result): """Test logic with vendor account returns response with data.""" response = geographics_logic.get_geographic_insights_by_region( **default_geographics_vendor_params) assert response.message == geographics_by_region_logic_expected_result assert mock_success_get_geographic_insights_by_region.called assert not mock_success_get_vendor_id_by_subaccount_id.called def test_get_geographic_insights_by_region_no_content( mock_no_content_get_geographic_insights_by_region, mock_success_get_vendor_id_by_subaccount_id, default_geographics_subaccount_params): """Test logic returns 204 response when no data is found.""" response = geographics_logic.get_geographic_insights_by_region( **default_geographics_subaccount_params) assert response.status == 204 assert mock_no_content_get_geographic_insights_by_region.called assert mock_success_get_vendor_id_by_subaccount_id.called def test_get_geographic_orchard_regions( mock_success_get_geographic_orchard_regions, geographics_orchard_regions_db_formatted_result): """Test logic return response with data.""" response = geographics_logic.get_geographic_orchard_regions() assert response.message == geographics_orchard_regions_db_formatted_result assert mock_success_get_geographic_orchard_regions.called def test_get_geographic_orchard_regions_no_content( mock_no_content_get_geographic_orchard_regions): """Test logic return response with data.""" response = geographics_logic.get_geographic_orchard_regions() assert response.status == 204 assert mock_no_content_get_geographic_orchard_regions.called def test_compute_previous_time_period_7_days_difference(): """Test that _compute_previous_time_period works for 7 days.""" start_date = '2018-02-01' end_date = '2018-02-08' time_period = geographics_logic._compute_previous_time_period( start_date, end_date) assert time_period assert time_period.start_date == '2018-01-24' assert time_period.end_date == '2018-01-31' def test_compute_previous_time_period_28_days_difference(): """Test that _compute_previous_time_period works for 28 days.""" start_date = '2018-03-01' end_date = '2018-03-29' time_period = geographics_logic._compute_previous_time_period( start_date, end_date) assert time_period assert time_period.start_date == '2018-01-31' assert time_period.end_date == '2018-02-28' def test_compute_previous_time_period_365_days_difference(): """Test that _compute_previous_time_period works for 365 days.""" start_date = '2017-03-01' end_date = '2018-03-01' result = geographics_logic._compute_previous_time_period( start_date, end_date) assert result is None def test_get_and_compare_current_and_previous_data_by_region( mock_success_get_geographic_insights_by_region): """Test that _get_and_compare_current_and_previous_data_by_region works.""" this_time_period = TimePeriod('2018-02-01', '2018-03-01') previous_time_period = TimePeriod('2018-01-01', '2018-01-31') response = geographics_logic.\ _get_and_compare_current_and_previous_data_by_region( previous_time_period, this_time_period, None, None, None, [1, 2], 1, None, ['theorchard']) assert response.status == 200 assert mock_success_get_geographic_insights_by_region.called def test_get_and_compare_current_and_previous_data_by_region_with_storeid( mock_success_get_geographic_insights_by_region): """Test that _get_and_compare_current_and_previous_data_by_region works.""" this_time_period = TimePeriod('2018-02-01', '2018-03-01') previous_time_period = TimePeriod('2018-01-01', '2018-01-31') response = geographics_logic.\ _get_and_compare_current_and_previous_data_by_region( previous_time_period, this_time_period, None, None, [286], [1, 2], 1, None, ['theorchard']) assert response.status == 200 assert mock_success_get_geographic_insights_by_region.called def test_get_and_compare_current_and_previous_data( mock_success_get_geographic_insights): """Test that _get_and_compare_current_and_previous_data works.""" this_time_period = TimePeriod('2018-02-01', '2018-03-01') previous_time_period = TimePeriod('2018-01-01', '2018-01-31') response = geographics_logic._get_and_compare_current_and_previous_data( previous_time_period, this_time_period, None, None, None, [1, 2], 1, None, ['theorchard']) assert response.status == 200 assert mock_success_get_geographic_insights.called def test_get_and_compare_current_and_previous_data_with_storeid( mock_success_get_geographic_insights): """Test that _get_and_compare_current_and_previous_data works.""" this_time_period = TimePeriod('2018-02-01', '2018-03-01') previous_time_period = TimePeriod('2018-01-01', '2018-01-31') response = geographics_logic._get_and_compare_current_and_previous_data( previous_time_period, this_time_period, None, None, [286], [1, 2], 1, None, ['theorchard']) assert response.status == 200 assert mock_success_get_geographic_insights.called def test_calculate_increase_decrease_percentage_for_territory( geographics_db_result_formatted_result, previous_geographics_db_result_formatted_result): """Test _calculate_increase_decrease_percentage works for territories.""" result = geographics_logic._calculate_increase_decrease_percentage( geographics_db_result_formatted_result, previous_geographics_db_result_formatted_result, 'territory_code') expected_result = [ { 'territory_code': 'FR', 'unique_listeners': 0, 'number_of_streams': 2, 'listeners_growth': 0.0, 'streams_growth': 100.0 }, { 'territory_code': 'GB', 'unique_listeners': 0, 'number_of_streams': 3, 'listeners_growth': 0.0, 'streams_growth': 50.0 }, { 'territory_code': 'US', 'unique_listeners': 0, 'number_of_streams': 4, 'listeners_growth': 0.0, 'streams_growth': 33.33 } ] assert result == expected_result def test_calculate_increase_decrease_percentage_for_region( geographics_by_region_db_result_formatted_result, previous_geographics_by_region_db_result_formatted_result): """Test that _calculate_increase_decrease_percentage works for regions.""" result = geographics_logic._calculate_increase_decrease_percentage( geographics_by_region_db_result_formatted_result, previous_geographics_by_region_db_result_formatted_result, 'region_code') expected_result = [ { 'region_name': 'Provence-Alpes', 'unique_listeners': 0, 'listeners_growth': 0.0, 'number_of_streams': 1, 'streams_growth': 0.0, 'region_code': 'FR-U', 'territory_code': 'FR' }, { 'region_name': 'New-York', 'unique_listeners': 0, 'listeners_growth': 0.0, 'number_of_streams': 3, 'streams_growth': 200.0, 'region_code': '501', 'territory_code': 'US' }, { 'region_name': 'Glasgow', 'unique_listeners': 0, 'listeners_growth': 0.0, 'number_of_streams': 2, 'streams_growth': 0.0, 'region_code': 'GB-GLG', 'territory_code': 'GB' } ] assert result == expected_result def test_get_geographics_latest_date( mock_success_get_geographics_latest_date): """Test that get_geographics_latest_date works.""" result = geographics_logic.get_geographics_latest_date( [1, 2], ['theorchard']) expected_result = {'last_activity_date': '2018-04-30'} assert mock_success_get_geographics_latest_date.called assert result.message == expected_result assert result.status == 200 def test_get_geographics_latest_date_error( mock_error_get_geographics_latest_date): """Test get_geographics_latest_date with response error.""" response = geographics_logic.get_geographics_latest_date( feed_ids=[1, 2], distributors=['theorchard']) assert response.status == 500 assert mock_error_get_geographics_latest_date.called