Exemplo n.º 1
0
    def test_insert_user_stats_mult_ranges_artist(self):
        """ Test if multiple time range data is inserted correctly """
        with open(self.path_to_data_file('user_top_artists_db.json')) as f:
            artists_data = json.load(f)

        db_stats.insert_user_artists(user_id=self.user['id'], artists=UserArtistStatJson(**{'all_time': artists_data}))
        db_stats.insert_user_artists(user_id=self.user['id'], artists=UserArtistStatJson(**{'year': artists_data}))

        result = db_stats.get_user_artists(1, 'all_time')
        self.assertDictEqual(result.all_time.dict(), artists_data)

        result = db_stats.get_user_artists(1, 'year')
        self.assertDictEqual(result.year.dict(), artists_data)
Exemplo n.º 2
0
    def test_user_page(self):
        response = self.client.get(
            url_for('user.profile', user_name=self.user.musicbrainz_id))
        self.assert200(response)
        self.assertContext('active_section', 'listens')

        # check that artist count is not shown if stats haven't been calculated yet
        response = self.client.get(
            url_for('user.profile', user_name=self.user.musicbrainz_id))
        self.assert200(response)
        self.assertTemplateUsed('user/profile.html')
        props = ujson.loads(self.get_context_variable('props'))
        self.assertIsNone(props['artist_count'])

        with open(self.path_to_data_file('user_top_artists_db.json')) as f:
            artists_data = ujson.load(f)

        db_stats.insert_user_artists(
            user_id=self.user.id,
            artists=UserArtistStatJson(**{'all_time': artists_data}))
        response = self.client.get(
            url_for('user.profile', user_name=self.user.musicbrainz_id))
        self.assert200(response)
        self.assertTemplateUsed('user/profile.html')
        props = ujson.loads(self.get_context_variable('props'))
        self.assertEqual(props['artist_count'], '2')
        self.assertDictEqual(props['spotify'], {})
Exemplo n.º 3
0
    def test_insert_user_artists(self):
        """ Test if artist stats are inserted correctly """
        with open(self.path_to_data_file('user_top_artists_db.json')) as f:
            artists_data = json.load(f)

        db_stats.insert_user_artists(user_id=self.user['id'], artists=UserArtistStatJson(**{'all_time': artists_data}))

        result = db_stats.get_user_artists(user_id=self.user['id'], stats_range='all_time')
        self.assertDictEqual(result.all_time.dict(), artists_data)
Exemplo n.º 4
0
def insert_user_artists(user_id: int, artists: UserArtistStatJson):
    """ Inserts artist stats calculated from Spark into the database.

        If stats are already present for some user, they are updated to the new
        values passed.

        Args: user_id: the row id of the user,
              artists: the top artists listened to by the user
    """
    _insert_user_jsonb_data(user_id=user_id,
                            column='artist',
                            data=artists.dict(exclude_none=True))
Exemplo n.º 5
0
    def test_handle_user_entity(self, mock_send_mail, mock_new_user_stats,
                                mock_get_by_mb_id, mock_db_insert):
        data = {
            'musicbrainz_id': 'iliekcomputers',
            'type': 'user_entity',
            'entity': 'artists',
            'stats_range': 'all_time',
            'from_ts': 1,
            'to_ts': 10,
            'count': 1,
            'data': [{
                'artist_name': 'Kanye West',
                'listen_count': 200,
            }],
        }
        mock_get_by_mb_id.return_value = {
            'id': 1,
            'musicbrainz_id': 'iliekcomputers'
        }
        mock_new_user_stats.return_value = True

        with self.app.app_context():
            current_app.config[
                'TESTING'] = False  # set testing to false to check the notifications
            handle_user_entity(data)

        mock_db_insert.assert_called_with(
            1,
            UserArtistStatJson(week=None,
                               year=None,
                               month=None,
                               all_time=UserArtistStatRange(
                                   to_ts=10,
                                   from_ts=1,
                                   count=1,
                                   artists=[
                                       UserArtistRecord(
                                           artist_msid=None,
                                           artist_mbids=[],
                                           listen_count=200,
                                           artist_name='Kanye West',
                                       )
                                   ])))
        mock_send_mail.assert_called_once()
    def test_delete(self):
        user_id = db_user.create(10, 'frank')

        user = db_user.get(user_id)
        self.assertIsNotNone(user)

        with open(self.path_to_data_file('user_top_artists_db.json')) as f:
            artists_data = ujson.load(f)
        db_stats.insert_user_artists(
            user_id=user_id,
            artists=UserArtistStatJson(**{'all_time': artists_data}),
        )
        user_stats = db_stats.get_user_artists(user_id, 'all_time')
        self.assertIsNotNone(user_stats)

        db_user.delete(user_id)
        user = db_user.get(user_id)
        self.assertIsNone(user)
        user_stats = db_stats.get_user_artists(user_id, 'all_time')
        self.assertIsNone(user_stats)
Exemplo n.º 7
0
    def insert_test_data(self):
        """ Insert test data into the database """

        with open(self.path_to_data_file('user_top_artists_db.json')) as f:
            user_artists = json.load(f)
        with open(self.path_to_data_file('user_top_releases_db.json')) as f:
            releases = json.load(f)
        with open(self.path_to_data_file('user_top_recordings_db.json')) as f:
            recordings = json.load(f)
        with open(self.path_to_data_file('user_listening_activity_db.json')) as f:
            listening_activity = json.load(f)
        with open(self.path_to_data_file('user_daily_activity_db.json')) as f:
            daily_activity = json.load(f)
        with open(self.path_to_data_file('user_artist_map_db.json')) as f:
            artist_map = json.load(f)
        with open(self.path_to_data_file('sitewide_top_artists_db.json')) as f:
            sitewide_artists = json.load(f)

        db_stats.insert_user_artists(self.user['id'], UserArtistStatJson(**{'all_time': user_artists}))
        db_stats.insert_user_releases(self.user['id'], UserReleaseStatJson(**{'all_time': releases}))
        db_stats.insert_user_recordings(self.user['id'], UserRecordingStatJson(**{'all_time': recordings}))
        db_stats.insert_user_listening_activity(
            self.user['id'], UserListeningActivityStatJson(**{'all_time': listening_activity}))
        db_stats.insert_user_daily_activity(self.user['id'], UserDailyActivityStatJson(**{'all_time': daily_activity}))
        db_stats.insert_user_artist_map(self.user['id'], UserArtistMapStatJson(**{'all_time': artist_map}))
        db_stats.insert_sitewide_artists('all_time', SitewideArtistStatJson(**sitewide_artists))

        return {
            'user_artists': user_artists,
            'user_releases': releases,
            'user_recordings': recordings,
            'user_listening_activity': listening_activity,
            'user_daily_activity': daily_activity,
            'user_artist_map': artist_map,
            'sitewide_artists': sitewide_artists
        }