Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check correctness of get_preferred_songs function #680

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions backend/experiment/rules/musical_preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ def feedback_info(self):
return info

def get_preferred_songs(self, result_set, n=5):
top_songs = result_set.values('section').annotate(
avg_score=Avg('score')).order_by('-score')[:n]
top_results = result_set.annotate(
avg_score=Avg('score')).order_by('score')[:n]
out_list = []
for s in top_songs:
section = Section.objects.get(pk=s.get('section'))
for result in top_results.all():
section = Section.objects.get(pk=result.section.id)
out_list.append({'artist': section.song.artist,
'name': section.song.name})
return out_list
47 changes: 47 additions & 0 deletions backend/experiment/rules/tests/test_musical_preferences.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from django.test import TestCase
from django.db.models import Avg

from experiment.rules.musical_preferences import MusicalPreferences

from experiment.models import Experiment
from participant.models import Participant
from result.models import Result
from section.models import Playlist
from session.models import Session

class MusicalPreferencesTest(TestCase):
fixtures = ['playlist', 'experiment']

@classmethod
def setUpTestData(cls):
cls.participant = Participant.objects.create()
cls.playlist = Playlist.objects.create(name='MusicalPrefences')
csv = ("SuperArtist,SuperSong,0.0,10.0,bat/artist1.mp3,0,0,0\n"
"SuperArtist,MehSong,0.0,10.0,bat/artist2.mp3,0,0,0\n"
"MehArtist,MehSong,0.0,10.0,bat/artist3.mp3,0,0,0\n"
"AwfulArtist,MehSong,0.0,10.0,bat/artist4.mp3,0,0,0\n"
"AwfulArtist,AwfulSong,0.0,10.0,bat/artist5.mp3,0,0,0\n")
cls.playlist.csv = csv
cls.playlist.update_sections()
cls.experiment = Experiment.objects.create(name='MusicalPreferences', rounds=5)
cls.session = Session.objects.create(
experiment=cls.experiment,
participant=cls.participant,
playlist=cls.playlist
)

def test_preferred_songs(self):
for index, section in enumerate(list(self.playlist.section_set.all())):
Result.objects.create(
question_key='like_song',
score=5-index,
section=section,
session=self.session
)
mp = MusicalPreferences()
preferred_sections = mp.get_preferred_songs(
self.session.result_set.order_by('?'), 3)
assert preferred_sections[0]['artist'] == 'SuperArtist'
assert preferred_sections[1]['name'] == 'MehSong'
assert preferred_sections[2]['artist'] == 'MehArtist'
assert 'AwfulArtist' not in [p['artist'] for p in preferred_sections]
Loading