"""Tests for Metric logic.""" from unittest.mock import MagicMock from oto import response from oto import status import pytest from social_analytics.constants import error from social_analytics.logic import metrics from social_analytics.models import artist_social_profile from social_analytics.models import metric @pytest.fixture def time_series_data(): """Fixture for time series data.""" return response.Response( {'items': [ { 'social_profile_id': 2, 'platform_id': 11872, 'label_id': 1, 'metric_id': 1, 'metric_value': 200.0, 'processed_datetime': '2017-09-25 11:19:15', 'activity_datetime': '2017-09-25 11:19:15' }, { 'social_profile_id': 2, 'platform_id': 11872, 'label_id': 1, 'metric_id': 1, 'metric_value': 350.0, 'processed_datetime': '2017-09-26 11:19:15', 'activity_datetime': '2017-09-26 11:19:15' } ]}) @pytest.fixture def artist_social_profiles(): """Fixture for artist social profiles.""" return response.Response( {'items': [ { 'social_profile_id': 4, 'artist_id': 3, 'label_id': 3 }, { 'social_profile_id': 5, 'artist_id': 3, 'label_id': 3 }, { 'social_profile_id': 6, 'artist_id': 3, 'label_id': 3 } ]}) @pytest.fixture def latest_metrics(): """Fixture for latest metrics.""" return [ response.Response( { 'social_profile_id': 4, 'platform': 'facebook', 'metric': 'fan_count', 'metric_value': 1234 }), response.Response( { 'social_profile_id': 5, 'platform': 'instagram', 'metric': 'followers', 'metric_value': 3456 }), response.Response( { 'social_profile_id': 6, 'platform': 'spotify', 'metric': 'followed_by', 'metric_value': 789 }), ] def test_get_latest_metrics_by_artist_id( monkeypatch, artist_social_profiles, latest_metrics): """Test get latest metrics by artist id.""" monkeypatch.setattr( artist_social_profile, 'get_artist_social_profiles_by_artist_id', MagicMock( return_value=artist_social_profiles)) monkeypatch.setattr( metric, 'get_latest_metric_by_social_profile_id', MagicMock( side_effect=latest_metrics)) artist_id = 3 result = metrics.get_latest_metrics_by_artist_id(artist_id) assert result assert len(result.message['items']) == 3 def test_get_latest_metrics_by_artist_id_no_id(): """Test get latest metrics by artist id no id.""" artist_id = None result = metrics.get_latest_metrics_by_artist_id(artist_id) assert not result assert result.status == status.BAD_REQUEST def test_get_latest_metrics_by_artist_id_no_artists_found(monkeypatch): """Test get latest metrics by artist id no artists found.""" not_found_response = response.create_not_found_response() monkeypatch.setattr( artist_social_profile, 'get_artist_social_profiles_by_artist_id', MagicMock( return_value=not_found_response)) artist_id = 10 result = metrics.get_latest_metrics_by_artist_id(artist_id) assert not result assert result.status == status.NOT_FOUND def test_get_latest_metrics_by_artist_id_no_metrics_found( monkeypatch, artist_social_profiles): """Test get latest metrics by artist id no metrics found.""" latest_metrics = [response.create_not_found_response()] * 3 monkeypatch.setattr( artist_social_profile, 'get_artist_social_profiles_by_artist_id', MagicMock( return_value=artist_social_profiles)) monkeypatch.setattr( metric, 'get_latest_metric_by_social_profile_id', MagicMock( side_effect=latest_metrics)) artist_id = 3 result = metrics.get_latest_metrics_by_artist_id(artist_id) assert result items = result.message['items'] for item in items: assert 'likes' not in item assert 'follwers' not in item assert 'followed_by' not in item def test_get_time_series_data_for_period( monkeypatch, time_series_data): """Test get time series data for period.""" monkeypatch.setattr( metric, 'get_time_series_data_for_period', MagicMock( return_value=time_series_data)) params = {} result = metrics.get_time_series_data_for_period(params) assert result assert len(result.message['items']) == 2 def test_get_time_series_data_for_period_bad_params(monkeypatch): """Test get time series data for period bad params.""" monkeypatch.setattr( metric, 'get_time_series_data_for_period', MagicMock( return_value=response.create_error_response( status.BAD_REQUEST, error.ERROR_MESSAGE_BAD_PARAMS))) params = {} result = metrics.get_time_series_data_for_period(params) assert not result assert result.status == status.BAD_REQUEST def test_get_time_series_data_for_period_not_found(monkeypatch): """Test get time series data for period not found.""" monkeypatch.setattr( metric, 'get_time_series_data_for_period', MagicMock( return_value=response.create_not_found_response())) params = {} result = metrics.get_time_series_data_for_period(params) assert not result assert result.status == status.NOT_FOUND