"""Tests for feed logic.""" from unittest.mock import patch import pytest from oto import response as oto_response from analytics.connectors import redis from analytics.logic import feed fetchall_path = "analytics.utils.data_availability.snowflake.fetchall" @pytest.fixture def mock_store_outages(): """Mock Store Outages Snowflake fetchall.""" with patch(fetchall_path) as fetchall: # feed_id, store_id, has_streams_outage, has_skips_saves_outage fetchall.return_value = [ # spotify (1, 286, True, False), # pandora (3, 708, False, True), # napster (11, 4, True, True), ] yield fetchall class TestGetFeed: """Test for get_feed logic.""" def test_get_feeds(self, mock_store_outages): """Test get_feeds.""" redis.client.flushall() # flush Fakeredis cache response = feed.get_feeds() assert mock_store_outages.call_count == 1 assert isinstance(response, oto_response.Response) assert response.status == 200 response_feeds = response.message assert 8 not in response_feeds assert 35 in response_feeds assert 36 in response_feeds assert 37 in response_feeds # assert 39 in response_feeds # assert response_feeds[39]['storename'] == 'VKontakte' # assert 40 in response_feeds # assert response_feeds[40]['storename'] == 'Odnoklassniki' assert 41 not in response_feeds assert 43 in response_feeds assert response_feeds[43]["storename"] == "SoundCloud" def test_get_feeds_store_error(self, mock_store_outages): """Test single errors.""" redis.client.flushall() # flush Fakeredis cache response = feed.get_feeds() assert mock_store_outages.call_count == 1 assert response.status == 200 feeds = response.message assert 1 in feeds spotify_feed = feeds[1] assert "error" in spotify_feed assert spotify_feed["error"] == {"code": "unreliable", "types": ["streams"]} assert 3 in feeds pandora_feed = feeds[3] assert "error" in pandora_feed assert pandora_feed["error"] == {"code": "unreliable", "types": ["skips_saves"]} def test_get_feeds_store_errors(self, mock_store_outages): """Test multiple errors.""" redis.client.flushall() # flush Fakeredis cache response = feed.get_feeds() assert mock_store_outages.call_count == 1 assert response.status == 200 feeds = response.message assert 11 in feeds napster_feed = feeds[11] assert "error" in napster_feed assert napster_feed["error"] == { "code": "unreliable", "types": ["streams", "skips_saves"], } def test_get_feeds_with_awa_data(self, mock_store_outages): """Test get_feeds with insights_awa_date FF enabled.""" redis.client.flushall() # flush Fakeredis cache response = feed.get_feeds() assert mock_store_outages.call_count == 1 assert isinstance(response, oto_response.Response) assert response.status == 200 response_feeds = response.message assert 44 in response_feeds assert response_feeds[44]["storename"] == "AWA" def test_get_feeds_with_beatport(self, mock_store_outages): """Test get_feeds for beatport.""" redis.client.flushall() # flush Fakeredis cache response = feed.get_feeds() assert mock_store_outages.call_count == 1 assert isinstance(response, oto_response.Response) assert response.status == 200 response_feeds = response.message assert 48 in response_feeds assert response_feeds[48]["storename"] == "Beatport"