"""Test geographics model with connection to test database.""" import json import pytest from analytics.connectors import redis from analytics.models import demographics, utils from tests.integration.models.db_setup import DBSetup CREATE_TABLE_FACT_DEMOGRAPHICS = """ CREATE TABLE fact_demographics LIKE facts.qa.fact_demographics""" CREATE_TABLE_SUMMARY_DEMOGRAPHICS_BY_ISRC = """ CREATE TABLE summary_demographics_by_isrc LIKE facts.qa.summary_demographics_by_isrc""" CREATE_TABLE_SUMMARY_DEMOGRAPHICS_BY_ARTIST = """ CREATE TABLE summary_demographics_by_artist LIKE facts.qa.summary_demographics_by_artist""" CREATE_TABLE_SUMMARY_DEMOGRAPHICS_BY_LABEL = """ CREATE TABLE summary_demographics_by_label LIKE facts.qa.summary_demographics_by_label""" @pytest.yield_fixture(scope='module', autouse=True) def db_setup(): """Create object for setting up test database.""" with DBSetup() as db: db.execute_queries( CREATE_TABLE_FACT_DEMOGRAPHICS, CREATE_TABLE_SUMMARY_DEMOGRAPHICS_BY_ISRC, CREATE_TABLE_SUMMARY_DEMOGRAPHICS_BY_ARTIST, CREATE_TABLE_SUMMARY_DEMOGRAPHICS_BY_LABEL ) db.execute_seed_scripts( 'demographics_model_fact_demographics_test_data.sql', 'summary_demographics_by_isrc.sql', 'summary_demographics_by_artist.sql', 'summary_demographics_by_label.sql' ) yield db def test_get_cohorts_filter_by_label(): """Test test_get_cohorts_filter_by_label.""" params = { 'labelid': 1, 'store_ids': [286], 'start_date': '2018-01-01', 'end_date': '2018-01-01', 'isrcs': None, 'artist_ids': None, 'subaccountid': None, 'territory_code': None, 'region_code': None } response = demographics.get_cohorts(**params) expected_data = [ ['<18', 'women', 2], ['25-34', 'women', 1], ['35-44', 'men', 2] ] cohorts = [demographics.Cohort(*c) for c in expected_data] expected_message = [cohorts[0], cohorts[1], cohorts[2]] assert response.message == expected_message def test_get_cohorts_filter_by_territory_code(): """Test get_cohorts filtered by territory code.""" params = { 'labelid': 1, 'store_ids': [286], 'start_date': '2018-01-01', 'end_date': '2018-01-01', 'isrcs': None, 'artist_ids': None, 'subaccountid': None, 'territory_code': 'FR', 'region_code': None } response = demographics.get_cohorts(**params) expected_data = [ ['35-44', 'men', 2] ] cohorts = [demographics.Cohort(*c) for c in expected_data] expected_message = [cohorts[0]] assert response.message == expected_message def test_get_cohorts_filter_by_territory_and_region_code(): """Test get_cohorts filtered by a territory and region code.""" params = { 'labelid': 1, 'store_ids': [286], 'start_date': '2018-01-01', 'end_date': '2018-01-01', 'isrcs': None, 'artist_ids': None, 'subaccountid': None, 'territory_code': 'FR', 'region_code': 'FR-U' } response = demographics.get_cohorts(**params) expected_data = [ ['35-44', 'men', 1] ] cohorts = [demographics.Cohort(*c) for c in expected_data] expected_message = [cohorts[0]] assert response.message == expected_message def test_get_cohorts_filter_by_isrc(): """Test get_cohorts filtered by ISRCs.""" params = { 'labelid': 1, 'store_ids': [286], 'start_date': '2018-01-01', 'end_date': '2018-01-01', 'isrcs': ['2', '3'], 'artist_ids': None, 'subaccountid': None, 'territory_code': None, 'region_code': None } response = demographics.get_cohorts(**params) expected_data = [ ['<18', 'women', 2], ['25-34', 'women', 1] ] cohorts = [demographics.Cohort(*c) for c in expected_data] expected_message = [cohorts[0], cohorts[1]] assert response.message == expected_message def test_get_cohorts_filter_by_artist(): """Test get_cohorts filtered by artist ID.""" params = { 'labelid': 1, 'store_ids': [286], 'start_date': '2018-01-01', 'end_date': '2018-01-01', 'isrcs': None, 'artist_ids': ['1', '2'], 'subaccountid': None, 'territory_code': None, 'region_code': None } response = demographics.get_cohorts(**params) expected_data = [ ['<18', 'women', 1], ['25-34', 'women', 1], ['35-44', 'men', 2] ] cohorts = [demographics.Cohort(*c) for c in expected_data] expected_message = [cohorts[0], cohorts[1], cohorts[2]] assert response.message == expected_message def test_get_cohorts_filter_by_label_apple_music(): """Test get_cohorts for apple music.""" params = { 'labelid': 1, 'store_ids': [1], 'start_date': '2018-01-01', 'end_date': '2018-01-01', 'isrcs': None, 'artist_ids': None, 'subaccountid': None, 'territory_code': None, 'region_code': None } response = demographics.get_cohorts(**params) expected_data = [ ['<18', 'men', 2], ['25-34', 'women', 1], ['35-44', 'men', 1] ] cohorts = [demographics.Cohort(*c) for c in expected_data] expected_message = [cohorts[0], cohorts[1], cohorts[2]] assert response.message == expected_message def test_get_cohorts_filter_by_territory_code_apple_music(): """Test get_cohorts filtered by territory code for apple music.""" params = { 'labelid': 1, 'store_ids': [1], 'start_date': '2018-01-01', 'end_date': '2018-01-01', 'isrcs': None, 'artist_ids': None, 'subaccountid': None, 'territory_code': 'FR', 'region_code': None } response = demographics.get_cohorts(**params) expected_data = [ ['35-44', 'men', 1] ] cohorts = [demographics.Cohort(*c) for c in expected_data] expected_message = [cohorts[0]] assert response.message == expected_message def test_get_cohorts_filter_by_territory_code_apple_music_cache(): """Test get_cohorts filtered by territory code for apple music.""" params = { 'labelid': 1, 'store_ids': [1], 'start_date': '2018-01-01', 'end_date': '2018-01-01', 'isrcs': None, 'artist_ids': None, 'subaccountid': None, 'territory_code': 'FR', 'region_code': None, 'query': 'get_summary_demographics' } cache_key = utils.get_cache_key(params) cached_result = redis.client.get(cache_key) expected_data = [ ['35-44', 'men', 1] ] assert json.loads(cached_result.decode('utf8')) == expected_data def test_get_cohorts_filter_by_territory_and_region_code_apple_music(): """Test get_cohorts filtered by territory & region code for apple music.""" params = { 'labelid': 1, 'store_ids': [1], 'start_date': '2018-01-01', 'end_date': '2018-01-01', 'isrcs': None, 'artist_ids': None, 'subaccountid': None, 'territory_code': 'GB', 'region_code': 'GB-GLG' } response = demographics.get_cohorts(**params) expected_data = [ ['25-34', 'women', 1], ] cohorts = [demographics.Cohort(*c) for c in expected_data] expected_message = [cohorts[0]] assert response.message == expected_message def test_get_cohorts_filter_by_isrc_apple_music(): """Test get_cohorts filtered by ISRCs for apple music.""" params = { 'labelid': 1, 'store_ids': [1], 'start_date': '2018-01-01', 'end_date': '2018-01-02', 'isrcs': ['2', '3'], 'artist_ids': None, 'subaccountid': None, 'territory_code': None, 'region_code': None } response = demographics.get_cohorts(**params) expected_data = [ ['<18', 'men', 2], ['25-34', 'women', 1], ['45-54', 'women', 1] ] cohorts = [demographics.Cohort(*c) for c in expected_data] expected_message = [cohorts[0], cohorts[1], cohorts[2]] assert response.message == expected_message def test_get_cohorts_filter_by_artist_apple_music(): """Test get_cohorts filtered by artist ID for apple music.""" params = { 'labelid': 1, 'store_ids': [1], 'start_date': '2018-01-01', 'end_date': '2018-01-01', 'isrcs': None, 'artist_ids': ['1', '2'], 'subaccountid': None, 'territory_code': None, 'region_code': None } response = demographics.get_cohorts(**params) expected_data = [ ['<18', 'men', 1], ['25-34', 'women', 1], ['35-44', 'men', 1] ] cohorts = [demographics.Cohort(*c) for c in expected_data] expected_message = [cohorts[0], cohorts[1], cohorts[2]] assert response.message == expected_message