1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 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 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
|
--[[--------------------------------------------------------------------------
Author: Michael Roth <mroth@nessie.de>
Copyright (c) 2004, 2005 Michael Roth <mroth@nessie.de>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--]]--------------------------------------------------------------------------
-- extended for LuaSQLite3, and for Lua 5.2 (using lunitx)
-- Copyright (c) 2005-13 Doug Currie
-- Same license as above
local sqlite3 = require(arg[1]) -- "lsqlite3complete" or "lsqlite3"
local os = os
--local lunit = require "lunitx"
local lunit = require "lunit"
local tests_sqlite3
if _VERSION >= 'Lua 5.2' then
tests_sqlite3 = lunit.module('tests-sqlite3','seeall')
_ENV = tests_sqlite3
else
module('tests_sqlite3', lunit.testcase, package.seeall)
tests_sqlite3 = _M
end
-- compat
function lunit_wrap (name, fcn)
tests_sqlite3['test_o_'..name] = fcn
end
function lunit_TestCase (name)
return lunit.module(name,'seeall')
end
-------------------------------
-- Print library versions --
-------------------------------
print ("SQLite v", sqlite3.version())
print ("lsqlite v", sqlite3.lversion())
-------------------------------
-- Basic open and close test --
-------------------------------
lunit_wrap("open_memory", function()
local db = assert_userdata( sqlite3.open_memory() )
assert( db:close() )
end)
lunit_wrap("open", function()
local filename = "/tmp/__lua-sqlite3-20040906135849." .. os.time()
local db = assert_userdata( sqlite3.open(filename) )
assert( db:close() )
os.remove(filename)
end)
-------------------------------------
-- Presence of db member functions --
-------------------------------------
local db_funcs = lunit_TestCase("Database Member Functions")
function db_funcs.setup()
db_funcs.db = assert( sqlite3.open_memory() )
end
function db_funcs.teardown()
assert( db_funcs.db:close() )
end
function db_funcs.test()
local db = db_funcs.db
assert_function( db.close )
assert_function( db.exec )
--e assert_function( db.irows )
assert_function( db.rows )
--e assert_function( db.cols )
--e assert_function( db.first_irow )
--e assert_function( db.first_row )
--e assert_function( db.first_cols )
assert_function( db.prepare )
assert_function( db.interrupt )
assert_function( db.last_insert_rowid )
assert_function( db.changes )
assert_function( db.total_changes )
end
---------------------------------------
-- Presence of stmt member functions --
---------------------------------------
local stmt_funcs = lunit_TestCase("Statement Member Functions")
function stmt_funcs.setup()
stmt_funcs.db = assert( sqlite3.open_memory() )
stmt_funcs.stmt = assert( stmt_funcs.db:prepare("CREATE TABLE test (id, content)") )
end
function stmt_funcs.teardown()
--e- assert( stmt_funcs.stmt:close() )
assert( stmt_funcs.stmt:finalize() ) --e+
assert( stmt_funcs.db:close() )
end
function stmt_funcs.test()
local stmt = stmt_funcs.stmt
--e assert_function( stmt.close )
assert_function( stmt.reset )
--e assert_function( stmt.exec )
assert_function( stmt.bind )
--e assert_function( stmt.irows )
--e assert_function( stmt.rows )
--e assert_function( stmt.cols )
--e assert_function( stmt.first_irow )
--e assert_function( stmt.first_row )
--e assert_function( stmt.first_cols )
--e assert_function( stmt.column_names )
--e assert_function( stmt.column_decltypes )
--e assert_function( stmt.column_count )
--e +
assert_function( stmt.isopen )
assert_function( stmt.step )
assert_function( stmt.reset )
assert_function( stmt.finalize )
assert_function( stmt.columns )
assert_function( stmt.bind )
assert_function( stmt.bind_values )
assert_function( stmt.bind_names )
assert_function( stmt.bind_blob )
assert_function( stmt.bind_parameter_count )
assert_function( stmt.bind_parameter_name )
assert_function( stmt.get_value )
assert_function( stmt.get_values )
assert_function( stmt.get_name )
assert_function( stmt.get_names )
assert_function( stmt.get_type )
assert_function( stmt.get_types )
assert_function( stmt.get_uvalues )
assert_function( stmt.get_unames )
assert_function( stmt.get_utypes )
assert_function( stmt.get_named_values )
assert_function( stmt.get_named_types )
assert_function( stmt.idata )
assert_function( stmt.inames )
assert_function( stmt.itypes )
assert_function( stmt.data )
assert_function( stmt.type )
--e +
end
------------------
-- Tests basics --
------------------
local basics = lunit_TestCase("Basics")
function basics.setup()
basics.db = assert_userdata( sqlite3.open_memory() )
end
function basics.teardown()
assert_number( basics.db:close() )
end
function basics.create_table()
assert_number( basics.db:exec("CREATE TABLE test (id, name)") )
end
function basics.drop_table()
assert_number( basics.db:exec("DROP TABLE test") )
end
function basics.insert(id, name)
assert_number( basics.db:exec("INSERT INTO test VALUES ("..id..", '"..name.."')") )
end
function basics.update(id, name)
assert_number( basics.db:exec("UPDATE test SET name = '"..name.."' WHERE id = "..id) )
end
function basics.test_create_drop()
basics.create_table()
basics.drop_table()
end
function basics.test_multi_create_drop()
basics.create_table()
basics.drop_table()
basics.create_table()
basics.drop_table()
end
function basics.test_insert()
basics.create_table()
basics.insert(1, "Hello World")
basics.insert(2, "Hello Lua")
basics.insert(3, "Hello sqlite3")
end
function basics.test_update()
basics.create_table()
basics.insert(1, "Hello Home")
basics.insert(2, "Hello Lua")
basics.update(1, "Hello World")
end
---------------------------------
-- Statement Column Info Tests --
---------------------------------
lunit_wrap("Column Info Test", function()
local db = assert_userdata( sqlite3.open_memory() )
assert_number( db:exec("CREATE TABLE test (id INTEGER, name TEXT)") )
local stmt = assert_userdata( db:prepare("SELECT * FROM test") )
assert_equal(2, stmt:columns(), "Wrong number of columns." )
local names = assert_table( stmt:get_names() )
assert_equal(2, #(names), "Wrong number of names.")
assert_equal("id", names[1] )
assert_equal("name", names[2] )
local types = assert_table( stmt:get_types() )
assert_equal(2, #(types), "Wrong number of declaration types.")
assert_equal("INTEGER", types[1] )
assert_equal("TEXT", types[2] )
assert_equal( sqlite3.OK, stmt:finalize() )
assert_equal( sqlite3.OK, db:close() )
end)
---------------------
-- Statement Tests --
---------------------
st = lunit_TestCase("Statement Tests")
function st.setup()
st.db = assert( sqlite3.open_memory() )
assert_equal( sqlite3.OK, st.db:exec("CREATE TABLE test (id, name)") )
assert_equal( sqlite3.OK, st.db:exec("INSERT INTO test VALUES (1, 'Hello World')") )
assert_equal( sqlite3.OK, st.db:exec("INSERT INTO test VALUES (2, 'Hello Lua')") )
assert_equal( sqlite3.OK, st.db:exec("INSERT INTO test VALUES (3, 'Hello sqlite3')") )
end
function st.teardown()
assert_equal( sqlite3.OK, st.db:close() )
end
function st.check_content(expected)
local stmt = assert( st.db:prepare("SELECT * FROM test ORDER BY id") )
local i = 0
for row in stmt:rows() do
i = i + 1
assert( i <= #(expected), "Too many rows." )
assert_equal(2, #(row), "Two result column expected.")
assert_equal(i, row[1], "Wrong 'id'.")
assert_equal(expected[i], row[2], "Wrong 'name'.")
end
assert_equal( #(expected), i, "Too few rows." )
assert_number( stmt:finalize() )
end
function st.test_setup()
assert_pass(function() st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3" } end)
assert_error(function() st.check_content{ "Hello World", "Hello Lua" } end)
assert_error(function() st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "To much" } end)
assert_error(function() st.check_content{ "Hello World", "Hello Lua", "Wrong" } end)
assert_error(function() st.check_content{ "Hello World", "Wrong", "Hello sqlite3" } end)
assert_error(function() st.check_content{ "Wrong", "Hello Lua", "Hello sqlite3" } end)
end
function st.test_questionmark_args()
local stmt = assert_userdata( st.db:prepare("INSERT INTO test VALUES (?, ?)") )
assert_number( stmt:bind_values(0, "Test") )
assert_error(function() stmt:bind_values("To few") end)
assert_error(function() stmt:bind_values(0, "Test", "To many") end)
end
function st.test_questionmark()
local stmt = assert_userdata( st.db:prepare("INSERT INTO test VALUES (?, ?)") )
assert_number( stmt:bind_values(4, "Good morning") )
assert_number( stmt:step() )
assert_number( stmt:reset() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
assert_number( stmt:bind_values(5, "Foo Bar") )
assert_number( stmt:step() )
assert_number( stmt:reset() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
assert_number( stmt:finalize() )
end
--[===[
function st.test_questionmark_multi()
local stmt = assert_userdata( st.db:prepare([[
INSERT INTO test VALUES (?, ?); INSERT INTO test VALUES (?, ?) ]]))
assert( stmt:bind_values(5, "Foo Bar", 4, "Good morning") )
assert_number( stmt:step() )
assert_number( stmt:reset() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
assert_number( stmt:finalize() )
end
]===]
function st.test_identifiers()
local stmt = assert_userdata( st.db:prepare("INSERT INTO test VALUES (:id, :name)") )
assert_number( stmt:bind_values(4, "Good morning") )
assert_number( stmt:step() )
assert_number( stmt:reset() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
assert_number( stmt:bind_values(5, "Foo Bar") )
assert_number( stmt:step() )
assert_number( stmt:reset() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
assert_number( stmt:finalize() )
end
--[===[
function st.test_identifiers_multi()
local stmt = assert_table( st.db:prepare([[
INSERT INTO test VALUES (:id1, :name1); INSERT INTO test VALUES (:id2, :name2) ]]))
assert( stmt:bind_values(5, "Foo Bar", 4, "Good morning") )
assert( stmt:exec() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
end
]===]
function st.test_identifiers_names()
--local stmt = assert_userdata( st.db:prepare({"name", "id"}, "INSERT INTO test VALUES (:id, $name)") )
local stmt = assert_userdata( st.db:prepare("INSERT INTO test VALUES (:id, $name)") )
assert_number( stmt:bind_names({name="Good morning", id=4}) )
assert_number( stmt:step() )
assert_number( stmt:reset() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
assert_number( stmt:bind_names({name="Foo Bar", id=5}) )
assert_number( stmt:step() )
assert_number( stmt:reset() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
assert_number( stmt:finalize() )
end
--[===[
function st:test_identifiers_multi_names()
local stmt = assert_table( st.db:prepare( {"name", "id1", "id2"},[[
INSERT INTO test VALUES (:id1, $name); INSERT INTO test VALUES ($id2, :name) ]]))
assert( stmt:bind_values("Hoho", 4, 5) )
assert( stmt:exec() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Hoho", "Hoho" }
end
]===]
function st.test_colon_identifiers_names()
local stmt = assert_userdata( st.db:prepare("INSERT INTO test VALUES (:id, :name)") )
assert_number( stmt:bind_names({name="Good morning", id=4}) )
assert_number( stmt:step() )
assert_number( stmt:reset() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
assert_number( stmt:bind_names({name="Foo Bar", id=5}) )
assert_number( stmt:step() )
assert_number( stmt:reset() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
assert_number( stmt:finalize() )
end
--[===[
function st:test_colon_identifiers_multi_names()
local stmt = assert_table( st.db:prepare( {":name", ":id1", ":id2"},[[
INSERT INTO test VALUES (:id1, $name); INSERT INTO test VALUES ($id2, :name) ]]))
assert( stmt:bind_values("Hoho", 4, 5) )
assert( stmt:exec() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Hoho", "Hoho" }
end
function st.test_dollar_identifiers_names()
local stmt = assert_table( st.db:prepare({"$name", "$id"}, "INSERT INTO test VALUES (:id, $name)") )
assert_table( stmt:bind_values("Good morning", 4) )
assert_table( stmt:exec() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
assert_table( stmt:bind_values("Foo Bar", 5) )
assert_table( stmt:exec() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
end
function st.test_dollar_identifiers_multi_names()
local stmt = assert_table( st.db:prepare( {"$name", "$id1", "$id2"},[[
INSERT INTO test VALUES (:id1, $name); INSERT INTO test VALUES ($id2, :name) ]]))
assert( stmt:bind_values("Hoho", 4, 5) )
assert( stmt:exec() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Hoho", "Hoho" }
end
]===]
function st.test_bind_by_names()
local stmt = assert_userdata( st.db:prepare("INSERT INTO test VALUES (:id, :name)") )
local args = { }
args.id = 5
args.name = "Hello girls"
assert( stmt:bind_names(args) )
assert_number( stmt:step() )
assert_number( stmt:reset() )
args.id = 4
args.name = "Hello boys"
assert( stmt:bind_names(args) )
assert_number( stmt:step() )
assert_number( stmt:reset() )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Hello boys", "Hello girls" }
assert_number( stmt:finalize() )
end
function st.test_last_insert_rowid()
local stmt = assert_userdata( st.db:prepare("INSERT INTO test VALUES (:id, :name)") )
local args = { }
args.id = 5
args.name = "Hello girls"
assert( stmt:bind_names(args) )
assert_number( stmt:step() )
assert_number( stmt:reset() )
assert_number( stmt:last_insert_rowid() )
assert_equal( stmt:last_insert_rowid(), 4 )
args.id = 4
args.name = "Hello boys"
assert( stmt:bind_names(args) )
assert_number( stmt:step() )
assert_number( stmt:reset() )
assert_number( stmt:last_insert_rowid() )
assert_equal( stmt:last_insert_rowid(), 5 )
st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Hello boys", "Hello girls" }
assert_number( stmt:finalize() )
end
--------------------------------
-- Tests binding of arguments --
--------------------------------
b = lunit_TestCase("Binding Tests")
function b.setup()
b.db = assert( sqlite3.open_memory() )
assert_number( b.db:exec("CREATE TABLE test (id, name, u, v, w, x, y, z)") )
end
function b.teardown()
assert_number( b.db:close() )
end
function b.test_auto_parameter_names()
local stmt = assert_userdata( b.db:prepare("INSERT INTO test VALUES(:a, $b, :a2, :b2, $a, :b, $a3, $b3)") )
local parameters = assert_number( stmt:bind_parameter_count() )
assert_equal( 8, parameters )
assert_equal( ":a", stmt:bind_parameter_name(1) )
assert_equal( "$b", stmt:bind_parameter_name(2) )
assert_equal( ":a2", stmt:bind_parameter_name(3) )
assert_equal( ":b2", stmt:bind_parameter_name(4) )
assert_equal( "$a", stmt:bind_parameter_name(5) )
assert_equal( ":b", stmt:bind_parameter_name(6) )
assert_equal( "$a3", stmt:bind_parameter_name(7) )
assert_equal( "$b3", stmt:bind_parameter_name(8) )
end
function b.test_auto_parameter_names()
local stmt = assert_userdata( b.db:prepare("INSERT INTO test VALUES($a, $b, $a2, $b2, $a, $b, $a3, $b3)") )
local parameters = assert_number( stmt:bind_parameter_count() )
assert_equal( 6, parameters )
assert_equal( "$a", stmt:bind_parameter_name(1) )
assert_equal( "$b", stmt:bind_parameter_name(2) )
assert_equal( "$a2", stmt:bind_parameter_name(3) )
assert_equal( "$b2", stmt:bind_parameter_name(4) )
assert_equal( "$a3", stmt:bind_parameter_name(5) )
assert_equal( "$b3", stmt:bind_parameter_name(6) )
end
function b.test_no_parameter_names_1()
local stmt = assert_userdata( b.db:prepare([[ SELECT * FROM test ]]))
local parameters = assert_number( stmt:bind_parameter_count() )
assert_equal( 0, (parameters) )
end
function b.test_no_parameter_names_2()
local stmt = assert_userdata( b.db:prepare([[ INSERT INTO test VALUES(?, ?, ?, ?, ?, ?, ?, ?) ]]))
local parameters = assert_number( stmt:bind_parameter_count() )
assert_equal( 8, (parameters) )
assert_nil( stmt:bind_parameter_name(1) )
end
----------------------------
-- Tests for update_hook --
----------------------------
uh = lunit_TestCase("Update Hook")
function uh.setup()
uh.db = assert( sqlite3.open_memory() )
uh.udtbl = {[sqlite3.INSERT]=0, [sqlite3.UPDATE]=0, [sqlite3.DELETE]=0}
uh.crtbl = {0, 0}
assert_nil(uh.db:update_hook( function(ud, op, dname, tname, rowid)
--print("Sqlite Update Hook:", op, dname, tname, rowid)
ud[op] = ud[op] + 1
end, uh.udtbl))
uh.uttblsz = function () local sz = 0; for _,_ in pairs(uh.udtbl) do sz = sz + 1 end return sz end
-- enable foreign keys!
assert_number( uh.db:exec("PRAGMA foreign_keys = ON;") )
assert_number( uh.db:exec("CREATE TABLE test ( id INTEGER PRIMARY KEY, content VARCHAR );") )
assert_number( uh.db:exec("CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );") )
assert_number( uh.db:exec("CREATE TABLE T2 ( id INTEGER PRIMARY KEY, content VARCHAR );") )
end
function uh.teardown()
--for k,v in pairs(uh.udtbl) do print(k,v) end
assert_equal( 3, uh.uttblsz() )
assert_number( uh.db:close() )
end
function uh.test_insert1()
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello World');") )
assert_equal( 1, uh.udtbl[sqlite3.INSERT] )
end
function uh.test_insert3()
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello World');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Lua');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Sqlite3');") )
assert_equal( 3, uh.udtbl[sqlite3.INSERT] )
assert_equal( 0, uh.udtbl[sqlite3.UPDATE] )
assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
end
function uh.test_insert3_update1()
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello World');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Lua');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Sqlite3');") )
assert_equal( sqlite3.OK, uh.db:exec("UPDATE test SET content = 'Hello Again World' WHERE id = 1;") )
assert_equal( 3, uh.udtbl[sqlite3.INSERT] )
assert_equal( 1, uh.udtbl[sqlite3.UPDATE] )
assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
end
function uh.test_insert3_delete1()
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello World');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Lua');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Sqlite3');") )
assert_equal( sqlite3.OK, uh.db:exec("DELETE FROM test WHERE id = 2;") )
assert_equal( 3, uh.udtbl[sqlite3.INSERT] )
assert_equal( 0, uh.udtbl[sqlite3.UPDATE] )
assert_equal( 1, uh.udtbl[sqlite3.DELETE] )
end
function uh.test_insert3_update1_delete1()
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello World');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Lua');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO test VALUES (NULL, 'Hello Sqlite3');") )
assert_equal( sqlite3.OK, uh.db:exec("UPDATE test SET content = 'Hello Again World' WHERE id = 1;") )
assert_equal( sqlite3.OK, uh.db:exec("DELETE FROM test WHERE id = 2;") )
assert_equal( 3, uh.udtbl[sqlite3.INSERT] )
assert_equal( 1, uh.udtbl[sqlite3.UPDATE] )
assert_equal( 1, uh.udtbl[sqlite3.DELETE] )
end
function uh.test_insert_select()
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello World');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello Lua');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello Sqlite3');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T2 SELECT * FROM T1;") )
assert_equal( 6, uh.udtbl[sqlite3.INSERT] )
assert_equal( 0, uh.udtbl[sqlite3.UPDATE] )
assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
end
function uh.test_trigger_insert()
assert_equal( sqlite3.OK, uh.db:exec([[
CREATE TRIGGER after_insert_T1
AFTER INSERT ON T1
FOR EACH ROW
BEGIN
INSERT INTO T2 VALUES(NEW.id, NEW.content);
END;
]]) )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello World');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello Lua');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello Sqlite3');") )
assert_equal( 6, uh.udtbl[sqlite3.INSERT] )
assert_equal( 0, uh.udtbl[sqlite3.UPDATE] )
assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
end
function uh.test_cascade_update()
assert_equal( sqlite3.OK, uh.db:exec("DROP TABLE T2;") )
assert_equal( sqlite3.OK, uh.db:exec([[
CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1 ON UPDATE CASCADE, content VARCHAR );
]]) )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'a');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'b');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'c');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'd');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T2 SELECT * FROM T1;") )
assert_equal( sqlite3.OK, uh.db:exec("UPDATE T1 SET id = id + 10 WHERE id < 3;") )
assert_equal( 8, uh.udtbl[sqlite3.INSERT] )
assert_equal( 4, uh.udtbl[sqlite3.UPDATE] )
assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
end
function uh.test_cascade_update_restrict()
assert_equal( sqlite3.OK, uh.db:exec("DROP TABLE T2;") )
assert_equal( sqlite3.OK, uh.db:exec([[
CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1 ON UPDATE RESTRICT, content VARCHAR );
]]) )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'a');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'b');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'c');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'd');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T2 SELECT * FROM T1;") )
-- the update_hook with rowid=11 *DOES* get triggered before the RESTRICT constraint is enforced
assert_equal( sqlite3.CONSTRAINT, uh.db:exec("UPDATE T1 SET id = id + 10 WHERE id < 3;") )
assert_equal( 8, uh.udtbl[sqlite3.INSERT] )
assert_equal( 1, uh.udtbl[sqlite3.UPDATE] )
assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
end
function uh.test_cascade_delete_restrict()
assert_equal( sqlite3.OK, uh.db:exec("DROP TABLE T2;") )
assert_equal( sqlite3.OK, uh.db:exec([[
CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1 ON DELETE RESTRICT, content VARCHAR );
]]) )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'a');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'b');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'c');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'd');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T2 SELECT * FROM T1;") )
-- the update_hook with rowid=1 *DOES* get triggered before the RESTRICT constraint is enforced
assert_equal( sqlite3.CONSTRAINT, uh.db:exec("DELETE FROM T1 WHERE id < 3;") )
assert_equal( 8, uh.udtbl[sqlite3.INSERT] )
assert_equal( 0, uh.udtbl[sqlite3.UPDATE] )
assert_equal( 1, uh.udtbl[sqlite3.DELETE] )
end
function uh.test_fk_violate_insert()
assert_equal( sqlite3.OK, uh.db:exec("DROP TABLE T2;") )
assert_equal( sqlite3.OK, uh.db:exec([[
CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1, content VARCHAR );
]]) )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'a');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'b');") )
-- no update hook triggered here
assert_equal( sqlite3.CONSTRAINT, uh.db:exec("INSERT INTO T2 VALUES(99, 'xxx')") )
assert_equal( 2, uh.udtbl[sqlite3.INSERT] )
assert_equal( 0, uh.udtbl[sqlite3.UPDATE] )
assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
end
function uh.test_fk_violate_update()
assert_equal( sqlite3.OK, uh.db:exec("DROP TABLE T2;") )
assert_equal( sqlite3.OK, uh.db:exec([[
CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1, content VARCHAR );
]]) )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'a');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T1 VALUES (NULL, 'b');") )
assert_equal( sqlite3.OK, uh.db:exec("INSERT INTO T2 VALUES (1, 'a');") )
-- update hook triggered
assert_equal( sqlite3.CONSTRAINT, uh.db:exec("UPDATE T2 SET id = 99 WHERE id = 1;") )
assert_equal( 3, uh.udtbl[sqlite3.INSERT] )
assert_equal( 1, uh.udtbl[sqlite3.UPDATE] )
assert_equal( 0, uh.udtbl[sqlite3.DELETE] )
end
----------------------------------------------
-- Tests for commit_hook and rollback_hook --
----------------------------------------------
crh = lunit_TestCase("Commit/Rollback Hook")
function crh.setup()
crh.db = assert( sqlite3.open_memory() )
crh.crtbl = {0, 0}
assert_number( crh.db:exec("PRAGMA foreign_keys = ON;") )
assert_number( crh.db:exec("CREATE TABLE T1 ( id INTEGER PRIMARY KEY, content VARCHAR );") )
assert_number( crh.db:exec("CREATE TABLE T2 ( id INTEGER PRIMARY KEY REFERENCES T1, content VARCHAR );") )
-- reset commit hook return value
crh.commit_hook_returnvalue = false
assert_nil( crh.db:commit_hook(function (ud)
-- print('Commit hook')
ud[1] = ud[1] + 1;
return crh.commit_hook_returnvalue
end, crh.crtbl))
assert_nil( crh.db:rollback_hook(function (ud)
-- print('Rollback hook')
ud[2] = ud[2] + 1
end, crh.crtbl))
end
function crh.teardown()
assert_number( crh.db:close() )
end
function crh.test_simple_transaction_commit()
assert_equal( sqlite3.OK, crh.db:exec("BEGIN;") )
assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello World');") )
assert_equal( sqlite3.OK, crh.db:exec("UPDATE T1 SET content = 'Hello Again World' WHERE id = 1;") )
assert_equal( sqlite3.OK, crh.db:exec("DELETE FROM T1 WHERE id = 2;") )
assert_equal( sqlite3.OK, crh.db:exec("COMMIT;") )
assert_equal( 1, crh.crtbl[1] )
assert_equal( 0, crh.crtbl[2] )
end
function crh.test_simple_transaction_prohibit()
-- if the commit hook returns anything except false or nil, the commit would turn into rollback
crh.commit_hook_returnvalue = 1
assert_equal( sqlite3.OK, crh.db:exec("BEGIN;") )
assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello World');") )
assert_equal( sqlite3.OK, crh.db:exec("UPDATE T1 SET content = 'Hello Again World' WHERE id = 1;") )
assert_equal( sqlite3.OK, crh.db:exec("DELETE FROM T1 WHERE id = 2;") )
assert_equal( sqlite3.CONSTRAINT, crh.db:exec("COMMIT;") )
-- commit hook gets called and returns 1 triggering a rollback
assert_equal( 1, crh.crtbl[1] )
assert_equal( 1, crh.crtbl[2] )
end
function crh.test_simple_transaction_rollback()
assert_equal( sqlite3.OK, crh.db:exec("BEGIN;") )
assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello World');") )
assert_equal( sqlite3.OK, crh.db:exec("UPDATE T1 SET content = 'Hello Again World' WHERE id = 1;") )
assert_equal( sqlite3.OK, crh.db:exec("DELETE FROM T1 WHERE id = 2;") )
assert_equal( sqlite3.OK, crh.db:exec("ROLLBACK;") )
assert_equal( 0, crh.crtbl[1] )
assert_equal( 1, crh.crtbl[2] )
end
function crh.test_statement_level_transaction()
assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello World');") )
assert_equal( sqlite3.OK, crh.db:exec("UPDATE T1 SET content = 'Hello Again World' WHERE id = 1;") )
assert_equal( sqlite3.OK, crh.db:exec("DELETE FROM T1 WHERE id = 2;") )
assert_equal( 3, crh.crtbl[1] )
assert_equal( 0, crh.crtbl[2] )
end
function crh.test_statement_level_fk_violate_insert()
assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T1 VALUES (NULL, 'a');") )
assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T1 VALUES (NULL, 'b');") )
-- implicit statement-level transaction rollback + error
assert_equal( sqlite3.CONSTRAINT, crh.db:exec("INSERT INTO T2 VALUES(99, 'xxx')") )
assert_equal( 2, crh.crtbl[1] )
assert_equal( 1, crh.crtbl[2] )
end
function crh.test_transaction_fk_violate_update()
assert_equal( sqlite3.OK, crh.db:exec("BEGIN;") )
assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T1 VALUES (NULL, 'Hello World');") )
assert_equal( sqlite3.OK, crh.db:exec("INSERT INTO T2 VALUES (1, 'xyz');") )
-- Doesn't trigger rollback hook because the implicit update statement transaction
-- is nested inside our explicit transaction. However we *do* get an error.
assert_equal( sqlite3.CONSTRAINT, crh.db:exec("UPDATE T2 SET id = 99 WHERE id = 1;") )
-- rollback explicitly
assert_equal( sqlite3.OK, crh.db:exec("ROLLBACK;") )
assert_equal( 0, crh.crtbl[1] )
assert_equal( 1, crh.crtbl[2] )
end
function crh.test_savepoint_nested_commit()
assert_equal( sqlite3.OK, crh.db:exec("SAVEPOINT S1;") )
assert_equal( sqlite3.OK, crh.db:exec(" INSERT INTO T1 VALUES (NULL, 'Hello World');") )
assert_equal( sqlite3.OK, crh.db:exec(" INSERT INTO T1 VALUES (NULL, 'Hello Lua');") )
assert_equal( sqlite3.OK, crh.db:exec(" SAVEPOINT S2;") )
assert_equal( sqlite3.OK, crh.db:exec(" INSERT INTO T1 VALUES (NULL, 'Hello Sqlite3');") )
-- nested transactions don't trigger commit/rollback hooks
assert_equal( sqlite3.OK, crh.db:exec(" RELEASE S2;") )
assert_equal( sqlite3.OK, crh.db:exec("RELEASE S1;") )
assert_equal( 1, crh.crtbl[1] )
assert_equal( 0, crh.crtbl[2] )
end
function crh.test_savepoint_nested_rollback()
assert_equal( sqlite3.OK, crh.db:exec("SAVEPOINT S1;") )
assert_equal( sqlite3.OK, crh.db:exec(" INSERT INTO T1 VALUES (NULL, 'Hello World');") )
assert_equal( sqlite3.OK, crh.db:exec(" INSERT INTO T1 VALUES (NULL, 'Hello Lua');") )
assert_equal( sqlite3.OK, crh.db:exec(" SAVEPOINT S2;") )
assert_equal( sqlite3.OK, crh.db:exec(" INSERT INTO T1 VALUES (NULL, 'Hello Sqlite3');") )
-- nested transactions don't trigger commit/rollback hooks
assert_equal( sqlite3.OK, crh.db:exec(" ROLLBACK TO S2;") )
assert_equal( sqlite3.OK, crh.db:exec("RELEASE S1;") )
assert_equal( 1, crh.crtbl[1] )
assert_equal( 0, crh.crtbl[2] )
end
--------------------------------------------
-- Tests loop break and statement reusage --
--------------------------------------------
----------------------------------------------------
-- Test garbage collected db with live statements --
----------------------------------------------------
gco = lunit_TestCase("Bug-Report: GC'd db with live prepared statements")
function gco.setup()
gco.db = assert( sqlite3.open_memory() )
assert_equal( sqlite3.OK, gco.db:exec("CREATE TABLE test (id, name)") )
assert_equal( sqlite3.OK, gco.db:exec("INSERT INTO test VALUES (1, 'Hello World')") )
assert_equal( sqlite3.OK, gco.db:exec("INSERT INTO test VALUES (2, 'Hello Lua')") )
assert_equal( sqlite3.OK, gco.db:exec("INSERT INTO test VALUES (3, 'Hello sqlite3')") )
end
-- gco.db:close()
function gco.test_gc()
local stmt = assert_userdata( gco.db:prepare("INSERT INTO test VALUES (?, ?)") )
assert_number( stmt:bind_values(4, "Good morning") )
assert_number( stmt:step() )
assert_number( stmt:reset() )
--st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning" }
gco.db = nil -- reap the db if unreferenced from stmt
collectgarbage()
assert_number( stmt:bind_values(5, "Foo Bar") )
assert_number( stmt:step() )
assert_number( stmt:reset() )
--st.check_content{ "Hello World", "Hello Lua", "Hello sqlite3", "Good morning", "Foo Bar" }
assert_number( stmt:finalize() )
end
function gco.teardown()
assert_true( (gco.db == nil) or (gco.db:close() == sqlite3.OK) )
end
if _VERSION >= 'Lua 5.3' then
-- this hack is avoid syntax errors in Lua < 5.3
local bitand = (load "return function (x, y) return x & y end")()
local bitshr = (load "return function (x, y) return x >> y end")()
l53 = lunit_TestCase("Lua 5.3 integers")
function l53.setup()
l53.db = assert( sqlite3.open_memory() )
assert_equal( sqlite3.OK, l53.db:exec("CREATE TABLE test (id, val)") )
assert_equal( sqlite3.OK, l53.db:exec("INSERT INTO test VALUES (1, 0x12345678abcdef09)") )
assert_equal( sqlite3.OK, l53.db:exec("INSERT INTO test VALUES (2, 5.1234567890123456)") )
assert_equal( sqlite3.OK, l53.db:exec("INSERT INTO test VALUES (3, 42)") )
end
-- l53.db:close()
function l53.test_intfloat()
local db = l53.db
for row in db:nrows("SELECT val FROM test WHERE id = 1") do
assert_equal (row.val, 0x12345678abcdef09)
assert_equal (bitand(row.val, 0xffffffff), 0xabcdef09)
assert_equal (bitshr(row.val, 32), 0x12345678)
end
for row in db:nrows("SELECT val FROM test WHERE id = 2") do
assert_equal (row.val, 5.1234567890123456)
end
for row in db:nrows("SELECT val FROM test WHERE id = 3") do
assert_equal (row.val, 42)
end
end
function l53.teardown()
assert_true( (l53.db == nil) or (l53.db:close() == sqlite3.OK) )
end
end
----------------------------
-- Test for bugs reported --
----------------------------
bug = lunit_TestCase("Bug-Report Tests")
function bug.setup()
bug.db = assert( sqlite3.open_memory() )
end
function bug.teardown()
assert_number( bug.db:close() )
end
--[===[
function bug.test_1()
bug.db:exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)")
local query = assert_userdata( bug.db:prepare("SELECT id FROM test WHERE value=?") )
assert_table ( query:bind_values("1") )
assert_nil ( query:first_cols() )
assert_table ( query:bind_values("2") )
assert_nil ( query:first_cols() )
end
]===]
function bug.test_nils() -- appeared in lua-5.1 (holes in arrays)
local function check(arg1, arg2, arg3, arg4, arg5)
assert_equal(1, arg1)
assert_equal(2, arg2)
assert_nil(arg3)
assert_equal(4, arg4)
assert_nil(arg5)
end
bug.db:create_function("test_nils", 5, function(arg1, arg2, arg3, arg4, arg5)
check(arg1, arg2, arg3, arg4, arg5)
end, {})
assert_number( bug.db:exec([[ SELECT test_nils(1, 2, NULL, 4, NULL) ]]) )
for arg1, arg2, arg3, arg4, arg5 in bug.db:urows([[ SELECT 1, 2, NULL, 4, NULL ]])
do check(arg1, arg2, arg3, arg4, arg5)
end
for row in bug.db:rows([[ SELECT 1, 2, NULL, 4, NULL ]])
do assert_table( row )
check(row[1], row[2], row[3], row[4], row[5])
end
end
----------------------------
-- Test for collation fun --
----------------------------
colla = lunit_TestCase("Collation Tests")
function colla.setup()
local function collate(s1,s2)
-- if p then print("collation callback: ",s1,s2) end
s1=s1:lower()
s2=s2:lower()
if s1==s2 then return 0
elseif s1<s2 then return -1
else return 1 end
end
colla.db = assert( sqlite3.open_memory() )
assert_nil(colla.db:create_collation('CINSENS',collate))
colla.db:exec[[
CREATE TABLE test(id INTEGER PRIMARY KEY,content COLLATE CINSENS);
INSERT INTO test VALUES(NULL,'hello world');
INSERT INTO test VALUES(NULL,'Buenos dias');
INSERT INTO test VALUES(NULL,'HELLO WORLD');
INSERT INTO test VALUES(NULL,'Guten Tag');
INSERT INTO test VALUES(NULL,'HeLlO WoRlD');
INSERT INTO test VALUES(NULL,'Bye for now');
]]
end
function colla.teardown()
assert_number( colla.db:close() )
end
function colla.test()
--for row in db:nrows('SELECT * FROM test') do
-- print(row.id,row.content)
--end
local n = 0
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
-------------------------------------
-- Tests for Online Backup API --
-------------------------------------
local db_bu = lunit_TestCase("Online Backup API")
function db_bu.setup()
db_bu.db_src = assert( sqlite3.open_memory() )
assert_equal( sqlite3.OK, db_bu.db_src:exec("CREATE TABLE test (id, name text)") )
assert_equal( sqlite3.OK, db_bu.db_src:exec("INSERT INTO test VALUES (1, 'Hello World')") )
assert_equal( sqlite3.OK, db_bu.db_src:exec("INSERT INTO test VALUES (2, 'Hello Lua')") )
assert_equal( sqlite3.OK, db_bu.db_src:exec("INSERT INTO test VALUES (3, 'Hello SQLite')") )
db_bu.filename = "/tmp/__lua-sqlite3-20161102233049." .. os.time()
db_bu.db_tgt = assert_userdata( sqlite3.open(db_bu.filename) )
end
function db_bu.teardown()
assert( db_bu.db_src:close() )
assert( db_bu.db_tgt:close() )
os.remove(db_bu.filename)
end
function db_bu.test()
assert_function( sqlite3.backup_init )
local bu = assert_userdata( sqlite3.backup_init(db_bu.db_tgt, 'main', db_bu.db_src, 'main') )
assert_function ( bu.step )
assert_function ( bu.remaining )
assert_function ( bu.pagecount )
assert_function ( bu.finish )
if true then
bu = nil
collectgarbage()
collectgarbage()
else
bu:finish()
end
bu = assert_userdata( sqlite3.backup_init(db_bu.db_tgt, 'main', db_bu.db_src, 'main') )
assert_equal( sqlite3.DONE, bu:step(-1) )
assert_equal( sqlite3.OK, bu:finish() )
bu = nil
local db = db_bu.db_tgt
for row in db:nrows("SELECT id as val FROM test WHERE name='Hello World'") do
assert_equal (row.val, 1)
end
for row in db:nrows("SELECT substr(name,7,3) as val FROM test WHERE id = 2") do
assert_equal (row.val,'Lua')
assert_equal (#row.val, 3)
end
for row in db:nrows("SELECT name as val FROM test WHERE id = 3") do
assert_equal (row.val, 'Hello SQLite')
assert_equal (#row.val, 12)
end
end
local db_bu_gc = lunit_TestCase("Online Backup API GC")
function db_bu_gc.setup()
db_bu_gc.db_src = assert( sqlite3.open_memory() )
assert_equal( sqlite3.OK, db_bu_gc.db_src:exec("CREATE TABLE test (id, name text)") )
assert_equal( sqlite3.OK, db_bu_gc.db_src:exec("INSERT INTO test VALUES (1, 'Hello World')") )
assert_equal( sqlite3.OK, db_bu_gc.db_src:exec("INSERT INTO test VALUES (2, 'Hello Lua')") )
assert_equal( sqlite3.OK, db_bu_gc.db_src:exec("INSERT INTO test VALUES (3, 'Hello SQLite')") )
db_bu_gc.filename = "/tmp/__lua-sqlite3-20161103120909." .. os.time()
db_bu_gc.db_tgt = assert_userdata( sqlite3.open(db_bu_gc.filename) )
end
function db_bu_gc.teardown()
os.remove(db_bu_gc.filename)
end
function db_bu_gc.test()
local bu = assert_userdata( sqlite3.backup_init(db_bu_gc.db_tgt, 'main', db_bu_gc.db_src, 'main') )
db_bu_gc.db_src = nil
db_bu_gc.db_tgt = nil
collectgarbage()
collectgarbage() -- should not close dbs even though db_bu_gc refs nil'd since they are referenced by bu
assert_equal( sqlite3.DONE, bu:step(-1) )
assert_equal( sqlite3.OK, bu:finish() )
bu = nil
collectgarbage()
collectgarbage() -- should now close dbs
local db = assert_userdata( sqlite3.open(db_bu_gc.filename) )
for row in db:nrows("SELECT id as val FROM test WHERE name='Hello World'") do
assert_equal (row.val, 1)
end
for row in db:nrows("SELECT substr(name,7,3) as val FROM test WHERE id = 2") do
assert_equal (row.val,'Lua')
assert_equal (#row.val, 3)
end
for row in db:nrows("SELECT name as val FROM test WHERE id = 3") do
assert_equal (row.val, 'Hello SQLite')
assert_equal (#row.val, 12)
end
assert( db:close() )
end
local db_bu_null = lunit_TestCase("Online Backup API NULL")
function db_bu_null.setup()
db_bu_null.db = assert( sqlite3.open_memory() )
end
function db_bu_null.teardown()
assert( db_bu_null.db:close() )
end
function db_bu_null.test()
local bu = assert_nil( sqlite3.backup_init(db_bu_null.db, 'main', db_bu_null.db, 'main') )
end
--------------------------------------
-- Functions added 2016-11-xx 0.9.4 --
--------------------------------------
r094 = lunit_TestCase("Functions added 0.9.4")
function r094.setup()
r094.db = assert( sqlite3.open_memory() )
r094.filename = "/tmp/__lua-sqlite3-20161112163049." .. os.time()
r094.db_fn = assert_userdata( sqlite3.open(r094.filename) )
end
function r094.teardown()
assert_number( r094.db:close() )
assert_number( r094.db_fn:close() )
end
function r094.test_db_filename()
assert_nil( r094.db:db_filename("frob") )
assert_equal( '', r094.db:db_filename("main") )
assert_nil( r094.db_fn:db_filename("frob") )
assert_equal( r094.filename, r094.db_fn:db_filename("main") )
-- from Wolfgang Oertl
local db_ptr = assert_userdata( r094.db:get_ptr() )
local db2 = assert_userdata( sqlite3.open_ptr(db_ptr) )
-- do something via connection 1
r094.db:exec("CREATE TABLE test1(a, b)")
r094.db:exec("INSERT INTO test1 VALUES(1, 2)")
-- see result via connection 2
for a, b in db2:urows("SELECT * FROM test1 ORDER BY a") do
assert_equal(a, 1)
assert_equal(b, 2)
end
assert_number( db2:close() )
end
lunit.main()
|