Esempio n. 1
0
    def test_folder_creation(self):
        def same_prefix_path(hashed):
            base = os.path.join(cache.BASE_DIR, 'some prefix')
            return os.path.join(base, hashed), base, hashed
        cache._path_for = same_prefix_path

        try:
            cache.set('key1', 'value1')
            cache.set('key2', 'value2')
        except OSError:
            self.fail('failed to set cache')
Esempio n. 2
0
    def test_collisions(self):
        def collision_hash(key):
            if key in {'key-1', 'key-2'}:
                return 'the same hashed value'
            else:
                return self.cache_hash_for(key)
        cache._hash_for = collision_hash

        cache.set('key-1', 'value-1')
        cache.set('key-2', 'value-2')

        self.assertEqual(cache.get('key-1'), 'value-1')
        self.assertEqual(cache.get('key-2'), 'value-2')
Esempio n. 3
0
def get_and_cache(url, use_cache=True, **kwargs):
    """
    Perform an HTTP GET request to the given url and optionally cache the
    result somewhere in the file system. The cached content will be used
    in the subsequent requests.
    Raises all HTTP errors

    :param url: URL of the page to retrieve
    :param use_cache: Whether to use cache
    :param \*\*kwargs: keyword arguments to pass to `requests.get`
    :return: The content page at the given URL, unicode
    """
    if not use_cache:
        r = requests.get(url, **kwargs)
        r.raise_for_status()
        content = r.text
    else:
        key = url + json.dumps(kwargs)
        content = cache.get(key)
        if content is None:
            content = get_and_cache(url, use_cache=False, **kwargs)
            cache.set(key, content)
    return content
Esempio n. 4
0
def get_and_cache(url, use_cache=True, **kwargs):
    """
    Perform an HTTP GET request to the given url and optionally cache the
    result somewhere in the file system. The cached content will be used
    in the subsequent requests.
    Raises all HTTP errors

    :param url: URL of the page to retrieve
    :param use_cache: Whether to use cache
    :param \*\*kwargs: keyword arguments to pass to `requests.get`
    :return: The content page at the given URL, unicode
    """
    if not use_cache:
        r = requests.get(url, **kwargs)
        r.raise_for_status()
        content = r.text
    else:
        key = url + json.dumps(kwargs)
        content = cache.get(key)
        if content is None:
            content = get_and_cache(url, use_cache=False, **kwargs)
            cache.set(key, content)
    return content
Esempio n. 5
0
 def test_objects(self):
     obj = {'this': ['is', ['a']], 'complex': 'object'}
     cache.set('obj', obj)
     self.assertEqual(obj, cache.get('obj'))
Esempio n. 6
0
 def test_enabled(self):
     cache.ENABLED = False
     cache.set('key', 'value')
     self.assertEqual(cache.get('key', 'something else'), 'something else')
     cache.ENABLED = True
Esempio n. 7
0
 def test_unicode(self):
     cache.set(u'\u84c4\u3048\u3066 \u304f\u3060\u3055\u3044',
               u'\u304a\u75b2\u308c\u3055\u307e')
     self.assertEqual(cache.get(u'\u84c4\u3048\u3066 \u304f\u3060\u3055\u3044'),
                      u'\u304a\u75b2\u308c\u3055\u307e')
Esempio n. 8
0
 def test_set_overwrite(self):
     cache.set('key', 'value')
     cache.set('key', 'another value', overwrite=True)
     self.assertEqual(cache.get('key'), 'another value')
     cache.set('key', 'something else', overwrite=False)
     self.assertEqual(cache.get('key'), 'another value')
Esempio n. 9
0
 def test_simple_set(self):
     cache.set('key', 'value')
     self.assertEqual(cache.get('key'), 'value')