from playlist.queries.constants import ALLOWED_ORDER_BY_KEYS, DEFAULT_PAGE_SIZE from playlist.queries.fetch_queries import PlacementsQuery from tests.unit.utils import raises_validation_error def test_default_args(): q = PlacementsQuery({"isrc": "QM6N22019571"}) params = q.query_params assert params["offset"] == 0 assert params["limit"] == DEFAULT_PAGE_SIZE assert params["sort_direction"] == "DESC" assert params["sort_key"] == "last_added_on_date" def test_invalid_isrc(): with raises_validation_error({"isrc": ["String does not match expected pattern."]}): PlacementsQuery({"isrc": "AHHHHH"}) def test_invalid_keys_are_deleted(): q = PlacementsQuery( {"isrc": "QM6N22019571", "limit": 50, "offset": 100, "a": 1, "b": 2} ) assert "a" not in q.query_params assert "b" not in q.query_params assert "isrc" in q.query_params def test_invalid_without_main_identifier(): with raises_validation_error( {"_schema": ["isrc, or global_participant_id, or product_id required"]} ): PlacementsQuery({"limit": 50, "offset": 100}) def test_invalid_with_incorrect_datatype(): with raises_validation_error({"offset": ["Not a valid integer."]}): PlacementsQuery({"limit": 50, "offset": "100-"}) def test_invalid_with_incorrect_value_sort_direction(): with raises_validation_error({"sort_direction": ["Must be one of: ASC, DESC."]}): PlacementsQuery({"limit": 50, "offset": 100, "sort_direction": "dsfgfg"}) def test_invalid_with_incorrect_value_sort_key(): with raises_validation_error( {"sort_key": [f"Must be one of: {', '.join(ALLOWED_ORDER_BY_KEYS)}."]} ): PlacementsQuery({"limit": 50, "offset": 100, "sort_key": "asbcada"}) def test_valid_table_names_isrc(): q = PlacementsQuery({"isrc": "QM6N22019571", "limit": 50, "offset": 100}) assert q.table_params == { "private_placement_table_name": "v_playlists_placements_by_isrc_playlist_private", "public_placement_table_name": "v_playlists_placements_by_isrc_playlist_public", "playlist_rollup_table": "streams_by_playlist_rollup", "placement_position_events_table_name": "v_playlists_placement_events_by_isrc_playlist", "available_storefronts_table": "v_playlists_available_storefronts_apple", } def test_valid_table_names_isrc_countries(): q = PlacementsQuery( { "isrc": "QM6N22019571", "limit": 50, "offset": 100, "stream_countries": ["US", "IN"], } ) assert q.table_params == { "private_placement_table_name": "v_playlists_placements_by_isrc_playlist_country_private", "public_placement_table_name": "v_playlists_placements_by_isrc_playlist_public", "playlist_rollup_table": "streams_by_playlist_country_rollup", "placement_position_events_table_name": "v_playlists_placement_events_by_isrc_playlist", "available_storefronts_table": "v_playlists_available_storefronts_apple", } def test_valid_table_names_gpid(): q = PlacementsQuery( { "global_participant_id": "90097833-cc23-470e-8643-2268e156497f", "limit": 50, "offset": 100, } ) assert q.table_params == { "private_placement_table_name": "v_playlists_placements_by_participant_isrc_playlist_private", "public_placement_table_name": "v_playlists_placements_by_participant_isrc_playlist_public", "playlist_rollup_table": "streams_by_playlist_rollup", "placement_position_events_table_name": "v_playlists_placement_events_by_isrc_playlist", "available_storefronts_table": "v_playlists_available_storefronts_apple", } def test_valid_table_names_gpid_countries(): q = PlacementsQuery( { "global_participant_id": "90097833-cc23-470e-8643-2268e156497f", "limit": 50, "offset": 100, "stream_countries": ["US", "IN"], } ) assert q.table_params == { "private_placement_table_name": "v_playlists_placements_by_participant_isrc_playlist_country_private", "public_placement_table_name": "v_playlists_placements_by_participant_isrc_playlist_public", "playlist_rollup_table": "streams_by_playlist_country_rollup", "placement_position_events_table_name": "v_playlists_placement_events_by_isrc_playlist", "available_storefronts_table": "v_playlists_available_storefronts_apple", } def test_table_names_when_global_participant_and_storefront_enabled_in_params(): q = PlacementsQuery( { "global_participant_id": "90097833-cc23-470e-8643-2268e156497f", "limit": 50, "offset": 100, "storefront_enabled": True, } ) assert q.table_params == { "private_placement_table_name": "v_playlists_placements_by_participant_isrc_playlist_country_private", "public_placement_table_name": "v_playlists_placements_by_participant_isrc_playlist_public", "playlist_rollup_table": "streams_by_playlist_rollup", "placement_position_events_table_name": "v_playlists_placement_events_by_isrc_playlist", "available_storefronts_table": "v_playlists_available_storefronts_apple", } def test_table_names_when_isrc_and_storefront_enabled_in_params(): q = PlacementsQuery( { "isrc": "QM6N22019571", "limit": 50, "offset": 100, "storefront_enabled": True, } ) assert q.table_params == { "private_placement_table_name": "v_playlists_placements_by_isrc_playlist_country_private", "public_placement_table_name": "v_playlists_placements_by_isrc_playlist_public", "playlist_rollup_table": "streams_by_playlist_rollup", "placement_position_events_table_name": "v_playlists_placement_events_by_isrc_playlist", "available_storefronts_table": "v_playlists_available_storefronts_apple", } def test_valid_query_params(): mocked_params = ["database_name", "schema_name"] user_params = { "global_participant_id": "90097833-cc23-470e-8643-2268e156497f", "sort_key": "last_added_on_date", "sort_direction": "DESC", "stream_countries": ["US", "IN"], "curator_countries": ["US", "IN"], "playlist_types": ["USER_GENERATED", "ALGORITHMIC"], "playlist_appearances": "current", "store_ids": ["286"], "min_followers": 1000, "limit": 50, "offset": 100, "distributors": ["theorchard", "awal"], "placement_position_events_table_name": "v_playlists_placement_events_by_isrc_playlist", "playlist_rollup_table": "streams_by_playlist_country_rollup", "public_placement_table_name": "v_playlists_placements_by_participant_isrc_playlist_public", "private_placement_table_name": "v_playlists_placements_by_participant_isrc_playlist_country_private", "available_storefronts_table": "v_playlists_available_storefronts_apple", "use_primary_playlist_type": False, "filter_incorrect_playlists": False, "insights_playlist_v2_current_past_enabled": False, "storefront_enabled": True, "filter_by_top_10_apple_music_markets": True, } q = PlacementsQuery(user_params) for param, value in q.query_params.items(): if param not in mocked_params: assert value == user_params[param]