@@ -980,7 +980,7 @@ describe("sqlite.tbl", function()
980980
981981 describe (" string_index:" , function ()
982982 local kv = tbl (" kvpair" , {
983- key = { " text" , primary = true , required = true , unique = true },
983+ key = { " text" , primary = true , required = true , default = " none " },
984984 len = " integer" ,
985985 }, db )
986986
@@ -1038,6 +1038,13 @@ describe("sqlite.tbl", function()
10381038 }]
10391039 )
10401040 end )
1041+
1042+ it (" insert with 0 or true to skip the primary key value." , function ()
1043+ kv [true ] = { len = 5 }
1044+ eq (5 , kv .none .len )
1045+ kv [" " ] = { len = 6 }
1046+ eq ({ key = " none" , len = 6 }, kv :where { len = 6 })
1047+ end )
10411048 end )
10421049
10431050 describe (" number_index" , function ()
@@ -1080,8 +1087,47 @@ describe("sqlite.tbl", function()
10801087 limit = 2 ,
10811088 }]
10821089 )
1090+ t [0 ] = { name = " x" }
1091+ eq (" x" , t [t .last_id ].name )
1092+ end )
1093+ -- t[""] = { len = 6 }
1094+ -- eq({ key = "none", len = 6 }, t:where { len = 6 })
1095+ end )
1096+
1097+ describe (" Relationships" , function ()
1098+ local todos = tbl (" todos_indexer" , {
1099+ id = true ,
1100+ title = " text" ,
1101+ project = {
1102+ reference = " projects.title" ,
1103+ required = true ,
1104+ on_delete = " cascade" ,
1105+ on_update = " cascade" ,
1106+ },
1107+ }, db )
1108+
1109+ local projects = tbl (" projects" , {
1110+ title = { type = " text" , primary = true , required = true , unique = true },
1111+ deadline = { " date" , default = db .lib .date (" now" , " 3 days" ) },
1112+ }, db )
1113+
1114+ it (" create new table with default values" , function ()
1115+ projects .neovim = {}
1116+ eq (true , projects .neovim .deadline == " 2021-09-05" )
1117+ projects [" sqlite" ] = {}
1118+ --- TODO: if you have sqilte.lua todos[2] return empty table
1119+ end )
1120+
1121+ it (" fails if foregin key doesn't exists" , function ()
1122+ eq (
1123+ false ,
1124+ pcall (function ()
1125+ todos [2 ].project = " ram"
1126+ end )
1127+ )
10831128 end )
10841129 end )
1130+
10851131 -- vim.loop.fs_unlink(db_path)
10861132 end )
10871133
0 commit comments