def test_process_request(self):
        self.middleware.process_request(self.request)

        self.assertTrue(RequestCache.get_cached_response(TEST_KEY).is_miss)
        self.assertFalse(
            RequestCache.get_cached_response(
                SHOULD_FORCE_CACHE_MISS_KEY).value)
Exemple #2
0
 def test_delete(self, mock_cache_delete):
     TieredCache.set_all_tiers(TEST_KEY, EXPECTED_VALUE)
     TieredCache.set_all_tiers(TEST_KEY_2, EXPECTED_VALUE)
     TieredCache.delete_all_tiers(TEST_KEY)
     self.assertTrue(RequestCache.get_cached_response(TEST_KEY).is_miss)
     self.assertEqual(
         RequestCache.get_cached_response(TEST_KEY_2).value, EXPECTED_VALUE)
     mock_cache_delete.assert_called_with(TEST_KEY)
    def test_process_request_force_django_cache_miss(self):
        request = RequestFactory().get(
            '/?{}=tRuE'.format(FORCE_CACHE_MISS_PARAM))

        self.middleware.process_request(request)

        self.assertTrue(RequestCache.get_cached_response(TEST_KEY).is_miss)
        self.assertTrue(
            RequestCache.get_cached_response(
                SHOULD_FORCE_CACHE_MISS_KEY).value)
Exemple #4
0
    def test_delete(self):
        RequestCache.set(TEST_KEY, EXPECTED_VALUE)
        RequestCache.set(TEST_KEY_2, EXPECTED_VALUE)
        RequestCache.delete(TEST_KEY)

        cached_response = RequestCache.get_cached_response(TEST_KEY)
        self.assertTrue(cached_response.is_miss)

        cached_response = RequestCache.get_cached_response(TEST_KEY_2)
        self.assertTrue(cached_response.is_hit)
        self.assertEqual(cached_response.value, EXPECTED_VALUE)
Exemple #5
0
 def test_set_all_tiers(self, mock_cache_set):
     mock_cache_set.return_value = EXPECTED_VALUE
     TieredCache.set_all_tiers(TEST_KEY, EXPECTED_VALUE,
                               TEST_DJANGO_TIMEOUT_CACHE)
     mock_cache_set.assert_called_with(TEST_KEY, EXPECTED_VALUE,
                                       TEST_DJANGO_TIMEOUT_CACHE)
     self.assertEqual(
         RequestCache.get_cached_response(TEST_KEY).value, EXPECTED_VALUE)
Exemple #6
0
    def test_get_cached_response_django_cache_hit(self, mock_cache_get):
        mock_cache_get.return_value = EXPECTED_VALUE
        cached_response = TieredCache.get_cached_response(TEST_KEY)
        self.assertTrue(cached_response.is_hit)
        self.assertEqual(cached_response.value, EXPECTED_VALUE)

        cached_response = RequestCache.get_cached_response(TEST_KEY)
        self.assertTrue(
            cached_response.is_hit,
            'Django cache hit should cache value in request cache.')
Exemple #7
0
    def test_get_cached_response_force_django_cache_miss(self, mock_cache_get):
        RequestCache.set(SHOULD_FORCE_CACHE_MISS_KEY, True)
        mock_cache_get.return_value = EXPECTED_VALUE
        cached_response = TieredCache.get_cached_response(TEST_KEY)
        self.assertTrue(cached_response.is_miss)

        cached_response = RequestCache.get_cached_response(TEST_KEY)
        self.assertTrue(
            cached_response.is_miss,
            'Forced Django cache miss should not cache value in request cache.'
        )
Exemple #8
0
    def flush(self):
        """Remove all products in basket and fire Segment 'Product Removed' Analytic event for each"""
        cached_response = RequestCache.get_cached_response(TEMPORARY_BASKET_CACHE_KEY)
        if cached_response.is_hit:
            # Do not track anything. This is a temporary basket calculation.
            return
        for line in self.all_lines():
            # Do not fire events for free items. The volume we see for edX.org leads to a dramatic increase in CPU
            # usage. Given that orders for free items are ignored, there is no need for these events.
            if line.stockrecord.price_excl_tax > 0:
                properties = translate_basket_line_for_segment(line)
                track_segment_event(self.site, self.owner, 'Product Removed', properties)

        # Call flush after we fetch all_lines() which is cleared during flush()
        super(Basket, self).flush()  # pylint: disable=bad-super-call
Exemple #9
0
    def add_product(self, product, quantity=1, options=None):
        """
        Add the indicated product to basket.

        Performs AbstractBasket add_product method and fires Google Analytics 'Product Added' event.
        """
        line, created = super(Basket, self).add_product(product, quantity, options)  # pylint: disable=bad-super-call
        cached_response = RequestCache.get_cached_response(TEMPORARY_BASKET_CACHE_KEY)
        if cached_response.is_hit:
            # Do not track anything. This is a temporary basket calculation.
            return line, created

        # Do not fire events for free items. The volume we see for edX.org leads to a dramatic increase in CPU
        # usage. Given that orders for free items are ignored, there is no need for these events.
        if line.stockrecord.price_excl_tax > 0:
            properties = translate_basket_line_for_segment(line)
            properties['cart_id'] = self.id
            track_segment_event(self.site, self.owner, 'Product Added', properties)
        return line, created
Exemple #10
0
 def test_clear(self):
     RequestCache.set(TEST_KEY, EXPECTED_VALUE)
     RequestCache.clear()
     cached_response = RequestCache.get_cached_response(TEST_KEY)
     self.assertTrue(cached_response.is_miss)
Exemple #11
0
 def test_get_cached_response_miss(self):
     cached_response = RequestCache.get_cached_response(TEST_KEY)
     self.assertTrue(cached_response.is_miss)
Exemple #12
0
 def test_get_cached_response_hit_with_cached_none(self):
     RequestCache.set(TEST_KEY, None)
     cached_response = RequestCache.get_cached_response(TEST_KEY)
     self.assertFalse(cached_response.is_miss)
     self.assertEqual(cached_response.value, None)
Exemple #13
0
 def test_get_cached_response_hit(self):
     RequestCache.set(TEST_KEY, EXPECTED_VALUE)
     cached_response = RequestCache.get_cached_response(TEST_KEY)
     self.assertFalse(cached_response.is_miss)
     self.assertEqual(cached_response.value, EXPECTED_VALUE)
Exemple #14
0
 def test_clear_all_tiers(self, mock_cache_clear):
     TieredCache.set_all_tiers(TEST_KEY, EXPECTED_VALUE)
     TieredCache.clear_all_tiers()
     self.assertTrue(RequestCache.get_cached_response(TEST_KEY).is_miss)
     mock_cache_clear.assert_called_once_with()
    def test_process_exception(self):
        response = self.middleware.process_exception(self.request,
                                                     EXPECTED_VALUE)

        self.assertEqual(response, None)
        self.assertTrue(RequestCache.get_cached_response(TEST_KEY).is_miss)