@@ -972,5 +972,74 @@ describe("sqlite.tbl", function()
972972 end )
973973 end )
974974 end )
975+
976+ describe (" :index access" , function ()
977+ -- local db_path = "/tmp/idx_db"
978+ -- vim.loop.fs_unlink(db_path)
979+ db = sql :open ()
980+
981+ describe (" string_index:" , function ()
982+ local kvpair = tbl (" kvpair" , {
983+ key = { " text" , primary = true , required = true , unique = true },
984+ value = " integer" ,
985+ }, db )
986+
987+ it (" access/insert-to table using primary key" , function ()
988+ kvpair .a = { value = 1 }
989+ eq ({ key = " a" , value = 1 }, kvpair .a )
990+ end )
991+
992+ it (" access/update a row field value" , function ()
993+ kvpair .a .value = 2
994+ eq (2 , kvpair .where ({ value = 2 }).value , " should have been set" )
995+ eq (2 , kvpair .a .value , " should have been set" )
996+ kvpair .a .value = 3
997+ eq ({ key = " a" , value = 3 }, kvpair .a , " should return values" )
998+ end )
999+
1000+ it (" remove a row using primary key" , function ()
1001+ kvpair .a = nil
1002+ eq (nil , kvpair .where { key = " a" }, " should be empty" )
1003+ eq ({}, kvpair .a , " should be empty" )
1004+ end )
1005+
1006+ it (" sets a row field value without creating the row first" , function ()
1007+ kvpair [" some key with spaces :D" ].value = 4
1008+ eq (kvpair [" some key with spaces :D" ], { key = " some key with spaces :D" , value = 4 })
1009+ kvpair [" some key with spaces :D" ] = nil
1010+ end )
1011+
1012+ it (" query using index" , function ()
1013+ kvpair .a .value , kvpair .b .value , kvpair .c .value = 1 , 2 , 3
1014+ eq (
1015+ {
1016+ { key = " a" , value = 1 },
1017+ { key = " b" , value = 2 },
1018+ },
1019+ kvpair [{
1020+ where = { value = { 1 , 2 , 3 } },
1021+ order_by = { asc = { " key" , " value" } },
1022+ limit = 2 ,
1023+ }]
1024+ )
1025+ end )
1026+ -- it("bulk update", function()
1027+ -- kvpair[{ value = { 1, 2, 3 } }] = { value = 10 }
1028+ -- eq(
1029+ -- {
1030+ -- { key = "a", value = 10 },
1031+ -- { key = "b", value = 10 },
1032+ -- },
1033+ -- kvpair[{
1034+ -- order_by = { asc = { "key" } },
1035+ -- limit = 2,
1036+ -- }]
1037+ -- )
1038+ -- end)
1039+ end )
1040+
1041+ -- vim.loop.fs_unlink(db_path)
1042+ end )
1043+
9751044 clean ()
9761045end )
0 commit comments