Skip to content

Commit

Permalink
chore: add override testcases for Program model
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza-56 committed Sep 12, 2024
1 parent 8b5f4b7 commit c06627c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
3 changes: 2 additions & 1 deletion course_discovery/apps/course_metadata/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3564,7 +3564,8 @@ def active_subjects(self):
if self.primary_subject_override not in subjects:
subjects = [self.primary_subject_override] + subjects
else:
subjects = [self.primary_subject_override] + [subject for subject in subjects if subject != self.primary_subject_override]
subjects = [self.primary_subject_override] + \
[subject for subject in subjects if subject != self.primary_subject_override]

return subjects

Expand Down
59 changes: 59 additions & 0 deletions course_discovery/apps/course_metadata/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3519,6 +3519,65 @@ def test_program_duration_override(self):
self.program.program_duration_override = ''
assert self.program.program_duration_override is not None

def test_active_subjects_with_no_override(self):
"""
Test that active_subjects returns the subjects from the associated courses
when no primary_subject_override is set.
"""

subject1 = SubjectFactory.create(name='Subject 1')
subject2 = SubjectFactory.create(name='Subject 2')
course1 = CourseFactory.create(subjects=[subject1])
course2 = CourseFactory.create(subjects=[subject2])
program = ProgramFactory.create(primary_subject_override=None, courses=[course1, course2])

expected_subjects = [subject1, subject2]
self.assertEqual(program.active_subjects, expected_subjects)

def test_active_subjects_with_primary_subject_override(self):
"""
Test that active_subjects includes the primary_subject_override at the beginning
when it is set.
"""
primary_subject_override = SubjectFactory.create(name='Primary Subject')
other_subject = SubjectFactory.create(name='Other Subject')
course = CourseFactory.create(subjects=[other_subject])

program = ProgramFactory.create(primary_subject_override=primary_subject_override, courses=[course])

expected_subjects = [primary_subject_override, other_subject]
self.assertEqual(program.active_subjects, expected_subjects)

def test_active_languages_with_no_override(self):
"""
Test that active_languages returns the languages from the associated courses
when no language_override is set.
"""

language_en = LanguageTag.objects.create(code='en', name='English')
language_fr = LanguageTag.objects.get(code='fr')

course_run1 = CourseRunFactory.create(language=language_en)
course_run2 = CourseRunFactory.create(language=language_fr)

program = ProgramFactory.create(language_override=None, courses=[course_run1.course, course_run2.course])

expected_languages = {language_en, language_fr}
self.assertEqual(program.active_languages, expected_languages)

def test_active_languages_with_language_override(self):
"""
Test that active_languages returns the language_override when it is set.
"""

language_es = LanguageTag.objects.get(code='es')
language_de = LanguageTag.objects.get(code='de')
course_run = CourseRunFactory.create(language=language_de)
program = ProgramFactory.create(language_override=language_es, courses=[course_run.course])

expected_languages = {language_es}
self.assertEqual(program.active_languages, expected_languages)


class ProgramSubscriptionTests(TestCase):

Expand Down

0 comments on commit c06627c

Please sign in to comment.