def test_event_cache(self, event_fetch, cache_set, cache_get): mbid = "ebe6ce0f-22c0-4fe7-bfd4-7a0397c9fe94" expected_key = b"event_ebe6ce0f-22c0-4fe7-bfd4-7a0397c9fe94" event = { 'id': 'ebe6ce0f-22c0-4fe7-bfd4-7a0397c9fe94', 'name': 'Taubertal-Festival 2004, Day 1', } event_fetch.return_value = {mbid: event} cache_get.return_value = None get_event_by_id(mbid) # Test that first time data is fetched database is queried cache_get.assert_called_with(expected_key) event_fetch.assert_called_with([mbid], includes=[ 'artist-rels', 'place-rels', 'series-rels', 'url-rels', 'release-group-rels' ]) cache_set.assert_called_with(key=expected_key, val=event, time=DEFAULT_CACHE_EXPIRATION) cache_get.return_value = event cache_set.reset_mock() event_fetch.reset_mock() get_event_by_id(mbid) # Test that second time data is fetched from cache cache_get.assert_called_with(expected_key) event_fetch.assert_not_called() cache_set.assert_not_called()
def test_get_event_by_id(self): self.event_query.return_value = [taubertal_festival_2004] event = mb_event.get_event_by_id('ebe6ce0f-22c0-4fe7-bfd4-7a0397c9fe94') self.assertDictEqual(event, { 'id': 'ebe6ce0f-22c0-4fe7-bfd4-7a0397c9fe94', 'name': 'Taubertal-Festival 2004, Day 1', })
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 == 'place': entity = get_place_by_id(str(id)) elif type == 'event': entity = get_event_by_id(str(id)) return entity
def entity(id): id = str(id) try: event = mb_event.get_event_by_id(id) except mb_exceptions.NoDataFoundException: raise NotFound( gettext( "Sorry, we couldn't find an event with that MusicBrainz ID.")) if 'url-rels' in event: external_reviews = list( filter(lambda rel: rel['type'] == 'review', event['url-rels'])) else: external_reviews = [] if 'artist-rels' in event and event['artist-rels']: artists_unique = [] for artist in event['artist-rels']: if artist not in artists_unique: artists_unique.append(artist) artists_sorted = sorted(artists_unique, key=itemgetter('type')) event['artists_grouped'] = groupby(artists_sorted, itemgetter('type')) if current_user.is_authenticated: my_reviews, _ = db_review.list_reviews(entity_id=event['id'], entity_type='event', user_id=current_user.id) my_review = my_reviews[0] if my_reviews else None else: my_review = None rating_form = RatingEditForm(entity_id=id, entity_type='event') rating_form.rating.data = my_review['rating'] if my_review else None limit = int(request.args.get('limit', default=10)) offset = int(request.args.get('offset', default=0)) reviews, count = db_review.list_reviews(entity_id=event['id'], entity_type='event', sort='popularity', limit=limit, offset=offset) avg_rating = get_avg_rating(event['id'], "event") return render_template('event/entity.html', id=event['id'], event=event, reviews=reviews, rating_form=rating_form, my_review=my_review, external_reviews=external_reviews, limit=limit, offset=offset, count=count, avg_rating=avg_rating, current_user=current_user)
def entity(id): id = str(id) try: event = mb_event.get_event_by_id(id) except mb_exceptions.NoDataFoundException: raise NotFound(gettext("Sorry, we couldn't find an event with that MusicBrainz ID.")) if 'url-rels' in event: external_reviews = list(filter(lambda rel: rel['type'] == 'review', event['url-rels'])) else: external_reviews = [] if 'artist-rels' in event and event['artist-rels']: artists_sorted = sorted(event['artist-rels'], key=itemgetter('type')) event['artists_grouped'] = groupby(artists_sorted, itemgetter('type')) if current_user.is_authenticated: my_reviews, my_count = db_review.list_reviews( entity_id=event['id'], entity_type='event', user_id=current_user.id ) my_review = my_reviews[0] if my_count else None else: my_review = None rating_form = RatingEditForm(entity_id=id, entity_type='event') rating_form.rating.data = my_review['rating'] if my_review else None limit = int(request.args.get('limit', default=10)) offset = int(request.args.get('offset', default=0)) reviews, count = db_review.list_reviews( entity_id=event['id'], entity_type='event', sort='popularity', limit=limit, offset=offset ) avg_rating = get_avg_rating(event['id'], "event") return render_template('event/entity.html', id=event['id'], event=event, reviews=reviews, rating_form=rating_form, my_review=my_review, external_reviews=external_reviews, limit=limit, offset=offset, count=count, avg_rating=avg_rating, current_user=current_user)
def entity(id): id = str(id) try: event = mb_event.get_event_by_id(id) except mb_exceptions.NoDataFoundException: raise NotFound( gettext( "Sorry, we couldn't find a event with that MusicBrainz ID.")) if 'artist-rels' in event and event['artist-rels']: artists_sorted = sorted(event['artist-rels'], key=itemgetter('type')) event['artists_grouped'] = groupby(artists_sorted, itemgetter('type')) if current_user.is_authenticated: my_reviews, my_count = db_review.list_reviews(entity_id=event['id'], entity_type='event', user_id=current_user.id) if my_count != 0: my_review = my_reviews[0] else: my_review = None else: my_review = None limit = int(request.args.get('limit', default=10)) offset = int(request.args.get('offset', default=0)) reviews, count = db_review.list_reviews(entity_id=event['id'], entity_type='event', sort='popularity', limit=limit, offset=offset) avg_rating = get_avg_rating(event['id'], "event") return render_template('event/entity.html', id=event['id'], event=event, reviews=reviews, my_review=my_review, limit=limit, offset=offset, count=count, avg_rating=avg_rating)
def test_unknown_place(self): self.event_query.return_value = [] mb_utils.reviewed_entities = MagicMock() mb_utils.reviewed_entities.return_value = ['40e6153d-a042-4c95-a0a9-b0a47e3825df'] event = mb_event.get_event_by_id('40e6153d-a042-4c95-a0a9-b0a47e3825df') self.assertEqual(event['name'], '[Unknown Event]')