Skip to content

Commit

Permalink
Hotfix(mpref): Fix preferred songs without section bug (#708) (#712)
Browse files Browse the repository at this point in the history
* fix(muspref): Only fetch results with existing sections

* test(mpref): Add test case for preferred songs without section

* refactor: Remove unnecessary filters from the session's result set queries

* test: Don't create the sectionless results connected to the current session

(cherry picked from commit d502f9b)
  • Loading branch information
drikusroor authored Jan 16, 2024
1 parent 9999fc1 commit 3e32b37
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
3 changes: 2 additions & 1 deletion backend/experiment/rules/musical_preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ def next_round(self, session, request_session=None):
known_songs = session.result_set.filter(
question_key='know_song', score=2).count()
all_results = Result.objects.filter(
question_key='like_song'
question_key='like_song',
section_id__isnull=False
)
top_participant = self.get_preferred_songs(like_results, 3)
top_all = self.get_preferred_songs(all_results, 3)
Expand Down
35 changes: 34 additions & 1 deletion backend/experiment/rules/tests/test_musical_preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,37 @@ def test_preferred_songs(self):
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]
assert 'AwfulArtist' not in [p['artist'] for p in preferred_sections]

def test_preferred_songs_results_without_section(self):
# Create 3 results with a section
for index, section in enumerate(list(self.playlist.section_set.all())):
if index < 3:
Result.objects.create(
question_key='like_song',
score=5-index,
section=section,
session=self.session
)

other_session = Session.objects.create(
experiment=self.experiment,
participant=self.participant,
playlist=self.playlist
)

for i in range(10):
Result.objects.create(
question_key='like_song',
score=5-i,
section=None,
session=other_session
)
mp = MusicalPreferences()

# Go to the last round (top_all = ... caused the error)
for i in range(self.session.experiment.rounds + 1):
self.session.increment_round()

# get_preferred_songs() called by top_all = ... in the final round should not raise an error
mp.next_round(self.session)

0 comments on commit 3e32b37

Please sign in to comment.