Example #1
0
    def test_cache_key_with_mixed_key_data(self):
        """
        cache_key should concatenate and convert all key data to base
        string to create the cache key.
        """
        from garage.cache import s2hex, cache_key, CACHE_KEY_SEPARATOR

        prefix = 'CACHE:'
        some_string = 'abcd1234 l’écriture 寫作'
        key_data = [
            some_string, 'a', 1, 'b', 2, 'c', 3, '寫作',
            {
                'key1': 'foo',
                'key2': 'bar',
            }
        ]

        elems = []
        for s in key_data:
            if not isinstance(s, six.string_types):
                s = pickle.dumps(s)
            elems.append(s)

        key_string = CACHE_KEY_SEPARATOR.join(elems)
        key = '{0}{1}'.format(prefix, s2hex(key_string))

        result = cache_key(*key_data, prefix=prefix)
        self._msg('test', 'create_cache_key', first=True)
        self._msg('prefix', prefix)
        self._msg('key data', key_data)
        self._msg('expected', key)
        self._msg('result', result)
        self.assertEqual(result, key)
Example #2
0
 def test_create_cache_key(self):
     """
     Ensure create_cache_key function is working.
     """
     from garage.cache import create_cache_key, cache_key
     some_string = 'abcd1234 l’écriture 寫作'
     key = create_cache_key(some_string)
     result = cache_key(some_string)
     self._msg('test', 'cache_key', first=True)
     self._msg('some_string', some_string)
     self._msg('cache_key', key)
     self._msg('result', result)
     self.assertEqual(result, key)
Example #3
0
    def test_cache_key_with_simple_key_string(self):
        """
        cache_key should return an MD5 hash of the input key string.
        """
        from garage.cache import s2hex, cache_key
        some_string = 'abcd1234 l’écriture 寫作'
        md5hash = 'c249c03ab4ee9b4045e653394fd60932'
        prefix = 'CACHE:'
        key = '%s%s' % (prefix, s2hex(some_string))
        result = cache_key(some_string, prefix=prefix)
        self._msg('test', 'create_cache_key', first=True)
        self._msg('prefix', prefix)
        self._msg('some_string', some_string)
        self._msg('expected', key)
        self._msg('result', result)
        self.assertEqual(result, key)

        result = cache_key([some_string], prefix=prefix)
        self._msg('test', 'create_cache_key', first=True)
        self._msg('prefix', prefix)
        self._msg('some_string', some_string)
        self._msg('expected', key)
        self._msg('result', result)
        self.assertEqual(result, key)