Ticket #7967: 7967-r8075.diff

File 7967-r8075.diff, 1.5 KB (added by Gulopine, 7 years ago)

Fixed __contains__ to always call has_key() and added tests to verify they work the same way.

  • django/core/cache/backends/base.py

     
    6363        """
    6464        return self.get(key) is not None
    6565
    66     __contains__ = has_key
     66    def __contains__(self, key):
     67        """
     68        Returns True if the key is in the cache and has not expired.
     69        """
     70        # This is a separate method, rather than just a copy of has_key(),
     71        # so that it always has the same functionality as has_key(), even
     72        # if a subclass overrides it.
     73        return self.has_key(key)
  • tests/regressiontests/cache/tests.py

     
    5656        cache.set("hello1", "goodbye1")
    5757        self.assertEqual(cache.has_key("hello1"), True)
    5858        self.assertEqual(cache.has_key("goodbye1"), False)
     59        cache.set("empty", None)
     60        self.assertEqual(cache.has_key("empty"), True)
    5961
    6062    def test_in(self):
    6163        cache.set("hello2", "goodbye2")
    6264        self.assertEqual("hello2" in cache, True)
    6365        self.assertEqual("goodbye2" in cache, False)
     66        cache.set("empty", None)
     67        self.assertEqual("empty" in cache, True)
    6468
    6569    def test_data_types(self):
    6670        stuff = {
Back to Top