-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add certificate_type to courses and programs serializer (#2307)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
6bbb241
commit 6bc1b1f
Showing
4 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import pytest | ||
from django.contrib.auth.models import AnonymousUser | ||
|
||
from cms.serializers import CoursePageSerializer | ||
from courses.factories import ( | ||
CourseRunEnrollmentFactory, | ||
CourseRunFactory, | ||
ProgramFactory, | ||
) | ||
from courses.models import CoursesTopic, Department | ||
from courses.serializers.v1.base import BaseProgramSerializer | ||
from courses.serializers.v2.courses import ( | ||
CourseRunSerializer, | ||
CourseWithCourseRunsSerializer, | ||
) | ||
from main.test_utils import assert_drf_json_equal | ||
|
||
pytestmark = [pytest.mark.django_db] | ||
|
||
|
||
@pytest.mark.parametrize("is_anonymous", [True, False]) | ||
@pytest.mark.parametrize("all_runs", [True, False]) | ||
@pytest.mark.parametrize( | ||
"certificate_type", ["MicroMasters Credential", "Certificate of Completion"] | ||
) | ||
def test_serialize_course( | ||
mocker, mock_context, is_anonymous, all_runs, certificate_type | ||
): | ||
"""Test Course serialization""" | ||
if is_anonymous: | ||
mock_context["request"].user = AnonymousUser() | ||
if all_runs: | ||
mock_context["all_runs"] = True | ||
user = mock_context["request"].user | ||
courseRun1 = CourseRunFactory.create() | ||
courseRun2 = CourseRunFactory.create(course=courseRun1.course) | ||
course = courseRun1.course | ||
topics = [CoursesTopic.objects.create(name=f"topic{num}") for num in range(3)] | ||
course.page.topics.set([topics[0], topics[1], topics[2]]) | ||
department = "a course departments" | ||
course.departments.set([Department.objects.create(name=department)]) | ||
program = ProgramFactory.create(program_type="Series") | ||
if certificate_type == "MicroMasters Credential": | ||
program.program_type = "MicroMasters®" | ||
program.add_requirement(course) | ||
program.save() | ||
|
||
CourseRunEnrollmentFactory.create( | ||
run=courseRun1, **({} if is_anonymous else {"user": user}) | ||
) | ||
|
||
data = CourseWithCourseRunsSerializer(instance=course, context=mock_context).data | ||
|
||
assert_drf_json_equal( | ||
data, | ||
{ | ||
"title": course.title, | ||
"readable_id": course.readable_id, | ||
"id": course.id, | ||
"courseruns": [ | ||
CourseRunSerializer(courseRun1).data, | ||
CourseRunSerializer(courseRun2).data, | ||
], | ||
"next_run_id": course.first_unexpired_run.id, | ||
"departments": [{"name": department}], | ||
"page": CoursePageSerializer(course.page).data, | ||
"certificate_type": certificate_type, | ||
"topics": [{"name": topic.name} for topic in topics], | ||
"programs": BaseProgramSerializer(course.programs, many=True).data | ||
if all_runs | ||
else None, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters