@@ -17,10 +17,10 @@ structure TestContext where
1717def setup (s : String) : IO TestContext := do
1818 let flags := SQLITE_OPEN_READWRITE ||| SQLITE_OPEN_CREATE
1919 let conn ← connect s flags
20- match ← conn.prepare "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT NOT NULL) ;" with
20+ match ← conn.prepare "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, height REAL) STRICT ;" with
2121 | Except.ok cursor => cursor.step
2222 | Except.error _ => pure false
23- match ← conn.prepare "INSERT INTO users (id, name) VALUES (1, 'John Doe');" with
23+ match ← conn.prepare "INSERT INTO users (id, name, height ) VALUES (1, 'John Doe', 2.3 );" with
2424 | Except.ok cursor => cursor.step
2525 | Except.error _ => pure false
2626 return ⟨conn⟩
@@ -43,41 +43,57 @@ def withTest (test : TestContext → IO Bool) : IO Bool := do
4343 cleanup ctx
4444 return false
4545
46- def testInsertData (ctx : TestContext) : IO Bool := do
47- match ← ctx.conn.prepare "INSERT INTO users (id, name) VALUES (?, ?);" with
48- | Except.ok cursor =>
49- cursor.bindInt 1 2
50- cursor.bindText 2 "Jane Doe"
51- let _ ← cursor.step -- This always returns false
52- return true
53- | Except.error _ => return false
54-
5546def testSelectData (ctx : TestContext) : IO Bool := do
5647 match ← ctx.conn.prepare "SELECT * FROM users WHERE id = 1;" with
5748 | Except.ok cursor =>
5849 let hasRow ← cursor.step
5950 if hasRow then
6051 let id ← cursor.columnInt 0
6152 let name ← cursor.columnText 1
62- return (id = 1 && name == "John Doe" )
53+ let height ← cursor.columnDouble 2
54+ return id == 1 && name == "John Doe" && height == 2 .3
55+ else
56+ return false
57+ | Except.error err => throw <| .userError err
58+
59+ def testInsertData (ctx : TestContext) : IO Bool := do
60+ let id := -2 ^ 63 |>.toInt64
61+ let name := "Jane Doe"
62+ let height := 20 .25
63+ match ← ctx.conn.prepare "INSERT INTO users (id, name, height) VALUES (?, ?, ?);" with
64+ | Except.ok cursor =>
65+ cursor.bindInt64 1 id
66+ cursor.bindText 2 name
67+ cursor.bindDouble 3 height
68+ let _ ← cursor.step -- This always returns false
69+ | Except.error err => throw <| .userError err
70+ match ← ctx.conn.prepare "SELECT * FROM users WHERE id = ?;" with
71+ | Except.ok cursor =>
72+ cursor.bindInt64 1 id
73+ let hasRow ← cursor.step
74+ if hasRow then
75+ let id' ← cursor.columnInt64 0
76+ let name' ← cursor.columnText 1
77+ let height' ← cursor.columnDouble 2
78+ return id == id' && name == name' && height == height'
6379 else
6480 return false
65- | Except.error _ => return false
81+ | Except.error err => throw <| .userError err
6682
6783def testParameterBinding (ctx : TestContext) : IO Bool := do
68- match ← ctx.conn.prepare "SELECT * FROM users WHERE id = ? AND name = ?;" with
84+ match ← ctx.conn.prepare "SELECT * FROM users WHERE id > ? AND name = ?;" with
6985 | Except.ok cursor =>
70- cursor.bindInt 1 1
86+ cursor.bindInt 1 0
7187 cursor.bindText 2 "John Doe"
7288 return true
73- | Except.error _ => return false
89+ | Except.error err => throw <| .userError err
7490
7591def testColumnCount (ctx : TestContext) : IO Bool := do
7692 match ← ctx.conn.prepare "SELECT * FROM users;" with
7793 | Except.ok cursor =>
7894 let count ← cursor.columnsCount
79- return count = 2
80- | Except.error _ => return false
95+ return count == 3
96+ | Except.error err => throw <| .userError err
8197
8298def testInvalidSyntax (ctx : TestContext) : IO Bool :=
8399 return match ← ctx.conn.prepare "INVALID SQL QUERY;" with
@@ -91,8 +107,8 @@ def testNonExistentTable (ctx : TestContext) : IO Bool :=
91107
92108def main (args : List String) := do
93109 lspecIO (.ofList [("test suite" , [
94- test "can insert data" (← withTest testInsertData),
95110 test "can select data" (← withTest testSelectData),
111+ test "can insert data" (← withTest testInsertData),
96112 test "can bind parameters" (← withTest testParameterBinding),
97113 test "can get column count" (← withTest testColumnCount),
98114 test "handles invalid SQL syntax" (← withTest testInvalidSyntax),
0 commit comments