Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 13, 2025
1 parent 90185d0 commit ab801c5
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 20 deletions.
5 changes: 4 additions & 1 deletion cms/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def test_filter_and_sort_catalog_pages_with_default_sorting(sort_by):
)
# Create course run with past start_date and enrollment_end, which should NOT appear in the catalog
past_run = CourseRunFactory.create(
past_start=True, past_enrollment_end=True, course__no_program=True, clean_disabled=True
past_start=True,
past_enrollment_end=True,
course__no_program=True,
clean_disabled=True,
)
all_runs = [
past_run,
Expand Down
1 change: 1 addition & 0 deletions courses/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def _create(cls, model_class, *args, **kwargs):

return obj


class CourseRunCertificateFactory(DjangoModelFactory):
"""Factory for CourseRunCertificate"""

Expand Down
12 changes: 9 additions & 3 deletions courses/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,10 +760,16 @@ def clean(self):
now = now_in_utc()

if not self.start_date and not self.enrollment_end:
raise ValidationError("Either start_date or enrollment_end must be provided.") # noqa: EM101
raise ValidationError(
"Either start_date or enrollment_end must be provided."
) # noqa: EM101

if (not self.start_date or self.start_date < now) and (not self.enrollment_end or self.enrollment_end < now):
raise ValidationError("Either start_date or enrollment_end must be in the future.") # noqa: EM101
if (not self.start_date or self.start_date < now) and (
not self.enrollment_end or self.enrollment_end < now
):
raise ValidationError(
"Either start_date or enrollment_end must be in the future."
) # noqa: EM101

if self.start_date and self.end_date and self.start_date > self.end_date:
raise ValidationError("End date must be later than start date.") # noqa: EM101
Expand Down
79 changes: 66 additions & 13 deletions courses/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
pytestmark = [pytest.mark.django_db]
now = now_in_utc()


def test_program_course_auto_position():
"""
If a course is added to a program with no position specified, it should be given the last position
Expand Down Expand Up @@ -108,7 +109,11 @@ def test_program_is_catalog_visible():
"""
program = ProgramFactory.create()
runs = CourseRunFactory.create_batch(
2, course__program=program, past_start=True, past_enrollment_end=True, clean_disabled=True
2,
course__program=program,
past_start=True,
past_enrollment_end=True,
clean_disabled=True,
)
assert program.is_catalog_visible is False

Expand Down Expand Up @@ -229,22 +234,60 @@ def test_courseware_url(settings):
@pytest.mark.parametrize(
"start_date, enrollment_end, end_date, expiration_date, expected_error",
[
(None, None, None, None, "Either start_date or enrollment_end must be provided."),
(now - timedelta(days=2), now - timedelta(days=2), None, None, "Either start_date or enrollment_end must be in the future."),
(now + timedelta(days=2), now + timedelta(days=3), now + timedelta(days=4), now + timedelta(days=5), None),
(now + timedelta(days=4), now + timedelta(days=3), now + timedelta(days=2), now + timedelta(days=5), "End date must be later than start date."),
(now + timedelta(days=2), now + timedelta(days=3), now + timedelta(days=4), now - timedelta(days=2), "Expiration date must be later than start date."),
(now + timedelta(days=2), now + timedelta(days=3), now + timedelta(days=4), now + timedelta(days=3), "Expiration date must be later than end date."),
]
(
None,
None,
None,
None,
"Either start_date or enrollment_end must be provided.",
),
(
now - timedelta(days=2),
now - timedelta(days=2),
None,
None,
"Either start_date or enrollment_end must be in the future.",
),
(
now + timedelta(days=2),
now + timedelta(days=3),
now + timedelta(days=4),
now + timedelta(days=5),
None,
),
(
now + timedelta(days=4),
now + timedelta(days=3),
now + timedelta(days=2),
now + timedelta(days=5),
"End date must be later than start date.",
),
(
now + timedelta(days=2),
now + timedelta(days=3),
now + timedelta(days=4),
now - timedelta(days=2),
"Expiration date must be later than start date.",
),
(
now + timedelta(days=2),
now + timedelta(days=3),
now + timedelta(days=4),
now + timedelta(days=3),
"Expiration date must be later than end date.",
),
],
)
def test_course_run_clean(start_date, enrollment_end, end_date, expiration_date, expected_error):
def test_course_run_clean(
start_date, enrollment_end, end_date, expiration_date, expected_error
):
course_run = CourseRunFactory.build(
start_date=start_date,
enrollment_end=enrollment_end,
end_date=end_date,
expiration_date=expiration_date,
)

if expected_error:
with pytest.raises(ValidationError) as exc_info:
course_run.clean()
Expand Down Expand Up @@ -347,7 +390,9 @@ def test_course_run_unexpired(end_days, enroll_days, expected):
enr_end_date = now + timedelta(days=enroll_days)
assert (
CourseRunFactory.create(
end_date=end_date, enrollment_end=enr_end_date, clean_disabled=True,
end_date=end_date,
enrollment_end=enr_end_date,
clean_disabled=True,
).is_unexpired
is expected
)
Expand Down Expand Up @@ -553,7 +598,11 @@ def test_course_is_catalog_visible():
"""
course = CourseFactory.create()
runs = CourseRunFactory.create_batch(
2, course=course, past_start=True, past_enrollment_end=True, clean_disabled=True,
2,
course=course,
past_start=True,
past_enrollment_end=True,
clean_disabled=True,
)
assert course.is_catalog_visible is False

Expand Down Expand Up @@ -798,7 +847,11 @@ def test_enrollment_is_ended():
past_course = CourseFactory.create()

past_course_runs = CourseRunFactory.create_batch(
3, end_date=past_date, course=past_course, course__program=past_program, clean_disabled=True,
3,
end_date=past_date,
course=past_course,
course__program=past_program,
clean_disabled=True,
)

program_enrollment = ProgramEnrollmentFactory.create(program=past_program)
Expand Down
8 changes: 6 additions & 2 deletions courses/serializers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,12 @@ def test_serialize_course( # noqa: PLR0913
course.page.topics.set([CourseTopic.objects.create(name=topic)])

# Create expired, enrollment_ended, future, and enrolled course runs
CourseRunFactory.create(course=course, end_date=now - timedelta(1), live=True, clean_disabled=True)
CourseRunFactory.create(course=course, enrollment_end=now - timedelta(1), live=True, clean_disabled=True)
CourseRunFactory.create(
course=course, end_date=now - timedelta(1), live=True, clean_disabled=True
)
CourseRunFactory.create(
course=course, enrollment_end=now - timedelta(1), live=True, clean_disabled=True
)
CourseRunFactory.create(
course=course, enrollment_start=now + timedelta(1), live=True
)
Expand Down
4 changes: 3 additions & 1 deletion courses/sync_external_courses/external_course_sync_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ def update_external_course_runs(external_courses, keymap): # noqa: C901, PLR091
log.info(
f"Price is Null for course run code: {external_course.course_run_code}" # noqa: G004
)
stats["course_runs_without_prices"].add(external_course.course_run_code)
stats["course_runs_without_prices"].add(
external_course.course_run_code
)

log.info(
f"Creating or Updating course page, title: {external_course.course_title}, course_code: {external_course.course_run_code}" # noqa: G004
Expand Down

0 comments on commit ab801c5

Please sign in to comment.