def test_recreate_chest(): with tmp_chest() as c: c[1] = 'one' c[2] = 'two' c.flush() c2 = Chest(path=c.path) assert c.items() == c2.items()
def test_del_on_temp_path(): c = Chest() c[1] = 'one' c.flush() fn = c.path del c import gc gc.collect() assert not os.path.exists(fn)
def test_context_manager(): with Chest() as c: c[1] = 1 c.flush() assert not os.path.exists(c.path) try: with Chest() as c: 1 / 0 except Exception as e: assert isinstance(e, ZeroDivisionError)
def tmp_chest(*args, **kwargs): c = Chest(*args, **kwargs) fn = c.path try: yield c finally: if os.path.exists(fn): with c.lock: c.drop() try: del c except: pass
def test_basic_json(): with tmp_chest(load=json.load, dump=json.dump, mode='t') as c: c[1] = [1, 2, 3] c[2] = 'two' c.flush() c2 = Chest(path=c.path, load=json.load, dump=json.dump, mode='t') assert c2[1] == c[1] assert c2[2] == c[2]
def test_del_on_normal_path(): path = '_chest_test_path' if os.path.exists(path): shutil.rmtree(path) c = Chest(path=path) c[1] = 'one' c.flush() del c import gc gc.collect() assert os.path.exists(path) c = Chest(path=path) c.drop()