diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-11-20 12:35:08 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-11-20 12:35:08 +0100 |
| commit | c4ca180fd7c2085e3ce3f8e32aa0c2cb724f4c53 (patch) | |
| tree | eb91601a04f3256e1732a72d3ebe8e1fa5d7a616 /test/common.ml | |
| parent | fb75994d097cb1d6a1a0ea871c2d2213aa4e3c31 (diff) | |
Reviewed tests
Diffstat (limited to 'test/common.ml')
| -rw-r--r-- | test/common.ml | 70 |
1 files changed, 47 insertions, 23 deletions
diff --git a/test/common.ml b/test/common.ml index 6223846..2a1e070 100644 --- a/test/common.ml +++ b/test/common.ml @@ -16,6 +16,39 @@ let connect : Configuration.Ast.setting -> Caqti_lwt.connection Lwt.t = let* () = Query.create db conf in Lwt.return db +(** Read the whole standard input (a configuration or a data document) *) +let read_stdin : unit -> string = fun () -> In_channel.input_all stdin + +(** Run [f] with a fresh in-memory database created from the configuration *) +let with_db : + Configuration.Ast.setting -> (Caqti_lwt.connection -> 'a Lwt.t) -> 'a = + fun conf f -> + Lwt_main.run + (let* db = connect conf in + f db) + +(** Name of the table targeted by a reference (a content or a list) *) +let target_table : Model.target -> string = function + | Model.Reference.List list -> + Model.Identifier.to_string list.Model.DataList.id + | Model.Reference.Content ref -> + Model.Identifier.to_string (Model.Content.id (Model.Reference.get ref)) + +(** Build a [SELECT json_object(...)] query returning one json object per row + of [table]. [columns] are (json key, SQL expression) pairs. *) +let json_select_sql : + ?where:string -> Model.Identifier.t -> (string * string) list -> string = + fun ?where table columns -> + let expr = + String.concat ", " + (ListLabels.map columns ~f:(fun (key, e) -> + Croquette_common.Sql.quote_literal key ^ ", " ^ e)) + in + let q = + "SELECT json_object(" ^ expr ^ ") from " ^ Model.Identifier.quote table + in + match where with None -> q | Some w -> q ^ " where " ^ w + (** Load the given content and return the values as a json *) let content_as_json : Model.content -> (int64, string, [ `One ]) Caqti.Template.Request.t = @@ -26,36 +59,27 @@ let content_as_json : |> Seq.filter (fun t -> let (Model.Field.T ty) = Model.Field.type_ t in match ty with Model.Typ.MultiReference _ -> false | _ -> true) + |> List.of_seq in - let buff = Buffer.create 64 in - let () = - Seq.iter - (fun field -> + let columns = + ListLabels.map fields ~f:(fun field -> let name = Model.Field.name field in - Buffer.add_string buff - ( Croquette_common.Sql.quote_literal (Model.Identifier.to_string name) - ^ "," ^ Model.Identifier.quote name ^ "," )) - fields + (Model.Identifier.to_string name, Model.Identifier.quote name)) in - - let () = + let columns = match Model.Content.parent content with - | None -> - let length = Buffer.length buff in - if length > 1 then Buffer.truncate buff (length - 1); - () + | None -> columns | Some _ -> - Buffer.add_string buff - ( Croquette_common.Sql.quote_literal "parent" ^ "," - ^ Model.Identifier.quote (Model.Identifier.parent) ) + columns + @ [ ("parent", Model.Identifier.quote Model.Identifier.parent) ] + in + let q = + json_select_sql + ~where:(Model.Identifier.quote Model.Identifier.id ^ "=?") + (Model.Content.id content) columns in - - let q = Buffer.contents buff in let open Caqti.Templater in - direct T.(int64 -->! string) - ("SELECT json_object(" ^ q ^ ") from " - ^ Model.Identifier.quote (Model.Content.id content) - ^ " where " ^ Model.Identifier.quote (Model.Identifier.id) ^ "=?") + direct T.(int64 -->! string) q let query_values (table : Model.content) (feature : Model.Identifier.t) = |
