(* The package's entry points (sql.mli): statements parsed as sqlite3 parses them, identifiers printed so they parse back, and the JSON modules it exposes reading and writing what sqlite3's JSON functions do. *) let test_parse () = (match Sql.parse "SELECT 1; SELECT 2" with | Ok stmts -> Alcotest.(check int) "two statements" 2 (List.length stmts) | Error e -> Alcotest.fail e); Alcotest.(check bool) "a typo is refused" true (Result.is_error (Sql.parse "SELEC 1")) let test_pp_ident () = Alcotest.(check string) "bare" "abc" (Fmt.str "%a" Sql.pp_ident "abc"); Alcotest.(check string) "a keyword is quoted" {|"select"|} (Fmt.str "%a" Sql.pp_ident "select"); Alcotest.(check string) "a quote is doubled" {|"a""b"|} (Fmt.str "%a" Sql.pp_ident {|a"b|}); Alcotest.check_raises "a NUL" (Invalid_argument "") (fun () -> try Fmt.kstr Stdlib.ignore "%a" Sql.pp_ident "a\000b" with Invalid_argument _ -> raise (Invalid_argument "")) (* json('{a:[1.50]}') and jsonb('{a:[1.50]}'), as sqlite3 3.54.1 answers them: the one text through Sql.Json, the other's bytes through Sql.Jsonb. *) let test_json () = let v = Sql.Json.of_string_exn Json.Codec.Value.t "{a:[1.50]}" in Alcotest.(check string) "json()" {|{"a":[1.50]}|} (Sql.Json.to_string Json.Codec.Value.t v); Alcotest.(check string) "jsonb()" "8C17615B45312E3530" (String.uppercase_ascii (Hex.encode (Sql.Jsonb.to_string Sql.Jsonb.Codec.Value.t (Object [ (Text "a", Array [ Float "1.50" ]) ])))) let values = Alcotest.testable (Fmt.Dump.list (Fmt.Dump.list Catalog.Value.pp)) ( = ) let empty () = Sql.v (Catalog.of_mem []) (* A placeholder no parameter binds reads NULL, in exec, which binds none, as past the end of query's parameters. *) let test_an_unbound_placeholder_is_null () = (match Sql.exec (empty ()) "SELECT ?" with | Sql.Rows rows -> Alcotest.check values "exec" [ [ Null ] ] rows | Sql.Done -> Alcotest.fail "a SELECT answers rows"); Alcotest.check values "query" [ [ Int 1L; Null ] ] (Sql.query (empty ()) "SELECT ?, ?" ~params:[ Int 1L ]) let test_explain_refuses_an_unknown_name () = match Sql.parse "SELECT x FROM nowhere" with | Ok [ Sql.Ast.Select s ] -> Alcotest.check_raises "no such table" (Catalog.Error (Prepare "no such table: nowhere")) (fun () -> ignore (Sql.explain_select (empty ()) s)) | _ -> Alcotest.fail "one SELECT" (* The cap is a count of rows, the connection's (sqlite3_limit): the default holds two thousand, and one that is not positive is refused at open. *) let test_the_recursion_cap () = Alcotest.check values "the default" [ [ Int 2000L ] ] (Sql.query (empty ()) "WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM c WHERE x \ < 2000) SELECT count(*) FROM c" ~params:[]); Alcotest.check_raises "zero" (Invalid_argument "recursion_cap 0 is not positive") (fun () -> ignore (Sql.v ~recursion_cap:0 (Catalog.of_mem []))) let test_json_indent () = Alcotest.(check string) "indent 2" "[\n 1\n]" (Sql.Json.to_string ~indent:2 Json.Codec.(list int) [ 1 ]) let suite = ( "sql", [ Alcotest.test_case "parse" `Quick test_parse; Alcotest.test_case "pp_ident" `Quick test_pp_ident; Alcotest.test_case "json" `Quick test_json; Alcotest.test_case "an unbound placeholder is NULL" `Quick test_an_unbound_placeholder_is_null; Alcotest.test_case "explain refuses an unknown name" `Quick test_explain_refuses_an_unknown_name; Alcotest.test_case "the recursion cap" `Quick test_the_recursion_cap; Alcotest.test_case "json indent" `Quick test_json_indent; ] )