"""Tests for Platform logic.""" from unittest.mock import MagicMock from oto import response, status import pytest from social_analytics.logic import platform from social_analytics.logic import instagram_collector from social_analytics.logic import spotify_collector @pytest.fixture def fixture_user_dict(): """Fixture with a user dict.""" return {'id': '123', 'name': 'abc'} @pytest.fixture def fixture_instagram_token(): """Fixture with a token.""" return 'token' @pytest.fixture def fixture_spotify_token(): """Fixture with a dict containing a token.""" return {'access_token': 'token'} @pytest.fixture def fixture_not_found(): """Fixture with not found response.""" return response.create_not_found_response() def test_get_instagram_user( monkeypatch, fixture_user_dict, fixture_instagram_token): """Test get instagram user.""" monkeypatch.setattr( instagram_collector, 'collect_instagram_metrics', MagicMock(return_value=fixture_user_dict)) monkeypatch.setattr( instagram_collector, 'refresh_token', MagicMock(return_value=fixture_instagram_token)) platform_id = '1' result = platform.get_instagram_user(platform_id) instagram_collector.refresh_token.assert_called_once() instagram_collector.collect_instagram_metrics.assert_called_once() assert result def test_get_instagram_user_not_found( monkeypatch, fixture_instagram_token): """Test get instagram user not found.""" monkeypatch.setattr( instagram_collector, 'collect_instagram_metrics', MagicMock(return_value=None)) monkeypatch.setattr( instagram_collector, 'refresh_token', MagicMock(return_value=fixture_instagram_token)) platform_id = '1' result = platform.get_instagram_user(platform_id) instagram_collector.refresh_token.assert_called_once() instagram_collector.collect_instagram_metrics.assert_called_once() assert not result assert result.status == status.NOT_FOUND def test_get_spotify_user( monkeypatch, fixture_user_dict, fixture_spotify_token): """Test get spotify user.""" monkeypatch.setattr( spotify_collector, 'collect_spotify_metrics', MagicMock(return_value=fixture_user_dict)) monkeypatch.setattr( spotify_collector, 'refresh_token', MagicMock(return_value=fixture_spotify_token)) platform_id = '1' result = platform.get_spotify_user(platform_id) spotify_collector.refresh_token.assert_called_once() spotify_collector.collect_spotify_metrics.assert_called_once() assert result def test_get_spotify_user_not_found( monkeypatch, fixture_spotify_token): """Test get spotify user not found.""" monkeypatch.setattr( spotify_collector, 'collect_spotify_metrics', MagicMock(return_value=None)) monkeypatch.setattr( spotify_collector, 'refresh_token', MagicMock(return_value=fixture_spotify_token)) platform_id = '1' result = platform.get_spotify_user(platform_id) spotify_collector.refresh_token.assert_called_once() spotify_collector.collect_spotify_metrics.assert_called_once() assert not result assert result.status == status.NOT_FOUND