|
| 1 | +import Sqlite |
1 | 2 | import LSpec |
2 | 3 |
|
3 | 4 | open LSpec |
| 5 | +open Sqlite.FFI |
| 6 | +open Sqlite.FFI.Constants |
4 | 7 |
|
5 | | -def fourIO : IO Nat := |
6 | | - pure 5 |
| 8 | +def assert := (· = true) |
7 | 9 |
|
8 | | -def fiveIO : IO Nat := |
9 | | - pure 5 |
| 10 | +instance : Testable (assert b) := |
| 11 | + if h : b = true then |
| 12 | + .isTrue h |
| 13 | + else |
| 14 | + .isFalse h s!"Expected true but got false" |
10 | 15 |
|
11 | | -def main := do |
12 | | - let four ← fourIO |
13 | | - let five ← fiveIO |
14 | | - lspecIO $ |
15 | | - test "fourIO equals 4" (four = 4) $ |
16 | | - test "fiveIO equals 5" (five = 5) |
| 16 | +structure TestContext where |
| 17 | + conn : Connection |
| 18 | + |
| 19 | +def setup (s : String) : IO TestContext := do |
| 20 | + let flags := SQLITE_OPEN_READWRITE ||| SQLITE_OPEN_CREATE |
| 21 | + let conn ← connect s flags |
| 22 | + |
| 23 | + match ← conn.prepare "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);" with |
| 24 | + | Except.ok cursor => cursor.step |
| 25 | + | Except.error _ => pure false |
| 26 | + |
| 27 | + match ← conn.prepare "INSERT INTO users (id, name) VALUES (1, 'John Doe');" with |
| 28 | + | Except.ok cursor => cursor.step |
| 29 | + | Except.error _ => pure false |
| 30 | + |
| 31 | + pure ⟨conn⟩ |
| 32 | + |
| 33 | +def cleanup (ctx : TestContext) : IO Unit := do |
| 34 | + match ← ctx.conn.prepare "DROP TABLE IF EXISTS users;" with |
| 35 | + | Except.ok cursor => do |
| 36 | + let _ ← cursor.step |
| 37 | + pure () |
| 38 | + | Except.error _ => pure () |
| 39 | + |
| 40 | +def withTest (test : TestContext → IO Bool) : IO Bool := do |
| 41 | + let ctx ← setup "test.sqlite3" |
| 42 | + try |
| 43 | + let result ← test ctx |
| 44 | + cleanup ctx |
| 45 | + pure result |
| 46 | + catch e => |
| 47 | + IO.println s!"Error: {e}" |
| 48 | + cleanup ctx |
| 49 | + pure false |
17 | 50 |
|
18 | | -#check main |
19 | | -#eval main |
| 51 | +def testInsertData (ctx : TestContext) : IO Bool := do |
| 52 | + match ← ctx.conn.prepare "INSERT INTO users (id, name) VALUES (?, ?);" with |
| 53 | + | Except.ok cursor => |
| 54 | + cursor.bindInt 1 2 |
| 55 | + cursor.bindText 2 "Jane Doe" |
| 56 | + let _ ← cursor.step |
| 57 | + pure true |
| 58 | + | Except.error _ => pure false |
20 | 59 |
|
21 | | -#lspec |
22 | | - test "four equals four" (4 = 4) $ |
23 | | - test "five equals five" (5 = 5) |
| 60 | +def testSelectData (ctx : TestContext) : IO Bool := do |
| 61 | + match ← ctx.conn.prepare "SELECT * FROM users WHERE id = 1;" with |
| 62 | + | Except.ok cursor => |
| 63 | + let hasRow ← cursor.step |
| 64 | + if hasRow then |
| 65 | + let id ← cursor.columnInt 0 |
| 66 | + let name ← cursor.columnText 1 |
| 67 | + pure (id = 1 && name == "John Doe") |
| 68 | + else |
| 69 | + pure false |
| 70 | + | Except.error _ => pure false |
| 71 | + |
| 72 | +def testParameterBinding (ctx : TestContext) : IO Bool := do |
| 73 | + match ← ctx.conn.prepare "SELECT * FROM users WHERE id = ? AND name = ?;" with |
| 74 | + | Except.ok cursor => |
| 75 | + cursor.bindInt 1 1 |
| 76 | + cursor.bindText 2 "John Doe" |
| 77 | + pure true |
| 78 | + | Except.error _ => pure false |
| 79 | + |
| 80 | +def testColumnCount (ctx : TestContext) : IO Bool := do |
| 81 | + match ← ctx.conn.prepare "SELECT * FROM users;" with |
| 82 | + | Except.ok cursor => |
| 83 | + let count ← cursor.columnsCount |
| 84 | + pure (count = 2) |
| 85 | + | Except.error _ => pure false |
| 86 | + |
| 87 | +def testInvalidSyntax (ctx : TestContext) : IO Bool := do |
| 88 | + match ← ctx.conn.prepare "INVALID SQL QUERY;" with |
| 89 | + | Except.error _ => pure true |
| 90 | + | Except.ok _ => pure false |
| 91 | + |
| 92 | +def testNonExistentTable (ctx : TestContext) : IO Bool := do |
| 93 | + match ← ctx.conn.prepare "SELECT * FROM non_existent_table;" with |
| 94 | + | Except.error _ => pure true |
| 95 | + | Except.ok _ => pure false |
| 96 | + |
| 97 | +def main := do |
| 98 | + let insertData ← (withTest testInsertData) |
| 99 | + let selectData ← (withTest testSelectData) |
| 100 | + let parameterBinding ← (withTest testParameterBinding) |
| 101 | + let columnCount ← (withTest testColumnCount) |
| 102 | + let invalidSyntax ← (withTest testInvalidSyntax) |
| 103 | + let nonExistentTable ← (withTest testNonExistentTable) |
| 104 | + |
| 105 | + lspecIO $ |
| 106 | + test "can insert data" (assert insertData) $ |
| 107 | + test "can select data" (assert selectData) $ |
| 108 | + test "can bind parameters" (assert parameterBinding) $ |
| 109 | + test "can get column count" (assert columnCount) $ |
| 110 | + test "handles invalid SQL syntax" (assert invalidSyntax) $ |
| 111 | + test "handles non-existent table" (assert nonExistentTable) |
0 commit comments