Skip to content

Commit

Permalink
feat: Add tag validation to ToontjeHoger(Kids)5Tempo rules
Browse files Browse the repository at this point in the history
  • Loading branch information
drikusroor committed Jun 7, 2024
1 parent 7a3bba3 commit 7c8365e
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
56 changes: 56 additions & 0 deletions backend/experiment/rules/tests/test_toontjehoger_5_tempo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from django.test import TestCase

from section.models import Playlist

from experiment.rules.toontjehoger_5_tempo import ToontjeHoger5Tempo


class ToontjeHoger5TempoTest(TestCase):

def test_validate_playlist_valid(self):
csv_data = (
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C3_P2_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C2_P1_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C4_P2_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C4_P2_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C5_P2_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C5_P2_CH,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C4_P1_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C2_P1_CH,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C3_P1_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C2_P2_OR,ch\n"
)
playlist = Playlist.objects.create(name='TestToontjeHoger5Tempo')
playlist.csv = csv_data
playlist.update_sections()

toontje_hoger_5_tempo_rules = ToontjeHoger5Tempo()
self.assertEqual(
toontje_hoger_5_tempo_rules.validate_playlist(playlist), []
)

def test_validate_playlist_invalid_tags(self):
csv_data = (
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,F4_P2_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C4_P9_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C4_P2_ZR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C6_P1_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C1_P3_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C1_P1_OZ,ch\n"
)
playlist = Playlist.objects.create(name='TestToontjeHoger5Tempo')
playlist.csv = csv_data
playlist.update_sections()

toontje_hoger_5_tempo_rules = ToontjeHoger5Tempo()
self.assertEqual(
toontje_hoger_5_tempo_rules.validate_playlist(playlist),
[
'Invalid tag: C1_P1_OZ',
'Invalid tag: C1_P3_OR',
'Invalid tag: C4_P2_ZR',
'Invalid tag: C4_P9_OR',
'Invalid tag: C6_P1_OR',
'Invalid tag: F4_P2_OR',
]
)
40 changes: 40 additions & 0 deletions backend/experiment/rules/tests/test_toontjehoger_kids_5_tempo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.test import TestCase

from section.models import Playlist

from experiment.rules.toontjehogerkids_5_tempo import ToontjeHogerKids5Tempo


class ToontjeHogerKids5TempoTest(TestCase):

# Toontje Hoger Kids 5 Tempo does not have the strict tag validation
# that Toontje Hoger 5 Tempo has. Therefore, we must ensure that
# the validate_playlist method does not raise any errors for tags
# that would be considered invalid in Toontje Hoger 5 Tempo.
def test_validate_playlist_valid(self):
csv_data = (
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C3_P2_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C2_P1_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C4_P2_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C4_P2_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C5_P2_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C5_P2_CH,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C4_P1_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C2_P1_CH,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C3_P1_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C2_P2_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,F4_P2_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C4_P9_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C4_P2_ZR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C6_P1_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C1_P3_OR,ch\n"
"Albania 2018 - Eugent Bushpepa,Mall,7.046,45.0,Eurovision/Set2/Karaoke/2018-11-00-07-046-k.mp3,C1_P1_OZ,ch\n"
)
playlist = Playlist.objects.create(name='TestToontjeHoger5Tempo')
playlist.csv = csv_data
playlist.update_sections()

toontje_hoger_5_tempo_rules = ToontjeHogerKids5Tempo()
self.assertEqual(
toontje_hoger_5_tempo_rules.validate_playlist(playlist), []
)
26 changes: 25 additions & 1 deletion backend/experiment/rules/toontjehoger_5_tempo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import logging
import random
from os.path import join
Expand All @@ -8,6 +9,7 @@
from experiment.actions.frontend_style import FrontendStyle, EFrontendStyle
from experiment.actions.playback import Multiplayer
from experiment.actions.styles import STYLE_NEUTRAL_INVERTED
from section.models import Playlist
from .base import Base
from experiment.utils import create_player_labels, non_breaking_spaces

Expand Down Expand Up @@ -83,7 +85,7 @@ def get_random_section_pair(self, session, genre):
valid_tag = False
tag_base = ""
tag_original = ""
while(not valid_tag):
while (not valid_tag):
track = random.choice([1, 2, 3, 4, 5])
pair = random.choice([1, 2])
tag_base = "{}{}_P{}_".format(genre.upper(), track, pair, )
Expand Down Expand Up @@ -253,3 +255,25 @@ def get_final_round(self, session):
)

return [*score, final, info]

def validate_tags(self, tags):

errors = []

for tag in tags:
if not re.match(r'^[CJR][1-5]_P[12]_(OR|CH)$', tag):
errors.append("Invalid tag: {}".format(tag))

return errors

def validate_playlist(self, playlist: Playlist):

errors = super().validate_playlist(playlist)
sections = playlist.section_set.all()

tags = sorted(list(set([section.tag for section in sections])))

# Check if all tags are valid
errors += self.validate_tags(tags)

return errors
4 changes: 4 additions & 0 deletions backend/experiment/rules/toontjehogerkids_5_tempo.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,7 @@ def get_final_round(self, session):
)

return [*score, final, info]

def validate_tags(self, tags):
# No validation needed foor TH5 Kids
return []

0 comments on commit 7c8365e

Please sign in to comment.