Example #1
0
    def should_return_played_tracs_from_the_beginning_of_the_age(self):
        most_played = TrackFactory()
        second_most_played = TrackFactory()
        entries = [
            PlaylistHistoryFactory(track=most_played),
            PlaylistHistoryFactory(track=most_played),
            PlaylistHistoryFactory(track=second_most_played),
        ]
        db.session.add_all(entries)
        db.session.commit()

        url = url_for('player.stats')
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.json['most_played_tracks'] == [
            {
                'track': TrackSerializer().serialize(most_played),
                'total': 2
            },
            {
                'track': TrackSerializer().serialize(second_most_played),
                'total': 1
            },
        ]
Example #2
0
    def should_return_most_played_genre_from_the_beginning_of_the_age(self):
        most_played = GenreFactory()
        second_most_played = GenreFactory()
        entries = [
            PlaylistHistoryFactory(track=TrackFactory(album=AlbumWithArtist(
                artists=[ArtistFactory(genres=[most_played])]))),
            PlaylistHistoryFactory(track=TrackFactory(album=AlbumWithArtist(
                artists=[
                    ArtistFactory(genres=[second_most_played, most_played])
                ]))),
        ]
        db.session.add_all(entries)
        db.session.commit()

        url = url_for('player.stats')
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.json['most_played_genres'] == [
            {
                'name': most_played.name,
                'total': 2
            },
            {
                'name': second_most_played.name,
                'total': 1
            },
        ]
Example #3
0
    def should_return_most_played_artist_from_selected_date(self):
        user = UserFactory()
        most_played = ArtistFactory()
        second_most_played = ArtistFactory()
        entries = [
            user,
            PlaylistHistoryFactory(track=TrackFactory(album=AlbumWithArtist(
                artists=[most_played])),
                                   user=user),
            PlaylistHistoryFactory(track=TrackFactory(album=AlbumWithArtist(
                artists=[second_most_played]))),
            PlaylistHistoryFactory(
                created=datetime.datetime(2014, 1, 1, tzinfo=tzutc()),
                track=TrackFactory(album=AlbumWithArtist(
                    artists=[second_most_played, most_played])),
                user=user),
        ]
        db.session.add_all(entries)
        db.session.commit()

        url = url_for('users.stats', pk=user.id, **{'from': '2015-06-01'})
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.json['most_played_artists'] == [
            {
                'artist': ArtistSerializer().serialize(most_played),
                'total': 1
            },
        ]
Example #4
0
    def should_returns_list_of_genres_in_queue(self):
        genres = GenreFactory.create_batch(2)
        track1 = TrackFactory(album=AlbumWithArtist(
            artists=[ArtistFactory(genres=genres)]))
        track2 = TrackFactory(album=AlbumWithArtist(
            artists=[ArtistFactory(genres=[genres[0]])]))
        tracks = [track1, track2]
        db.session.add_all(tracks)
        db.session.commit()

        for track in tracks:
            self.redis.rpush(
                config.PLAYLIST_REDIS_KEY,
                json.dumps({
                    'uri': track.spotify_uri,
                    'user': '******'
                }))

        url = url_for('player.queue-meta')
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.json['genres'] == {
            genres[0].name: 2,
            genres[1].name: 1
        }
Example #5
0
    def should_return_most_played_genre_from_selected_date(self):
        most_played = GenreFactory()
        second_most_played = GenreFactory()
        entries = [
            PlaylistHistoryFactory(track=TrackFactory(album=AlbumWithArtist(
                artists=[ArtistFactory(genres=[most_played])]))),
            PlaylistHistoryFactory(
                created=datetime.datetime(2014, 1, 1, tzinfo=tzutc()),
                track=TrackFactory(album=AlbumWithArtist(artists=[
                    ArtistFactory(genres=[second_most_played, most_played])
                ]))),
        ]
        db.session.add_all(entries)
        db.session.commit()

        url = url_for('player.stats', **{'from': '2015-06-01'})
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.json['most_played_genres'] == [
            {
                'name': most_played.name,
                'total': 1
            },
        ]
Example #6
0
    def should_return_user_played_time_from_selected_date(self):
        most_played = UserFactory()
        second_most_played = UserFactory()
        entries = [
            PlaylistHistoryFactory(user=most_played,
                                   track=TrackFactory(duration=1000)),
            PlaylistHistoryFactory(user=most_played,
                                   created=datetime.datetime(2014,
                                                             1,
                                                             1,
                                                             tzinfo=tzutc()),
                                   track=TrackFactory(duration=500)),
            PlaylistHistoryFactory(user=second_most_played,
                                   track=TrackFactory(duration=700)),
        ]
        db.session.add_all(entries)
        db.session.commit()

        url = url_for('player.stats', **{'from': '2015-06-01'})
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.json['total_play_time_per_user'] == [
            {
                'user': UserSerializer().serialize(most_played),
                'total': 1000
            },
            {
                'user': UserSerializer().serialize(second_most_played),
                'total': 700
            },
        ]
Example #7
0
    def should_return_user_played_time_from_the_beginning_of_the_age(self):
        most_played = UserFactory()
        second_most_played = UserFactory()
        entries = [
            PlaylistHistoryFactory(user=most_played,
                                   track=TrackFactory(duration=1000)),
            PlaylistHistoryFactory(user=most_played,
                                   track=TrackFactory(duration=500)),
            PlaylistHistoryFactory(user=second_most_played,
                                   track=TrackFactory(duration=700)),
        ]
        db.session.add_all(entries)
        db.session.commit()

        url = url_for('player.stats')
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.json['total_play_time_per_user'] == [
            {
                'user': UserSerializer().serialize(most_played),
                'total': 1500
            },
            {
                'user': UserSerializer().serialize(second_most_played),
                'total': 700
            },
        ]
Example #8
0
    def test_add_track_into_queue(self):
        tracks = [TrackFactory(), TrackFactory(), TrackFactory()]
        db.session.add_all(tracks)
        db.session.commit()

        random_tracks = Random.get_tracks(count=3)
        assert set(random_tracks) == set(tracks)
Example #9
0
    def should_return_played_tracs_from_selected_date(self):
        most_played = TrackFactory()
        second_most_played = TrackFactory()
        entries = [
            PlaylistHistoryFactory(track=most_played),
            PlaylistHistoryFactory(track=most_played),
            PlaylistHistoryFactory(track=second_most_played),
            PlaylistHistoryFactory(track=second_most_played,
                                   created=datetime.datetime(2014,
                                                             1,
                                                             1,
                                                             tzinfo=tzutc())),
            PlaylistHistoryFactory(
                created=datetime.datetime(2014, 1, 1, tzinfo=tzutc())),
        ]
        db.session.add_all(entries)
        db.session.commit()

        url = url_for('player.stats', **{'from': '2015-06-01'})
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.json['most_played_tracks'] == [
            {
                'track': TrackSerializer().serialize(most_played),
                'total': 2
            },
            {
                'track': TrackSerializer().serialize(second_most_played),
                'total': 1
            },
        ]
Example #10
0
    def should_return_most_played_artist_from_the_beginning_of_the_age(self):
        user = UserFactory()
        most_played = ArtistFactory()
        second_most_played = ArtistFactory()
        entries = [
            user,
            PlaylistHistoryFactory(track=TrackFactory(album=AlbumWithArtist(
                artists=[most_played])),
                                   user=user),
            PlaylistHistoryFactory(track=TrackFactory(album=AlbumWithArtist(
                artists=[second_most_played, most_played])),
                                   user=user),
            PlaylistHistoryFactory(track=TrackFactory(album=AlbumWithArtist(
                artists=[second_most_played]))),
        ]
        db.session.add_all(entries)
        db.session.commit()

        url = url_for('users.stats', pk=user.id)
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.json['most_played_artists'] == [
            {
                'artist': ArtistSerializer().serialize(most_played),
                'total': 2
            },
            {
                'artist': ArtistSerializer().serialize(second_most_played),
                'total': 1
            },
        ]
Example #11
0
    def test_add_some_tracks_into_queue(self):
        tracks = [TrackFactory(), TrackFactory(), TrackFactory()]
        users = [UserFactory(), UserFactory(), UserFactory()]

        db.session.add_all(tracks + users)
        db.session.commit()

        response = self.client.post(url_for('player.random'),
                                    data=json.dumps({'tracks': 2}))

        assert response.status_code == httplib.CREATED
        assert Queue.length() == 2
Example #12
0
    def should_return_total_played_time_from_the_beginning_of_the_age(self):
        entries = [
            PlaylistHistoryFactory(track=TrackFactory(duration=1000)),
            PlaylistHistoryFactory(track=TrackFactory(duration=500)),
            PlaylistHistoryFactory(track=TrackFactory(duration=700)),
        ]
        db.session.add_all(entries)
        db.session.commit()

        url = url_for('player.stats')
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.json['total_play_time'] == 2200
Example #13
0
    def should_return_most_played_artist_in_selected_time_window(self):
        most_played = ArtistFactory()
        entries = [
            PlaylistHistoryFactory(created=datetime.datetime(2015,
                                                             5,
                                                             1,
                                                             tzinfo=tzutc()),
                                   track=TrackFactory(album=AlbumWithArtist(
                                       artists=[most_played]))),
            PlaylistHistoryFactory(
                created=datetime.datetime(2015, 3, 1, tzinfo=tzutc())),
            PlaylistHistoryFactory(
                created=datetime.datetime(2015, 7, 1, tzinfo=tzutc())),
        ]
        db.session.add_all(entries)
        db.session.commit()

        url = url_for('player.stats', **{
            'to': '2015-06-01',
            'from': '2015-04-01'
        })
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.json['most_played_artists'] == [
            {
                'artist': ArtistSerializer().serialize(most_played),
                'total': 1
            },
        ]
Example #14
0
    def should_fire_stop_event(self):
        track = TrackFactory()
        user = UserFactory()

        db.session.add_all([track, user])
        db.session.commit()

        self.redis.get.return_value = json.dumps({
            'uri': track.spotify_uri,
            'user': user.id,
        })

        url = url_for('player.current')
        response = self.client.delete(url)

        assert response.status_code == 200
        self.redis.publish.assert_called_once_with(
            self.app.config.get('PLAYER_CHANNEL'),
            json.dumps({
                'event': 'stop',
                'user': str(user.id),
                'track': str(track.id),
                'uri': str(track.spotify_uri),
                'by': str(current_user.id),
            }))
Example #15
0
    def should_return_zero_when_elapsed_time_cant_be_pulled(self):
        track = TrackFactory()
        user = UserFactory()

        db.session.add_all([track, user])
        db.session.commit()

        mock_redis_values = {
            'fm:player:current':
            json.dumps({
                'uri': track.spotify_uri,
                'user': user.id
            }),
            'fm:player:elapsed_time':
            None
        }
        self.redis.get.side_effect = lambda x: mock_redis_values.get(x)

        url = url_for('player.current')
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.json['track'] == TrackSerializer().serialize(track)
        assert response.json['user'] == UserSerializer().serialize(user)
        assert response.json['player']['elapsed_time'] == 0
        assert response.json['player']['elapsed_percentage'] == 0
        assert response.json['player']['elapsed_seconds'] == 0
Example #16
0
    def should_return_track_data(self):
        track = TrackFactory(duration=10000)
        user = UserFactory()

        db.session.add_all([track, user])
        db.session.commit()

        now = datetime.datetime.utcnow()
        start = now - datetime.timedelta(seconds=5)
        start = start.replace(tzinfo=dateutil.tz.tzutc())

        mock_redis_values = {
            'fm:player:current':
            json.dumps({
                'uri': track.spotify_uri,
                'user': user.id
            }),
            'fm:player:start_time':
            start.isoformat()
        }
        self.redis.get.side_effect = lambda x: mock_redis_values.get(x)

        url = url_for('player.current')

        # We have to mock utcnow so we don't get drift in the test
        with mock.patch('fm.views.player.datetime') as _dt:
            _dt.utcnow.return_value = now
            response = self.client.get(url)

        assert response.status_code == 200, response.json
        assert response.json['track'] == TrackSerializer().serialize(track)
        assert response.json['user'] == UserSerializer().serialize(user)
        assert response.json['player']['elapsed_time'] == 5000
        assert response.json['player']['elapsed_percentage'] == 50
        assert response.json['player']['elapsed_seconds'] == 5
Example #17
0
    def test_add_track_into_queue(self):
        track = TrackFactory()
        user = UserFactory()
        db.session.add_all([track, user])
        db.session.commit()

        Queue.add(track.spotify_uri, user.id)
        assert Queue.length() == 1
Example #18
0
    def should_return_total_played_time_from_selected_date(self):
        entries = [
            PlaylistHistoryFactory(track=TrackFactory(duration=1000)),
            PlaylistHistoryFactory(created=datetime.datetime(2014,
                                                             1,
                                                             1,
                                                             tzinfo=tzutc()),
                                   track=TrackFactory(duration=500)),
            PlaylistHistoryFactory(track=TrackFactory(duration=700)),
        ]
        db.session.add_all(entries)
        db.session.commit()

        url = url_for('player.stats', **{'from': '2015-06-01'})
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.json['total_play_time'] == 1700
Example #19
0
    def should_return_total_played_time_of_all_tracks_in_queue(self):
        tracks = TrackFactory.create_batch(3) * 2  # add some duplicates
        db.session.add_all(tracks + TrackFactory.create_batch(2))
        db.session.commit()

        for track in tracks:
            self.redis.rpush(
                config.PLAYLIST_REDIS_KEY,
                json.dumps({
                    'uri': track.spotify_uri,
                    'user': '******'
                }))

        url = url_for('player.queue-meta')
        response = self.client.get(url)

        assert response.status_code == 200
        assert sum(t.duration for t in tracks) == response.json['play_time']
Example #20
0
    def should_should_raise_exception_on_echonest_error(self):
        self.get_track_analysis.side_effect = EchoNestError

        t = TrackFactory()

        db.session.add(t)
        db.session.commit()

        result = update_analysis.delay(t.id)

        assert result.wait() is False
        self.get_track_analysis.assert_called_once_with(t.spotify_uri)
Example #21
0
    def should_return_track_instance(self):
        track = TrackFactory()
        user = UserFactory()

        db.session.add_all([track, user])
        db.session.commit()

        self.redis.get.return_value = json.dumps({
            'uri': track.spotify_uri,
            'user': user.id
        })

        assert CurrentView().get_current_track() == (track, user)
Example #22
0
    def should_udpate_track_analysis(self):

        self.get_track_analysis.return_value = {'danceability': 0.5164314670162907}

        t = TrackFactory()

        db.session.add(t)
        db.session.commit()

        result = update_analysis.delay(t.id)
        track = Track.query.all()[0]

        assert result.wait() is True
        assert track.audio_summary['danceability'] == 0.5164314670162907
        self.get_track_analysis.assert_called_once_with(track.spotify_uri)
Example #23
0
    def should_returns_total_number_of_trucks_in_queue(self):
        tracks = TrackFactory.create_batch(3)
        db.session.add_all(tracks)
        db.session.commit()

        for track in tracks:
            self.redis.rpush(
                config.PLAYLIST_REDIS_KEY,
                json.dumps({
                    'uri': track.spotify_uri,
                    'user': '******'
                }))

        url = url_for('player.queue-meta')
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.json['total'] == 3
Example #24
0
    def should_update_existing_track(self):
        album = AlbumFactory(spotify_uri=TRACK_DATA['album']['uri'])
        track = TrackFactory(name='Foo',
                             spotify_uri=TRACK_DATA['uri'],
                             album=album)

        db.session.add_all([album, track])
        db.session.commit()

        assert Track.query.count() == 1

        add.delay(TRACK_DATA, self.user.id)

        track = Track.query.one()

        assert not track.name == 'Foo'
        assert track.name == TRACK_DATA['name']
        assert track.spotify_uri == TRACK_DATA['uri']
        assert track.album_id == Album.query.first().id
Example #25
0
    def should_returns_list_of_users_total_number_of_tracks_in_queue(self):
        tracks = TrackFactory.create_batch(3)
        users = UserFactory.create_batch(2)
        db.session.add_all(tracks + users)
        db.session.commit()

        users_in_queue = [users[0], users[0], users[1]]
        for track in tracks:
            self.redis.rpush(
                config.PLAYLIST_REDIS_KEY,
                json.dumps({
                    'uri': track.spotify_uri,
                    'user': users_in_queue.pop().id
                }))

        url = url_for('player.queue-meta')
        response = self.client.get(url)

        assert response.status_code == 200
        assert response.json['users'] == {users[0].id: 2, users[1].id: 1}
Example #26
0
    def should_return_non_false_values_when_something_is_in_queue(self):
        tracks = TrackFactory.create_batch(
            3,
            album=AlbumWithArtist(
                artists=[ArtistFactory(genres=GenreFactory.create_batch(2))]))
        db.session.add_all(tracks)
        db.session.commit()

        for track in tracks:
            self.redis.rpush(
                config.PLAYLIST_REDIS_KEY,
                json.dumps({
                    'uri': track.spotify_uri,
                    'user': '******'
                }))
        url = url_for('player.queue-meta')
        response = self.client.get(url)
        assert response.json['total']
        assert response.json['genres']
        assert response.json['users']
        assert response.json['play_time']
Example #27
0
 def should_populate_artists_name(self):
     track = TrackFactory()
     serialized = TrackSerializer().serialize(track)
     assert serialized['artists'][0]['name'], 'artist name is empty'