"""Unit tests for geographics model layer.""" from unittest import mock from unittest.mock import MagicMock import pytest from analytics.models import geographics as geographics_models @pytest.fixture() def mock_snowflake(monkeypatch, mock_config): """Mock for Snowflake database client.""" snowflake_mock = MagicMock() monkeypatch.setattr( 'analytics.models.geographics.snowflake', snowflake_mock) return snowflake_mock def test_get_geographic_insights( monkeypatch, mock_snowflake, geographics_db_result, default_geographics_params): """Test get_geographic_insights.""" get_sql_mock = mock.Mock() monkeypatch.setattr( geographics_models, '_get_geographic_insights_sql', get_sql_mock) mock_snowflake.fetchall.return_value = geographics_db_result expected_result = [ { 'territory_code': 'GB', 'unique_listeners': 0, 'number_of_streams': 321 }, { 'territory_code': 'FR', 'unique_listeners': 0, 'number_of_streams': 654 } ] response = geographics_models.\ get_geographic_insights(**default_geographics_params) get_sql_mock.assert_called_once_with({ 'start_date': '2017-01-01', 'end_date': '2017-12-01', 'labelid': 1, 'subaccountid': 2, 'artist_ids': None, 'store_ids': [1, 286], 'feed_ids': [1, 2], 'isrcs': None, 'distributors': ['theorchard'], 'query': 'get_geographic_insights_v3'}, 'get_geographics_insights_worldwide_from_label_summary') assert response.message == expected_result def test__get_geographic_insights_sql(): """Test that get_geographic_insights_sql return summary label sql. When called with no ISRCs and no artists, it should use summary_geographics_by_label. """ params = { 'start_date': '2017-01-01', 'end_date': '2017-12-01', 'labelid': 21786, 'subaccountid': None, 'artist_ids': None, 'store_ids': [1, 286], 'feed_ids': [1, 2], 'isrcs': None, 'distributors': ['theorchard']} sql = geographics_models. \ _get_geographic_insights_sql( params, 'get_geographics_insights_worldwide_from_label_summary') assert 'summary_geographics_by_label' in sql assert ':start_date' in sql assert ':end_date' in sql assert ':labelid' in sql assert ':subaccountid' not in sql assert ':isrcs' not in sql assert ':artist_ids' not in sql assert ':store_ids' in sql assert ':feed_ids' in sql assert ':distributors' in sql def test__get_geographic_insights_sql_by_isrc_with_clauses(): """Test that get_geographic_insights_sql return right sql w/ clauses. When called with ISRCs and artists and store ids, it should use summary_geographics_by_isrc with artist clause, isrc clause and store id clause. """ params = { 'start_date': '2017-01-01', 'end_date': '2017-12-01', 'labelid': 21786, 'subaccountid': None, 'artist_ids': [1], 'store_ids': [1, 286], 'feed_ids': [1, 2], 'isrcs': ['abc'], 'distributors': ['theorchard']} sql = geographics_models. \ _get_geographic_insights_sql( params, 'get_geographics_insights_worldwide_from_isrc_summary') assert 'summary_geographics_by_isrc' in sql assert ':start_date' in sql assert ':end_date' in sql assert ':labelid' in sql assert ':subaccountid' not in sql assert ':isrcs' in sql assert ':artist_ids' in sql assert ':store_ids' in sql assert ':feed_ids' in sql assert ':distributors' in sql def test__get_geographic_insights_sql_by_label_with_clauses(): """Test that get_geographic_insights_sql return summary label sql. When called with no ISRCs and no artists and store ids, it should use summary_geographics_by_label with store ids clause. """ params = { 'start_date': '2017-01-01', 'end_date': '2017-12-01', 'labelid': 21786, 'subaccountid': None, 'artist_ids': None, 'store_ids': [1, 286], 'feed_ids': [1, 2], 'isrcs': None, 'distributors': ['theorchard']} sql = geographics_models. \ _get_geographic_insights_sql( params, 'get_geographics_insights_worldwide_from_label_summary') assert 'summary_geographics_by_label' in sql assert ':start_date' in sql assert ':end_date' in sql assert ':labelid' in sql assert ':subaccountid' not in sql assert ':isrcs' not in sql assert ':artist_ids' not in sql assert ':store_ids' in sql assert ':feed_ids' in sql assert ':distributors' in sql def test__get_geographic_insights_sql_by_artist_with_clauses(): """Test that get_geographic_insights_sql return summary artist sql. When called with no ISRCs and artists and store ids, it should use summary_geographics_by_label with store ids and artist clause. """ params = { 'start_date': '2017-01-01', 'end_date': '2017-12-01', 'labelid': 21786, 'subaccountid': None, 'artist_ids': [1], 'store_ids': [1, 286], 'feed_ids': [1, 2], 'isrcs': None, 'distributors': None} sql = geographics_models. \ _get_geographic_insights_sql( params, 'get_geographics_insights_worldwide_from_artist_summary') assert 'summary_geographics_by_artist' in sql assert ':start_date' in sql assert ':end_date' in sql assert ':labelid' in sql assert ':subaccountid' not in sql assert ':isrcs' not in sql assert ':artist_ids' in sql assert ':store_ids' in sql assert ':feed_ids' in sql assert ':distributors' not in sql def test__choose_geographics_query_for_label(): """Test that _choose_geographics_query chooses the correct query name. When called with no ISRCs and no artists, it should use summary_geographics_by_label. """ params = { 'start_date': '2017-01-01', 'end_date': '2017-12-01', 'labelid': 21786, 'subaccountid': None, 'artist_ids': None, 'store_ids': [1, 286], 'feed_ids': [1, 2], 'isrcs': None, 'distributors': ['theorchard'] } query_name = geographics_models._choose_geographics_query(params) assert query_name == \ 'get_geographics_insights_worldwide_from_label_summary' def test__choose_geographics_query_for_artists(): """Test that _choose_geographics_query returns the correct query name. When called with no ISRCs and some artists, it should use summary_geographics_by_artist. """ params = { 'start_date': '2017-01-01', 'end_date': '2017-12-01', 'labelid': 21786, 'subaccountid': None, 'artist_ids': [123, 456], 'store_ids': [1, 286], 'feed_ids': [1, 2], 'isrcs': None, 'distributors': ['theorchard'] } query_name = geographics_models._choose_geographics_query(params) assert query_name == \ 'get_geographics_insights_worldwide_from_artist_summary' def test__choose_geographics_query_for_isrcs(): """Test that _choose_geographics_query returns the correct query name. When called with some ISRCs and no artists, it should use summary_geographics_by_isrc. """ params = { 'start_date': '2017-01-01', 'end_date': '2017-12-01', 'labelid': 21786, 'subaccountid': None, 'artist_ids': None, 'store_ids': [1, 286], 'feed_ids': [1, 2], 'isrcs': ['isrc1', 'isrc2'], 'distributors': ['theorchard'] } query_name = geographics_models._choose_geographics_query(params) assert query_name == \ 'get_geographics_insights_worldwide_from_isrc_summary' def test_get_geographic_insights_with_no_result( monkeypatch, mock_snowflake, default_geographics_params): """Test get_geographic_insights with no result back from DB.""" get_sql_mock = mock.Mock() monkeypatch.setattr( geographics_models, '_get_geographic_insights_sql', get_sql_mock) mock_snowflake.fetchall.return_value = [] response = geographics_models.\ get_geographic_insights(**default_geographics_params) get_sql_mock.assert_called_once_with({ 'start_date': '2017-01-01', 'end_date': '2017-12-01', 'labelid': 1, 'subaccountid': 2, 'artist_ids': None, 'store_ids': [1, 286], 'feed_ids': [1, 2], 'isrcs': None, 'distributors': ['theorchard'], 'query': 'get_geographic_insights_v3'}, 'get_geographics_insights_worldwide_from_label_summary') assert response.message is None assert response.errors is None assert response.status == 204 def test__choose_geographics_by_region_query_for_label(): """Test that _choose_geographics_by_region_query chooses label. When called with no ISRCs and no artists, it should use summary_geographics_by_label. """ params = { 'start_date': '2017-01-01', 'end_date': '2017-12-01', 'labelid': 21786, 'subaccountid': None, 'artist_ids': None, 'store_ids': [1, 286], 'feed_ids': [1, 2], 'isrcs': None, 'distributors': ['theorchard'] } query_name = geographics_models._choose_geographics_by_region_query(params) assert query_name == \ 'get_geographics_insights_by_region_from_label_summary' def test__choose_geographics_by_region_query_for_artists(): """Test that _choose_geographics_by_region_query chooses artist. When called with no ISRCs and some artists, it should use summary_geographics_by_artist. """ params = { 'start_date': '2017-01-01', 'end_date': '2017-12-01', 'labelid': 21786, 'subaccountid': None, 'artist_ids': [123, 456], 'store_ids': [1, 286], 'feed_ids': [1, 2], 'isrcs': None, 'distributors': ['theorchard'] } query_name = geographics_models._choose_geographics_by_region_query(params) assert query_name == \ 'get_geographics_insights_by_region_from_artist_summary' def test__choose_geographics_by_region_query_for_isrcs(): """Test that _choose_geographics_by_region_query chooses by isrc. When called with some ISRCs and no artists, it should use summary_geographics_by_isrc. """ params = { 'start_date': '2017-01-01', 'end_date': '2017-12-01', 'labelid': 21786, 'subaccountid': None, 'artist_ids': None, 'store_ids': [1, 286], 'feed_ids': [1, 2], 'isrcs': ['isrc1', 'isrc2'], 'distributors': ['theorchard'] } query_name = geographics_models._choose_geographics_by_region_query(params) assert query_name == \ 'get_geographics_insights_by_region_from_isrc_summary' def test_get_geographic_insights_by_region( monkeypatch, mock_snowflake, geographics_db_result_by_region, default_geographics_params): """Test get_geographic_insights_by_region.""" get_sql_mock = mock.Mock() monkeypatch.setattr( geographics_models, '_get_geographic_insights_sql', get_sql_mock) mock_snowflake.fetchall.return_value = geographics_db_result_by_region expected_result = [ { 'region_name': 'Provence-Alpes', 'region_code': 'FR-U', 'territory_code': 'FR', 'unique_listeners': 0, 'number_of_streams': 654 }, { 'region_name': 'Glasgow', 'region_code': 'GB-GLG', 'territory_code': 'GB', 'unique_listeners': 0, 'number_of_streams': 321 } ] response = geographics_models.\ get_geographic_insights_by_region(**default_geographics_params) get_sql_mock.assert_called_once_with({ 'start_date': '2017-01-01', 'end_date': '2017-12-01', 'labelid': 1, 'subaccountid': 2, 'artist_ids': None, 'store_ids': [1, 286], 'feed_ids': [1, 2], 'isrcs': None, 'distributors': ['theorchard'], 'query': 'get_geographic_insights_by_region_v3' }, 'get_geographics_insights_by_region_from_label_summary') assert response.message == expected_result def test_get_geographic_insights_by_region_with_no_result( monkeypatch, mock_snowflake, default_geographics_params): """Test get_geographic_insights_by_region with no result back from DB.""" get_sql_mock = mock.Mock() monkeypatch.setattr( geographics_models, '_get_geographic_insights_sql', get_sql_mock) mock_snowflake.fetchall.return_value = [] response = geographics_models.\ get_geographic_insights_by_region(**default_geographics_params) get_sql_mock.assert_called_once_with({ 'start_date': '2017-01-01', 'end_date': '2017-12-01', 'labelid': 1, 'subaccountid': 2, 'artist_ids': None, 'store_ids': [1, 286], 'feed_ids': [1, 2], 'isrcs': None, 'distributors': ['theorchard'], 'query': 'get_geographic_insights_by_region_v3' }, 'get_geographics_insights_by_region_from_label_summary') assert response.message is None assert response.errors is None assert response.status == 204 def test_get_geographic_orchard_regions( monkeypatch, mock_snowflake, geographics_db_result_orchard_regions): """Test test_get_geographic_orchard_regions.""" mock_snowflake.fetchall.return_value = \ geographics_db_result_orchard_regions expected_result = [ { 'region_code': None, 'region_name': 'Provence-Alpes', 'territory_code': 'FR' }, { 'region_code': None, 'region_name': 'Glasgow', 'territory_code': 'GB' }, { 'region_code': None, 'region_name': 'New-York', 'territory_code': 'US' } ] response = geographics_models.get_geographic_orchard_regions() assert response.message == expected_result def test_get_geographic_orchard_regions_with_no_result( monkeypatch, mock_snowflake): """Test get_geographic_orchard_regions with no result back from DB.""" mock_snowflake.fetchall.return_value = [] response = geographics_models.get_geographic_orchard_regions() assert response.message is None assert response.errors is None assert response.status == 204 def test_get_geographics_latest_date( monkeypatch, mock_snowflake, geographics_db_result_orchard_max_date_by_label): """Test test_get_geographics_latest_date.""" mock_snowflake.fetchone.side_effect = [ geographics_db_result_orchard_max_date_by_label] expected_result = '2018-04-30' response = geographics_models.get_geographics_latest_date( [1, 2], ['theorchard']) assert response.message == expected_result def test_get_geographics_latest_date_with_no_result( monkeypatch, mock_snowflake): """Test get_geographics_latest_date with no result back from DB.""" mock_snowflake.fetchone.side_effect = [None] response = geographics_models.get_geographics_latest_date( [1, 2], ['theorchard']) assert response.message is None assert response.errors is None assert response.status == 200