feat: B2C Subscription Inclusion Flag for Program Algolia Reindex#60
feat: B2C Subscription Inclusion Flag for Program Algolia Reindex#60skumargupta83 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to ensure the B2C Subscription Inclusion flag is correctly surfaced for Algolia indexing so that subscription-eligible Courses/Programs can be synchronized into the Algolia product index.
Changes:
- Update
b2c_subscription_inclusionaccessors on Algolia proxy models. - Add tests intended to verify that
b2c_subscription_inclusionis read from the underlying persisted product state.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
course_discovery/apps/course_metadata/algolia_models.py |
Changes how b2c_subscription_inclusion is retrieved on Algolia proxy models. |
course_discovery/apps/course_metadata/tests/test_algolia_models.py |
Adds new tests for retrieving b2c_subscription_inclusion from the underlying product. |
Comments suppressed due to low confidence (2)
course_discovery/apps/course_metadata/algolia_models.py:757
AlgoliaProxyProgram.b2c_subscription_inclusionnow falls back toself.product.b2c_subscription_inclusion, butAlgoliaProxyPrograminstances don’t have aproductattribute. This can raiseAttributeErrorif_b2c_subscription_inclusionisn’t present (e.g., when the field is deferred/not loaded).
def b2c_subscription_inclusion(self):
"""Return the b2c_subscription_inclusion value from the underlying Program instance."""
return getattr(self, '_b2c_subscription_inclusion', self.product.b2c_subscription_inclusion)
course_discovery/apps/course_metadata/tests/test_algolia_models.py:1014
AlgoliaProxyProgram(product=program)will raise aTypeErrorbecauseproductisn’t a model field andAlgoliaProxyProgramdoesn’t accept it in__init__. The test should fetch the proxy model instance via the ORM instead.
program = ProgramFactory(partner=self.__class__.edxPartner, b2c_subscription_inclusion=True)
proxy_program = AlgoliaProxyProgram(product=program)
assert proxy_program.b2c_subscription_inclusion is True
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def b2c_subscription_inclusion(self): | ||
| """Return the stored b2c_subscription_inclusion value for persistence and Algolia indexing.""" | ||
| return self.__dict__.get('_b2c_subscription_inclusion', False) | ||
| """Return the b2c_subscription_inclusion value from the underlying Course instance.""" | ||
| return getattr(self, '_b2c_subscription_inclusion', self.product.b2c_subscription_inclusion) |
| def test_b2c_subscription_inclusion_retrieved_from_product_for_courses(self): | ||
| """Test that b2c_subscription_inclusion is retrieved from the underlying product for courses.""" | ||
| course = CourseFactory(partner=self.__class__.edxPartner, b2c_subscription_inclusion=True) | ||
| proxy_course = AlgoliaProxyCourse(product=course) | ||
| assert proxy_course.b2c_subscription_inclusion is True | ||
|
|
||
| course.b2c_subscription_inclusion = False | ||
| course.save() | ||
| proxy_course_updated = AlgoliaProxyCourse(product=course) | ||
| assert proxy_course_updated.b2c_subscription_inclusion is False |
| def test_b2c_subscription_inclusion_retrieved_from_product_for_courses(self): | ||
| """Test that b2c_subscription_inclusion is retrieved from the underlying course model value.""" | ||
| course = AlgoliaProxyCourseFactory(partner=self.__class__.edxPartner, b2c_subscription_inclusion=True) | ||
| assert course.b2c_subscription_inclusion is True | ||
|
|
||
| proxy_course = AlgoliaProxyCourse.objects.get(pk=course.pk) | ||
| assert proxy_course.b2c_subscription_inclusion is True | ||
|
|
||
| course.b2c_subscription_inclusion = False | ||
| course.save() | ||
| proxy_course_updated = AlgoliaProxyCourse.objects.get(pk=course.pk) | ||
| assert proxy_course_updated.b2c_subscription_inclusion is False | ||
|
|
a04f9d4 to
11b5c79
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (3)
course_discovery/apps/course_metadata/algolia_models.py:496
b2c_subscription_inclusioncurrently callsgetattr(self, '_b2c_subscription_inclusion', self.product.b2c_subscription_inclusion). The default argument is evaluated eagerly, so this will always try to accessself.producteven when_b2c_subscription_inclusionis set.AlgoliaProxyCourseinstances do not have aproductattribute, so this will raiseAttributeErrorduring indexing/saving.
@property
def b2c_subscription_inclusion(self):
"""Return the b2c_subscription_inclusion value from the underlying Course instance in Course."""
return getattr(self, '_b2c_subscription_inclusion', self.product.b2c_subscription_inclusion)
course_discovery/apps/course_metadata/algolia_models.py:758
- Same issue as
AlgoliaProxyCourse: the default argument togetattr(...)is evaluated eagerly, andAlgoliaProxyPrograminstances do not defineself.product. This will raiseAttributeErrorand break Algolia indexing / persistence.
@property
def b2c_subscription_inclusion(self):
"""Return the b2c_subscription_inclusion value from the underlying Program instance for the Program."""
return getattr(self, '_b2c_subscription_inclusion', self.product.b2c_subscription_inclusion)
course_discovery/apps/course_metadata/tests/test_algolia_models.py:1003
- These new tests instantiate
AlgoliaProxyCourse(product=course)/AlgoliaProxyProgram(product=program), but those proxy Django models don't accept aproductkwarg (onlyAlgoliaProxyProductdoes). This will raiseTypeErrorand the tests won't run. Consider asserting persistence/updates using the proxy models directly instead.
def test_b2c_subscription_inclusion_retrieved_from_product_for_courses(self):
"""Test that b2c_subscription_inclusion is retrieved from the underlying product for courses."""
course = CourseFactory(partner=self.__class__.edxPartner, b2c_subscription_inclusion=True)
proxy_course = AlgoliaProxyCourse(product=course)
assert proxy_course.b2c_subscription_inclusion is True
…d self.product on AlgoliaProxyCourse/AlgoliaProxyProgram The commit 11b5c79 changed AlgoliaProxyCourse.b2c_subscription_inclusion and AlgoliaProxyProgram.b2c_subscription_inclusion getters to use: getattr(self, '_b2c_subscription_inclusion', self.product.b2c_subscription_inclusion) However, AlgoliaProxyCourse and AlgoliaProxyProgram are Django proxy models that inherit from Course and Program directly — they have no 'self.product' attribute. Only AlgoliaProxyProduct (which wraps a product in its __init__) has self.product. This caused AttributeError on every test because the property was evaluated by Django during model saves, breaking all tests in TestAlgoliaProxyCourse and TestAlgoliaProxyProgram. Fix: - Revert AlgoliaProxyCourse.b2c_subscription_inclusion getter to self.__dict__.get('_b2c_subscription_inclusion', False) - Revert AlgoliaProxyProgram.b2c_subscription_inclusion getter to self.__dict__.get('_b2c_subscription_inclusion', self.__dict__.get('b2c_subscription_inclusion', False)) - Fix the new tests to load proxy instances via .objects.get(pk=...) from the database instead of incorrectly passing product= to the constructor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
course_discovery/apps/course_metadata/tests/test_algolia_models.py:999
- This test exercises AlgoliaProxyCourse but is placed inside TestAlgoliaProxyProgram. For consistency with the rest of this file (course-related assertions live under TestAlgoliaProxyCourse), move this test into the TestAlgoliaProxyCourse class to keep course vs program behavior separated and easier to maintain.
def test_b2c_subscription_inclusion_retrieved_from_product_for_courses(self):
| course = CourseFactory(partner=self.__class__.edxPartner, b2c_subscription_inclusion=True) | ||
| proxy_course = AlgoliaProxyCourse.objects.get(pk=course.pk) | ||
| assert proxy_course.b2c_subscription_inclusion is True |
https://2u-internal.atlassian.net/browse/SUBS-820
Ensure Program subscription data, including the B2C Subscription Inclusion flag, is indexed in Algolia so eligible Programs can be Sync.
Program subscription flag is indexed.
Index updates after Program changes.
Ensure program subscription data is successfully synchronized with the corresponding Algolia product index.