Commit 9feab946 by Robert Raposa Committed by Christopher Lee

Fix cache enrollments of None.

LEARNER-5001
parent 7faa62c7
......@@ -20,6 +20,9 @@ Condition = get_model('offer', 'Condition')
logger = logging.getLogger(__name__)
CACHE_MISS = object()
class ProgramCourseRunSeatsCondition(SingleItemConsumptionConditionMixin, Condition):
class Meta(object):
app_label = 'programs'
......@@ -52,8 +55,9 @@ class ProgramCourseRunSeatsCondition(SingleItemConsumptionConditionMixin, Condit
resource=resource_name,
username=basket.owner.username,
)
data_list = cache.get(cache_key)
if not data_list:
data_list = cache.get(cache_key, CACHE_MISS)
if data_list is CACHE_MISS:
data_list = None
user = basket.owner.username
try:
if waffle.switch_is_active("debug_logging_for_excessive_lms_calls"):
......
......@@ -22,7 +22,7 @@ class ProgramCourseRunSeatsConditionTests(ProgramTestMixin, TestCase):
def setUp(self):
super(ProgramCourseRunSeatsConditionTests, self).setUp()
self.condition = factories.ProgramCourseRunSeatsConditionFactory()
self.test_product = ProductFactory(stockrecords__price_excl_tax=10)
self.test_product = ProductFactory(stockrecords__price_excl_tax=10, categories=[])
self.site.siteconfiguration.enable_partial_program = True
def test_name(self):
......@@ -266,3 +266,25 @@ class ProgramCourseRunSeatsConditionTests(ProgramTestMixin, TestCase):
with mock.patch('ecommerce.programs.conditions.traverse_pagination') as mock_processing_entitlements:
self.assertFalse(self.condition.is_satisfied(offer, basket))
mock_processing_entitlements.assert_not_called()
@httpretty.activate
def test_get_lms_resource_for_user_caching_none(self):
"""
LMS resource should be properly cached when enrollments is None.
"""
basket = factories.BasketFactory(site=self.site, owner=factories.UserFactory())
resource_name = 'test_resource_name'
mock_endpoint = mock.Mock()
mock_endpoint.get.return_value = None
return_value = self.condition._get_lms_resource_for_user(basket, resource_name, mock_endpoint) # pylint: disable=protected-access
self.assertEqual(return_value, [])
self.assertEquals(mock_endpoint.get.call_count, 1, 'Endpoint should be called before caching.')
mock_endpoint.reset_mock()
return_value = self.condition._get_lms_resource_for_user(basket, resource_name, mock_endpoint) # pylint: disable=protected-access
self.assertEqual(return_value, [])
self.assertEquals(mock_endpoint.get.call_count, 0, 'Endpoint should NOT be called after caching.')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment