import pytest from datetime import datetime from flask import url_for from src.constants.http_status import HTTP_200_OK, HTTP_202_ACCEPTED, HTTP_400_BAD_REQUEST from tests.legacy.apollo_api import factories as apollo_factories @pytest.mark.parametrize( "type,vendor,response_items", ( ( "track", "spotify", [ { "id": "aaa", "uri": "spotify:track:aaa", "timestamp": "2021-02-09T10:00:00", "type": "track", "vendor": "spotify", "country_code": None, } ], ), ( "track", None, [ { "id": "123", "uri": "123", "timestamp": "2021-02-09T11:00:00", "type": "track", "vendor": "apple", "country_code": None, }, { "id": "aaa", "uri": "spotify:track:aaa", "timestamp": "2021-02-09T10:00:00", "type": "track", "vendor": "spotify", "country_code": None, }, ], ), ( "track", "apple", [ { "id": "123", "uri": "123", "timestamp": "2021-02-09T11:00:00", "type": "track", "vendor": "apple", "country_code": None, } ], ), ( "playlist", "spotify", [ { "id": "www", "uri": "spotify:playlist:www", "timestamp": "2021-02-09T10:02:00", "type": "playlist", "vendor": "spotify", "country_code": None, }, { "id": "qqq", "uri": "spotify:playlist:qqq", "timestamp": "2021-02-09T10:01:00", "type": "playlist", "vendor": "spotify", "country_code": None, }, ], ), ( "playlist", None, [ { "id": "pl.ttt", "uri": "pl.ttt", "timestamp": "2021-02-09T12:00:00", "type": "playlist", "vendor": "apple", "country_code": "us", }, { "id": "www", "uri": "spotify:playlist:www", "timestamp": "2021-02-09T10:02:00", "type": "playlist", "vendor": "spotify", "country_code": None, }, { "id": "qqq", "uri": "spotify:playlist:qqq", "timestamp": "2021-02-09T10:01:00", "type": "playlist", "vendor": "spotify", "country_code": None, }, ], ), ( "playlist", "apple", [ { "id": "pl.ttt", "uri": "pl.ttt", "timestamp": "2021-02-09T12:00:00", "type": "playlist", "vendor": "apple", "country_code": "us", } ], ), ( "playlist", "amazon", [ { "id": "1MA30N", "uri": "1MA30N", "timestamp": "2021-02-09T12:00:00", "type": "playlist", "vendor": "amazon", "country_code": None, } ], ), ( None, "spotify", [ { "id": "www", "uri": "spotify:playlist:www", "timestamp": "2021-02-09T10:02:00", "type": "playlist", "vendor": "spotify", "country_code": None, }, { "id": "qqq", "uri": "spotify:playlist:qqq", "timestamp": "2021-02-09T10:01:00", "type": "playlist", "vendor": "spotify", "country_code": None, }, { "id": "aaa", "uri": "spotify:track:aaa", "timestamp": "2021-02-09T10:00:00", "type": "track", "vendor": "spotify", "country_code": None, }, ], ), ( "playlist,track", "spotify", [ { "id": "www", "uri": "spotify:playlist:www", "timestamp": "2021-02-09T10:02:00", "type": "playlist", "vendor": "spotify", "country_code": None, }, { "id": "qqq", "uri": "spotify:playlist:qqq", "timestamp": "2021-02-09T10:01:00", "type": "playlist", "vendor": "spotify", "country_code": None, }, { "id": "aaa", "uri": "spotify:track:aaa", "timestamp": "2021-02-09T10:00:00", "type": "track", "vendor": "spotify", "country_code": None, }, ], ), ), ) def test_get_recent_search(db_session, client, patch_auth_user, user_id, type, vendor, response_items): patch_auth_user(user_id) apollo_factories.ApolloRecentSearchFactory.create( user_id="6784fa8b-d468-4237-a181-1b9ce2759561", created_at=datetime(2021, 2, 9, 10, 0, 3), uri="789", source="applemusic", search_type=2, country_code="us", ) apollo_factories.ApolloRecentSearchFactory.create( user_id="6784fa8b-d468-4237-a181-1b9ce2759515", created_at=datetime(2021, 2, 9, 10, 0, 2), uri="spotify:playlist:hhh", source="spotify", search_type=3, country_code=None, ) apollo_factories.ApolloRecentSearchFactory.create( user_id="6784fa8b-d468-4237-a181-1b9ce2759561", created_at=datetime(2021, 2, 9, 10, 0, 1), uri="1234", source="applemusic", search_type=2, country_code="us", ) apollo_factories.ApolloRecentSearchFactory.create( user_id=user_id, uri="123", search_type=2, created_at=datetime(2021, 2, 9, 11, 0, 0), source="applemusic", country_code=None, ) apollo_factories.ApolloRecentSearchFactory.create( user_id=user_id, uri="pl.ttt", search_type=0, created_at=datetime(2021, 2, 9, 12, 0, 0), source="applemusic", country_code="us", ) apollo_factories.ApolloRecentSearchFactory.create( user_id=user_id, uri="spotify:playlist:qqq", search_type=3, created_at=datetime(2021, 2, 9, 10, 1, 0), source="spotify", country_code=None, ) apollo_factories.ApolloRecentSearchFactory.create( user_id=user_id, uri="spotify:playlist:www", search_type=3, created_at=datetime(2021, 2, 9, 10, 2, 0), source="spotify", country_code=None, ) apollo_factories.ApolloRecentSearchFactory.create( user_id=user_id, uri="spotify:track:aaa", search_type=2, created_at=datetime(2021, 2, 9, 10, 0, 0), source="spotify", country_code=None, ) apollo_factories.ApolloRecentSearchFactory.create( user_id=user_id, uri="1MA30N", search_type=3, created_at=datetime(2021, 2, 9, 12, 0, 0), source="amazon", country_code=None, ) url = url_for("recent_search.get_recent_search", type=type, vendor=vendor) response = client.get(url) assert response.status_code == HTTP_200_OK assert response.json["items"] == response_items @pytest.mark.parametrize( "body,status", ( ({"uri": "spotify:playlist:aaa", "timestamp": 1676025230, "searchTerm": "TEST"}, HTTP_202_ACCEPTED), ({"uri": "pl.aaa", "timestamp": 1676025230, "searchTerm": "TEST", "country_code": "us"}, HTTP_202_ACCEPTED), ({"uri": "B07K2FVM3G", "timestamp": 1676025230, "searchTerm": "TEST"}, HTTP_202_ACCEPTED), ({"uri": "B07K2FVM3G", "timestamp": 1676025230, "searchTerm": "TEST", "type": "track"}, HTTP_400_BAD_REQUEST), ({"uri": "pl.aaa", "timestamp": 1676025230, "searchTerm": "TEST", "type": "track"}, HTTP_400_BAD_REQUEST), ( {"uri": "spotify:playlist:aaa", "timestamp": 1676025230, "searchTerm": "TEST", "type": "track"}, HTTP_400_BAD_REQUEST, ), ( {"uri": "spotify:track:aaa", "timestamp": 1676025230, "searchTerm": "TEST", "type": "track"}, HTTP_202_ACCEPTED, ), ( {"uri": "B07K2FVM3G", "timestamp": 1676025230, "searchTerm": "TEST", "type": "track", "dsp": "spotify"}, HTTP_400_BAD_REQUEST, ), ( {"uri": "pl.aaa", "timestamp": 1676025230, "searchTerm": "TEST", "type": "track", "dsp": "spotify"}, HTTP_400_BAD_REQUEST, ), ( {"uri": "B07K2FVM3G", "timestamp": 1676025230, "searchTerm": "TEST", "type": "track", "dsp": "amazon"}, HTTP_400_BAD_REQUEST, ), ( {"uri": "pl.aaa", "timestamp": 1676025230, "searchTerm": "TEST", "type": "track", "dsp": "apple"}, HTTP_400_BAD_REQUEST, ), ( { "uri": "spotify:playlist:aaa", "timestamp": 1676025230, "searchTerm": "TEST", "type": "track", "dsp": "spotify", }, HTTP_400_BAD_REQUEST, ), ( { "uri": "spotify:track:aaa", "timestamp": 1676025230, "searchTerm": "TEST", "type": "track", "dsp": "spotify", }, HTTP_202_ACCEPTED, ), ), ) def test_post_recent_search(db_session, client, patch_auth_user, user_id, body, status): patch_auth_user(user_id) response = client.post(url_for("recent_search.post_recent_search"), json=body) assert response.status_code == status