Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899(* A view over the in-memory store: the store keeps its text ({!Catalog.Spi.define}) and the engine checks, reads and expands it. Each expected answer is the sqlite3 CLI's (3.54.1) for the same statements. *)
module Value = Catalog.Value
let vrows = Alcotest.list (Alcotest.testable (Fmt.Dump.list Value.pp) (List.equal Value.equal))
let i n = Value.Int nlet run db sql = match Sql.exec db sql with Sql.Rows r -> r | Sql.Done -> []
let refused db msg sql = match Sql.exec db sql with | _ -> Alcotest.failf "%s: accepted, sqlite3 refuses it with %S" sql msg | exception Catalog.Error e -> Alcotest.(check string) sql msg (Fmt.str "%a" Catalog.Error.pp e)
let fresh () = let db = Sql.v (Catalog.of_mem []) in ignore (run db "CREATE TABLE t(a, b); INSERT INTO t VALUES (1, 10), (2, 20), (3, 30)"); db
(* lang_createview.html: a view's rows are its SELECT's, under the names its result columns or its column list give. sqlite3 answers 2|40 3|60, then 30. *)let test_a_view_reads_its_select () = let db = fresh () in ignore (run db "CREATE VIEW v AS SELECT a, b * 2 AS d FROM t WHERE a > 1"); Alcotest.check vrows "its rows" [ [ i 2L; i 40L ]; [ i 3L; i 60L ] ] (run db "SELECT * FROM v"); ignore (run db "CREATE VIEW w(x, y) AS SELECT a, b FROM t"); Alcotest.check vrows "under its column list" [ [ i 30L ] ] (run db "SELECT y FROM w WHERE x = 3"); ignore (run db "INSERT INTO t VALUES (4, 40)"); Alcotest.check vrows "a write to its table shows through it" [ [ i 2L; i 40L ]; [ i 3L; i 60L ]; [ i 4L; i 80L ] ] (run db "SELECT * FROM v")
(* A view's name is a name among tables and views (build.c, sqlite3CheckObjectName and sqlite3StartTable). *)let test_a_view_takes_a_table_name () = let db = fresh () in ignore (run db "CREATE VIEW v AS SELECT a FROM t"); refused db "view v already exists" "CREATE VIEW v AS SELECT 1"; refused db "view v already exists" "CREATE TABLE v(z)"; refused db "table t already exists" "CREATE VIEW t AS SELECT 1"; ignore (run db "CREATE VIEW IF NOT EXISTS v AS SELECT 1"); Alcotest.check vrows "IF NOT EXISTS kept the first" [ [ i 1L ]; [ i 2L ]; [ i 3L ] ] (run db "SELECT * FROM v"); refused db "parameters are not allowed in views" "CREATE VIEW p AS SELECT ?"
(* lang_dropview.html: DROP VIEW removes the definition, and each DROP refuses the other kind by name. *)let test_drop_view () = let db = fresh () in ignore (run db "CREATE VIEW v AS SELECT a FROM t"); ignore (run db "DROP VIEW v"); refused db "no such table: v" "SELECT * FROM v"; refused db "no such view: v" "DROP VIEW v"; ignore (run db "DROP VIEW IF EXISTS v"); ignore (run db "CREATE VIEW w AS SELECT a FROM t"); refused db "use DROP TABLE to delete table t" "DROP VIEW t"; refused db "use DROP VIEW to delete view w" "DROP TABLE w"; refused db "no such trigger: tr" "DROP TRIGGER tr"
(* A definition is part of the schema a transaction undoes. *)let test_a_rollback_undoes_a_view () = let db = fresh () in ignore (run db "BEGIN; CREATE VIEW u AS SELECT 7; ROLLBACK"); refused db "no such table: u" "SELECT * FROM u"
(* select.c, sqlite3ViewGetColumnNames: a view reached again while its own columns are being worked out is circular. *)let test_a_circular_view () = let db = fresh () in ignore (run db "CREATE VIEW c1 AS SELECT * FROM c2"); ignore (run db "CREATE VIEW c2 AS SELECT * FROM c1"); refused db "view c1 is circularly defined" "SELECT * FROM c1"
let suite = ( "view", [ Alcotest.test_case "a view reads its select" `Quick test_a_view_reads_its_select; Alcotest.test_case "a view takes a table name" `Quick test_a_view_takes_a_table_name; Alcotest.test_case "drop view" `Quick test_drop_view; Alcotest.test_case "a rollback undoes a view" `Quick test_a_rollback_undoes_a_view; Alcotest.test_case "a circular view" `Quick test_a_circular_view; ] )