Пример #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)
Пример #2
0
    def test_s2hex(self):
        """
        Ensure s2hex function returns proper values.
        """
        from garage.cache import s2hex

        some_string = 'abcd1234'
        md5hash = 'e19d5cd5af0378da05f63f891c7467af'
        result = s2hex(some_string)
        self._msg('test', 's2hex', first=True)
        self._msg('some_string', some_string)
        self._msg('result', result)
        self.assertEqual(result, md5hash)

        some_string = 'abcd1234 l’écriture 寫作'
        md5hash = 'c249c03ab4ee9b4045e653394fd60932'
        result = s2hex(some_string)
        self._msg('test', 's2hex', first=True)
        self._msg('some_string', some_string)
        self._msg('result', result)
        self.assertEqual(result, md5hash)

        # cPickle and pickle produces different output for the
        # identical data structure
        try:
            import cPickle
            # using cPickle.dumps
            md5hash = 'e3166d75522955dce348b412695a7965'
        except ImportError:
            # using pickle.dumps
            md5hash = 'ae0f56e9c34a13072ddf950d4d0e32d3'
            
        data = ['foo', 'bar', 'baz']
        result = s2hex(data)
        self._msg('test', 's2hex', first=True)
        self._msg('data', data)
        self._msg('result', result)
        self.assertEqual(result, md5hash)
Пример #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)