Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: updated test for course runs which are deactivated but api star…
Browse files Browse the repository at this point in the history
…ts sending them again
Anas12091101 committed Jan 31, 2025
1 parent e5a6eb1 commit 951559b
Showing 3 changed files with 33 additions and 21 deletions.
5 changes: 4 additions & 1 deletion courses/management/commands/sync_external_course_runs.py
Original file line number Diff line number Diff line change
@@ -140,7 +140,10 @@ def log_stats(self, stats):
f"External Course Codes: {stats.get('course_runs_expired') or 0}.\n"
)
self.log_style_success(
f"Number of Course Runs Deleted {len(stats['course_runs_deleted'])}."
f"Number of Course Runs Deactivated {len(stats['course_runs_deactivated'])}."
)
self.log_style_success(
f"External Course Codes: {stats.get('course_runs_deactivated') or 0}.\n"
)

def log_style_success(self, log_msg):
22 changes: 12 additions & 10 deletions courses/sync_external_courses/external_course_sync_api.py
Original file line number Diff line number Diff line change
@@ -306,14 +306,14 @@ def update_external_course_runs(external_courses, keymap): # noqa: C901, PLR091
"course_runs_without_prices": set(),
"certificates_created": set(),
"certificates_updated": set(),
"deactivated_course_runs": set(),
"course_runs_deactivated": set(),
}

external_course_run_codes = [run["course_run_code"] for run in external_courses]
deactivated_course_run_codes = deactivate_removed_course_runs(
external_course_run_codes, platform.name.lower()
)
stats["deactivated_course_runs"] = deactivated_course_run_codes
stats["course_runs_deactivated"] = deactivated_course_run_codes

for external_course_json in external_courses:
external_course = ExternalCourse(external_course_json, keymap)
@@ -508,22 +508,23 @@ def create_or_update_product_and_product_version(external_course, course_run):
Returns:
tuple: (product is created, product version is created)
"""
current_price = course_run.current_price
if not current_price or current_price != external_course.price:
product, product_created = Product.objects.get_or_create(
product, product_created = Product.all_objects.get_or_create(
content_type=ContentType.objects.get_for_model(CourseRun),
object_id=course_run.id,
)
if not product_created and not product.is_active:
product.is_active = True
product.save()
if not product_created and not product.is_active:
product.is_active = True
product.save()

current_price = course_run.current_price
if not current_price or current_price != external_course.price:
ProductVersion.objects.create(
product=product,
price=external_course.price,
description=course_run.courseware_id,
)
return product_created, True
return False, False
return product_created, False


def generate_external_course_run_tag(course_run_code):
@@ -843,10 +844,11 @@ def deactivate_removed_course_runs(external_course_run_codes, platform_name):
start_date__gt=now_in_utc(),
live=True,
).exclude(external_course_run_id__in=external_course_run_codes)
course_runs.update(live=False)

Product.objects.filter(object_id__in=Subquery(course_runs.values("id"))).update(
is_active=False
)
course_runs.update(live=False)

log.info(
f"Deactivated {course_runs.count()} course runs for platform {platform_name}."
27 changes: 17 additions & 10 deletions courses/sync_external_courses/external_course_sync_api_test.py
Original file line number Diff line number Diff line change
@@ -446,16 +446,17 @@ def test_parse_external_course_data_str():
indirect=True,
)
@pytest.mark.parametrize(
("create_existing_course_run", "empty_dates"),
("create_existing_course_run", "empty_dates", "is_live"),
[
(True, True),
(True, False),
(False, False),
(True, True, True),
(True, False, True),
(False, False, True),
(True, False, False),
],
)
@pytest.mark.django_db
def test_create_or_update_external_course_run(
create_existing_course_run, empty_dates, external_course_data
create_existing_course_run, empty_dates, external_course_data, is_live
):
"""
Tests that `create_or_update_external_course_run` creates or updates a course run
@@ -470,6 +471,7 @@ def test_create_or_update_external_course_run(
enrollment_start=None,
enrollment_end=None,
expiration_date=None,
live=is_live,
)
if empty_dates:
run.start_date = None
@@ -494,6 +496,7 @@ def test_create_or_update_external_course_run(
"start_date": external_course.start_date,
"end_date": external_course.end_date,
"enrollment_end": external_course.enrollment_end,
"live": True,
}
else:
expected_data = {
@@ -758,12 +761,14 @@ def test_fetch_external_courses_error(
"expected_price",
"expected_product_created",
"expected_product_version_created",
"existing_product_is_active",
),
[
(True, None, float(100), float(100), False, True),
(False, None, float(100), float(100), True, True),
(True, float(100), float(100), float(100), False, False),
(True, float(100), float(111), float(111), False, True),
(True, None, float(100), float(100), False, True, True),
(False, None, float(100), float(100), True, True, True),
(True, float(100), float(100), float(100), False, False, True),
(True, float(100), float(111), float(111), False, True, True),
(True, float(100), float(100), float(100), False, False, False)
],
)
@pytest.mark.django_db
@@ -775,6 +780,7 @@ def test_create_or_update_product_and_product_version( # noqa: PLR0913
expected_price,
expected_product_created,
expected_product_version_created,
existing_product_is_active,
):
"""
Tests that `create_or_update_product_and_product_version` creates or updates products and versions as required.
@@ -796,7 +802,7 @@ def test_create_or_update_product_and_product_version( # noqa: PLR0913
course_run, _, _ = create_or_update_external_course_run(course, external_course)

if create_existing_product:
product = ProductFactory.create(content_object=course_run)
product = ProductFactory.create(content_object=course_run, is_active=existing_product_is_active)

if existing_price:
ProductVersionFactory.create(product=product, price=existing_price)
@@ -809,6 +815,7 @@ def test_create_or_update_product_and_product_version( # noqa: PLR0913
assert version_created == expected_product_version_created
assert course_run.products.first().latest_version.description
assert course_run.products.first().latest_version.text_id
assert course_run.products.first().is_active == True


@pytest.mark.django_db

0 comments on commit 951559b

Please sign in to comment.