-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CB-231: Use direct database access for entity: event
- Loading branch information
Showing
9 changed files
with
211 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from collections import defaultdict | ||
from mbdata import models | ||
from critiquebrainz.frontend.external.musicbrainz_db import mb_session, DEFAULT_CACHE_EXPIRATION | ||
from critiquebrainz.frontend.external.musicbrainz_db.utils import get_entities_by_gids | ||
from critiquebrainz.frontend.external.musicbrainz_db.includes import check_includes | ||
from critiquebrainz.frontend.external.musicbrainz_db.serialize import to_dict_events | ||
from critiquebrainz.frontend.external.musicbrainz_db.helpers import get_relationship_info | ||
from brainzutils import cache | ||
|
||
|
||
def get_event_by_id(mbid): | ||
"""Get event with the MusicBrainz ID. | ||
Args: | ||
mbid (uuid): MBID(gid) of the event. | ||
Returns: | ||
Dictionary containing the event information. | ||
""" | ||
key = cache.gen_key(mbid) | ||
event = cache.get(key) | ||
if not event: | ||
event = _get_event_by_id(mbid) | ||
cache.set(key=key, val=event, time=DEFAULT_CACHE_EXPIRATION) | ||
return event | ||
|
||
|
||
def _get_event_by_id(mbid): | ||
return fetch_multiple_events( | ||
[mbid], | ||
includes=['artist-rels', 'place-rels', 'series-rels', 'url-rels', 'release-group-rels'], | ||
).get(mbid) | ||
|
||
|
||
def fetch_multiple_events(mbids, *, includes=None): | ||
"""Get info related to multiple events using their MusicBrainz IDs. | ||
Args: | ||
mbids (list): List of MBIDs of events. | ||
includes (list): List of information to be included. | ||
Returns: | ||
Dictionary containing info of multiple events keyed by their mbid. | ||
""" | ||
if includes is None: | ||
includes = [] | ||
includes_data = defaultdict(dict) | ||
check_includes('event', includes) | ||
with mb_session() as db: | ||
query = db.query(models.Event) | ||
events = get_entities_by_gids( | ||
query=query, | ||
entity_type='event', | ||
mbids=mbids, | ||
) | ||
event_ids = [event.id for event in events.values()] | ||
|
||
if 'artist-rels' in includes: | ||
get_relationship_info( | ||
db=db, | ||
target_type='artist', | ||
source_type='event', | ||
source_entity_ids=event_ids, | ||
includes_data=includes_data, | ||
) | ||
if 'place-rels' in includes: | ||
get_relationship_info( | ||
db=db, | ||
target_type='place', | ||
source_type='event', | ||
source_entity_ids=event_ids, | ||
includes_data=includes_data, | ||
) | ||
if 'series-rels' in includes: | ||
get_relationship_info( | ||
db=db, | ||
target_type='series', | ||
source_type='event', | ||
source_entity_ids=event_ids, | ||
includes_data=includes_data, | ||
) | ||
if 'url-rels' in includes: | ||
get_relationship_info( | ||
db=db, | ||
target_type='url', | ||
source_type='event', | ||
source_entity_ids=event_ids, | ||
includes_data=includes_data, | ||
) | ||
if 'release-group-rels' in includes: | ||
get_relationship_info( | ||
db=db, | ||
target_type='release_group', | ||
source_type='event', | ||
source_entity_ids=event_ids, | ||
includes_data=includes_data, | ||
) | ||
|
||
events = {str(mbid): to_dict_events(events[mbid], includes_data[events[mbid].id]) for mbid in mbids} | ||
return events |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
critiquebrainz/frontend/external/musicbrainz_db/tests/event_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from unittest import TestCase | ||
from unittest.mock import MagicMock | ||
from critiquebrainz.frontend.external.musicbrainz_db import event as mb_event | ||
from critiquebrainz.frontend.external.musicbrainz_db.test_data import taubertal_festival_2004, event_ra_hall_uk | ||
from critiquebrainz.frontend.external.musicbrainz_db.tests import setup_cache | ||
|
||
class EventTestCase(TestCase): | ||
|
||
def setUp(self): | ||
setup_cache() | ||
mb_event.mb_session = MagicMock() | ||
self.mock_db = mb_event.mb_session.return_value.__enter__.return_value | ||
self.event_query = self.mock_db.query.return_value.filter.return_value.all | ||
|
||
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 test_fetch_multiple_events(self): | ||
self.event_query.return_value = [taubertal_festival_2004, event_ra_hall_uk] | ||
events = mb_event.fetch_multiple_events( | ||
['ebe6ce0f-22c0-4fe7-bfd4-7a0397c9fe94', '40e6153d-a042-4c95-a0a9-b0a47e3825ce'], | ||
) | ||
self.assertEqual(events['ebe6ce0f-22c0-4fe7-bfd4-7a0397c9fe94']['name'], 'Taubertal-Festival 2004, Day 1') | ||
self.assertEqual(events['40e6153d-a042-4c95-a0a9-b0a47e3825ce']['name'], '1996-04-17: Royal Albert Hall, London, England, UK') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters