mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-22 02:53:09 +00:00
1. disambiguation: testdb --> testcoll (test collection)
2. let `c` as cllient, `db` as `c[db_name]` 3. clear up as lua-check
This commit is contained in:
@@ -20,6 +20,7 @@ local function _create_client()
|
||||
end
|
||||
|
||||
function test_auth()
|
||||
local ok, err, ret
|
||||
local c = mongo.client(
|
||||
{
|
||||
host = host, port = port,
|
||||
@@ -28,64 +29,71 @@ function test_auth()
|
||||
local db = c[db_name]
|
||||
db:auth(username, password)
|
||||
|
||||
db.testdb:dropIndex("*")
|
||||
db.testdb:drop()
|
||||
db.testcoll:dropIndex("*")
|
||||
db.testcoll:drop()
|
||||
|
||||
local ok, err, ret = db.testdb:safe_insert({test_key = 1});
|
||||
ok, err, ret = db.testcoll:safe_insert({test_key = 1});
|
||||
assert(ok and ret and ret.n == 1, err)
|
||||
|
||||
local ok, err, ret = db.testdb:safe_insert({test_key = 1});
|
||||
ok, err, ret = db.testcoll:safe_insert({test_key = 1});
|
||||
assert(ok and ret and ret.n == 1, err)
|
||||
end
|
||||
|
||||
function test_insert_without_index()
|
||||
local db = _create_client()
|
||||
db[db_name].testdb:dropIndex("*")
|
||||
db[db_name].testdb:drop()
|
||||
local ok, err, ret
|
||||
local c = _create_client()
|
||||
local db = c[db_name]
|
||||
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1});
|
||||
db.testcoll:dropIndex("*")
|
||||
db.testcoll:drop()
|
||||
|
||||
ok, err, ret = db.testcoll:safe_insert({test_key = 1});
|
||||
assert(ok and ret and ret.n == 1, err)
|
||||
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1});
|
||||
ok, err, ret = db.testcoll:safe_insert({test_key = 1});
|
||||
assert(ok and ret and ret.n == 1, err)
|
||||
end
|
||||
|
||||
function test_insert_with_index()
|
||||
local db = _create_client()
|
||||
local ok, err, ret
|
||||
local c = _create_client()
|
||||
local db = c[db_name]
|
||||
|
||||
db[db_name].testdb:dropIndex("*")
|
||||
db[db_name].testdb:drop()
|
||||
db.testcoll:dropIndex("*")
|
||||
db.testcoll:drop()
|
||||
|
||||
db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"})
|
||||
db.testcoll:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"})
|
||||
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1})
|
||||
assert(ok and ret and ret.n == 1)
|
||||
ok, err, ret = db.testcoll:safe_insert({test_key = 1})
|
||||
assert(ok and ret and ret.n == 1, err)
|
||||
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1})
|
||||
assert(ok == false and string.find(err, "duplicate key error"))
|
||||
ok, err, ret = db.testcoll:safe_insert({test_key = 1})
|
||||
assert(ok == false and string.find(err, "duplicate key error"))
|
||||
end
|
||||
|
||||
function test_find_and_remove()
|
||||
local db = _create_client()
|
||||
local ok, err, ret
|
||||
local c = _create_client()
|
||||
local db = c[db_name]
|
||||
|
||||
db[db_name].testdb:dropIndex("*")
|
||||
db[db_name].testdb:drop()
|
||||
db.testcoll:dropIndex("*")
|
||||
db.testcoll:drop()
|
||||
|
||||
db[db_name].testdb:ensureIndex({test_key = 1}, {test_key2 = -1}, {unique = true, name = "test_index"})
|
||||
db.testcoll:ensureIndex({test_key = 1}, {test_key2 = -1}, {unique = true, name = "test_index"})
|
||||
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 1})
|
||||
ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_key2 = 1})
|
||||
assert(ok and ret and ret.n == 1, err)
|
||||
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 2})
|
||||
ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_key2 = 2})
|
||||
assert(ok and ret and ret.n == 1, err)
|
||||
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 2, test_key2 = 3})
|
||||
ok, err, ret = db.testcoll:safe_insert({test_key = 2, test_key2 = 3})
|
||||
assert(ok and ret and ret.n == 1, err)
|
||||
|
||||
local ret = db[db_name].testdb:findOne({test_key2 = 1})
|
||||
ret = db.testcoll:findOne({test_key2 = 1})
|
||||
assert(ret and ret.test_key2 == 1, err)
|
||||
|
||||
local ret = db[db_name].testdb:find({test_key2 = {['$gt'] = 0}}):sort({test_key = 1}, {test_key2 = -1}):skip(1):limit(1)
|
||||
ret = db.testcoll:find({test_key2 = {['$gt'] = 0}}):sort({test_key = 1}, {test_key2 = -1}):skip(1):limit(1)
|
||||
assert(ret:count() == 3)
|
||||
assert(ret:count(true) == 1)
|
||||
if ret:hasNext() then
|
||||
@@ -93,33 +101,34 @@ function test_find_and_remove()
|
||||
end
|
||||
assert(ret and ret.test_key2 == 1)
|
||||
|
||||
db[db_name].testdb:delete({test_key = 1})
|
||||
db[db_name].testdb:delete({test_key = 2})
|
||||
db.testcoll:delete({test_key = 1})
|
||||
db.testcoll:delete({test_key = 2})
|
||||
|
||||
local ret = db[db_name].testdb:findOne({test_key = 1})
|
||||
ret = db.testcoll:findOne({test_key = 1})
|
||||
assert(ret == nil)
|
||||
end
|
||||
|
||||
|
||||
function test_expire_index()
|
||||
local db = _create_client()
|
||||
local ok, err, ret
|
||||
local c = _create_client()
|
||||
local db = c[db_name]
|
||||
|
||||
db[db_name].testdb:dropIndex("*")
|
||||
db[db_name].testdb:drop()
|
||||
db.testcoll:dropIndex("*")
|
||||
db.testcoll:drop()
|
||||
|
||||
db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index", expireAfterSeconds = 1, })
|
||||
db[db_name].testdb:ensureIndex({test_date = 1}, {expireAfterSeconds = 1, })
|
||||
db.testcoll:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index", expireAfterSeconds = 1, })
|
||||
db.testcoll:ensureIndex({test_date = 1}, {expireAfterSeconds = 1, })
|
||||
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1, test_date = bson.date(os.time())})
|
||||
ok, err, ret = db.testcoll:safe_insert({test_key = 1, test_date = bson.date(os.time())})
|
||||
assert(ok and ret and ret.n == 1, err)
|
||||
|
||||
local ret = db[db_name].testdb:findOne({test_key = 1})
|
||||
ret = db.testcoll:findOne({test_key = 1})
|
||||
assert(ret and ret.test_key == 1)
|
||||
|
||||
for i = 1, 60 do
|
||||
skynet.sleep(100);
|
||||
print("check expire", i)
|
||||
local ret = db[db_name].testdb:findOne({test_key = 1})
|
||||
ret = db.testcoll:findOne({test_key = 1})
|
||||
if ret == nil then
|
||||
return
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user