From a4a40e854169f2a6b6e9aa8f2bf7d2f8b49cec9a Mon Sep 17 00:00:00 2001 From: Muhammad Afaq Shuaib Date: Wed, 4 Sep 2024 16:30:35 +0500 Subject: [PATCH] test: add unit test of the method along with fixing some n+1 issues --- .../commands/populate_product_catalog.py | 2 +- .../apps/course_metadata/models.py | 4 ++-- .../apps/course_metadata/tests/test_models.py | 24 +++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/course_discovery/apps/course_metadata/management/commands/populate_product_catalog.py b/course_discovery/apps/course_metadata/management/commands/populate_product_catalog.py index ab07b6f9368..1803beeee92 100644 --- a/course_discovery/apps/course_metadata/management/commands/populate_product_catalog.py +++ b/course_discovery/apps/course_metadata/management/commands/populate_product_catalog.py @@ -74,7 +74,7 @@ def get_products(self, product_type, product_source): ] if (product_type := product_type.lower()) in ['executive_education', 'bootcamp', 'ocm_course']: - queryset = Course.objects.available() + queryset = Course.objects.available().select_related('partner', 'type') if product_type == 'ocm_course': queryset = queryset.filter(type__slug__in=ocm_course_catalog_types) diff --git a/course_discovery/apps/course_metadata/models.py b/course_discovery/apps/course_metadata/models.py index 70b5c8ae5c3..0b2debe194c 100644 --- a/course_discovery/apps/course_metadata/models.py +++ b/course_discovery/apps/course_metadata/models.py @@ -1718,8 +1718,8 @@ def languages_codes(self, exclude_inactive_runs=False): exclude_inactive_runs (bool): whether to exclude inactive runs """ if exclude_inactive_runs: - return ','.join(set(course_run.language.code for course_run in self.active_course_runs.filter(language__isnull=False, restricted_run__isnull=True))) - return ','.join(set(course_run.language.code for course_run in self.course_runs.filter(language__isnull=False, restricted_run__isnull=True))) + return ', '.join(set(course_run.language.code for course_run in self.active_course_runs.filter(language__isnull=False, restricted_run__isnull=True))) + return ', '.join(set(course_run.language.code for course_run in self.course_runs.filter(language__isnull=False, restricted_run__isnull=True))) @property def first_enrollable_paid_seat_price(self): diff --git a/course_discovery/apps/course_metadata/tests/test_models.py b/course_discovery/apps/course_metadata/tests/test_models.py index f268e9f1eae..24f047c4fe3 100644 --- a/course_discovery/apps/course_metadata/tests/test_models.py +++ b/course_discovery/apps/course_metadata/tests/test_models.py @@ -100,6 +100,30 @@ def test_image_url(self): course.image = None assert course.image_url == course.card_image_url + def test_language_codes(self): + partner = factories.PartnerFactory.create() + source = factories.SourceFactory.create(slug="edx") + course = factories.CourseFactory( + product_source=source, + partner=partner, + additional_metadata=None, + ) + LanguageTag.objects.create(code='en', name='English') + course_run = CourseRunFactory( + course=Course.objects.all()[0], + status=CourseRunStatus.Published, + language=LanguageTag.objects.get(code='en') + ) + SeatFactory.create(course_run=course_run) + CourseRunFactory( + course=Course.objects.all()[0], + status=CourseRunStatus.Unpublished, + language=LanguageTag.objects.get(code='es'), + enrollment_end=datetime.datetime.now() - datetime.timedelta(days=5) + ) + assert course.languages_codes() in ['en, es', 'es, en'] + assert course.languages_codes(exclude_inactive_runs=True) == 'en' + def test_validate_history_created_only_on_change(self): """ Validate that course history object would be created if the object is changed otherwise not.