From d458eb425ba4eb2aefb9be86234bef5b4a63e05b Mon Sep 17 00:00:00 2001 From: FokinPV Date: Fri, 7 Nov 2014 20:25:35 +0300 Subject: [PATCH] Added close method to CachingMiddleware. --- tests/test_middlewares.py | 18 +++++++++++++++++- tinydb/middlewares.py | 3 +++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/test_middlewares.py b/tests/test_middlewares.py index a4da222f..72e7ed25 100644 --- a/tests/test_middlewares.py +++ b/tests/test_middlewares.py @@ -1,6 +1,8 @@ +import os + from tinydb import TinyDB from tinydb.middlewares import CachingMiddleware -from tinydb.storages import MemoryStorage +from tinydb.storages import MemoryStorage, JSONStorage if 'xrange' not in dir(__builtins__): xrange = range # Python 3 support @@ -71,3 +73,17 @@ def test_nested(): # Verify contents assert element == storage.read() + + +def test_caching_json_write(tmpdir): + path = str(tmpdir.join('test.db')) + + db = TinyDB(path, storage=CachingMiddleware(JSONStorage)) + + db.insert({'key':'value'}) + + db.close() + + # Verify database filesize + statinfo = os.stat(path) + assert statinfo.st_size != 0 diff --git a/tinydb/middlewares.py b/tinydb/middlewares.py index b6fc2102..fa084636 100644 --- a/tinydb/middlewares.py +++ b/tinydb/middlewares.py @@ -112,3 +112,6 @@ def flush(self): self.storage.write(self.cache) self._cache_modified_count = 0 + + def close(self): + self.flush()