def test_search(self): # results are in cache cache.get = MagicMock(return_value=self.all_albums) self.assertDictEqual(spotify.search("test-query"), self.all_albums) # results are not in cache cache.get = MagicMock(return_value=None) spotify._get = MagicMock(return_value=self.all_albums) self.assertDictEqual(spotify.search(query="Test Artist"), self.all_albums)
def test_search(self): # results are in cache cache.get = MagicMock(return_value=self.all_albums) self.assertDictEqual(spotify.search("test-query"), self.all_albums) # results are not in cache cache.get = MagicMock(return_value=None) spotify._get = MagicMock(return_value=self.all_albums) self.assertDictEqual(spotify.search(query="Test Artist"), self.all_albums)
def test_search(self, cache_get, spotify_get): # results are in cache cache_get.return_value = self.all_albums self.assertDictEqual(spotify.search("test-query"), self.all_albums) # results are not in cache cache_get.reset_mock() cache_get.return_value = None spotify_get.return_value = self.all_albums self.assertDictEqual(spotify.search(query="Test Artist"), self.all_albums)
def spotify(): release_group_id = request.args.get('release_group_id') if not release_group_id: return redirect(url_for('frontend.index')) release_group = musicbrainz.get_release_group_by_id(release_group_id) if not release_group: flash.error( gettext("Only existing release groups can be mapped to Spotify!")) return redirect(url_for('search.index')) page = int(request.args.get('page', default=1)) if page < 1: return redirect(url_for('.spotify')) limit = 16 offset = (page - 1) * limit # Removing punctuation from the string punctuation_map = dict((ord(char), None) for char in string.punctuation) query = release_group['title'].translate(punctuation_map) # Searching... response = spotify_api.search(query, 'album', limit, offset).get('albums') albums_ids = [x['id'] for x in response['items']] full_response = spotify_api.get_multiple_albums(albums_ids) return render_template('mapping/spotify.html', release_group=release_group, search_results=[ full_response[id] for id in albums_ids if id in full_response ], page=page, limit=limit, count=response.get('total'))
def test_search(self): self.assertDictEqual( spotify.search("Random name!", 'album'), dict( url= "https://api.spotify.com/v1/search?q=Random%20name%21&type=album&limit=20&offset=0" ))
def spotify(): release_group_id = request.args.get('release_group_id') if not release_group_id: return redirect(url_for('frontend.index')) release_group = musicbrainz.get_release_group_by_id(release_group_id) if not release_group: flash(gettext("Only existing release groups can be mapped to Spotify!"), 'error') return redirect(url_for('search.index')) page = int(request.args.get('page', default=1)) if page < 1: return redirect(url_for('.spotify')) limit = 16 offset = (page - 1) * limit # Removing punctuation from the string punctuation_map = dict((ord(char), None) for char in string.punctuation) query = unicode(release_group['title']).translate(punctuation_map) # Searching... response = spotify_api.search(query, 'album', limit, offset).get('albums') albums_ids = [x['id'] for x in response['items']] full_response = spotify_api.get_multiple_albums(albums_ids) return render_template( 'mapping/spotify.html', release_group=release_group, search_results=[full_response[id] for id in albums_ids if id in full_response], page=page, limit=limit, count=response.get('total'))
def spotify_add(): release_group_id = request.args.get('release_group_id') if not release_group_id: return redirect(url_for('frontend.index')) try: release_group = mb_release_group.get_release_group_by_id( release_group_id) except mb_exceptions.NoDataFoundException: flash.error( gettext("Only existing release groups can be mapped to Spotify!")) return redirect(url_for('search.index')) page = int(request.args.get('page', default=1)) if page < 1: return redirect(url_for('.spotify_add')) limit = 16 offset = (page - 1) * limit # Removing punctuation from the string punctuation_map = dict((ord(char), None) for char in string.punctuation) query = release_group['title'].translate(punctuation_map) # Searching... try: response = spotify_api.search(query, item_types='album', limit=limit, offset=offset).get('albums') except ExternalServiceException as e: current_app.logger.error("Error while searching Spotify API: %s", str(e), exc_info=True) raise ServiceUnavailable(e) albums_ids = [x['id'] for x in response['items']] try: full_response = spotify_api.get_multiple_albums(albums_ids) except ExternalServiceException as e: current_app.logger.error("Error while getting albums from Spotify: %s", str(e), exc_info=True) raise ServiceUnavailable(e) search_results = [ full_response[id] for id in albums_ids if id in full_response ] return render_template('mapping/spotify.html', release_group=release_group, search_results=search_results, page=page, limit=limit, count=response.get('total'))
def spotify_add(): release_group_id = request.args.get('release_group_id') if not release_group_id: return redirect(url_for('frontend.index')) try: release_group = mb_release_group.get_release_group_by_id(release_group_id) except mb_exceptions.NoDataFoundException: flash.error(gettext("Only existing release groups can be mapped to Spotify!")) return redirect(url_for('search.index')) page = int(request.args.get('page', default=1)) if page < 1: return redirect(url_for('.spotify_add')) limit = 16 offset = (page - 1) * limit # Removing punctuation from the string punctuation_map = dict((ord(char), None) for char in string.punctuation) query = release_group['title'].translate(punctuation_map) # Searching... try: response = spotify_api.search(query, item_types='album', limit=limit, offset=offset).get('albums') except ExternalServiceException as e: current_app.logger.error("Error while searching Spotify API: %s", str(e), exc_info=True) raise ServiceUnavailable(e) albums_ids = [x['id'] for x in response['items']] try: full_response = spotify_api.get_multiple_albums(albums_ids) except ExternalServiceException as e: current_app.logger.error("Error while getting albums from Spotify: %s", str(e), exc_info=True) raise ServiceUnavailable(e) search_results = [full_response[id] for id in albums_ids if id in full_response] return render_template('mapping/spotify.html', release_group=release_group, search_results=search_results, page=page, limit=limit, count=response.get('total'))
def test_search(self): self.assertDictEqual( spotify.search("Random name!", 'album'), dict(url="https://api.spotify.com/v1/search?q=Random%20name%21&type=album&limit=20&offset=0"))