def test_mode_subdir_nocreate(self): if sys.platform == 'win32': # Mode argument is ignored on Windows; see aiolmdb.h return oldmask = os.umask(0) try: for mode in 0o777, 0o755, 0o700: path = self.create_dir() aiolmdb.open(path, subdir=True, create=False, mode=mode) fmode = mode & ~0o111 assert self.path_mode(path + '/data.mdb') == fmode assert self.path_mode(path + '/lock.mdb') == fmode finally: os.umask(oldmask)
def test_readonly_true_noexist(self): path = self.create_dir(create=False) # Open readonly missing store should fail. self.assertRaises( lmdb.Error, lambda: aiolmdb.open(path, readonly=True, create=True)) # And create=True should not have mkdir'd it. assert not os.path.exists(path)
def test_open_unref_does_not_leak(self): temp_dir = self.create_dir() env = aiolmdb.open(temp_dir) ref = weakref.ref(env) env = None testlib.debug_collect() self.assertIsNone(ref())
def test_subdir_false_junk(self): path = self.create_file() fp = open(path, 'wb') fp.write(b'A' * 8192) fp.close() self.assertRaises(lmdb.InvalidError, lambda: aiolmdb.open(path, subdir=False))
def test_readonly_env_sub_eperm(self): # https://github.com/dw/py-aiolmdb/issues/109 path, env = self.create_env() env.close() env = aiolmdb.open(path, max_dbs=10, readonly=True) self.assertRaises(lmdb.ReadonlyError, lambda: env.open_db(b'node_schedules', create=True))
def test_readonly_env_sub(self): # https://github.com/dw/py-aiolmdb/issues/109 path, env = self.create_env() self.assertIsNotNone(env.open_db(b'node_schedules')) env.close() env = aiolmdb.open(path, max_dbs=10, readonly=True) db = env.open_db(b'node_schedules', create=False) self.assertIsNotNone(db)
def test_readonly_true_exist(self): path, env = self.create_env() env2 = aiolmdb.open(path, readonly=True) assert env2.path() == path # Attempting a write txn should fail. @asyncio.coroutine def txn(txn): yield from env2.get_default_db().put(b'a', b'a') self.assertAsyncRaises(lmdb.ReadonlyError, txn) # Flag should be set. assert env2.flags()['readonly']
def test_weakref_callback_invoked_once(self): temp_dir = self.create_dir() env = aiolmdb.open(temp_dir) env.close() count = [0] def callback(ref): count[0] += 1 ref = weakref.ref(env, callback) env = None testlib.debug_collect() self.assertIsNone(ref()) self.assertEqual(count[0], 1)
def test_copy_compact(self): _, env = self.create_env() yield from env.get_default_db().put(b'a', b'b') dest_dir = self.create_dir() yield from env.copy(dest_dir, compact=True) assert os.path.exists(dest_dir + '/data.mdb') cenv = aiolmdb.open(dest_dir) self.assertEqual((yield from cenv.get_default_db().get(b'a')), b'b') env.close() @asyncio.coroutine def txn(): yield from env.copy(self.create_dir()) self.assertAsyncRaises(Exception, txn)
def test_copyfd_compact(self): path, env = self.create_env() yield from env.get_default_db().put(b'a', b'b') dst_path = self.create_file(create=False) fp = open(dst_path, 'wb') yield from env.copyfd(fp.fileno(), compact=True) dstenv = aiolmdb.open(dst_path, subdir=False) assert (yield from dstenv.get_default_db().get(b'a')) == b'b' env.close() @asyncio.coroutine def txn(): yield from env.copyfd(fp.fileno()) self.assertAsyncRaises(Exception, txn()) fp.close()
def test_subdir_true_exist_create(self): path, env = self.create_env() assert aiolmdb.open(path, subdir=True, create=True).path() == path
def test_readonly_env_main(self): path, env = self.create_env() env.close() env = aiolmdb.open(path, readonly=True) env.open_db(None)
def test_reopen(self): path, env = self.create_env() env.open_db(b'subdb1') env.close() env = aiolmdb.open(path, max_dbs=10) env.open_db(b'subdb1')
def _test_reader_check_child(path): """Function to run in child process since we can't use fork() on win32.""" env = aiolmdb.open(path, max_spare_txns=0) env.begin() os._exit(0)
def test_bad_paths(self): self.assertRaises(Exception, lambda: aiolmdb.open('/doesnt/exist/at/all')) self.assertRaises(Exception, lambda: aiolmdb.open(self.create_file()))
def create_env(self, path=None, max_dbs=10, **kwargs): if not path: path = self.create_dir() env = aiolmdb.open(path, max_dbs=max_dbs, **kwargs) self.cleanups.append(env.close) return path, env