def test_does_not_cache_none_results(self, sp_artist_mock):
        sp_artist_mock.is_loaded = False
        artist1 = translator.to_artist(sp_artist_mock)

        sp_artist_mock.is_loaded = True
        artist2 = translator.to_artist(sp_artist_mock)

        assert artist1 is None
        assert artist2 is not None
Beispiel #2
0
    def test_does_not_cache_none_results(self, sp_artist_mock):
        sp_artist_mock.is_loaded = False
        artist1 = translator.to_artist(sp_artist_mock)

        sp_artist_mock.is_loaded = True
        artist2 = translator.to_artist(sp_artist_mock)

        assert artist1 is None
        assert artist2 is not None
def search(config, session, query=None, uris=None, exact=False):
    # TODO Respect `uris` argument
    # TODO Support `exact` search

    if query is None:
        logger.debug('Ignored search without query')
        return models.SearchResult(uri='spotify:search')

    if 'uri' in query:
        return _search_by_uri(config, session, query)

    sp_query = translator.sp_search_query(query)
    if not sp_query:
        logger.debug('Ignored search with empty query')
        return models.SearchResult(uri='spotify:search')

    uri = 'spotify:search:%s' % urllib.quote(sp_query.encode('utf-8'))
    logger.info('Searching Spotify for: %s', sp_query)

    if session.connection.state is not spotify.ConnectionState.LOGGED_IN:
        logger.info('Spotify search aborted: Spotify is offline')
        return models.SearchResult(uri=uri)

    sp_search = session.search(sp_query,
                               album_count=config['search_album_count'],
                               artist_count=config['search_artist_count'],
                               track_count=config['search_track_count'])
    sp_search.load()

    albums = [translator.to_album(sp_album) for sp_album in sp_search.albums]
    artists = [
        translator.to_artist(sp_artist) for sp_artist in sp_search.artists
    ]
    tracks = [translator.to_track(sp_track) for sp_track in sp_search.tracks]

    return models.SearchResult(uri=uri,
                               albums=albums,
                               artists=artists,
                               tracks=tracks)
Beispiel #4
0
def search(config, session, query=None, uris=None, exact=False):
    # TODO Respect `uris` argument
    # TODO Support `exact` search

    if query is None:
        logger.debug('Ignored search without query')
        return models.SearchResult(uri='spotify:search')

    if 'uri' in query:
        return _search_by_uri(config, session, query)

    sp_query = translator.sp_search_query(query)
    if not sp_query:
        logger.debug('Ignored search with empty query')
        return models.SearchResult(uri='spotify:search')

    uri = 'spotify:search:%s' % urllib.quote(sp_query.encode('utf-8'))
    logger.info('Searching Spotify for: %s', sp_query)

    if session.connection.state is not spotify.ConnectionState.LOGGED_IN:
        logger.info('Spotify search aborted: Spotify is offline')
        return models.SearchResult(uri=uri)

    sp_search = session.search(
        sp_query,
        album_count=config['search_album_count'],
        artist_count=config['search_artist_count'],
        track_count=config['search_track_count'])
    sp_search.load()

    albums = [
        translator.to_album(sp_album) for sp_album in sp_search.albums]
    artists = [
        translator.to_artist(sp_artist) for sp_artist in sp_search.artists]
    tracks = [
        translator.to_track(sp_track) for sp_track in sp_search.tracks]

    return models.SearchResult(
        uri=uri, albums=albums, artists=artists, tracks=tracks)
    def test_caches_results(self, sp_artist_mock):
        artist1 = translator.to_artist(sp_artist_mock)
        artist2 = translator.to_artist(sp_artist_mock)

        assert artist1 is artist2
    def test_successful_translation(self, sp_artist_mock):
        artist = translator.to_artist(sp_artist_mock)

        assert artist.uri == 'spotify:artist:abba'
        assert artist.name == 'ABBA'
    def test_returns_none_if_unloaded(self, sp_artist_mock):
        sp_artist_mock.is_loaded = False

        artist = translator.to_artist(sp_artist_mock)

        assert artist is None
Beispiel #8
0
    def test_caches_results(self, sp_artist_mock):
        artist1 = translator.to_artist(sp_artist_mock)
        artist2 = translator.to_artist(sp_artist_mock)

        assert artist1 is artist2
Beispiel #9
0
    def test_successful_translation(self, sp_artist_mock):
        artist = translator.to_artist(sp_artist_mock)

        assert artist.uri == 'spotify:artist:abba'
        assert artist.name == 'ABBA'
Beispiel #10
0
    def test_returns_none_if_unloaded(self, sp_artist_mock):
        sp_artist_mock.is_loaded = False

        artist = translator.to_artist(sp_artist_mock)

        assert artist is None