1- import Sqlite
21import LSpec
2+ import SQLite
33
44open LSpec
5- open Sqlite .FFI
6- open Sqlite .FFI.Constants
5+ open SQLite .FFI
6+ open SQLite .FFI.Constants
77
88instance (b : Bool) : Testable b :=
99 if h : b = true then
@@ -17,43 +17,40 @@ 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-
2120 match ← conn.prepare "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);" with
2221 | Except.ok cursor => cursor.step
2322 | Except.error _ => pure false
24-
2523 match ← conn.prepare "INSERT INTO users (id, name) VALUES (1, 'John Doe');" with
2624 | Except.ok cursor => cursor.step
2725 | Except.error _ => pure false
28-
29- pure ⟨conn⟩
26+ return ⟨conn⟩
3027
3128def cleanup (ctx : TestContext) : IO Unit := do
3229 match ← ctx.conn.prepare "DROP TABLE IF EXISTS users;" with
3330 | Except.ok cursor => do
3431 let _ ← cursor.step
35- pure ()
36- | Except.error _ => pure ()
32+ return ()
33+ | Except.error _ => return ()
3734
3835def withTest (test : TestContext → IO Bool) : IO Bool := do
3936 let ctx ← setup "test.sqlite3"
4037 try
4138 let result ← test ctx
4239 cleanup ctx
43- pure result
40+ return result
4441 catch e =>
4542 IO.println s!"Error: {e}"
4643 cleanup ctx
47- pure false
44+ return false
4845
4946def testInsertData (ctx : TestContext) : IO Bool := do
5047 match ← ctx.conn.prepare "INSERT INTO users (id, name) VALUES (?, ?);" with
5148 | Except.ok cursor =>
5249 cursor.bindInt 1 2
5350 cursor.bindText 2 "Jane Doe"
54- let _ ← cursor.step
55- pure true
56- | Except.error _ => pure false
51+ let _ ← cursor.step -- This always returns false
52+ return true
53+ | Except.error _ => return false
5754
5855def testSelectData (ctx : TestContext) : IO Bool := do
5956 match ← ctx.conn.prepare "SELECT * FROM users WHERE id = 1;" with
@@ -62,41 +59,42 @@ def testSelectData (ctx : TestContext) : IO Bool := do
6259 if hasRow then
6360 let id ← cursor.columnInt 0
6461 let name ← cursor.columnText 1
65- pure (id = 1 && name == "John Doe")
62+ return (id = 1 && name == "John Doe")
6663 else
67- pure false
68- | Except.error _ => pure false
64+ return false
65+ | Except.error _ => return false
6966
7067def testParameterBinding (ctx : TestContext) : IO Bool := do
7168 match ← ctx.conn.prepare "SELECT * FROM users WHERE id = ? AND name = ?;" with
7269 | Except.ok cursor =>
7370 cursor.bindInt 1 1
7471 cursor.bindText 2 "John Doe"
75- pure true
76- | Except.error _ => pure false
72+ return true
73+ | Except.error _ => return false
7774
7875def testColumnCount (ctx : TestContext) : IO Bool := do
7976 match ← ctx.conn.prepare "SELECT * FROM users;" with
8077 | Except.ok cursor =>
8178 let count ← cursor.columnsCount
82- pure ( count = 2)
83- | Except.error _ => pure false
84-
85- def testInvalidSyntax (ctx : TestContext) : IO Bool := do
86- match ← ctx.conn.prepare "INVALID SQL QUERY;" with
87- | Except.error _ => pure true
88- | Except.ok _ => pure false
89-
90- def testNonExistentTable (ctx : TestContext) : IO Bool := do
91- match ← ctx.conn.prepare "SELECT * FROM non_existent_table;" with
92- | Except.error _ => pure true
93- | Except.ok _ => pure false
94-
95- def main := do
96- lspecIO $
97- test "can insert data" (← withTest testInsertData) $
98- test "can select data" (← withTest testSelectData) $
99- test "can bind parameters" (← withTest testParameterBinding) $
100- test "can get column count" (← withTest testColumnCount) $
101- test "handles invalid SQL syntax" (← withTest testInvalidSyntax) $
79+ return count = 2
80+ | Except.error _ => return false
81+
82+ def testInvalidSyntax (ctx : TestContext) : IO Bool :=
83+ return match ← ctx.conn.prepare "INVALID SQL QUERY;" with
84+ | Except.error _ => true
85+ | Except.ok _ => false
86+
87+ def testNonExistentTable (ctx : TestContext) : IO Bool :=
88+ return match ← ctx.conn.prepare "SELECT * FROM non_existent_table;" with
89+ | Except.error _ => true
90+ | Except.ok _ => false
91+
92+ def main (args : List String) := do
93+ lspecIO (.ofList [("test suite", [
94+ test "can insert data" (← withTest testInsertData),
95+ test "can select data" (← withTest testSelectData),
96+ test "can bind parameters" (← withTest testParameterBinding),
97+ test "can get column count" (← withTest testColumnCount),
98+ test "handles invalid SQL syntax" (← withTest testInvalidSyntax),
10299 test "handles non-existent table" (← withTest testNonExistentTable)
100+ ])]) args
0 commit comments