Skip to content

Commit

Permalink
Updated v1 course API to only pull live programs; added tests (#1989)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkachel authored Nov 8, 2023
1 parent a0be7b1 commit 28f0fcf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
10 changes: 9 additions & 1 deletion courses/serializers/v1/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ def get_programs(self, instance):
if self.context.get("all_runs", False):
from courses.serializers.v1.base import BaseProgramSerializer

return BaseProgramSerializer(instance.programs, many=True).data
programs = (
models.Program.objects.select_related("page")
.filter(pk__in=[program.id for program in instance.programs])
.filter(live=True)
.filter(page__live=True)
.all()
)

return BaseProgramSerializer(programs, many=True).data

return None

Expand Down
49 changes: 49 additions & 0 deletions courses/views/v1/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,55 @@ def test_get_course(
assert_drf_json_equal(course_data, course_from_fixture, ignore_order=True)


@pytest.mark.parametrize("course_catalog_course_count", [1], indirect=True)
@pytest.mark.parametrize("course_catalog_program_count", [1], indirect=True)
@pytest.mark.parametrize("program_is_live", [True, False])
@pytest.mark.parametrize("program_page_is_live", [True, False])
def test_get_course_by_readable_id(
user_drf_client,
course_catalog_data,
mock_context,
django_assert_max_num_queries,
program_is_live,
program_page_is_live,
):
"""Test the view that handles a request for single Course"""
courses, _, _ = course_catalog_data
course = courses[0]

if not program_is_live:
course.programs[0].live = False
course.programs[0].save()

if not program_page_is_live:
course.programs[0].page.live = False
course.programs[0].page.save()

num_queries = num_queries_from_course(course, "v1")
with django_assert_max_num_queries(num_queries) as context:
resp = user_drf_client.get(
reverse("v1:courses_api-list"),
{"readable_id": course.readable_id, "live": True},
)
duplicate_queries_check(context)
course_data = resp.json()
course_from_fixture = dict(
CourseWithCourseRunsSerializer(
instance=course,
context={
**mock_context,
"all_runs": True,
},
).data
)
assert_drf_json_equal(course_data, [course_from_fixture], ignore_order=True)

if program_is_live and program_page_is_live:
assert len(course_data[0]["programs"]) == 1
else:
assert course_data[0]["programs"] == []


@pytest.mark.parametrize("course_catalog_course_count", [1], indirect=True)
@pytest.mark.parametrize("course_catalog_program_count", [1], indirect=True)
def test_create_course(
Expand Down

0 comments on commit 28f0fcf

Please sign in to comment.