From: Vsevolod Stakhov Date: Mon, 25 Jan 2016 16:16:19 +0000 (+0000) Subject: Add lua_sqlite3 unit tests X-Git-Tag: 1.1.2~44 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2a81086ec387a9a874900bceed0b0cca2032bdc9;p=thirdparty%2Frspamd.git Add lua_sqlite3 unit tests --- diff --git a/src/lua/lua_sqlite3.c b/src/lua/lua_sqlite3.c index 34a5a9e194..4442cacabe 100644 --- a/src/lua/lua_sqlite3.c +++ b/src/lua/lua_sqlite3.c @@ -136,7 +136,7 @@ lua_sqlite3_bind_statements (lua_State *L, gint start, gint end, gsize slen; gdouble n; - g_assert (start < end && start > 0 && end > 0); + g_assert (start <= end && start > 0 && end > 0); for (i = start; i <= end; i ++) { type = lua_type (L, i); @@ -194,7 +194,7 @@ lua_sqlite3_sql (lua_State *L) if (top > 2) { /* Push additional arguments to sqlite3 */ - lua_sqlite3_bind_statements (L, 2, top, stmt); + lua_sqlite3_bind_statements (L, 3, top, stmt); } rc = sqlite3_step (stmt); @@ -305,7 +305,7 @@ lua_sqlite3_rows (lua_State *L) if (top > 2) { /* Push additional arguments to sqlite3 */ - lua_sqlite3_bind_statements (L, 2, top, stmt); + lua_sqlite3_bind_statements (L, 3, top, stmt); } /* Create C closure */ diff --git a/test/lua/unit/sqlite3.lua b/test/lua/unit/sqlite3.lua new file mode 100644 index 0000000000..9fec2b6941 --- /dev/null +++ b/test/lua/unit/sqlite3.lua @@ -0,0 +1,49 @@ +context("Sqlite3 API", function() + local sqlite3 = require "rspamd_sqlite3" + + test("Sqlite3 open", function() + os.remove('/tmp/rspamd_unit_test_sqlite3.sqlite') + local db = sqlite3.open('/tmp/rspamd_unit_test_sqlite3.sqlite') + assert_not_nil(db, "should be able to create sqlite3 db") + db = sqlite3.open('/non/existent/path/rspamd_unit_test_sqlite3.sqlite') + assert_nil(db, "should not be able to create sqlite3 db") + os.remove('/tmp/rspamd_unit_test_sqlite3.sqlite') + end) + + test("Sqlite3 query", function() + os.remove('/tmp/rspamd_unit_test_sqlite3-1.sqlite') + local db = sqlite3.open('/tmp/rspamd_unit_test_sqlite3-1.sqlite') + assert_not_nil(db, "should be able to create sqlite3 db") + + local ret = db:sql([[ + CREATE TABLE x (id INT, value TEXT); + ]]) + assert_true(ret, "should be able to create table") + local ret = db:sql([[ + INSERT INTO x VALUES (?1, ?2); + ]], 1, 'test') + assert_true(ret, "should be able to insert row") + os.remove('/tmp/rspamd_unit_test_sqlite3-1.sqlite') + end) + + test("Sqlite3 rows", function() + os.remove('/tmp/rspamd_unit_test_sqlite3-2.sqlite') + local db = sqlite3.open('/tmp/rspamd_unit_test_sqlite3-2.sqlite') + assert_not_nil(db, "should be able to create sqlite3 db") + + local ret = db:sql([[ + CREATE TABLE x (id INT, value TEXT); + ]]) + assert_true(ret, "should be able to create table") + local ret = db:sql([[ + INSERT INTO x VALUES (?1, ?2); + ]], 1, 'test') + assert_true(ret, "should be able to insert row") + + for row in db:rows([[SELECT * FROM x;]]) do + assert_equal(row.id, 1) + assert_equal(row.value, 'test') + end + os.remove('/tmp/rspamd_unit_test_sqlite3-2.sqlite') + end) +end) \ No newline at end of file