"""Test streams model with connection to test database.""" import pytest from analytics.consts import models as model_consts from analytics.models import streams from tests.integration.models.db_setup import DBSetup CREATE_SUMMARY_SOS = 'CREATE TABLE summary_sos LIKE FACTS.QA.summary_sos' CREATE_SUMMARY_METRICS_BY_ISRC = 'CREATE TABLE summary_metrics_by_isrc LIKE' \ ' FACTS.QA.summary_metrics_by_isrc' INSERT_INTO_SUMMARY_SOS = """ INSERT INTO summary_sos (labelid, subaccountid, download_activity_date, isrc, artistid, storeid, streams_from_passive_discovery, streams_from_active_discovery, streams_from_collection, overall_number_of_streams) VALUES (7123, NULL, '2017-12-01', 'DED831000251', 161611, 286, 0, 2, 2, 4), (7123, NULL, '2017-12-02', 'DED831000251', 161611, 286, 0, 1, 2, 3), (7123, NULL, '2017-12-02', 'DED831000251', 161611, 1, 0, 1, 3, 4), (7123, NULL, '2017-12-01', 'DED831000396', 161540, 286, 1, 2, 0, 3), (7123, 1540, '2017-12-01', 'FI3EM0800039', 392645, 286, 1, 1, 1, 3), (15064, NULL, '2017-12-01', 'USA560844308', 118152, 286, 0, 2, 2, 4), (15064, NULL, '2017-12-01', 'USA560844308', 118152, 286, 0, 1, 2, 3)""" INSERT_INTO_summary_metrics_by_isrc = """ INSERT INTO summary_metrics_by_isrc (labelid, subaccountid, download_activity_date, isrc, artistid, storeid, streams_from_passive_discovery, streams_from_active_discovery, streams_from_collection, overall_number_of_streams, skips, saves) VALUES (7123, NULL, '2017-12-01', 'DED831000251', 161611, 286, 0, 2, 2, 4, 2, 2), (7123, NULL, '2017-12-02', 'DED831000251', 161611, 286, 0, 1, 2, 3, 2, 1), (7123, NULL, '2017-12-02', 'DED831000251', 161611, 1, 0, 1, 3, 4, 3, 1), (7123, NULL, '2017-12-01', 'DED831000396', 161540, 286, 1, 2, 0, 3, 0, 2), (7123, 1540, '2017-12-01', 'FI3EM0800039', 392645, 286, 1, 1, 1, 3, 1, 1), (15064, NULL, '2017-12-01', 'USA560844308', 118152, 286, 0, 2, 2, 4, 2, 2), (15064, NULL, '2017-12-01', 'USA560844308', 118152, 286, 0, 1, 2, 3, 2, 1)""" @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_SUMMARY_SOS, INSERT_INTO_SUMMARY_SOS, CREATE_SUMMARY_METRICS_BY_ISRC, INSERT_INTO_summary_metrics_by_isrc) yield db class TestWithFullParamsList(object): """Test get_streams with full parameter list.""" params = { 'start_date': '2017-12-01', 'end_date': '2017-12-03', 'labelid': 7123, 'subaccountid': None, 'storeids': [model_consts.STORES.get('SPOTIFY')], 'isrcs': ['DED831000251'], 'artistids': [161611]} def test_get_streams(self): """Test get_streams.""" assert streams.get_streams(**self.params).message == [ { 'date': '2017-12-01', 'overall_number_of_streams': 4, 'streams_from_active_discovery': 2, 'streams_from_collection': 2, 'streams_from_passive_discovery': 0}, { 'date': '2017-12-02', 'overall_number_of_streams': 3, 'streams_from_active_discovery': 1, 'streams_from_collection': 2, 'streams_from_passive_discovery': 0}, { 'date': '2017-12-03', 'overall_number_of_streams': 0, 'streams_from_active_discovery': 0, 'streams_from_collection': 0, 'streams_from_passive_discovery': 0}] class TestWithFullParamsList_V2(object): """Test get_streams with full parameter list.""" params = { 'start_date': '2017-12-01', 'end_date': '2017-12-03', 'labelid': 7123, 'subaccountid': None, 'storeids': [model_consts.STORES.get('SPOTIFY')], 'isrcs': ['DED831000251'], 'artistids': [161611]} def test_get_streams(self): """Test get_streams.""" assert streams.get_streams_v2(**self.params).message == [ { 'date': '2017-12-01', 'overall_number_of_streams': 4, 'overall_number_of_spotify_streams': 4, 'streams_from_active_discovery': 2, 'streams_from_collection': 2, 'streams_from_passive_discovery': 0, 'skips': 2, 'saves': 2}, { 'date': '2017-12-02', 'overall_number_of_streams': 3, 'overall_number_of_spotify_streams': 3, 'streams_from_active_discovery': 1, 'streams_from_collection': 2, 'streams_from_passive_discovery': 0, 'skips': 2, 'saves': 1}, { 'date': '2017-12-03', 'overall_number_of_streams': 0, 'overall_number_of_spotify_streams': 0, 'streams_from_active_discovery': 0, 'streams_from_collection': 0, 'streams_from_passive_discovery': 0, 'skips': 0, 'saves': 0}] class TestWithMinimalParamsList(object): """Test get_streams with minimal parameter list.""" params = { 'start_date': '2017-12-01', 'end_date': '2017-12-03', 'labelid': 7123, 'subaccountid': None, 'storeids': [], 'isrcs': [], 'artistids': []} def test_get_streams(self): """Test get_streams.""" assert streams.get_streams(**self.params).message == [ { 'date': '2017-12-01', 'overall_number_of_streams': 10, 'streams_from_active_discovery': 5, 'streams_from_collection': 3, 'streams_from_passive_discovery': 2}, { 'date': '2017-12-02', 'overall_number_of_streams': 7, 'streams_from_active_discovery': 2, 'streams_from_collection': 5, 'streams_from_passive_discovery': 0}, { 'date': '2017-12-03', 'overall_number_of_streams': 0, 'streams_from_active_discovery': 0, 'streams_from_collection': 0, 'streams_from_passive_discovery': 0}] class TestWithMinimalParamsList_V2(object): """Test get_streams with minimal parameter list.""" params = { 'start_date': '2017-12-01', 'end_date': '2017-12-03', 'labelid': 7123, 'subaccountid': None, 'storeids': [], 'isrcs': [], 'artistids': []} def test_get_streams(self): """Test get_streams.""" assert streams.get_streams_v2(**self.params).message == [ { 'date': '2017-12-01', 'overall_number_of_streams': 10, 'overall_number_of_spotify_streams': 10, 'streams_from_active_discovery': 5, 'streams_from_collection': 3, 'streams_from_passive_discovery': 2, 'skips': 3, 'saves': 5}, { 'date': '2017-12-02', 'overall_number_of_streams': 7, 'overall_number_of_spotify_streams': 3, 'streams_from_active_discovery': 2, 'streams_from_collection': 5, 'streams_from_passive_discovery': 0, 'skips': 5, 'saves': 2}, { 'date': '2017-12-03', 'overall_number_of_streams': 0, 'overall_number_of_spotify_streams': 0, 'streams_from_active_discovery': 0, 'streams_from_collection': 0, 'streams_from_passive_discovery': 0, 'skips': 0, 'saves': 0}] class TestWithUnrecognizedParamValues(object): """Test get_streams with parameter list with unrecognized values.""" params = { 'start_date': '2017-12-01', 'end_date': '2017-12-03', 'labelid': 7123, 'subaccountid': None, 'storeids': [], 'isrcs': ['NOTAREALISRC001'], 'artistids': []} def test_get_streams(self): """Test get_streams.""" assert streams.get_streams(**self.params).message == [ { 'date': '2017-12-01', 'overall_number_of_streams': 0, 'streams_from_active_discovery': 0, 'streams_from_collection': 0, 'streams_from_passive_discovery': 0}, { 'date': '2017-12-02', 'overall_number_of_streams': 0, 'streams_from_active_discovery': 0, 'streams_from_collection': 0, 'streams_from_passive_discovery': 0}, { 'date': '2017-12-03', 'overall_number_of_streams': 0, 'streams_from_active_discovery': 0, 'streams_from_collection': 0, 'streams_from_passive_discovery': 0}] class TestWithUnrecognizedParamValues_V2(object): """Test get_streams with parameter list with unrecognized values.""" params = { 'start_date': '2017-12-01', 'end_date': '2017-12-03', 'labelid': 7123, 'subaccountid': None, 'storeids': [], 'isrcs': ['NOTAREALISRC001'], 'artistids': []} def test_get_streams(self): """Test get_streams.""" assert streams.get_streams_v2(**self.params).message == [ { 'date': '2017-12-01', 'overall_number_of_streams': 0, 'overall_number_of_spotify_streams': 0, 'streams_from_active_discovery': 0, 'streams_from_collection': 0, 'streams_from_passive_discovery': 0, 'skips': 0, 'saves': 0}, { 'date': '2017-12-02', 'overall_number_of_streams': 0, 'overall_number_of_spotify_streams': 0, 'streams_from_active_discovery': 0, 'streams_from_collection': 0, 'streams_from_passive_discovery': 0, 'skips': 0, 'saves': 0}, { 'date': '2017-12-03', 'overall_number_of_streams': 0, 'overall_number_of_spotify_streams': 0, 'streams_from_active_discovery': 0, 'streams_from_collection': 0, 'streams_from_passive_discovery': 0, 'skips': 0, 'saves': 0}] class TestWithSubaccountidInParamsList(object): """Test get_streams with subaccountid in parameter list.""" params = { 'start_date': '2017-12-01', 'end_date': '2017-12-03', 'labelid': 7123, 'subaccountid': 1540, 'storeids': [model_consts.STORES.get('SPOTIFY')], 'isrcs': ['FI3EM0800039'], 'artistids': [392645]} def test_get_streams(self): """Test get_streams.""" assert streams.get_streams(**self.params).message == [ { 'date': '2017-12-01', 'overall_number_of_streams': 3, 'streams_from_active_discovery': 1, 'streams_from_collection': 1, 'streams_from_passive_discovery': 1}, { 'date': '2017-12-02', 'overall_number_of_streams': 0, 'streams_from_active_discovery': 0, 'streams_from_collection': 0, 'streams_from_passive_discovery': 0}, { 'date': '2017-12-03', 'overall_number_of_streams': 0, 'streams_from_active_discovery': 0, 'streams_from_collection': 0, 'streams_from_passive_discovery': 0}] class TestWithSubaccountidInParamsList_V2(object): """Test get_streams with subaccountid in parameter list.""" params = { 'start_date': '2017-12-01', 'end_date': '2017-12-03', 'labelid': 7123, 'subaccountid': 1540, 'storeids': [model_consts.STORES.get('SPOTIFY')], 'isrcs': ['FI3EM0800039'], 'artistids': [392645]} def test_get_streams(self): """Test get_streams.""" assert streams.get_streams_v2(**self.params).message == [ { 'date': '2017-12-01', 'overall_number_of_streams': 3, 'overall_number_of_spotify_streams': 3, 'streams_from_active_discovery': 1, 'streams_from_collection': 1, 'streams_from_passive_discovery': 1, 'skips': 1, 'saves': 1}, { 'date': '2017-12-02', 'overall_number_of_streams': 0, 'overall_number_of_spotify_streams': 0, 'streams_from_active_discovery': 0, 'streams_from_collection': 0, 'streams_from_passive_discovery': 0, 'skips': 0, 'saves': 0}, { 'date': '2017-12-03', 'overall_number_of_streams': 0, 'overall_number_of_spotify_streams': 0, 'streams_from_active_discovery': 0, 'streams_from_collection': 0, 'streams_from_passive_discovery': 0, 'skips': 0, 'saves': 0}]