Graphs and tables for your Spotify account.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
2.2 KiB

  1. from django.test import TestCase
  2. from api.models import Track, Genre, Artist
  3. from login.models import User
  4. from api import utils
  5. import math
  6. import pprint
  7. class GenreDataTestCase(TestCase):
  8. def setUp(self):
  9. test_user = User.objects.create(id="chrisshi", refresh_token="blah", access_token="blah",
  10. access_expires_in=10)
  11. genre = Genre.objects.create(name="classical", num_songs=3)
  12. artist_1 = Artist.objects.create(id='art1', name="Beethoven")
  13. artist_2 = Artist.objects.create(id='art2', name="Mozart")
  14. artist_3 = Artist.objects.create(id='art3', name='Chopin')
  15. track_1 = Track.objects.create(id='track1', year=2013,
  16. popularity=5, runtime=20,
  17. name='concerto1',
  18. genre=genre)
  19. track_1.users.add(test_user)
  20. track_1.artists.add(artist_1)
  21. track_1.artists.add(artist_2)
  22. track_2 = Track.objects.create(id='track2', year=2013,
  23. popularity=5, runtime=20,
  24. name='concerto2',
  25. genre=genre)
  26. track_2.users.add(test_user)
  27. track_2.artists.add(artist_2)
  28. track_2.artists.add(artist_3)
  29. track_2.artists.add(artist_1)
  30. track_3 = Track.objects.create(id='track3', year=2013,
  31. popularity=5, runtime=20,
  32. name='concerto3',
  33. genre=genre)
  34. track_3.users.add(test_user)
  35. track_3.artists.add(artist_1)
  36. track_3.artists.add(artist_3)
  37. def test_get_artist_counts_in_genre(self):
  38. test_user = User.objects.get(id='chrisshi')
  39. artist_counts = utils.get_artists_in_genre(test_user, 'classical', 10)
  40. # pprint.pprint(artist_counts)
  41. self.assertTrue(math.isclose(artist_counts['Beethoven'], 1.3, rel_tol=0.05))
  42. self.assertTrue(math.isclose(artist_counts['Mozart'], 0.85, rel_tol=0.05))
  43. self.assertTrue(math.isclose(artist_counts['Chopin'], 0.85, rel_tol=0.05))
  44. self.assertTrue(math.isclose(sum(artist_counts.values()), 3, rel_tol=0.01))