MCPcopy
hub / github.com/django/django / test_view_decorator

Method test_view_decorator

tests/cache/tests.py:2743–2823  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2741 self.assertEqual(result.content, b"Hello World 1")
2742
2743 def test_view_decorator(self):
2744 # decorate the same view with different cache decorators
2745 default_view = cache_page(3)(hello_world_view)
2746 default_with_prefix_view = cache_page(3, key_prefix="prefix1")(hello_world_view)
2747
2748 explicit_default_view = cache_page(3, cache="default")(hello_world_view)
2749 explicit_default_with_prefix_view = cache_page(
2750 3, cache="default", key_prefix="prefix1"
2751 )(hello_world_view)
2752
2753 other_view = cache_page(1, cache="other")(hello_world_view)
2754 other_with_prefix_view = cache_page(1, cache="other", key_prefix="prefix2")(
2755 hello_world_view
2756 )
2757
2758 request = self.factory.get("/view/")
2759
2760 # Request the view once
2761 response = default_view(request, "1")
2762 self.assertEqual(response.content, b"Hello World 1")
2763
2764 # Request again -- hit the cache
2765 response = default_view(request, "2")
2766 self.assertEqual(response.content, b"Hello World 1")
2767
2768 # Requesting the same view with the explicit cache should yield the
2769 # same result
2770 response = explicit_default_view(request, "3")
2771 self.assertEqual(response.content, b"Hello World 1")
2772
2773 # Requesting with a prefix will hit a different cache key
2774 response = explicit_default_with_prefix_view(request, "4")
2775 self.assertEqual(response.content, b"Hello World 4")
2776
2777 # Hitting the same view again gives a cache hit
2778 response = explicit_default_with_prefix_view(request, "5")
2779 self.assertEqual(response.content, b"Hello World 4")
2780
2781 # And going back to the implicit cache will hit the same cache
2782 response = default_with_prefix_view(request, "6")
2783 self.assertEqual(response.content, b"Hello World 4")
2784
2785 # Requesting from an alternate cache won't hit cache
2786 response = other_view(request, "7")
2787 self.assertEqual(response.content, b"Hello World 7")
2788
2789 # But a repeated hit will hit cache
2790 response = other_view(request, "8")
2791 self.assertEqual(response.content, b"Hello World 7")
2792
2793 # And prefixing the alternate cache yields yet another cache entry
2794 response = other_with_prefix_view(request, "9")
2795 self.assertEqual(response.content, b"Hello World 9")
2796
2797 # But if we wait a couple of seconds...
2798 time.sleep(2)
2799
2800 # ... the default cache will still hit

Callers

nothing calls this directly

Calls 3

cache_pageFunction · 0.90
sleepMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected