|
| 1 | +import Base |
| 2 | +import ./check.bend as C |
| 3 | +import ../src/core/storedb.bend as S |
| 4 | +import ../src/server/db.bend as D |
| 5 | + |
| 6 | +# Values through SQLite and back (D.Db, bun:sqlite): text that reads like |
| 7 | +# a number stays the text it was, numbers land in INTEGER columns as |
| 8 | +# integers, and a delete finds the key it was given. |
| 9 | + |
| 10 | +def handle(+r: String) -> String: |
| 11 | + String.drop(r, 3n) |
| 12 | + |
| 13 | +def items(os: List<&2, S.Op>, acc: List<&2, List<&2, String>>) -> List<&2, List<&2, String>>: |
| 14 | + match os: |
| 15 | + case Nil{}: |
| 16 | + List.reverse(&2, List<&2, String>, acc) |
| 17 | + case o <> t: |
| 18 | + items(t, S.Sql.of(o) <> acc) |
| 19 | + |
| 20 | +def row(rs: List<&2, List<&2, String>>) -> String: |
| 21 | + match rs: |
| 22 | + case r <> _: |
| 23 | + String.join(r, "|") |
| 24 | + case Nil{}: |
| 25 | + "<none>" |
| 26 | + |
| 27 | +def main() -> IO(Unit): |
| 28 | + do IO<Unit>: |
| 29 | + o : String <- D.Db.open(":memory:") |
| 30 | + +h : String = handle(o) |
| 31 | + e : String <- D.Db.exec(h, S.Sdb.schema()) |
| 32 | + C.Check.str("schema made", e, "ok") |
| 33 | + w : String <- D.Db.script(h, items([S.Put{"setting", ["#42", "#007", "0"]}, S.Put{"setting", ["plain", "007", "0"]}, |
| 34 | + S.Put{"thread", ["#7", "p", "#007", "5", "6", "idle", "0", "0", "1", "0", "", "", "", "0", "{}"]}, |
| 35 | + S.Put{"obj", ["mem", "#9", "\"#9\""]}, S.Put{"line", ["1", "0", "12", ""]}], Nil{})) |
| 36 | + C.Check.str("written", String.take(w, 2n), "ok") |
| 37 | + a : List<&2, List<&2, String>> <- D.Db.query(h, "SELECT k, v, typeof(v) FROM setting WHERE k = '#42'", [], 5) |
| 38 | + C.Check.str("#42 / #007 round-trip as text", row(a), "#42|#007|text") |
| 39 | + b : List<&2, List<&2, String>> <- D.Db.query(h, "SELECT v, typeof(v) FROM setting WHERE k = 'plain'", [], 5) |
| 40 | + C.Check.str("007 stays text in a TEXT column", row(b), "007|text") |
| 41 | + c : List<&2, List<&2, String>> <- D.Db.query(h, "SELECT id, title, created, typeof(created), pinned FROM thread", [], 5) |
| 42 | + C.Check.str("thread: text kept, numbers integers", row(c), "#7|#007|5|integer|1") |
| 43 | + l : List<&2, List<&2, String>> <- D.Db.query(h, "SELECT seq + 1, len, typeof(off) FROM line", [], 5) |
| 44 | + C.Check.str("line numbers are integers", row(l), "2|12|integer") |
| 45 | + d : String <- D.Db.script(h, items([S.Del{"setting", ["#42"]}, S.Del{"obj", ["mem", "#9"]}], Nil{})) |
| 46 | + C.Check.str("deleted", String.take(d, 2n), "ok") |
| 47 | + x : List<&2, List<&2, String>> <- D.Db.query(h, "SELECT count(*) FROM setting WHERE k = '#42'", [], 5) |
| 48 | + C.Check.str("delete by a #-key", row(x), "0") |
| 49 | + y : List<&2, List<&2, String>> <- D.Db.query(h, "SELECT count(*) FROM obj", [], 5) |
| 50 | + C.Check.str("obj delete by (domain, #-key)", row(y), "0") |
| 51 | + z : String <- D.Db.close(h) |
| 52 | + C.Check.str("closed", z, "ok") |
0 commit comments