def test_get_events_for_feed_honors_count_parameter(self):
        db_user_timeline_event.create_user_track_recommendation_event(
            user_id=self.user['id'],
            metadata=UserTimelineEventMetadata(
                track_name="Sunflower",
                artist_name="Swae Lee & Post Malone",
                recording_msid=str(uuid.uuid4()),
                artist_msid=str(uuid.uuid4()),
            ))
        db_user_timeline_event.create_user_track_recommendation_event(
            user_id=self.user['id'],
            metadata=UserTimelineEventMetadata(
                track_name="Da Funk",
                artist_name="Daft Punk",
                recording_msid=str(uuid.uuid4()),
                artist_msid=str(uuid.uuid4()),
            ))

        events = db_user_timeline_event.get_recording_recommendation_events_for_feed(
            user_ids=(self.user['id'], ),
            min_ts=0,
            max_ts=int(time.time()) + 10,
            count=1,
        )

        # 2 events exist, should return only one, the one that is newer
        self.assertEqual(1, len(events))
        self.assertEqual('Da Funk', events[0].metadata.track_name)
    def test_get_events_for_feed_returns_events(self):
        db_user_timeline_event.create_user_track_recommendation_event(
            user_id=self.user['id'],
            metadata=UserTimelineEventMetadata(
                track_name="Sunflower",
                artist_name="Swae Lee & Post Malone",
                recording_msid=str(uuid.uuid4()),
                artist_msid=str(uuid.uuid4()),
            ))

        new_user = db_user.get_or_create(2, 'superman')
        db_user_timeline_event.create_user_track_recommendation_event(
            user_id=new_user['id'],
            metadata=UserTimelineEventMetadata(
                track_name="Sunflower",
                artist_name="Swae Lee & Post Malone",
                recording_msid=str(uuid.uuid4()),
                artist_msid=str(uuid.uuid4()),
            ))

        events = db_user_timeline_event.get_recording_recommendation_events_for_feed(
            user_ids=(self.user['id'], new_user['id']),
            min_ts=0,
            max_ts=int(time.time()) + 10,
            count=50,
        )
        self.assertEqual(2, len(events))
        self.assertEqual(new_user['id'], events[0].user_id)
        self.assertEqual(self.user['id'], events[1].user_id)
    def test_it_returns_recording_recommendation_events(self):
        # create a recording recommendation ourselves
        db_user_timeline_event.create_user_track_recommendation_event(
            user_id=self.main_user['id'],
            metadata=UserTimelineEventMetadata(
                track_name="Lose yourself to dance",
                artist_name="Daft Punk",
                recording_msid=str(uuid.uuid4()),
                artist_msid=str(uuid.uuid4()),
            ))

        # create a recording recommendation for a user we follow
        db_user_timeline_event.create_user_track_recommendation_event(
            user_id=self.following_user_1['id'],
            metadata=UserTimelineEventMetadata(
                track_name="Sunflower",
                artist_name="Swae Lee & Post Malone",
                recording_msid=str(uuid.uuid4()),
                artist_msid=str(uuid.uuid4()),
            ))

        # this should show up in the events
        r = self.client.get(
            url_for('user_timeline_event_api_bp.user_feed',
                    user_name=self.main_user['musicbrainz_id']),
            headers={'Authorization': f"Token {self.main_user['auth_token']}"},
            query_string={'max_ts': int(time.time()) + 1})
        self.assert200(r)

        # first, let's remove the own follow events, we don't care about those in this test.
        payload = self.remove_own_follow_events(r.json['payload'])

        # now, check for both the recording recommendations and their order
        self.assertEqual(2, payload['count'])
        self.assertEqual('recording_recommendation',
                         payload['events'][0]['event_type'])
        self.assertEqual('following_1', payload['events'][0]['user_name'])
        self.assertEqual(
            'Sunflower',
            payload['events'][0]['metadata']['track_metadata']['track_name'])
        self.assertEqual(
            'Swae Lee & Post Malone',
            payload['events'][0]['metadata']['track_metadata']['artist_name'])

        self.assertEqual('recording_recommendation',
                         payload['events'][1]['event_type'])
        self.assertEqual(self.main_user['musicbrainz_id'],
                         payload['events'][1]['user_name'])
        self.assertEqual(
            'Lose yourself to dance',
            payload['events'][1]['metadata']['track_metadata']['track_name'])
        self.assertEqual(
            'Daft Punk',
            payload['events'][1]['metadata']['track_metadata']['artist_name'])
    def test_get_events_for_feed_honors_time_parameters(self):
        ts = int(time.time())
        db_user_timeline_event.create_user_track_recommendation_event(
            user_id=self.user['id'],
            metadata=UserTimelineEventMetadata(
                track_name="Sunflower",
                artist_name="Swae Lee & Post Malone",
                recording_msid=str(uuid.uuid4()),
                artist_msid=str(uuid.uuid4()),
            ))
        db_user_timeline_event.create_user_track_recommendation_event(
            user_id=self.user['id'],
            metadata=UserTimelineEventMetadata(
                track_name="Da Funk",
                artist_name="Daft Punk",
                recording_msid=str(uuid.uuid4()),
                artist_msid=str(uuid.uuid4()),
            ))

        time.sleep(3)
        new_user = db_user.get_or_create(4, 'new_user')
        db_user_timeline_event.create_user_track_recommendation_event(
            user_id=new_user['id'],
            metadata=UserTimelineEventMetadata(
                track_name="Da Funk",
                artist_name="Daft Punk",
                recording_msid=str(uuid.uuid4()),
                artist_msid=str(uuid.uuid4()),
            ))

        # max_ts is too low, won't return anything
        events = db_user_timeline_event.get_recording_recommendation_events_for_feed(
            user_ids=(self.user['id'], new_user['id']),
            min_ts=0,
            max_ts=ts,
            count=50,
        )
        self.assertListEqual([], events)

        # check that it honors min_ts as well
        events = db_user_timeline_event.get_recording_recommendation_events_for_feed(
            user_ids=(self.user['id'], new_user['id']),
            min_ts=ts + 1,
            max_ts=ts + 10,
            count=50,
        )
        self.assertEqual(1, len(events))
 def test_create_user_track_recommendation_sets_event_type_correctly(self):
     event = db_user_timeline_event.create_user_track_recommendation_event(
         user_id=self.user['id'],
         metadata=UserTimelineEventMetadata(
             track_name="Sunflower",
             artist_name="Swae Lee & Post Malone",
             recording_msid=str(uuid.uuid4()),
             artist_msid=str(uuid.uuid4()),
         ))
     self.assertEqual(UserTimelineEventType.RECORDING_RECOMMENDATION,
                      event.event_type)
 def test_it_raises_database_exceptions_if_something_goes_wrong(
         self, mock_db_connect):
     with self.assertRaises(DatabaseException):
         db_user_timeline_event.create_user_timeline_event(
             user_id=self.user['id'],
             event_type=UserTimelineEventType.RECORDING_RECOMMENDATION,
             metadata=UserTimelineEventMetadata(
                 track_name="Sunflower",
                 artist_name="Swae Lee & Post Malone",
                 recording_msid=str(uuid.uuid4()),
                 artist_msid=str(uuid.uuid4()),
             ))
 def test_get_events_only_gets_events_for_the_specified_user(self):
     db_user_timeline_event.create_user_track_recommendation_event(
         user_id=self.user['id'],
         metadata=UserTimelineEventMetadata(
             track_name="Sunflower",
             artist_name="Swae Lee & Post Malone",
             recording_msid=str(uuid.uuid4()),
             artist_msid=str(uuid.uuid4()),
         ))
     new_user = db_user.get_or_create(2, 'captain america')
     db_user_timeline_event.create_user_track_recommendation_event(
         user_id=new_user['id'],
         metadata=UserTimelineEventMetadata(
             track_name="Fade",
             artist_name="Kanye West",
             recording_msid=str(uuid.uuid4()),
             artist_msid=str(uuid.uuid4()),
         ))
     events = db_user_timeline_event.get_user_track_recommendation_events(
         user_id=new_user['id'], )
     self.assertEqual(1, len(events))
     self.assertEqual(new_user['id'], events[0].user_id)
    def test_it_returns_all_types_of_events_sorted_by_time_in_descending_order(
            self):
        with open(self.path_to_data_file('valid_single.json'), 'r') as f:
            payload = json.load(f)

        # send a listen from the past
        ts = int(time.time())
        payload['payload'][0]['listened_at'] = ts - 10
        response = self.send_data(payload, user=self.following_user_1)
        self.assert200(response)
        self.assertEqual(response.json['status'], 'ok')

        # make a user you're following follow a new user
        new_user_1 = db_user.get_or_create(104, 'new_user_1')
        db_user_relationship.insert(self.following_user_1['id'],
                                    new_user_1['id'], 'follow')

        time.sleep(
            1.5
        )  # sleep a bit to avoid ordering conflicts, cannot mock this time as it comes from postgres

        # create a recording recommendation for a user we follow
        db_user_timeline_event.create_user_track_recommendation_event(
            user_id=self.following_user_1['id'],
            metadata=UserTimelineEventMetadata(
                track_name="Sunflower",
                artist_name="Swae Lee & Post Malone",
                recording_msid=str(uuid.uuid4()),
                artist_msid=str(uuid.uuid4()),
            ))

        time.sleep(2)

        r = self.client.get(
            url_for('user_timeline_event_api_bp.user_feed',
                    user_name=self.main_user['musicbrainz_id']),
            headers={'Authorization': f"Token {self.main_user['auth_token']}"},
        )
        self.assert200(r)
        self.assertEqual(
            5, r.json['payload']
            ['count'])  # 3 events we created + 2 own follow events
        self.assertEqual('recording_recommendation',
                         r.json['payload']['events'][0]['event_type'])
        self.assertEqual('follow',
                         r.json['payload']['events'][1]['event_type'])
        self.assertEqual('listen', r.json['payload']['events'][4]
                         ['event_type'])  # last event should be a listen
 def test_it_adds_rows_to_the_database(self):
     event = db_user_timeline_event.create_user_timeline_event(
         user_id=self.user['id'],
         event_type=UserTimelineEventType.RECORDING_RECOMMENDATION,
         metadata=UserTimelineEventMetadata(
             track_name="Sunflower",
             artist_name="Swae Lee & Post Malone",
             recording_msid=str(uuid.uuid4()),
             artist_msid=str(uuid.uuid4()),
         ))
     events = db_user_timeline_event.get_user_track_recommendation_events(
         user_id=self.user['id'],
         count=1,
     )
     self.assertEqual(1, len(events))
     self.assertEqual(event.id, events[0].id)
     self.assertEqual(event.created, events[0].created)
     self.assertEqual('Sunflower', events[0].metadata.track_name)
Example #10
0
def create_user_timeline_event(
    user_id: int,
    event_type: UserTimelineEventType,
    metadata: UserTimelineEventMetadata,
) -> UserTimelineEvent:
    """ Creates a user timeline event in the database and returns the event.
    """
    try:
        with db.engine.connect() as connection:
            result = connection.execute(
                sqlalchemy.text("""
                INSERT INTO user_timeline_event (user_id, event_type, metadata)
                    VALUES (:user_id, :event_type, :metadata)
                RETURNING id, user_id, event_type, metadata, created
                """), {
                    'user_id': user_id,
                    'event_type': event_type.value,
                    'metadata': ujson.dumps(metadata.dict()),
                })

            r = dict(result.fetchone())
            return UserTimelineEvent(**r)
    except Exception as e:
        raise DatabaseException(str(e))