From 3c479828a62c4d0f9ee706b9d4dcae4839c294f9 Mon Sep 17 00:00:00 2001 From: adam Date: Thu, 14 Mar 2013 11:43:24 +0700 Subject: [PATCH] #51 --- luaejdb/Makefile | 7 +++++-- luaejdb/luabson.c | 5 ++--- luaejdb/test/Makefile | 8 +++----- luaejdb/test/t1.lua | 45 ++++++++++++++++++++++++++++++++++----------- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/luaejdb/Makefile b/luaejdb/Makefile index 11f1b7d..7d324ac 100644 --- a/luaejdb/Makefile +++ b/luaejdb/Makefile @@ -1,11 +1,14 @@ -CFLAGS=-g -O0 -fPIC -std=c99 -Wall -D_GNU_SOURCE +CFLAGS=-g -O2 -fPIC -std=c99 -Wall -D_GNU_SOURCE build: luarocks --pack-binary-rock CFLAGS='$(CFLAGS)' make -test: build +check: build make -C ./test +check-valgrind: build + make RUNCMD="valgrind --tool=memcheck --leak-check=full --error-exitcode=1" check -C ./test + clean: rm -rf *.so *.rock make -C ./test clean diff --git a/luaejdb/luabson.c b/luaejdb/luabson.c index b540d0a..33e4321 100644 --- a/luaejdb/luabson.c +++ b/luaejdb/luabson.c @@ -46,9 +46,8 @@ int print_bson(lua_State *L) { void lua_push_bsontype_table(lua_State* L, int bsontype) { lua_newtable(L); //table lua_newtable(L); //metatable - lua_pushstring(L, "__bsontype"); lua_pushinteger(L, bsontype); - lua_settable(L, -3); + lua_setfield(L, -2, "__bsontype"); lua_setmetatable(L, -2); } @@ -68,7 +67,7 @@ static void lua_push_bson_value(lua_State *L, bson_iterator *it) { break; case BSON_NULL: case BSON_UNDEFINED: - lua_pushnil(L); + lua_push_bsontype_table(L, bt); break; case BSON_INT: lua_pushinteger(L, bson_iterator_int(it)); diff --git a/luaejdb/test/Makefile b/luaejdb/test/Makefile index 8ebba23..24a397f 100644 --- a/luaejdb/test/Makefile +++ b/luaejdb/test/Makefile @@ -1,9 +1,7 @@ +#RUNENV = LD_LIBRARY_PATH=.:.. DYLD_LIBRARY_PATH=.:.. - - -test: - lua ./t1.lua - +check: + $(RUNENV) $(RUNCMD) lua ./t1.lua clean: rm -rf ./testdb* diff --git a/luaejdb/test/t1.lua b/luaejdb/test/t1.lua index 6c869a4..75c749b 100644 --- a/luaejdb/test/t1.lua +++ b/luaejdb/test/t1.lua @@ -1,5 +1,3 @@ - - --package.path package.path = "../?.lua;" .. package.path package.cpath = "../?.so;" .. package.cpath @@ -10,10 +8,9 @@ assert(type(ejdb) == "table") local Q = ejdb.Q local db = ejdb:open("testdb"); -local q = Q("name", "Andy"):F("_id"):Eq("510f7fa91ad6270a00000000"):F("age"):Gt(20):Lt(40):F("score"):In({11, 22.12333, 1362835380447, db.toNull()}):Max(232); +local q = Q("name", "Andy"):F("_id"):Eq("510f7fa91ad6270a00000000"):F("age"):Gt(20):Lt(40):F("score"):In({ 11, 22.12333, 1362835380447, db.toNull() }):Max(232); -local sv = ejdb.print_bson(q:toBSON()); -local ev = [[.name(2)=Andy +assert([[.name(2)=Andy ._id(7)=510f7fa91ad6270a00000000 .age(3)= ..$gt(16)=20 @@ -27,18 +24,44 @@ local ev = [[.name(2)=Andy ...4(10)=BSON_NULL -]] -assert(sv == ev) +]] == ejdb.print_bson(q:toBSON())) + +local obj = ejdb.from_bson(q:toBSON()) +--print(inspect(obj)) +assert(obj.name == "Andy") +assert(obj._id == "510f7fa91ad6270a00000000") +assert(type(obj.age) == "table" and getmetatable(obj.age).__bsontype == db.BSON_OBJECT) +assert(obj["age"]["$gt"] == 20) +assert(obj["age"]["$lt"] == 40) +assert(type(obj.score) == "table" and getmetatable(obj.score).__bsontype == db.BSON_OBJECT) +assert(type(obj.score["$in"]) == "table" and getmetatable(obj.score["$in"]).__bsontype == db.BSON_ARRAY) +assert(#obj.score["$in"] == 4) -sv = q:toBSON(); -print(inspect(ejdb.from_bson(sv))) +-- Second +-- +q = Q("name", "Andy"):F("bdate"):Eq(db.toDate(1362835380447)):KV("car", Q("name", "Lamborghini"):KV("maxspeed", 320)):KV("dst", db.toNull()); +assert([[.name(2)=Andy +.bdate(9)=1362835380447 +.car(3)= +..name(2)=Lamborghini +..maxspeed(16)=320 +.dst(10)=BSON_NULL +]] == ejdb.print_bson(q:toBSON())); + +obj = ejdb.from_bson(q:toBSON()) +assert(obj.name == "Andy") +assert(type(obj.bdate) == "table" and getmetatable(obj.bdate).__bsontype == db.BSON_DATE) +assert(obj.bdate[1] == 1362835380447) +assert(type(obj.dst) == "table" and getmetatable(obj.dst).__bsontype == db.BSON_NULL) ---print(inspect(q._oarr)) ---print(db:find("mycoll", q)) db:close() +collectgarbage() +collectgarbage() +collectgarbage() + -- 2.7.4