Browse Source

Make genre data test more comprehensive

Modified the genre data retrieval test to be more comprehensive.
master
Chris Shyi 6 years ago
parent
commit
cc071d621c
  1. 45
      api/tests.py
  2. 9
      api/views.py

45
api/tests.py

@ -7,7 +7,8 @@ import pprint
class GenreDataTestCase(TestCase):
def setUp(self):
@classmethod
def setUpTestData(cls):
test_user = User.objects.create(id="chrisshi", refresh_token="blah", access_token="blah",
access_expires_in=10)
genre = Genre.objects.create(name="classical", num_songs=3)
@ -40,11 +41,49 @@ class GenreDataTestCase(TestCase):
track_3.artists.add(artist_1)
track_3.artists.add(artist_3)
def test_get_artist_counts_in_genre(self):
pop_genre = Genre.objects.create(name='pop', num_songs=3)
pop_artist1 = Artist.objects.create(id='art4', name="Taylor Swift")
pop_artist2 = Artist.objects.create(id='art5', name="Justin Bieber")
pop_artist3 = Artist.objects.create(id='art6', name="Rihanna")
pop_track_1 = Track.objects.create(id='track4', year=2013,
popularity=5, runtime=20,
name='poptrack1',
genre=pop_genre)
pop_track_1.users.add(test_user)
pop_track_1.artists.add(pop_artist1)
pop_track_1.artists.add(pop_artist2)
pop_track_2 = Track.objects.create(id='track5', year=2013,
popularity=5, runtime=20,
name='poptrack2',
genre=pop_genre)
pop_track_2.users.add(test_user)
pop_track_2.artists.add(pop_artist3)
pop_track_2.artists.add(pop_artist2)
pop_track_2.artists.add(pop_artist1)
pop_track_3 = Track.objects.create(id='track6', year=2013,
popularity=5, runtime=20,
name='poptrack3',
genre=pop_genre)
pop_track_3.users.add(test_user)
pop_track_3.artists.add(pop_artist3)
pop_track_3.artists.add(pop_artist2)
pop_track_3.artists.add(pop_artist1)
def test_get_artist_counts_two_genres(self):
test_user = User.objects.get(id='chrisshi')
artist_counts = utils.get_artists_in_genre(test_user, 'classical', 10)
artist_counts = utils.get_artists_in_genre(test_user, 'classical')
# pprint.pprint(artist_counts)
self.assertTrue(math.isclose(artist_counts['Beethoven'], 1.3, rel_tol=0.05))
self.assertTrue(math.isclose(artist_counts['Mozart'], 0.85, rel_tol=0.05))
self.assertTrue(math.isclose(artist_counts['Chopin'], 0.85, rel_tol=0.05))
self.assertTrue(math.isclose(sum(artist_counts.values()), 3, rel_tol=0.01))
# test the pop genre
artist_counts = utils.get_artists_in_genre(test_user, 'pop')
self.assertTrue(math.isclose(artist_counts['Taylor Swift'], 1.125, rel_tol=0.05))
self.assertTrue(math.isclose(artist_counts['Justin Bieber'], 1.125, rel_tol=0.05))
self.assertTrue(math.isclose(artist_counts['Rihanna'], 0.75, rel_tol=0.05))
self.assertTrue(math.isclose(sum(artist_counts.values()), 3, rel_tol=0.01))

9
api/views.py

@ -23,9 +23,9 @@ ARTIST_LIMIT = 50
FEATURES_LIMIT = 100
# ARTIST_LIMIT = 25
# FEATURES_LIMIT = 25
TRACKS_TO_QUERY = 100
TRACKS_TO_QUERY = 300
console_logging = True
CONSOLE_LOGGING = True
# parse_library {{{ #
@ -53,9 +53,6 @@ def parse_library(request, user_secret):
headers=user_headers,
params=payload).json()['items']
if console_logging:
tracks_processed = 0
tracks_processed = 0
for track_dict in saved_tracks_response:
# add artists {{{ #
@ -92,7 +89,7 @@ def parse_library(request, user_secret):
# }}} add audio features #
if console_logging:
if CONSOLE_LOGGING:
tracks_processed += 1
print("Added track #{}: {} - {}".format(
offset + tracks_processed,

Loading…
Cancel
Save