Skip to content

Commit

Permalink
test: Add test cases for CommonExternalCoursePages and its creation
Browse files Browse the repository at this point in the history
  • Loading branch information
marslanabdulrauf committed Nov 28, 2024
1 parent 4be17b2 commit 3531794
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 22 deletions.
34 changes: 33 additions & 1 deletion cms/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
BlogIndexPage,
CatalogPage,
CertificatePage,
CommonExternalCoursePage,
CompaniesLogoCarouselSection,
CourseIndexPage,
CoursePage,
Expand All @@ -28,6 +29,7 @@
ExternalCoursePage,
ExternalProgramPage,
FacultyMembersPage,
ForTeamsExternalCoursePage,
ForTeamsPage,
FrequentlyAskedQuestion,
FrequentlyAskedQuestionPage,
Expand All @@ -36,6 +38,7 @@
LearningJourneySection,
LearningOutcomesPage,
LearningStrategyFormSection,
LearningTechniquesExternalCoursePage,
LearningTechniquesPage,
NewsAndEventsBlock,
NewsAndEventsPage,
Expand All @@ -52,7 +55,7 @@
WebinarPage,
WhoShouldEnrollPage,
)
from courses.factories import CourseFactory, ProgramFactory
from courses.factories import CourseFactory, PlatformFactory, ProgramFactory

factory.Faker.add_provider(internet)

Expand Down Expand Up @@ -576,3 +579,32 @@ class LearningStrategyFormPageFactory(wagtail_factories.PageFactory):

class Meta:
model = LearningStrategyFormSection


class CommonExternalCoursePageFactory(wagtail_factories.PageFactory):
"""CommonExternalCoursePage factory class"""

title = factory.fuzzy.FuzzyText()

class Meta:
model = CommonExternalCoursePage


class LearningTechniqueExternalCoursPageFactory(LearningTechniquesPageFactory):
"""LearningTechniqueExternalCoursPage factory class"""

platform = factory.SubFactory(PlatformFactory)
title = factory.fuzzy.FuzzyText(prefix=f"{platform} - ")

class Meta:
model = LearningTechniquesExternalCoursePage


class ForTeamsExternalCoursePageFactory(ForTeamsPageFactory):
"""ForTeamsExternalCoursePage factory class"""

platform = factory.SubFactory(PlatformFactory)
title = factory.fuzzy.FuzzyText(prefix=f"{platform} - ")

class Meta:
model = ForTeamsExternalCoursePage
184 changes: 166 additions & 18 deletions cms/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
from wagtail.test.utils.form_data import querydict_from_html

from cms.constants import (
B2B_SECTION,
COMMON_EXTERNAL_COURSE_SLUG,
FORMAT_HYBRID,
FORMAT_ONLINE,
FORMAT_OTHER,
HOW_YOU_WILL_LEARN_SECTION,
ON_DEMAND_WEBINAR,
ON_DEMAND_WEBINAR_BUTTON_TITLE,
UPCOMING_WEBINAR,
Expand All @@ -27,13 +26,16 @@
)
from cms.factories import (
CertificatePageFactory,
CommonExternalCoursePageFactory,
CompaniesLogoCarouselPageFactory,
CourseIndexPageFactory,
CoursePageFactory,
CoursesInProgramPageFactory,
EnterprisePageFactory,
ExternalCoursePageFactory,
ExternalProgramPageFactory,
FacultyMembersPageFactory,
ForTeamsExternalCoursePageFactory,
ForTeamsPageFactory,
FrequentlyAskedQuestionFactory,
FrequentlyAskedQuestionPageFactory,
Expand All @@ -42,8 +44,10 @@
LearningJourneyPageFactory,
LearningOutcomesPageFactory,
LearningStrategyFormPageFactory,
LearningTechniqueExternalCoursPageFactory,
LearningTechniquesPageFactory,
NewsAndEventsPageFactory,
PlatformFactory,
ProgramFactory,
ProgramPageFactory,
ResourcePageFactory,
Expand All @@ -59,6 +63,7 @@
)
from cms.models import (
CertificatePage,
CommonExternalCoursePage,
CourseIndexPage,
CoursesInProgramPage,
ExternalCoursePage,
Expand Down Expand Up @@ -2095,21 +2100,26 @@ def test_certificatepage_saved_no_signatories_external_courseware(
assert resp.status_code == 302


def test_external_course_page_auto_created_sections(superuser_client):
def _create_external_course_page(superuser_client, course_id, slug):
"""
Creating ExternalCoursePage should also create sub pages:
1. LearningTechniquesPage
2. ForTeamsPage
Creates and publishes an ExternalCoursePage via the Wagtail admin API.
Args:
superuser_client (Client): Superuser client to send the API request.
course_id (int): ID of the course to associate with the page.
slug (str): Slug for the new ExternalCoursePage.
Asserts:
Response status code is 302 (successful redirection).
"""
external_course_page_slug = "icon-grid-6064"
course = CourseFactory.create()

post_data = {
"course": course.id,
"course": course_id,
"title": "Icon Grid #6064",
"subhead": "testing #6064",
"format": "Online",
"content-count": 0,
"slug": external_course_page_slug,
"slug": slug,
"action-publish": "action-publish",
}
response = superuser_client.post(
Expand All @@ -2119,12 +2129,27 @@ def test_external_course_page_auto_created_sections(superuser_client):
),
post_data,
)

assert response.status_code == 302
external_course_page = ExternalCoursePage.objects.get(
slug=external_course_page_slug
)
assert external_course_page.course.id == course.id


def _is_static_pages_created(external_course_slug, course_id):
"""
Validates the creation of static child pages under an ExternalCoursePage.
Args:
external_course_slug (str): The slug of the ExternalCoursePage.
course_id (int): The ID of the associated course.
Asserts:
- The ExternalCoursePage matches the given course ID.
- At least two child pages exist.
- A `LearningTechniquesPage` and a `ForTeamsPage` are present as child pages.
Returns:
tuple: The `LearningTechniquesPage` and `ForTeamsPage` child pages.
"""
external_course_page = ExternalCoursePage.objects.get(slug=external_course_slug)
assert external_course_page.course.id == course_id
assert len(external_course_page.child_pages) >= 2
learning_technical_page = (
external_course_page.get_child_page_of_type_including_draft(
Expand All @@ -2136,6 +2161,129 @@ def test_external_course_page_auto_created_sections(superuser_client):
)
assert learning_technical_page
assert for_teams_page
assert learning_technical_page.title == HOW_YOU_WILL_LEARN_SECTION["title"]
assert len(learning_technical_page.technique_items) == 6
assert for_teams_page.title == B2B_SECTION["title"]

return learning_technical_page, for_teams_page


def _create_static_external_course_pages(platform=None):
"""
Creates static external course pages under a CommonExternalCoursePage.
Args:
platform (Platform, optional): Optional platform object for customizing
attributes like headings. Defaults to None.
Returns:
tuple:
- `LearningTechniqueExternalCoursPage` with platform-specific attributes.
- `ForTeamsExternalCoursePage` under the same parent page.
"""
common_external_pages = CommonExternalCoursePageFactory.create()
tech_heading = f"{platform.name} - heading" if platform else "heading"
learning_tech_page = LearningTechniqueExternalCoursPageFactory.create(
platform=platform,
technique_items__0__techniques__heading=tech_heading,
technique_items__0__techniques__sub_heading="sub_heading",
technique_items__0__techniques__image__image__title="image-title",
parent=common_external_pages,
)
b2b_page = ForTeamsExternalCoursePageFactory.create(
platform=platform, parent=common_external_pages
)
return learning_tech_page, b2b_page


def test_common_external_course_pages():
"""
Tests the creation of a CommonExternalCoursePage and its relationship
to a CourseIndexPage.
"""
course_page = CourseIndexPageFactory.create()
assert CommonExternalCoursePage.can_create_at(course_page)
common_folder = CommonExternalCoursePageFactory.create(
title="common external course pages"
)
assert common_folder.slug == COMMON_EXTERNAL_COURSE_SLUG
assert common_folder.title == "common external course pages"


def test_external_course_page_wo_static_page(superuser_client):
"""Tests that an ExternalCoursePage is created without static child pages."""
external_course_page_slug = "icon-grid-6064"
course = CourseFactory.create()
_create_external_course_page(superuser_client, course.id, external_course_page_slug)
external_course_page = ExternalCoursePage.objects.get(
slug=external_course_page_slug
)
assert external_course_page.course.id == course.id
assert not external_course_page.get_child_page_of_type_including_draft(
LearningTechniquesPage
)
assert not external_course_page.get_child_page_of_type_including_draft(ForTeamsPage)


def test_external_course_page_with_static_pages(superuser_client):
"""Tests the creation of an ExternalCoursePage with static child pages."""
platform = PlatformFactory.create()
course = CourseFactory.create(platform=platform)
learning_tech_page, b2b_page = _create_static_external_course_pages(platform)
external_course_page_slug = "icon-grid-6064"
_create_external_course_page(superuser_client, course.id, external_course_page_slug)

learning_technical_page, for_teams_page = _is_static_pages_created(
external_course_page_slug, course.id
)

assert learning_technical_page.title == learning_tech_page.title
assert learning_technical_page.technique_items == learning_tech_page.technique_items
assert for_teams_page.title == b2b_page.title


def test_external_course_page_with_static_pages_wo_platform(superuser_client):
"""
Tests the creation of an ExternalCoursePage and its static child pages
without a platform.
"""
platform = PlatformFactory.create()
learning_tech_page, b2b_page = _create_static_external_course_pages()

course = CourseFactory.create(platform=platform)
external_course_page_slug = "icon-grid-6064"
_create_external_course_page(superuser_client, course.id, external_course_page_slug)
learning_technical_page, for_teams_page = _is_static_pages_created(
external_course_page_slug, course.id
)

assert learning_technical_page.title == learning_tech_page.title
assert learning_technical_page.technique_items == learning_tech_page.technique_items
assert for_teams_page.title == b2b_page.title


def test_external_course_page_with_static_pages_with_and_wo_platform(superuser_client):
"""
Tests the creation of an ExternalCoursePage with static child pages,
comparing the results with and without a platform.
"""
platform = PlatformFactory.create()
learning_tech_page_no_platform, b2b_page_no_platform = (
_create_static_external_course_pages()
)
learning_tech_page, b2b_page = _create_static_external_course_pages(platform)

course = CourseFactory.create(platform=platform)
external_course_page_slug = "icon-grid-6064"
_create_external_course_page(superuser_client, course.id, external_course_page_slug)
learning_technical_page, for_teams_page = _is_static_pages_created(
external_course_page_slug, course.id
)

assert learning_technical_page.title != learning_tech_page_no_platform.title
assert learning_technical_page.title == learning_tech_page.title
assert (
learning_technical_page.technique_items
!= learning_tech_page_no_platform.technique_items
)
assert learning_technical_page.technique_items == learning_tech_page.technique_items

assert for_teams_page.title != b2b_page_no_platform.title
assert for_teams_page.title == b2b_page.title
12 changes: 9 additions & 3 deletions cms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ def create_how_you_will_learn_section(platform=None):

learning_tech_page = learning_tech_page.first()
if not learning_tech_page:
learning_tech_page = LearningTechniquesExternalCoursePage.objects.get(
platform__isnull=True
learning_tech_page = (
LearningTechniquesExternalCoursePage.objects.filter(
platform__isnull=True
).first()
or None
)

return (
Expand All @@ -34,7 +37,10 @@ def create_b2b_section(platform=None):

b2b_page = b2b_page.first()
if not b2b_page:
b2b_page = ForTeamsExternalCoursePage.objects.get(platform__isnull=True)
b2b_page = (
ForTeamsExternalCoursePage.objects.filter(platform__isnull=True).first()
or None
)

return (
ForTeamsPage(
Expand Down

0 comments on commit 3531794

Please sign in to comment.