def test_open_unref_does_not_leak(self): temp_dir = testlib.temp_dir() env = lmdb.open(temp_dir) ref = weakref.ref(env) env = None testlib.debug_collect() assert ref() is None
def test_readonly_true_noexist(self): path = testlib.temp_dir(create=False) # Open readonly missing store should fail. self.assertRaises(lmdb.Error, lambda: lmdb.open(path, readonly=True, create=True)) # And create=True should not have mkdir'd it. assert not os.path.exists(path)
def test_copy_compact(self): _, env = testlib.temp_env() txn = env.begin(write=True) txn.put(B('a'), B('b')) txn.commit() dest_dir = testlib.temp_dir() env.copy(dest_dir, compact=True) assert os.path.exists(dest_dir + '/data.mdb') cenv = lmdb.open(dest_dir) ctxn = cenv.begin() assert ctxn.get(B('a')) == B('b') env.close() self.assertRaises(Exception, lambda: env.copy(testlib.temp_dir()))
def test_mode_subdir_nocreate(self): oldmask = os.umask(0) try: for mode in OCT('777'), OCT('755'), OCT('700'): path = testlib.temp_dir() env = lmdb.open(path, subdir=True, create=False, mode=mode) fmode = mode & ~OCT('111') assert testlib.path_mode(path + '/data.mdb') == fmode assert testlib.path_mode(path + '/lock.mdb') == fmode finally: os.umask(oldmask)
def test_weakref_callback_invoked_once(self): temp_dir = testlib.temp_dir() env = lmdb.open(temp_dir) env.close() count = [0] def callback(ref): count[0] += 1 ref = weakref.ref(env, callback) env = None testlib.debug_collect() assert ref() is None assert count[0] == 1
def test_copy_compact(self): _, env = testlib.temp_env() txn = env.begin(write=True) txn.put(B('a'), B('b')) txn.commit() dest_dir = testlib.temp_dir() env.copy(dest_dir, compact=True) assert os.path.exists(dest_dir + '/data.mdb') cenv = lmdb.open(dest_dir) ctxn = cenv.begin() assert ctxn.get(B('a')) == B('b') # Test copy with transaction provided dest_dir = testlib.temp_dir() with env.begin(write=True) as txn: copy_txn = env.begin() txn.put(B('b'), B('b')) assert ctxn.get(B('b')) is None if have_txn_patch: env.copy(dest_dir, compact=True, txn=copy_txn) assert os.path.exists(dest_dir + '/data.mdb') cenv = lmdb.open(dest_dir) ctxn = cenv.begin() assert ctxn.get(B('a')) == B('b') # Verify that the write that occurred outside the txn isn't seen in the # copy assert ctxn.get(B('b')) is None else: self.assertRaises( Exception, lambda: env.copy(dest_dir, compact=True, txn=copy_txn)) env.close() self.assertRaises(Exception, lambda: env.copy(testlib.temp_dir()))
def test_open_close(self): temp_dir = testlib.temp_dir() env = lmdb.open(temp_dir) with env.begin() as txn: pass env.close() r1 = weakref.ref(env) r2 = weakref.ref(txn) env = None txn = None testlib.debug_collect() assert r1() is None assert r2() is None
def test_mode_subdir_nocreate(self): if sys.platform == 'win32': # Mode argument is ignored on Windows; see lmdb.h return oldmask = os.umask(0) try: for mode in OCT('777'), OCT('755'), OCT('700'): path = testlib.temp_dir() env = lmdb.open(path, subdir=True, create=False, mode=mode) fmode = mode & ~OCT('111') assert testlib.path_mode(path + '/data.mdb') == fmode assert testlib.path_mode(path + '/lock.mdb') == fmode finally: os.umask(oldmask)
def test_subdir_true_noexist_create(self): path = testlib.temp_dir(create=False) path_, env = testlib.temp_env(path, subdir=True, create=True) assert path_ == path assert env.path() == path
def test_subdir_true_noexist_nocreate(self): path = testlib.temp_dir(create=False) self.assertRaises( lmdb.Error, lambda: testlib.temp_env(path, subdir=True, create=False)) assert not os.path.exists(path)
def test_cmd_rewrite(self): frompath, env = testlib.temp_env() env.open_db(b'subdb') env.close() topath = testlib.temp_dir() call_tool('rewrite -e %s -E %s subdb' % (frompath, topath))