Ejemplo n.º 1
0
    def test_set_hash_type(self):
        store = FileStore(self._dir, hash_type='md5')

        store.put('foo', 'bar', 10)
        md5 = hashlib.md5(encode('foo')).hexdigest()

        full_dir = os.path.join(self._dir, md5[0:2], md5[2:4])

        assert os.path.exists(full_dir)
Ejemplo n.º 2
0
    def test_forget_with_missing_file(self):
        store = FileStore(tempfile.gettempdir())
        md5 = hashlib.md5(encode('foo')).hexdigest()
        full_dir = os.path.join(tempfile.gettempdir(), md5[0:2], md5[2:4])
        full_path = os.path.join(full_dir, md5)

        mock = flexmock(os.path)
        mock.should_receive('exists').once().with_args(full_path).and_return(False)

        self.assertFalse(store.forget('foo'))
Ejemplo n.º 3
0
    def test_put_creates_missing_directories(self):
        store = flexmock(FileStore(tempfile.gettempdir()))
        md5 = hashlib.md5(encode('foo')).hexdigest()
        full_dir = os.path.join(tempfile.gettempdir(), md5[0:2], md5[2:4])
        full_path = os.path.join(full_dir, md5)
        store.should_receive('_create_cache_directory').once().with_args(full_path)
        mock = flexmock(builtins)
        handler = flexmock()
        mock.should_receive('open').once().with_args(full_path, 'wb').and_return(handler)
        handler.should_receive('write').once()

        store.put('foo', '0000000000', 0)
Ejemplo n.º 4
0
    def test_forget_with_missing_file(self):
        store = FileStore(self._dir)

        sha = hashlib.sha256(encode('foo')).hexdigest()
        full_dir = os.path.join(self._dir, sha[0:2], sha[2:4], sha[4:6],
                                sha[6:8], sha[8:10], sha[10:12], sha[12:14],
                                sha[14:16])
        full_path = os.path.join(full_dir, sha)

        mock = flexmock(os.path)
        mock.should_receive('exists').once().with_args(full_path).and_return(
            False)

        self.assertFalse(store.forget('foo'))
Ejemplo n.º 5
0
    def test_forever_store_values_with_high_timestamp(self):
        store = flexmock(FileStore(tempfile.gettempdir()))

        contents = b'9999999999' + store.serialize('bar')

        md5 = hashlib.md5(encode('foo')).hexdigest()
        full_dir = os.path.join(tempfile.gettempdir(), md5[0:2], md5[2:4])
        full_path = os.path.join(full_dir, md5)

        mock = flexmock(builtins)
        handler = flexmock()
        mock.should_receive('open').once().with_args(full_path, 'wb').and_return(handler)
        handler.should_receive('write').once().with_args(contents)

        store.forever('foo', 'bar')
Ejemplo n.º 6
0
    def test_put_creates_missing_directories(self):
        store = flexmock(FileStore(self._dir))
        sha = hashlib.sha256(encode('foo')).hexdigest()
        full_dir = os.path.join(self._dir, sha[0:2], sha[2:4], sha[4:6],
                                sha[6:8], sha[8:10], sha[10:12], sha[12:14],
                                sha[14:16])
        full_path = os.path.join(full_dir, sha)
        store.should_receive('_create_cache_directory').once().with_args(
            full_path)
        mock = flexmock(builtins)
        handler = flexmock()
        mock.should_receive('open').once().with_args(full_path,
                                                     'wb').and_return(handler)
        handler.should_receive('write').once()

        store.put('foo', '0000000000', 0)
Ejemplo n.º 7
0
    def test_store_items_properly_store_values(self):
        store = flexmock(FileStore(tempfile.gettempdir()))

        contents = b'1111111111' + store.serialize('bar')

        md5 = hashlib.md5(encode('foo')).hexdigest()
        full_dir = os.path.join(tempfile.gettempdir(), md5[0:2], md5[2:4])
        full_path = os.path.join(full_dir, md5)

        store.should_receive('_expiration').with_args(10).and_return(1111111111)

        mock = flexmock(builtins)
        handler = flexmock()
        mock.should_receive('open').once().with_args(full_path, 'wb').and_return(handler)
        handler.should_receive('write').once().with_args(contents)

        store.put('foo', 'bar', 10)
Ejemplo n.º 8
0
    def test_forever_store_values_with_high_timestamp(self):
        store = flexmock(FileStore(self._dir))

        contents = b'9999999999' + store.serialize('bar')

        sha = hashlib.sha256(encode('foo')).hexdigest()
        full_dir = os.path.join(self._dir, sha[0:2], sha[2:4], sha[4:6],
                                sha[6:8], sha[8:10], sha[10:12], sha[12:14],
                                sha[14:16])
        full_path = os.path.join(full_dir, sha)

        mock = flexmock(builtins)
        handler = flexmock()
        mock.should_receive('open').once().with_args(full_path,
                                                     'wb').and_return(handler)
        handler.should_receive('write').once().with_args(contents)

        store.forever('foo', 'bar')
Ejemplo n.º 9
0
    def test_expired_items_return_none(self):
        store = flexmock(FileStore(tempfile.gettempdir()))
        contents = b'0000000000' + store.serialize('bar')

        flexmock(os.path).should_receive('exists').once().and_return(True)

        mock = flexmock(builtins)
        handler = flexmock()

        md5 = hashlib.md5(encode('foo')).hexdigest()
        full_dir = os.path.join(tempfile.gettempdir(), md5[0:2], md5[2:4])
        full_path = os.path.join(full_dir, md5)

        mock.should_receive('open').once().with_args(full_path, 'rb').and_return(handler)
        handler.should_receive('read').once().and_return(contents)

        store.should_receive('forget').once().with_args('foo')

        store.get('foo')
Ejemplo n.º 10
0
    def test_store_items_properly_store_values(self):
        store = flexmock(FileStore(self._dir))

        contents = b'1111111111' + store.serialize('bar')

        sha = hashlib.sha256(encode('foo')).hexdigest()
        full_dir = os.path.join(self._dir, sha[0:2], sha[2:4], sha[4:6],
                                sha[6:8], sha[8:10], sha[10:12], sha[12:14],
                                sha[14:16])
        full_path = os.path.join(full_dir, sha)

        store.should_receive('_expiration').with_args(10).and_return(
            1111111111)

        mock = flexmock(builtins)
        handler = flexmock()
        mock.should_receive('open').once().with_args(full_path,
                                                     'wb').and_return(handler)
        handler.should_receive('write').once().with_args(contents)

        store.put('foo', 'bar', 10)
Ejemplo n.º 11
0
    def test_expired_items_return_none(self):
        store = flexmock(FileStore(self._dir))
        contents = b'0000000000' + store.serialize('bar')

        flexmock(os.path).should_receive('exists').once().and_return(True)

        mock = flexmock(builtins)
        handler = flexmock()

        sha = hashlib.sha256(encode('foo')).hexdigest()
        full_dir = os.path.join(self._dir, sha[0:2], sha[2:4], sha[4:6],
                                sha[6:8], sha[8:10], sha[10:12], sha[12:14],
                                sha[14:16])
        full_path = os.path.join(full_dir, sha)

        mock.should_receive('open').once().with_args(full_path,
                                                     'rb').and_return(handler)
        handler.should_receive('read').once().and_return(contents)

        store.should_receive('forget').once().with_args('foo')

        store.get('foo')