Skip to content

Commit

Permalink
test: product and version stats
Browse files Browse the repository at this point in the history
  • Loading branch information
asadali145 committed Jul 22, 2024
1 parent a948fa1 commit 10d03fa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
48 changes: 38 additions & 10 deletions courses/sync_external_courses/emeritus_api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
parse_emeritus_data_str,
update_emeritus_course_runs,
)
from ecommerce.factories import ProductFactory, ProductVersionFactory
from mitxpro.test_utils import MockResponse
from mitxpro.utils import clean_url

Expand Down Expand Up @@ -92,6 +93,16 @@ def emeritus_course_json_with_bad_data(emeritus_course_json):
return bad_data_emeritus_course_json


@pytest.fixture
def emeritus_course_json_with_null_price(emeritus_course_json):
"""
Emeritus course JSON with null price.
"""
emeritus_course_json = emeritus_course_json.copy()
emeritus_course_json["list_price"] = None
return emeritus_course_json


@pytest.mark.parametrize(
("emeritus_course_run_code", "expected_course_run_tag"),
[
Expand Down Expand Up @@ -300,12 +311,13 @@ def test_create_or_update_emeritus_course_run(create_existing_course_run, empty_
assert getattr(course_runs[0], attr_name) == expected_value


@pytest.mark.parametrize("create_existing_course_runs", [True, False])
@pytest.mark.parametrize("create_existing_data", [True, False])
@pytest.mark.django_db
def test_update_emeritus_course_runs(
create_existing_course_runs,
create_existing_data,
emeritus_expired_course_json,
emeritus_course_json_with_bad_data,
emeritus_course_json_with_null_price,
):
"""
Tests that `update_emeritus_course_runs` creates new courses and updates existing.
Expand All @@ -317,15 +329,15 @@ def test_update_emeritus_course_runs(

platform = PlatformFactory.create(name=EmeritusKeyMap.PLATFORM_NAME.value)

if create_existing_course_runs:
if create_existing_data:
for run in random.sample(emeritus_course_runs, len(emeritus_course_runs) // 2):
course = CourseFactory.create(
title=run["program_name"],
platform=platform,
external_course_id=run["course_code"],
is_external=True,
)
CourseRunFactory.create(
course_run = CourseRunFactory.create(
course=course,
external_course_run_id=run["course_run_code"],
enrollment_start=None,
Expand All @@ -344,18 +356,23 @@ def test_update_emeritus_course_runs(
duration="",
description="",
)
product = ProductFactory.create(content_object=course_run)
ProductVersionFactory.create(product=product, price=run["list_price"])

emeritus_course_runs.append(emeritus_expired_course_json)
emeritus_course_runs.append(emeritus_course_json_with_bad_data)
emeritus_course_runs.append(emeritus_course_json_with_null_price)
stats = update_emeritus_course_runs(emeritus_course_runs)
courses = Course.objects.filter(platform=platform)

num_courses_created = 2 if create_existing_course_runs else 4
num_existing_courses = 2 if create_existing_course_runs else 0
num_course_runs_created = 2 if create_existing_course_runs else 4
num_course_runs_updated = 2 if create_existing_course_runs else 0
num_course_pages_created = 2 if create_existing_course_runs else 4
num_course_pages_updated = 2 if create_existing_course_runs else 0
num_courses_created = 2 if create_existing_data else 4
num_existing_courses = 2 if create_existing_data else 0
num_course_runs_created = 3 if create_existing_data else 5
num_course_runs_updated = 2 if create_existing_data else 0
num_course_pages_created = 2 if create_existing_data else 4
num_course_pages_updated = 2 if create_existing_data else 0
num_products_created = 2 if create_existing_data else 4
num_product_versions_created = 2 if create_existing_data else 4
assert len(courses) == 4
assert len(stats["course_runs_skipped"]) == 1
assert len(stats["course_runs_expired"]) == 1
Expand All @@ -365,6 +382,9 @@ def test_update_emeritus_course_runs(
assert len(stats["course_runs_updated"]) == num_course_runs_updated
assert len(stats["course_pages_created"]) == num_course_pages_created
assert len(stats["course_pages_updated"]) == num_course_pages_updated
assert len(stats["products_created"]) == num_products_created
assert len(stats["product_versions_created"]) == num_product_versions_created
assert len(stats["course_runs_without_prices"]) == 1

for emeritus_course_run in emeritus_course_runs:
if (
Expand All @@ -386,6 +406,14 @@ def test_update_emeritus_course_runs(
== 1
)
assert hasattr(course, "externalcoursepage")
assert (
course.courseruns.filter(
external_course_run_id=emeritus_course_run["course_run_code"]
)
.first()
.current_price
== emeritus_course_run["list_price"]
)

course_page = course.externalcoursepage
if emeritus_course_run["program_for"]:
Expand Down
4 changes: 2 additions & 2 deletions courses/sync_external_courses/test_data/batch_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
"start_date": "2099-06-30",
"end_date": "2099-12-29",
"Category": "Healthcare",
"list_price": null,
"list_price": 7650,
"list_currency": null,
"total_weeks": 28,
"product_family": "SEPO",
Expand All @@ -146,7 +146,7 @@
"start_date": "2098-06-30",
"end_date": "2099-02-23",
"Category": "Technology",
"list_price": null,
"list_price": 7650,
"list_currency": null,
"total_weeks": 29,
"product_family": "SEPO",
Expand Down

0 comments on commit 10d03fa

Please sign in to comment.