(*--------------------------------------------------------------------------- Copyright (c) 2025 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (* The SQL text a typed query runs as ({!Sql.text_of_query}), read back by this engine's parser and executor. Most cases check the exact text and its parameters; the last runs the text and the typed query and compares. *) open Catalog.Query type layer = { hash : string; name : string; exit_status : int64; overlay : string option; weird : string; } let hash = Catalog.Col.v "hash" Catalog.text (fun l -> l.hash) let name = Catalog.Col.v "package_name" Catalog.text (fun l -> l.name) let exit_status = Catalog.Col.v "exit_status" Catalog.int (fun l -> l.exit_status) let overlay = Catalog.Col.v "overlay_handle" Catalog.(nullable text) (fun l -> l.overlay) let weird = Catalog.Col.v "odd \"name\"!" Catalog.text (fun l -> l.weird) let layers = Catalog.Table.map "layers" (fun hash name exit_status overlay weird -> { hash; name; exit_status; overlay; weird }) |> Catalog.Table.col hash |> Catalog.Table.col name |> Catalog.Table.col exit_status |> Catalog.Table.col overlay |> Catalog.Table.col weird |> Catalog.Table.seal let value = Alcotest.testable Sqlite.Value.pp ( = ) let check label sql params q = Alcotest.(check (pair string (list value))) label (sql, params) (Sql.text_of_query q) let hashes p = from layers @@ fun l -> where (p l) @@ (select Fun.id |> col (get l hash) |> seal) (* A literal is a parameter, so no value is ever quoted into the text. *) let test_parameters () = check "a string literal is a parameter" "SELECT \"layers\".\"hash\" FROM \"layers\" WHERE \ \"layers\".\"package_name\" = ?" [ Text "O'Brien" ] (hashes (fun l -> Expr.(get l name = text "O'Brien"))) let test_quoted_identifier () = check "a quote in a name is doubled" "SELECT \"layers\".\"odd \"\"name\"\"!\" FROM \"layers\"" [] (from layers @@ fun l -> select Fun.id |> col (get l weird) |> seal) let test_operators () = check "an operand of an operator is parenthesised" "SELECT \"layers\".\"hash\" FROM \"layers\" WHERE \ (\"layers\".\"exit_status\" >= ?) AND (\"layers\".\"package_name\" <> ?)" [ Int 0L; Text "lwt" ] (hashes (fun l -> Expr.(get l exit_status >= int 0L && get l name <> text "lwt"))) let test_like_in_isnull () = check "LIKE" "SELECT \"layers\".\"hash\" FROM \"layers\" WHERE \ \"layers\".\"package_name\" LIKE ?" [ Text "co%" ] (hashes (fun l -> Expr.(like (get l name) "co%"))); check "IN list" "SELECT \"layers\".\"hash\" FROM \"layers\" WHERE \"layers\".\"hash\" IN \ (?, ?, ?)" [ Text "a"; Text "b"; Text "c" ] (hashes (fun l -> Expr.(in_ (get l hash) [ "a"; "b"; "c" ]))); check "IS NOT NULL" "SELECT \"layers\".\"hash\" FROM \"layers\" WHERE \ \"layers\".\"overlay_handle\" IS NOT NULL" [] (hashes (fun l -> Expr.(is_not_null (get l overlay)))) let test_coalesce_count () = check "COALESCE" "SELECT \"layers\".\"hash\", coalesce(\"layers\".\"overlay_handle\", ?) \ FROM \"layers\"" [ Text "none" ] ( from layers @@ fun l -> select (fun h o -> (h, o)) |> col (get l hash) |> col Expr.(coalesce (get l overlay) "none") |> seal ); check "COUNT(*)" "SELECT count(*) FROM \"layers\"" [] (from layers @@ fun _ -> group Fun.id |> agg Expr.count_star |> seal_group) let test_distinct_order_limit () = check "DISTINCT, ORDER BY DESC, LIMIT and OFFSET" "SELECT DISTINCT \"layers\".\"package_name\" FROM \"layers\" ORDER BY \ \"layers\".\"package_name\" DESC LIMIT 5 OFFSET 2" [] ( from layers @@ fun l -> select Fun.id |> col (get l name) |> seal |> distinct |> order_by (get l name) `Desc |> limit 5 |> offset 2 ) (* The text, run through the string parser with its parameters, returns what the typed query does, including a value carrying SQL metacharacters. *) let test_render_then_run_agrees () = let db = Sqlite.in_memory () in Sqlite.Raw.exec db "CREATE TABLE layers (hash TEXT, package_name TEXT, exit_status INTEGER, \ overlay_handle TEXT, \"odd \"\"name\"\"!\" TEXT)" ~params:[]; List.iter (Sqlite.insert db layers) [ { hash = "h1"; name = "O'Brien"; exit_status = 0L; overlay = None; weird = ""; }; { hash = "h2"; name = "cohttp"; exit_status = 0L; overlay = None; weird = ""; }; { hash = "h3"; name = "lwt"; exit_status = 1L; overlay = None; weird = ""; }; ]; let q = hashes (fun l -> Expr.(get l name = text "O'Brien" && get l exit_status = int 0L)) in let sql, params = Sql.text_of_query q in Alcotest.(check (list (list value))) "render then run == run" (List.map (fun h -> [ Sqlite.Text h ]) (Sqlite.run db q)) (Sqlite.Raw.query db sql ~params) let suite = ( "render", [ Alcotest.test_case "literals are parameters" `Quick test_parameters; Alcotest.test_case "quoted identifier" `Quick test_quoted_identifier; Alcotest.test_case "operators + parens" `Quick test_operators; Alcotest.test_case "like / in / is not null" `Quick test_like_in_isnull; Alcotest.test_case "coalesce / count" `Quick test_coalesce_count; Alcotest.test_case "distinct / order / limit" `Quick test_distinct_order_limit; Alcotest.test_case "render then run agrees" `Quick test_render_then_run_agrees; ] )