test_aggregators.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from datetime import datetime, timedelta
  2. import pytest
  3. import time_machine
  4. from django.contrib.auth import get_user_model
  5. from django.urls import reverse
  6. from django.utils import timezone
  7. from music.aggregators import live_charts, scrobble_counts, week_of_scrobbles
  8. from profiles.models import UserProfile
  9. from scrobbles.models import Scrobble
  10. def build_scrobbles(client, request_data, num=7, spacing=2):
  11. url = reverse("scrobbles:mopidy-webhook")
  12. user = get_user_model().objects.create(username="Test User")
  13. UserProfile.objects.create(user=user, timezone="US/Eastern")
  14. for i in range(num):
  15. client.post(url, request_data, content_type="application/json")
  16. s = Scrobble.objects.last()
  17. s.user = user
  18. s.timestamp = timezone.now() - timedelta(days=i * spacing)
  19. s.played_to_completion = True
  20. s.save()
  21. @pytest.mark.django_db
  22. @time_machine.travel(datetime(2022, 3, 4, 1, 24))
  23. def test_scrobble_counts_data(client, mopidy_track_request_data):
  24. build_scrobbles(client, mopidy_track_request_data)
  25. user = get_user_model().objects.first()
  26. count_dict = scrobble_counts(user)
  27. assert count_dict == {
  28. "alltime": 7,
  29. "month": 2,
  30. "today": 1,
  31. "week": 3,
  32. "year": 7,
  33. }
  34. @pytest.mark.django_db
  35. def test_week_of_scrobbles_data(client, mopidy_track_request_data):
  36. build_scrobbles(client, mopidy_track_request_data, 7, 1)
  37. user = get_user_model().objects.first()
  38. week = week_of_scrobbles(user)
  39. assert list(week.values()) == [1, 1, 1, 1, 1, 1, 1]
  40. @pytest.mark.django_db
  41. def test_top_tracks_by_day(client, mopidy_track_request_data):
  42. build_scrobbles(client, mopidy_track_request_data, 7, 1)
  43. user = get_user_model().objects.first()
  44. tops = live_charts(user)
  45. assert tops[0].title == "Same in the End"
  46. @pytest.mark.django_db
  47. def test_top_tracks_by_week(client, mopidy_track_request_data):
  48. build_scrobbles(client, mopidy_track_request_data, 7, 1)
  49. user = get_user_model().objects.first()
  50. tops = live_charts(user, chart_period="week")
  51. assert tops[0].title == "Same in the End"
  52. @pytest.mark.django_db
  53. def test_top_tracks_by_month(client, mopidy_track_request_data):
  54. build_scrobbles(client, mopidy_track_request_data, 7, 1)
  55. user = get_user_model().objects.first()
  56. tops = live_charts(user, chart_period="month")
  57. assert tops[0].title == "Same in the End"
  58. @pytest.mark.django_db
  59. def test_top_tracks_by_year(client, mopidy_track_request_data):
  60. build_scrobbles(client, mopidy_track_request_data, 7, 1)
  61. user = get_user_model().objects.first()
  62. tops = live_charts(user, chart_period="year")
  63. assert tops[0].title == "Same in the End"
  64. @pytest.mark.django_db
  65. def test_top__artists_by_week(client, mopidy_track_request_data):
  66. build_scrobbles(client, mopidy_track_request_data, 7, 1)
  67. user = get_user_model().objects.first()
  68. tops = live_charts(user, chart_period="week", media_type="Artist")
  69. assert tops[0].name == "Sublime"
  70. @pytest.mark.django_db
  71. def test_top__artists_by_month(client, mopidy_track_request_data):
  72. build_scrobbles(client, mopidy_track_request_data, 7, 1)
  73. user = get_user_model().objects.first()
  74. tops = live_charts(user, chart_period="month", media_type="Artist")
  75. assert tops[0].name == "Sublime"
  76. @pytest.mark.django_db
  77. def test_top__artists_by_year(client, mopidy_track_request_data):
  78. build_scrobbles(client, mopidy_track_request_data, 7, 1)
  79. user = get_user_model().objects.first()
  80. tops = live_charts(user, chart_period="year", media_type="Artist")
  81. assert tops[0].name == "Sublime"