Skip to content

Commit

Permalink
refactor: parameterized tests are used
Browse files Browse the repository at this point in the history
  • Loading branch information
marslanabdulrauf committed Feb 27, 2025
1 parent 22b0632 commit 39bf49d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
6 changes: 4 additions & 2 deletions cms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1508,19 +1508,21 @@ def product(self):
return self.course

def clean(self):
"""Validates that there is only one internal or external course page per course"""
if (
isinstance(self, CoursePage)
and ExternalCoursePage.objects.filter(course=self.course).exists()
):
raise ValidationError(
"There is already an external course page associated with this course."
{"course": "There is already an external course page associated with this course."}

)
elif (
isinstance(self, ExternalCoursePage)
and CoursePage.objects.filter(course=self.course).exists()
):
raise ValidationError(
"There is already an internal course page associated with this course."
{"course": "There is already an internal course page associated with this course."}
)

super().clean()
Expand Down
37 changes: 14 additions & 23 deletions cms/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2424,31 +2424,22 @@ def test_child_page_with_static_pages_with_platform(superuser_client):
assert for_teams_page.title == b2b_page.title


def test_external_course_page_with_same_course_run_in_internal_course_page():
"""
Tests that a CoursePage with the same course run as another CoursePage.
"""
course_page = CoursePageFactory.create()

with pytest.raises(ValidationError) as context:
ExternalCoursePageFactory.create(course=course_page.course)

assert (
str(context.value)
== "{'__all__': ['There is already an internal course page associated with this course.']}"
)


def test_internal_course_page_with_same_course_run_in_external_course_page():
@pytest.mark.parametrize(
"existing_course_factory, new_course_factory, expected_error",
[
(CoursePageFactory, ExternalCoursePageFactory,
"{'course': ['There is already an internal course page associated with this course.']}"),
(ExternalCoursePageFactory, CoursePageFactory,
"{'course': ['There is already an external course page associated with this course.']}"),
]
)
def test_prevent_duplicate_pages_with_same_course(existing_course_factory, new_course_factory, expected_error):
"""
Tests that an ExternalCoursePage with the same course run as another ExternalCoursePage.
Tests that an error is raised when trying to create a duplicate course page.
"""
external_course_page = ExternalCoursePageFactory.create()
course_page = existing_course_factory.create()

with pytest.raises(ValidationError) as context:
CoursePageFactory.create(course=external_course_page.course)
new_course_factory.create(course=course_page.course)

assert (
str(context.value)
== "{'__all__': ['There is already an external course page associated with this course.']}"
)
assert (str(context.value) == expected_error)

0 comments on commit 39bf49d

Please sign in to comment.