LuaSQLite3

Check-in [4e931d1ae0]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add unit tests to confirm proper NUL handling when fetching column.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 4e931d1ae0779bed682a8c412eb8634bd92af050
User & Date: e 2016-11-02 15:17:48
Context
2016-11-02
17:38
Implement suggested lua_createtable() performance improvement from Egil in ticket [0439ca1556]. check-in: a56548c06d user: e tags: trunk
15:17
Add unit tests to confirm proper NUL handling when fetching column. check-in: 4e931d1ae0 user: e tags: trunk
2015-01-07
02:12
Hack test file so it parses in Lua < 5.3 check-in: 7c4cdefcb9 user: e tags: fsl_9w, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to test/tests-sqlite3.lua.

1001
1002
1003
1004
1005
1006
1007
1008






























































1009
1010
    for row in colla.db:nrows('SELECT * FROM test WHERE content="hElLo wOrLd"') do
      -- print(row.id,row.content)
      assert_equal (row.content:lower(), "hello world")
      n = n + 1
    end
    assert_equal (n, 3)
end































































lunit.main(arg)









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
    for row in colla.db:nrows('SELECT * FROM test WHERE content="hElLo wOrLd"') do
      -- print(row.id,row.content)
      assert_equal (row.content:lower(), "hello world")
      n = n + 1
    end
    assert_equal (n, 3)
end

--------------------------------------
-- Tests for NULs in BLOBs and TEXT --
--------------------------------------

local db_blobNULs = lunit_TestCase("Blob NULs")

function db_blobNULs.setup()
  db_blobNULs.db = assert( sqlite3.open_memory() )
  assert_equal( sqlite3.OK, db_blobNULs.db:exec("CREATE TABLE test (id, name blob)") )
  assert_equal( sqlite3.OK, db_blobNULs.db:exec("INSERT INTO test VALUES (1, CAST('Hello' || x'00' || 'World' || x'00' AS BLOB))") )
  assert_equal( sqlite3.OK, db_blobNULs.db:exec("INSERT INTO test VALUES (2, CAST('Hello' || x'00' || 'Lua' || x'00' AS BLOB))") )
  assert_equal( sqlite3.OK, db_blobNULs.db:exec("INSERT INTO test VALUES (3, CAST('Hello' || x'00' || 'SQLite' || x'00' AS BLOB))") )
end

function db_blobNULs.teardown()
  assert( db_blobNULs.db:close() )
end

function db_blobNULs.test()
    local db = db_blobNULs.db
    for row in db:nrows("SELECT id as val FROM test WHERE name=CAST('Hello' || x'00' || 'World' || x'00' AS BLOB)") do
      assert_equal (row.val, 1)
    end
    for row in db:nrows("SELECT substr(name,7,4) as val FROM test WHERE id = 2") do
      assert_equal (row.val,'Lua\0')
      assert_equal (#row.val, 4)
    end
    for row in db:nrows("SELECT name as val FROM test WHERE id = 3") do
      assert_equal (row.val, 'Hello\0SQLite\0')
      assert_equal (#row.val, 13)
    end
end

local db_textNULs = lunit_TestCase("Text NULs")

function db_textNULs.setup()
  db_textNULs.db = assert( sqlite3.open_memory() )
  assert_equal( sqlite3.OK, db_textNULs.db:exec("CREATE TABLE test (id, name text)") )
  assert_equal( sqlite3.OK, db_textNULs.db:exec("INSERT INTO test VALUES (1, CAST('Hello' || x'00' || 'World' || x'00' AS TEXT))") )
  assert_equal( sqlite3.OK, db_textNULs.db:exec("INSERT INTO test VALUES (2, CAST('Hello' || x'00' || 'Lua' || x'00' AS TEXT))") )
  assert_equal( sqlite3.OK, db_textNULs.db:exec("INSERT INTO test VALUES (3, CAST('Hello' || x'00' || 'SQLite' || x'00' AS TEXT))") )
end

function db_textNULs.teardown()
  assert( db_textNULs.db:close() )
end

function db_textNULs.test()
    local db = db_textNULs.db
    for row in db:nrows("SELECT id as val FROM test WHERE name=CAST('Hello' || x'00' || 'World' || x'00' AS TEXT)") do
      assert_equal (row.val, 1)
    end
    for row in db:nrows("SELECT substr(CAST(name AS BLOB),7,4) as val FROM test WHERE id = 2") do
      assert_equal (row.val,'Lua\0')
      assert_equal (#row.val, 4)
    end
    for row in db:nrows("SELECT name as val FROM test WHERE id = 3") do
      assert_equal (row.val, 'Hello\0SQLite\0')
      assert_equal (#row.val, 13)
    end
end

lunit.main(arg)