def test_count_prefix(testset1): with TemporaryDirectory() as dbpath: print('Using temporary directory {} for database'.format(dbpath)) schema = Schema3() with zlmdb.Database(dbpath) as db: with db.begin(write=True) as txn: for user in testset1: schema.users[txn, user.authid] = user n = len(testset1) tests = [ (None, n), (u'', n), (u'test-', n), (u'test-1', 111), (u'test-11', 11), (u'test-111', 1), ] with zlmdb.Database(dbpath) as db: with db.begin() as txn: for prefix, num in tests: cnt = schema.users.count(txn, prefix) assert cnt == num
def test_fill_check(testset1): with TemporaryDirectory() as dbpath: print('Using temporary directory {} for database'.format(dbpath)) schema = Schema3() with zlmdb.Database(dbpath) as db: with db.begin(write=True) as txn: for user in testset1: schema.users[txn, user.authid] = user with zlmdb.Database(dbpath) as db: with db.begin() as txn: for user in testset1: _user = schema.users[txn, user.authid] assert _user assert _user == user
def test_select(testset1): testset1_keys = set([user.authid for user in testset1]) with TemporaryDirectory() as dbpath: print('Using temporary directory {} for database'.format(dbpath)) schema = Schema3() with zlmdb.Database(dbpath) as db: with db.begin(write=True) as txn: for user in testset1: schema.users[txn, user.authid] = user with zlmdb.Database(dbpath) as db: with db.begin() as txn: i = 0 for authid, user in schema.users.select(txn): i += 1 assert user assert authid == user.authid assert authid in testset1_keys
def test_count_all(testset1): with TemporaryDirectory() as dbpath: print('Using temporary directory {} for database'.format(dbpath)) schema = Schema3() with zlmdb.Database(dbpath) as db: # count on empty table with db.begin() as txn: cnt = schema.users.count(txn) assert cnt == 0 # fill (and count on each insert) with db.begin(write=True) as txn: i = 0 for user in testset1: schema.users[txn, user.authid] = user i += 1 # table count within filling transaction cnt = schema.users.count(txn) assert cnt == i # table count within transaction cnt = schema.users.count(txn) assert cnt == len(testset1) # table count in new transaction with db.begin() as txn: cnt = schema.users.count(txn) assert cnt == len(testset1) # table count in new connection with zlmdb.Database(dbpath) as db: with db.begin() as txn: cnt = schema.users.count(txn) assert cnt == len(testset1)