diff options
Diffstat (limited to 'test/common.ml')
| -rw-r--r-- | test/common.ml | 198 |
1 files changed, 157 insertions, 41 deletions
diff --git a/test/common.ml b/test/common.ml index 2a1e070..b1032e9 100644 --- a/test/common.ml +++ b/test/common.ml @@ -10,7 +10,9 @@ let connect : Configuration.Ast.setting -> Caqti_lwt.connection Lwt.t = on PRAGMA foreign_keys: the tests insert references to rows that are not present in the referenced table. *) let config = Caqti.Connect.Config.(set tweaks_version (1, 7) default) in - let* conn = Caqti_lwt_unix.connect ~config (Uri.of_string "sqlite3::memory:") in + let* conn = + Caqti_lwt_unix.connect ~config (Uri.of_string "sqlite3::memory:") + in let* db = Caqti_lwt.or_fail conn in let module Db = (val db) in let* () = Query.create db conf in @@ -34,8 +36,8 @@ let target_table : Model.target -> string = function | 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. *) +(** 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 -> @@ -49,47 +51,81 @@ let json_select_sql : in match where with None -> q | Some w -> q ^ " where " ^ w -(** Load the given content and return the values as a json *) +(** Build the [(json key, SQL expression)] columns selecting the single-column + fields of a table: one quoted column per field, skipping multivalued + references (they live in dedicated tables). With [~annotate_references], a + monovalued reference column is followed by a [[<field>_table]] literal + naming the targeted table, so the output is self-describing. *) +let field_columns : + ?annotate_references:bool -> Model.field list -> (string * string) list = + fun ?(annotate_references = false) fields -> + List.concat_map + (fun field -> + let name = Model.Field.name field in + let (Model.Field.T ty) = Model.Field.type_ field in + let key = Model.Identifier.to_string name in + match ty with + | Model.Typ.MultiReference _ -> [] + | Model.Typ.MonoReference reference when annotate_references -> + [ + (key, Model.Identifier.quote name); + ( key ^ "_table", + Croquette_common.Sql.quote_literal + (target_table reference.Model.Typ.t) ); + ] + | _ -> [ (key, Model.Identifier.quote name) ]) + fields + +(** Run a [json_select_sql] query over [table] and collect one json string per + row. With [~where] and [~id], the query is restricted to the rows where the + [where] column equals [id]. *) +let collect_json : + Caqti_lwt.connection -> + ?where:Model.Identifier.t -> + ?id:Int64.t -> + Model.Identifier.t -> + (string * string) list -> + string list Lwt.t = + fun db ?where ?id table columns -> + let module Db = (val db) in + let open Caqti.Templater in + let ( let* ) = Lwt.bind in + match (where, id) with + | Some col, Some id -> + let request = + direct + T.(int64 -->* string) + (json_select_sql + ~where:(Model.Identifier.quote col ^ "=?") + table columns) + in + let* res = Db.collect_list request id in + Caqti_lwt.or_fail res + | None, None -> + let request = + direct T.(unit -->* string) (json_select_sql table columns) + in + let* res = Db.collect_list request () in + Caqti_lwt.or_fail res + | _ -> invalid_arg "collect_json: [where] and [id] must be given together" + +(** Request returning one row of a content table as a json object *) let content_as_json : Model.content -> (int64, string, [ `One ]) Caqti.Template.Request.t = fun content -> - let fields = - Model.Content.fields content - (* Remove the multivaluated fields from the query *) - |> 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 columns = - ListLabels.map fields ~f:(fun field -> - let name = Model.Field.name field in - (Model.Identifier.to_string name, Model.Identifier.quote name)) - in let columns = + field_columns (List.of_seq (Model.Content.fields content)) + @ match Model.Content.parent content with - | None -> columns - | Some _ -> - columns - @ [ ("parent", Model.Identifier.quote Model.Identifier.parent) ] + | None -> [] + | Some _ -> [ ("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 open Caqti.Templater in - direct T.(int64 -->! string) q - -let query_values (table : Model.content) - (feature : Model.Identifier.t) = let open Caqti.Templater in - direct T.(int64 -->* string) - ("SELECT json_object(" - ^ Croquette_common.Sql.quote_literal (Model.Identifier.to_string feature) - ^ ", " ^ Model.Identifier.quote (Model.Identifier.target) ^ ") from " - ^ Model.Identifier.quote (Model.Identifier.dot (Model.Content.id table) feature) - ^ " where " ^ Model.Identifier.quote (Model.Identifier.parent) ^ "=?") + direct + T.(int64 -->! string) + (json_select_sql + ~where:(Model.Identifier.quote Model.Identifier.id ^ "=?") + (Model.Content.id content) columns) (** Query all the multireferences tables for the given content and the given id. Collect all the values and return them in the same json format as the same @@ -100,7 +136,6 @@ let collect_references : int64 -> (string * int64 * string) list Lwt.t = fun db content id -> - let module Db = (val db) in let ( let* ) = Lwt.bind in let content_id = Model.Identifier.to_string (Model.Content.id content) in @@ -117,10 +152,14 @@ let collect_references : Lwt_list.fold_left_s (fun acc field -> let field_name = Model.Field.name field in - let* json_result = - Db.collect_list (query_values content field_name) id + let* json = + collect_json db ~where:Model.Identifier.parent ~id + (Model.Identifier.dot (Model.Content.id content) field_name) + [ + ( Model.Identifier.to_string field_name, + Model.Identifier.quote Model.Identifier.target ); + ] in - let* json = Caqti_lwt.or_fail json_result in let line = ListLabels.fold_left ~init:acc json ~f:(fun acc json -> (content_id, id, json) :: acc) @@ -130,3 +169,80 @@ let collect_references : in results + +(** A provider giving a deterministic value for each field of a row, depending + on the field type and the row number. References point to the row of id 1 of + their target. *) +let provider_for_fields : Model.field list -> Int64.t -> string -> Value.value = + fun fields row field_name -> + match + List.find_opt + (fun field -> + String.equal field_name + (Model.Identifier.to_string (Model.Field.name field))) + fields + with + | None -> Value.none + | Some field -> ( + let (Model.Field.T ty) = Model.Field.type_ field in + match ty with + | Model.Typ.Text -> + Value.single + (Model.Typ.Text, Some (field_name ^ "-" ^ Int64.to_string row)) + | Model.Typ.Integer -> Value.single (Model.Typ.Integer, Some row) + | Model.Typ.MonoReference reference -> + Value.single (Model.Typ.MonoReference reference, Some 1L) + | Model.Typ.MultiReference reference -> + (* Several values per row, to highlight the multivalued data *) + let values = if Int64.equal row 1L then [ 1L; 2L ] else [ 2L ] in + Value.single (Model.Typ.MultiReference reference, List.to_seq values)) + +(** Same, for the fields of a content *) +let provider_for : Model.content -> Int64.t -> string -> Value.value = + fun content row field_name -> + provider_for_fields + (List.of_seq (Model.Content.fields content)) + row field_name + +(** Render a [(named, data)] pair as a single line, whatever the field type *) +let show_field : + 'db 'ocaml. + ('db, 'ocaml, Model.target) Model.Typ.t Model.Typ.named -> 'ocaml -> string + = + fun named data -> + let name = Model.Identifier.to_string (Model.Typ.name named) in + let show : type db ocaml. + (db, ocaml, Model.target) Model.Typ.t -> ocaml -> string = + fun typ data -> + match typ with + | Model.Typ.Text -> ( + match data with + | Some s -> name ^ " = \"" ^ s ^ "\"" + | None -> name ^ " = NULL") + | Model.Typ.Integer -> ( + match data with + | Some i -> name ^ " = " ^ Int64.to_string i + | None -> name ^ " = NULL") + | Model.Typ.MonoReference _ -> ( + match data with + | Some i -> name ^ " -> " ^ Int64.to_string i + | None -> name ^ " = NULL") + | Model.Typ.MultiReference _ -> + let targets = List.of_seq data |> List.sort compare in + name ^ " -> [" + ^ String.concat "," (List.map Int64.to_string targets) + ^ "]" + in + show (Model.Typ.typ named) data + +(** Read a configuration from the standard input and run [f] with a fresh + in-memory database created from it *) +let exec_with_conf : + (Configuration.Ast.setting -> Caqti_lwt.connection -> 'a Lwt.t) -> + ('a, Configuration.Read_conf.error) result = + fun f -> + let ( let+ ) opt cont = Result.map cont opt in + let+ configuration = + Configuration.Read_conf.read_string_conf (read_stdin ()) + in + with_db configuration (fun db -> f configuration db) |
