"""Unit tests for placements metadata model layer.""" import datetime from unittest.mock import patch import flexmock import pytest from sound_recordings.constants.owner_category import CURATED, EDITORIAL from sound_recordings.constants.placements import DEFAULT_MIN_FOLLOWERS from sound_recordings.logic import permissions from sound_recordings.models import placements_metadata DB_STOREFRONT_OBJECT = '[{"storefront": "US", "position": 10}]' RESULT_STOREFRONTS_OBJECT = [{"storefront": "US", "position": 10}] @pytest.fixture def sql_result_placements_for_isrc(): """Mock db results for placements for an ISRC.""" return [ ( "79zEEDH3U1wGiDtNnKUiSO", 286, "Accept Tornillo records", "spotify:playlist:79zEEDH3U1wGiDtNnKUiSO", "global", datetime.datetime(2014, 9, 26, 14, 40, 2), "https://dhfsk7jl2g0km.cloudfront.net/playlist-79zEEDH3U1wGiDtNnKUiSO.jpeg", None, 0, 33, 22, 22, 11, "US1", "Spotify", ), # noqa ( "2Spbrs7gYeR5VVVqfrB9xO", 286, "Blood Of The Nations", "spotify:playlist:2Spbrs7gYeR5VVVqfrB9xO", "global", datetime.datetime(2014, 4, 14, 11, 41, 35), "https://dhfsk7jl2g0km.cloudfront.net/playlist-2Spbrs7gYeR5VVVqfrB9xO.jpeg", None, 0, 12, 1, 1, 0, "US1", "Spotify", ), # noqa ( "2kgoFl4O2gjfS7y0EzG4fP", 286, "Blood Of The Nations", "spotify:playlist:2kgoFl4O2gjfS7y0EzG4fP", "global", datetime.datetime(2014, 4, 14, 11, 43, 28), "https://dhfsk7jl2g0km.cloudfront.net/playlist-2kgoFl4O2gjfS7y0EzG4fP.jpeg", None, 0, 12, 1, 1, 0, "US1", "Spotify", ), # noqa ] @pytest.fixture def sql_result_placements_for_isrc_with_count(): """Mock db results for placements for an ISRC with count.""" return [ ( 13, '[\n {\n "added": "Tue, 03 Nov 2020 08:53:54 Z",\n "artwork_url": "https://i.scdn.co/image/ab67706f00000003f8d929f77861867e92162012",\n "count": 13,\n "followers": 10072,\n "isrc": "DED831000251",\n "owner_category": "EDITORIAL",\n "playlist_id": "37i9dQZF1DX4nIDgulGW6Z",\n "playlist_name": "Kasarihevi on paskimmillaankin parasta",\n "playlist_uri": "spotify:playlist:37i9dQZF1DX4nIDgulGW6Z",\n "position": 47,\n "store_id": 286,\n "total_streams": 23,\n "track_count": 50,\n "user_display_name": ""\n },\n {\n "added": "Sat, 04 Jul 2020 19:28:03 Z",\n "artwork_url": "https://mosaic.scdn.co/640/ab67616d0000b2731870e57bcd78041d8c3cd202ab67616d0000b273312f2e71ea2ffd2d6bae1c90ab67616d0000b27372833c1ae3343cbfb4617073ab67616d0000b273eab13a1a01fb57d6e9e8ddaf",\n "count": 13,\n "followers": 1165,\n "isrc": "DED831000251",\n "owner_category": "USER_GENERATED",\n "playlist_id": "0JpvR1nVBBSbHt6fN51mZn",\n "playlist_name": "Classic Hard Rock/Metal",\n "playlist_uri": "spotify:playlist:0JpvR1nVBBSbHt6fN51mZn",\n "position": 705,\n "previous_position": 705,\n "store_id": 286,\n "total_streams": 0,\n "track_count": 897,\n "user_display_name": ""\n },\n {\n "added": "Mon, 10 Feb 2020 07:34:37 Z",\n "artwork_url": "https://mosaic.scdn.co/640/ab67616d0000b2732457ab409ae106f8ae1dff89ab67616d0000b2737313aec4d82754542d602f9eab67616d0000b273975ae67d8fb422586b8e2f83ab67616d0000b273c8783655136b2725c5c7f564",\n "count": 13,\n "followers": 0,\n "isrc": "DED831000251",\n "owner_category": "USER_GENERATED",\n "playlist_id": "1Grmv8UTNK8LqIIm2o1id4",\n "playlist_name": "Ut på tur",\n "playlist_uri": "spotify:playlist:1Grmv8UTNK8LqIIm2o1id4",\n "position": 285,\n "previous_position": 285,\n "store_id": 286,\n "total_streams": 0,\n "track_count": 762,\n "user_display_name": ""\n }\n]', ) ] # noqa @pytest.fixture def sql_result_placement(): """Mock db results for a single placement with storefronts.""" return ( "pl.33ff9509b5a6449387cc4caea0d4db82", 1, "Indie Hits: 2004", "https://itunes.apple.com/us/playlist/pl.33ff9509b5a6449387cc4caea0d4db82", None, datetime.datetime(2018, 11, 13, 2, 30, 18), "https://is3-ssl.mzstatic.com/image/c.jpeg", None, None, None, None, None, 8, "USA560434620", "Apple Music", "EDITORIAL", 29, '[\n {\n "added": "Sun, 11 Nov 2018 02:30:04 Z",\n "country": "US",\n "playlist_uri": "https://itunes.apple.com/us/playlist/pl.33ff9509b5a6449387cc4caea0d4db82",\n "position": 12,\n "previous_position": 11,\n "storefront": "US",\n "total_streams": 4,\n "track_count": 24\n },\n {\n "added": "Tue, 13 Nov 2018 02:30:18 Z",\n "country": "MX",\n "playlist_uri": "https://itunes.apple.com/mx/playlist/pl.33ff9509b5a6449387cc4caea0d4db82",\n "position": 12,\n "previous_position": 11,\n "storefront": "MX",\n "total_streams": 3,\n "track_count": 25\n },\n {\n "added": "Thu, 22 Jun 2017 00:00:00 Z",\n "country": "AU",\n "playlist_uri": "https://itunes.apple.com/au/playlist/pl.33ff9509b5a6449387cc4caea0d4db82",\n "position": 12,\n "previous_position": 11,\n "storefront": "AU",\n "total_streams": 1,\n "track_count": 25\n }\n]', ) # noqa @pytest.fixture def sql_result_placements_grouped_storefronts(sql_result_placements_for_isrc): """Mock db results for recent placements.""" result = _add_properties_to_mock_sql_result( sql_result_placements_for_isrc, ["EDITORIAL", 10, None] ) result.append( ( "pl.33ff9509b5a6449387cc4caea0d4db82", 1, "Indie Hits: 2004", "https://itunes.apple.com/us/playlist/pl.33ff9509b5a6449387cc4caea0d4db82", None, datetime.datetime(2018, 11, 13, 2, 30, 18), "https://is3-ssl.mzstatic.com/image/c.jpeg", None, None, None, None, None, 8, "USA560434620", "Apple Music", "EDITORIAL", 29, '[\n {\n "added": "Sun, 11 Nov 2018 02:30:04 Z",\n "country": "US",\n "playlist_uri": "https://itunes.apple.com/us/playlist/pl.33ff9509b5a6449387cc4caea0d4db82",\n "position": 12,\n "previous_position": 11,\n "storefront": "US",\n "total_streams": 4,\n "track_count": 24\n },\n {\n "added": "Tue, 13 Nov 2018 02:30:18 Z",\n "country": "MX",\n "playlist_uri": "https://itunes.apple.com/mx/playlist/pl.33ff9509b5a6449387cc4caea0d4db82",\n "position": 12,\n "previous_position": 11,\n "storefront": "MX",\n "total_streams": 3,\n "track_count": 25\n },\n {\n "added": "Thu, 22 Jun 2017 00:00:00 Z",\n "country": "AU",\n "playlist_uri": "https://itunes.apple.com/au/playlist/pl.33ff9509b5a6449387cc4caea0d4db82",\n "position": 12,\n "previous_position": 11,\n "storefront": "AU",\n "total_streams": 1,\n "track_count": 25\n }\n]', ) # noqa ) return result @pytest.fixture def sql_result_recent_placements_grouped_storefronts(sql_result_placements_for_isrc): """Mock db results for recent placements.""" result = _add_properties_to_mock_sql_result( sql_result_placements_for_isrc, ["EDITORIAL", 10, 100, None] ) result.append( ( "pl.33ff9509b5a6449387cc4caea0d4db82", 1, "Indie Hits: 2004", "https://itunes.apple.com/us/playlist/pl.33ff9509b5a6449387cc4caea0d4db82", None, datetime.datetime(2018, 11, 13, 2, 30, 18), "https://is3-ssl.mzstatic.com/image/c.jpeg", None, None, None, None, None, 8, "USA560434620", "Apple Music", "EDITORIAL", 29, 100, '[\n {\n "added": "Sun, 11 Nov 2018 02:30:04 Z",\n "country": "US",\n "playlist_uri": "https://itunes.apple.com/us/playlist/pl.33ff9509b5a6449387cc4caea0d4db82",\n "position": 12,\n "previous_position": 11,\n "storefront": "US",\n "total_streams": 4,\n "track_count": 24\n },\n {\n "added": "Tue, 13 Nov 2018 02:30:18 Z",\n "country": "MX",\n "playlist_uri": "https://itunes.apple.com/mx/playlist/pl.33ff9509b5a6449387cc4caea0d4db82",\n "position": 12,\n "previous_position": 11,\n "storefront": "MX",\n "total_streams": 3,\n "track_count": 25\n },\n {\n "added": "Thu, 22 Jun 2017 00:00:00 Z",\n "country": "AU",\n "playlist_uri": "https://itunes.apple.com/au/playlist/pl.33ff9509b5a6449387cc4caea0d4db82",\n "position": 12,\n "previous_position": 11,\n "storefront": "AU",\n "total_streams": 1,\n "track_count": 25\n }\n]', ) # noqa ) return result @pytest.fixture def sql_result_placements_total_count(): """Mock db results for placements total count.""" return [(19,)] @pytest.fixture(autouse=True) def mock_artist_permissions(): """Mock get_artist_info_ids.""" flexmock(permissions).should_receive("get_artist_info_ids").and_return( [917636, 1552280] ) @pytest.fixture def mock_snowflake_fetchall_get_placements_for_isrc( sql_result_placements_for_isrc_with_count, ): """Mock Snowflake fetchall for placements for an ISRC.""" with patch( "sound_recordings.models.placements_metadata.snowflake.fetchall" ) as fetchall: fetchall.return_value = sql_result_placements_for_isrc_with_count yield fetchall @pytest.fixture def mock_snowflake_fetchone_placement(sql_result_placement): """Mock Snowflake fetchall for placements for an ISRC & playlist_id.""" with patch( "sound_recordings.models.placements_metadata.snowflake.fetchone" ) as fetchone: fetchone.return_value = sql_result_placement yield fetchone @pytest.fixture def mock_snowflake_fetchall_recent_placements_metadata( sql_result_recent_placements_grouped_storefronts, ): """Mock Snowflake fetchall for recent placements.""" with patch( "sound_recordings.models.placements_metadata.snowflake.fetchall" ) as fetchall: fetchall.return_value = sql_result_recent_placements_grouped_storefronts yield fetchall @pytest.fixture def mock_snowflake_fetchall_placements_total_count(sql_result_placements_total_count): """Mock Snowflake fetchall for placements total count.""" with patch( "sound_recordings.models.placements_metadata.snowflake.fetchall" ) as fetchall: fetchall.return_value = sql_result_placements_total_count yield fetchall @pytest.fixture def common_placements_metadata_model_result(): """Mock common placements metadata with grouped storefronts.""" common_placements_metadata_model_result = [ { "playlist_id": "79zEEDH3U1wGiDtNnKUiSO", "store_id": 286, "playlist_name": "Accept Tornillo records", "playlist_uri": "spotify:playlist:79zEEDH3U1wGiDtNnKUiSO", "storefront": "global", "added": datetime.datetime(2014, 9, 26, 14, 40, 2), "artwork_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-79zEEDH3U1wGiDtNnKUiSO.jpeg", # noqa "country": None, "followers": 0, "track_count": 33, "position": 22, "previous_position": 22, "total_streams": 11, "isrc": "US1", "user_display_name": "Spotify", "owner_category": "EDITORIAL", "count": 10, }, { "playlist_id": "2Spbrs7gYeR5VVVqfrB9xO", "store_id": 286, "playlist_name": "Blood Of The Nations", "playlist_uri": "spotify:playlist:2Spbrs7gYeR5VVVqfrB9xO", "storefront": "global", "added": datetime.datetime(2014, 4, 14, 11, 41, 35), "artwork_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-2Spbrs7gYeR5VVVqfrB9xO.jpeg", # noqa "country": None, "followers": 0, "track_count": 12, "position": 1, "previous_position": 1, "total_streams": 0, "isrc": "US1", "user_display_name": "Spotify", "owner_category": "EDITORIAL", "count": 10, }, { "playlist_id": "2kgoFl4O2gjfS7y0EzG4fP", "store_id": 286, "playlist_name": "Blood Of The Nations", "playlist_uri": "spotify:playlist:2kgoFl4O2gjfS7y0EzG4fP", "storefront": "global", "added": datetime.datetime(2014, 4, 14, 11, 43, 28), "artwork_url": "https://dhfsk7jl2g0km.cloudfront.net/playlist-2kgoFl4O2gjfS7y0EzG4fP.jpeg", # noqa "country": None, "followers": 0, "track_count": 12, "position": 1, "previous_position": 1, "total_streams": 0, "isrc": "US1", "user_display_name": "Spotify", "owner_category": "EDITORIAL", "count": 10, }, ] return common_placements_metadata_model_result @pytest.fixture def placements_metadata_model_result(): """Mock model results for placements metadata with grouped storefronts.""" return { "total_count": 13, "items": [ { "added": "Tue, 03 Nov 2020 08:53:54 Z", "artwork_url": "https://i.scdn.co/image/ab67706f00000003f8d929f77861867e92162012", # noqa "count": 13, "followers": 10072, "isrc": "DED831000251", "owner_category": "EDITORIAL", "playlist_id": "37i9dQZF1DX4nIDgulGW6Z", "playlist_name": "Kasarihevi on paskimmillaankin parasta", "playlist_uri": "spotify:playlist:37i9dQZF1DX4nIDgulGW6Z", "position": 47, "store_id": 286, "total_streams": 23, "track_count": 50, "user_display_name": "", "storefront": None, "country": None, "previous_position": None, "storefronts": None, }, { "added": "Sat, 04 Jul 2020 19:28:03 Z", "artwork_url": "https://mosaic.scdn.co/640/ab67616d0000b2731870e57bcd78041d8c3cd202ab67616d0000b273312f2e71ea2ffd2d6bae1c90ab67616d0000b27372833c1ae3343cbfb4617073ab67616d0000b273eab13a1a01fb57d6e9e8ddaf", # noqa "count": 13, "followers": 1165, "isrc": "DED831000251", "owner_category": "USER_GENERATED", "playlist_id": "0JpvR1nVBBSbHt6fN51mZn", "playlist_name": "Classic Hard Rock/Metal", "playlist_uri": "spotify:playlist:0JpvR1nVBBSbHt6fN51mZn", "position": 705, "previous_position": 705, "store_id": 286, "total_streams": 0, "track_count": 897, "user_display_name": "", "storefront": None, "country": None, "storefronts": None, }, { "added": "Mon, 10 Feb 2020 07:34:37 Z", "artwork_url": "https://mosaic.scdn.co/640/ab67616d0000b2732457ab409ae106f8ae1dff89ab67616d0000b2737313aec4d82754542d602f9eab67616d0000b273975ae67d8fb422586b8e2f83ab67616d0000b273c8783655136b2725c5c7f564", # noqa "count": 13, "followers": 0, "isrc": "DED831000251", "owner_category": "USER_GENERATED", "playlist_id": "1Grmv8UTNK8LqIIm2o1id4", "playlist_name": "Ut på tur", "playlist_uri": "spotify:playlist:1Grmv8UTNK8LqIIm2o1id4", "position": 285, "previous_position": 285, "store_id": 286, "total_streams": 0, "track_count": 762, "user_display_name": "", "storefront": None, "country": None, "storefronts": None, }, ], } @pytest.fixture def placement_model_result(): """Mock model result for placement with grouped storefronts.""" return { "playlist_id": "pl.33ff9509b5a6449387cc4caea0d4db82", "store_id": 1, "playlist_name": "Indie Hits: 2004", "playlist_uri": "https://itunes.apple.com/us/playlist/pl.33ff9509b5a6449387cc4caea0d4db82", # noqa "storefront": None, "added": datetime.datetime(2018, 11, 13, 2, 30, 18), "artwork_url": "https://is3-ssl.mzstatic.com/image/c.jpeg", "country": None, "followers": None, "track_count": None, "position": None, "previous_position": None, "total_streams": 8, "isrc": "USA560434620", "user_display_name": "Apple Music", "owner_category": "EDITORIAL", "count": 29, "storefronts": [ { "added": datetime.datetime(2018, 11, 11, 2, 30, 4), "country": "US", "playlist_uri": "https://itunes.apple.com/us/playlist/pl.33ff9509b5a6449387cc4caea0d4db82", # noqa "position": 12, "previous_position": 11, "storefront": "US", "total_streams": 4, "track_count": 24, }, { "added": datetime.datetime(2018, 11, 13, 2, 30, 18), "country": "MX", "playlist_uri": "https://itunes.apple.com/mx/playlist/pl.33ff9509b5a6449387cc4caea0d4db82", # noqa "position": 12, "previous_position": 11, "storefront": "MX", "total_streams": 3, "track_count": 25, }, { "added": datetime.datetime(2017, 6, 22, 0, 0), "country": "AU", "playlist_uri": "https://itunes.apple.com/au/playlist/pl.33ff9509b5a6449387cc4caea0d4db82", # noqa "position": 12, "previous_position": 11, "storefront": "AU", "total_streams": 1, "track_count": 25, }, ], } @pytest.fixture def recent_placements_metadata_model_result(common_placements_metadata_model_result): """Mock model results for placements metadata with grouped storefronts.""" recent_placements_metadata_model_result = common_placements_metadata_model_result recent_placements_metadata_model_result = _add_properties_to_expected_result( recent_placements_metadata_model_result, ["total_count", "storefronts"], [100, None], ) recent_placements_metadata_model_result.append( { "playlist_id": "pl.33ff9509b5a6449387cc4caea0d4db82", "store_id": 1, "playlist_name": "Indie Hits: 2004", "playlist_uri": "https://itunes.apple.com/us/playlist/pl.33ff9509b5a6449387cc4caea0d4db82", # noqa "storefront": None, "added": datetime.datetime(2018, 11, 13, 2, 30, 18), "artwork_url": "https://is3-ssl.mzstatic.com/image/c.jpeg", "country": None, "followers": None, "track_count": None, "position": None, "previous_position": None, "total_streams": 8, "isrc": "USA560434620", "user_display_name": "Apple Music", "owner_category": "EDITORIAL", "count": 29, "total_count": 100, "storefronts": [ { "added": datetime.datetime(2018, 11, 11, 2, 30, 4), "country": "US", "playlist_uri": "https://itunes.apple.com/us/playlist/pl.33ff9509b5a6449387cc4caea0d4db82", # noqa "position": 12, "previous_position": 11, "storefront": "US", "total_streams": 4, "track_count": 24, }, { "added": datetime.datetime(2018, 11, 13, 2, 30, 18), "country": "MX", "playlist_uri": "https://itunes.apple.com/mx/playlist/pl.33ff9509b5a6449387cc4caea0d4db82", # noqa "position": 12, "previous_position": 11, "storefront": "MX", "total_streams": 3, "track_count": 25, }, { "added": datetime.datetime(2017, 6, 22, 0, 0), "country": "AU", "playlist_uri": "https://itunes.apple.com/au/playlist/pl.33ff9509b5a6449387cc4caea0d4db82", # noqa "position": 12, "previous_position": 11, "storefront": "AU", "total_streams": 1, "track_count": 25, }, ], } ) return recent_placements_metadata_model_result @pytest.fixture def placements_total_count_model_result(): """Mock model results for placements total count.""" return 19 def test_get_placements_metadata( mock_snowflake_fetchall_get_placements_for_isrc, placements_metadata_model_result ): """Test get_placements_metadata_sound_recording function.""" permissions_filter = {"label_ids": [1], "artist_ids": None, "subaccount_ids": [2]} result = placements_metadata.get_placements_metadata( permissions_filter, 25, 0, "DESC", "added", "5", [1, 286], [CURATED, EDITORIAL], DEFAULT_MIN_FOLLOWERS, ) assert result == placements_metadata_model_result assert mock_snowflake_fetchall_get_placements_for_isrc.call_count == 1 def test_get_placement(mock_snowflake_fetchone_placement, placement_model_result): """Test get_placement function.""" permissions_filter = {"label_ids": [1], "artist_ids": None, "subaccount_ids": [2]} result = placements_metadata.get_placement( permissions_filter, "anisrc", playlist_id="aplaylistid2342342" ) assert result == placement_model_result assert mock_snowflake_fetchone_placement.call_count == 1 def test_get_recent_placements_metadata( mock_snowflake_fetchall_recent_placements_metadata, recent_placements_metadata_model_result, ): """Test get_recent_placements_metadata function with grouped param=True.""" permissions_filter = {"label_ids": [1], "artist_ids": None, "subaccount_ids": [2]} result = placements_metadata.get_recent_placements_metadata( permissions_filter, 25, 0, [1, 286], [CURATED, EDITORIAL], DEFAULT_MIN_FOLLOWERS ) assert mock_snowflake_fetchall_recent_placements_metadata.call_count == 1 assert result == recent_placements_metadata_model_result def test_get_recent_placements_metadata_with_label_ids( mock_snowflake_fetchall_recent_placements_metadata, recent_placements_metadata_model_result, ): """Test get_recent_placements_metadata function with grouped param=True.""" permissions_filter = {"label_ids": [1], "artist_ids": None, "subaccount_ids": [2]} result = placements_metadata.get_recent_placements_metadata( permissions_filter, 25, 0, [1, 286], [CURATED, EDITORIAL], DEFAULT_MIN_FOLLOWERS, label_ids=[1, 2, 3], ) assert mock_snowflake_fetchall_recent_placements_metadata.call_count == 1 assert result == recent_placements_metadata_model_result assert ( "labelid IN (:label_ids_for_filtering)" in mock_snowflake_fetchall_recent_placements_metadata.call_args[0][0] ) def test_get_recent_placements_metadata_with_label_ids_empty_array( mock_snowflake_fetchall_recent_placements_metadata, recent_placements_metadata_model_result, ): """Test get_recent_placements_metadata function with grouped param=True.""" permissions_filter = {"label_ids": [1], "artist_ids": None, "subaccount_ids": [2]} result = placements_metadata.get_recent_placements_metadata( permissions_filter, 25, 0, [1, 286], [CURATED, EDITORIAL], DEFAULT_MIN_FOLLOWERS, [], ) assert mock_snowflake_fetchall_recent_placements_metadata.call_count == 1 assert result == recent_placements_metadata_model_result assert ( "labelid IN (:label_ids_for_filtering)" not in mock_snowflake_fetchall_recent_placements_metadata.call_args[0][0] ) def test_get_recent_placements_metadata_with_subaccount_ids( mock_snowflake_fetchall_recent_placements_metadata, recent_placements_metadata_model_result, ): """Test get_recent_placements_metadata function with grouped param=True.""" permissions_filter = {"label_ids": [1], "artist_ids": None, "subaccount_ids": [2]} result = placements_metadata.get_recent_placements_metadata( permissions_filter, 25, 0, [1, 286], [CURATED, EDITORIAL], DEFAULT_MIN_FOLLOWERS, subaccount_ids=[1, 2, 3], ) assert mock_snowflake_fetchall_recent_placements_metadata.call_count == 1 assert result == recent_placements_metadata_model_result assert ( "subaccountid IN (:subaccount_ids_for_filtering)" in mock_snowflake_fetchall_recent_placements_metadata.call_args[0][0] ) def test_get_recent_placements_metadata_with_label_and_subaccount_ids( mock_snowflake_fetchall_recent_placements_metadata, recent_placements_metadata_model_result, ): """Test get_recent_placements_metadata function with grouped param=True.""" permissions_filter = {"label_ids": [1], "artist_ids": None, "subaccount_ids": [2]} result = placements_metadata.get_recent_placements_metadata( permissions_filter, 25, 0, [1, 286], [CURATED, EDITORIAL], DEFAULT_MIN_FOLLOWERS, label_ids=[4, 5, 6], subaccount_ids=[1, 2, 3], ) assert mock_snowflake_fetchall_recent_placements_metadata.call_count == 1 assert result == recent_placements_metadata_model_result assert "labelid IN (:label_ids_for_filtering) " def test_get_placements_total_count( mock_snowflake_fetchall_placements_total_count, placements_total_count_model_result ): """Test get_placements_total_count function.""" permissions_filter = {"label_ids": [1], "artist_ids": None, "subaccount_ids": [2]} result = placements_metadata.get_placements_total_count( permissions_filter, [CURATED, EDITORIAL], DEFAULT_MIN_FOLLOWERS ) assert mock_snowflake_fetchall_placements_total_count.call_count == 1 assert result == placements_total_count_model_result def test_get_placements_total_count_with_label_ids( mock_snowflake_fetchall_placements_total_count, placements_total_count_model_result ): """Test get_placements_total_count function.""" permissions_filter = {"label_ids": [1], "artist_ids": None, "subaccount_ids": [2]} result = placements_metadata.get_placements_total_count( permissions_filter, [CURATED, EDITORIAL], DEFAULT_MIN_FOLLOWERS, label_ids=[1, 2, 3], ) assert mock_snowflake_fetchall_placements_total_count.call_count == 1 assert result == placements_total_count_model_result assert ( "labelid IN (:label_ids_for_filtering)" in mock_snowflake_fetchall_placements_total_count.call_args[0][0] ) def test_get_placements_total_count_with_label_ids_empty_array( mock_snowflake_fetchall_placements_total_count, placements_total_count_model_result ): """Test get_placements_total_count function.""" permissions_filter = {"label_ids": [1], "artist_ids": None, "subaccount_ids": [2]} result = placements_metadata.get_placements_total_count( permissions_filter, [CURATED, EDITORIAL], DEFAULT_MIN_FOLLOWERS, [] ) assert mock_snowflake_fetchall_placements_total_count.call_count == 1 assert result == placements_total_count_model_result assert ( "labelid IN (:label_ids_for_filtering)" not in mock_snowflake_fetchall_placements_total_count.call_args[0][0] ) def test_get_placements_total_count_with_subaccount_ids( mock_snowflake_fetchall_placements_total_count, placements_total_count_model_result ): """Test get_placements_total_count function.""" permissions_filter = {"label_ids": [1], "artist_ids": None, "subaccount_ids": [2]} result = placements_metadata.get_placements_total_count( permissions_filter, [CURATED, EDITORIAL], DEFAULT_MIN_FOLLOWERS, subaccount_ids=[1, 2, 3], ) assert mock_snowflake_fetchall_placements_total_count.call_count == 1 assert result == placements_total_count_model_result assert ( "subaccountid IN (:subaccount_ids_for_filtering)" in mock_snowflake_fetchall_placements_total_count.call_args[0][0] ) def test_get_placements_total_count_with_label_and_subaccount_ids( mock_snowflake_fetchall_placements_total_count, placements_total_count_model_result ): """Test get_placements_total_count function.""" permissions_filter = {"label_ids": [1], "artist_ids": None, "subaccount_ids": [2]} result = placements_metadata.get_placements_total_count( permissions_filter, [CURATED, EDITORIAL], DEFAULT_MIN_FOLLOWERS, label_ids=[4, 5, 6], subaccount_ids=[1, 2, 3], ) assert mock_snowflake_fetchall_placements_total_count.call_count == 1 assert result == placements_total_count_model_result assert "labelid IN (:label_ids_for_filtering) " def test_get_placements_table(): """Test _get_placements_table with no feature flags enabled.""" target_table = placements_metadata._get_placements_table() assert ( target_table == "RECENT_PLACEMENTS_BY_TRACK_PLAYLIST_COUNTRY_FEED_ROLLUP" ) # noqa def _add_properties_to_mock_sql_result(mock_result, values): """Append properties to a mock sql result.""" result = [] for item in mock_result: item = list(item) for value in values: item.append(value) result.append(tuple(item)) return result def _add_properties_to_expected_result(model_result, properties, values): """Append properties and values to expected model result.""" for item in model_result: for i in range(len(properties)): item[properties[i]] = values[i] return model_result