def test_label_cache(self, label_fetch, cache_set, cache_get): mbid = "1aed8c3b-8e1e-46f8-b558-06357ff5f298" expected_key = b"label_1aed8c3b-8e1e-46f8-b558-06357ff5f298" label = { "id": "1aed8c3b-8e1e-46f8-b558-06357ff5f298", "name": "Dreamville", "type": "Imprint", "area": "United States", } label_fetch.return_value = {mbid: label} cache_get.return_value = None get_label_by_id(mbid) # Test that first time data is fetched database is queried cache_get.assert_called_with(expected_key) label_fetch.assert_called_with([mbid], includes=['artist-rels', 'url-rels']) cache_set.assert_called_with(key=expected_key, val=label, time=DEFAULT_CACHE_EXPIRATION) cache_get.return_value = label cache_set.reset_mock() label_fetch.reset_mock() get_label_by_id(mbid) # Test that second time data is fetched from cache cache_get.assert_called_with(expected_key) label_fetch.assert_not_called() cache_set.assert_not_called()
def get_entity_by_id(id, type='release_group'): """A wrapper to call the correct get_*_by_id function.""" if type == 'release_group': entity = get_release_group_by_id(str(id)) elif type == 'artist': entity = get_artist_by_id(str(id)) elif type == 'label': entity = get_label_by_id(str(id)) elif type == 'place': entity = get_place_by_id(str(id)) elif type == 'event': entity = get_event_by_id(str(id)) elif type == 'work': entity = get_work_by_id(str(id)) return entity
def entity(id): id = str(id) try: label = mb_label.get_label_by_id(id) except mb_exceptions.NoDataFoundException: raise NotFound( gettext( "Sorry, we couldn't find a label with that MusicBrainz ID.")) label_reviews_limit = LABEL_REVIEWS_LIMIT if request.args.get('reviews') == "all": label_reviews_limit = None if current_user.is_authenticated: my_reviews, my_count = db_review.list_reviews( entity_id=label['id'], entity_type='label', user_id=current_user.id, ) my_review = my_reviews[0] if my_count else None else: my_review = None reviews_offset = 0 reviews, reviews_count = db_review.list_reviews( entity_id=label['id'], entity_type='label', sort='popularity', limit=label_reviews_limit, offset=reviews_offset, ) avg_rating = get_avg_rating(label['id'], "label") rating_form = RatingEditForm(entity_id=id, entity_type='label') rating_form.rating.data = my_review['rating'] if my_review else None return render_template('label/entity.html', id=label['id'], label=label, reviews=reviews, my_review=my_review, reviews_limit=label_reviews_limit, reviews_count=reviews_count, avg_rating=avg_rating, rating_form=rating_form, current_user=current_user)