def test_handle_missing_musicbrainz_data(self, mock_get_by_mb_id, mock_db_insert): data = { 'type': 'missing_musicbrainz_data', 'musicbrainz_id': 'vansika', 'missing_musicbrainz_data': [{ "artist_name": "Katty Peri", "listened_at": "2020-04-29 23:56:23", "release_name": "No Place Is Home", "recording_name": "How High" }], 'source': 'cf' } mock_get_by_mb_id.return_value = {'id': 1, 'musicbrainz_id': 'vansika'} with self.app.app_context(): handle_missing_musicbrainz_data(data) mock_db_insert.assert_called_with( 1, UserMissingMusicBrainzDataJson(missing_musicbrainz_data=[ UserMissingMusicBrainzDataRecord( artist_name="Katty Peri", listened_at="2020-04-29 23:56:23", release_name="No Place Is Home", recording_name="How High") ]), 'cf')
def prepare_messages(missing_musicbrainz_data_itr, from_date, to_date, ti): """ Create messages to send the data to the webserver via RabbitMQ Args: missing_musicbrainz_data_itr (iterator): Data missing from the MusicBrainz. from_date (datetime): Date from which start fetching listens. to_date (datetime): Date upto which fetch listens. ti (datetime): Timestamp when the first func (main) of the script was called. Returns: messages: A list of messages to be sent via RabbitMQ """ missing_musicbrainz_data = defaultdict(list) current_ts = str(datetime.utcnow()) for row in missing_musicbrainz_data_itr: try: missing_musicbrainz_data[row.user_name].append(UserMissingMusicBrainzDataRecord(** { 'artist_msid': row.artist_msid, 'artist_name': row.artist_name, 'listened_at': str(row.listened_at), 'recording_msid': row.recording_msid, 'release_msid': row.release_msid, 'release_name': row.release_name, 'track_name': row.track_name, } ).dict()) except ValidationError: current_app.logger.warning("""Invalid entry present in missing musicbrainz data for user: {user_name}, skipping""" .format(user_name=row.user_name), exc_info=True) total_time = '{:.2f}'.format((time.monotonic() - ti) / 60) try: messages = [UserCreateDataframesMessage(**{ 'type': 'cf_recommendations_recording_dataframes', 'dataframe_upload_time': current_ts, 'total_time': total_time, 'from_date': str(from_date.strftime('%b %Y')), 'to_date': str(to_date.strftime('%b %Y')), }).dict()] except ValidationError: current_app.logger.warning("Invalid entry present in dataframe creation message", exc_info=True) for user_name, data in missing_musicbrainz_data.items(): try: messages.append(UserMissingMusicBrainzDataMessage(**{ 'type': 'missing_musicbrainz_data', 'musicbrainz_id': user_name, 'missing_musicbrainz_data': data, 'source': 'cf' }).dict()) except ValidationError: current_app.logger.warning("ValidationError while calculating missing_musicbrainz_data for {user_name}." "\nData: {data}".format(user_name=user_name, data=data), exc_info=True) return messages
def test_handle_missing_musicbrainz_data(self, mock_get_by_mb_id, mock_db_insert): data = { 'type': 'missing_musicbrainz_data', 'musicbrainz_id': 'vansika', 'missing_musicbrainz_data': [{ "artist_msid": "f26d35e3-5fdd-43cf-8b94-71936451bc07", "artist_name": "Katty Peri", "listened_at": "2020-04-29 23:56:23", "recording_msid": "568eeea3-9255-4878-9df8-296043344e04", "release_msid": "8c5ba30c-4851-48fd-ac02-1b194cdb34d1", "release_name": "No Place Is Home", "track_name": "How High" }], 'source': 'cf' } mock_get_by_mb_id.return_value = {'id': 1, 'musicbrainz_id': 'vansika'} with self.app.app_context(): handle_missing_musicbrainz_data(data) mock_db_insert.assert_called_with( 1, UserMissingMusicBrainzDataJson(missing_musicbrainz_data=[ UserMissingMusicBrainzDataRecord( artist_msid="f26d35e3-5fdd-43cf-8b94-71936451bc07", artist_name="Katty Peri", listened_at="2020-04-29 23:56:23", recording_msid="568eeea3-9255-4878-9df8-296043344e04", release_msid="8c5ba30c-4851-48fd-ac02-1b194cdb34d1", release_name="No Place Is Home", track_name="How High") ]), 'cf')