summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-11-21 11:23:41 +0100
committerSébastien Dailly <sebastien@dailly.me>2026-11-21 11:23:41 +0100
commitce13c93cf14cb71894481b0303a7058a72a6f4ce (patch)
treebd93186a65f6645cb4401f188d266d743cc2fde7 /test
parent172a02b80d8dbdcfe09129d7fa76015bb5ec4d79 (diff)
Load values from the database
Diffstat (limited to 'test')
-rw-r--r--test/common.ml198
-rw-r--r--test/insert.ml286
-rw-r--r--test/insert_list.ml102
-rw-r--r--test/load.ml37
-rw-r--r--test/select.ml46
-rw-r--r--test/tests.ml2
-rw-r--r--test/update.ml50
7 files changed, 329 insertions, 392 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)
diff --git a/test/insert.ml b/test/insert.ml
index 1b12596..9133832 100644
--- a/test/insert.ml
+++ b/test/insert.ml
@@ -1,7 +1,9 @@
-open StdLabels
-
let name = __FILE__
-let description = "Insert data and check the result"
+
+let description =
+ "Insert two rows of the given content with a generated provider, and display \
+ the inserted rows. The configuration is read on the standard input"
+
let speclist () = []
type t =
@@ -14,267 +16,33 @@ let pp : Format.formatter -> t -> unit =
(fun formatter (content_id, id, content) ->
Format.fprintf formatter "@[<v 2>%s:%Ld@;%s@]" content_id id content))
-type content = Model.content
-type value = Value.value
-type properties = (string * value) list
-
-(* A subcontent to insert, with one entry (properties) per row *)
-type content_to_insert = content * properties list
-
-(* One entry of the "values" list: the properties of a row, and the entries
- of each subcontent (identified by a key equal to the subcontent name) *)
-type entry = properties * content_to_insert list
-
-(** [insert_and_get_result db content entry] will insert the value in the
- database and return the informations for the content just inserted.
-
- [content] is the type definition for the element we want to insert [entry]
- contains all the values to insert in the database. *)
-let insert_and_get_result :
- ?parent:Model.content Value.t ->
+(** Insert one row of [content], and return its json representation together
+ with one line per value of each multivalued reference field *)
+let insert_one :
Caqti_lwt.connection ->
- content ->
- properties ->
- (string * int64 * string) Lwt.t =
- fun ?parent db content entry ->
+ Model.content ->
+ Int64.t ->
+ (string * Int64.t * string) list Lwt.t =
+ fun db content row ->
let ( let* ) = Lwt.bind in
- let* inserted =
- Query.insert ?parent db content (fun v ->
- match List.assoc_opt v entry with Some v -> v | None -> Value.none)
- in
- let id =
- match Value.get inserted Value.id_ref with
- | Some id -> id
- | None -> assert false
- in
+ let* id = Query.insert db content (Common.provider_for content row) in
let content_id = Model.Identifier.to_string (Model.Content.id content) in
let module Db = (val db) in
let* json_result = Db.find (Common.content_as_json content) id in
let* json = Caqti_lwt.or_fail json_result in
- Lwt.return (content_id, id, json)
-
-(** Insert one row and collect the result line, together with the lines of its
- multivalued references. Return the accumulator and the inserted value. *)
-let insert_one :
- Caqti_lwt.connection ->
- ?parent:Model.content Value.t ->
- content ->
- properties ->
- (string * Int64.t * string) list ->
- ((string * Int64.t * string) list * Model.content Value.t) Lwt.t =
- fun db ?parent content entry acc ->
- let ( let* ) = Lwt.bind in
- let* content_id, id, json = insert_and_get_result ?parent db content entry in
let* ext = Common.collect_references db content id in
- Lwt.return (List.append ext ((content_id, id, json) :: acc), Value.v content id)
-
-module S = Configuration.Json5.Decode
-
-let ( let* ) = Lwt.bind
-
-(* bind in the decoder monad *)
-let ( let** ) = S.( let* )
-
-(* Force a list of Lwt.t sequentially: the sqlite connection does not support
- concurrent usage, and some of these Lwt.t perform inserts *)
-let seq_lwt : 'a Lwt.t list -> 'a list Lwt.t =
- fun l -> Lwt_list.map_s (fun x -> x) l
-
-let is_field : content -> string -> bool =
- fun content key ->
- Seq.exists
- (fun field ->
- String.equal key Model.(Identifier.to_string (Field.name field)))
- (Model.Content.fields content)
-
-(** Decode the value of a field: the json nature of the value tells the type
- (numeric, text), or it is a reference (an integer for a list target, an
- object holding the properties of a new row of the target content) *)
-let rec value :
- Caqti_lwt.connection ->
- content ->
- string ->
- (string * value) Lwt.t S.decoder =
- fun db content key ->
- S.one_of
- [
- ( "numeric",
- S.map
- (fun v ->
- Lwt.return (key, Value.single (Model.Typ.Integer, Some (Int64.of_int v))))
- S.int );
- ( "text",
- S.map (fun v -> Lwt.return (key, Value.single (Model.Typ.Text, Some v))) S.string
- );
- ("reference", reference_value db content key false);
- ("references", reference_value db content key true);
- ("other", S.succeed (Lwt.return (key, Value.none)));
- ]
-
-(** Decode a reference field: single value or list of values, depending on
- [multivaluated].
-
- A list target is referenced by the id of one of its rows (an integer). A
- content target is referenced by an object holding the properties of a new
- row, inserted in its own table — the inserts are chained in a single Lwt
- promise, because the sqlite connection does not support concurrent usage. *)
-and reference_value :
- Caqti_lwt.connection ->
- content ->
- string ->
- bool ->
- (string * value) Lwt.t S.decoder =
- fun db content key multivaluated ->
- match Model.Content.find_target content key with
- | None -> S.fail "Not a feature"
- | Some t -> (
- let reference = Model.Typ.{ t; constraints = None } in
- match t with
- | Model.Reference.List _ ->
- if multivaluated then
- S.map
- (fun ids ->
- Lwt.return
- ( key,
- Value.single
- ( Model.Typ.MultiReference reference,
- List.to_seq (List.map ~f:Int64.of_int ids) ) ))
- (S.list S.int)
- else
- S.map
- (fun v ->
- Lwt.return
- ( key,
- Value.single
- (Model.Typ.MonoReference reference, Some (Int64.of_int v))
- ))
- S.int
- | Model.Reference.Content target_ref ->
- let target = Model.Reference.get target_ref in
- if multivaluated then
- let** entries = S.list (properties db target) in
- S.succeed
- (let* entries = seq_lwt entries in
- let* ids =
- Lwt_list.map_s
- (fun props ->
- let* _, id, _ = insert_and_get_result db target props in
- Lwt.return id)
- entries
- in
- Lwt.return
- ( key,
- Value.single
- (Model.Typ.MultiReference reference, List.to_seq ids) ))
- else
- let** props = properties db target in
- S.succeed
- (let* props = props in
- let* _, id, _ = insert_and_get_result db target props in
- Lwt.return
- (key, Value.single (Model.Typ.MonoReference reference, Some id)))
- )
-
-and properties : Caqti_lwt.connection -> content -> properties Lwt.t S.decoder =
- fun db content -> S.map seq_lwt (S.key_value_pairs_seq (value db content))
-
-(** One entry of the "values" list of a content: each key is decoded once, as a
- field value or as the entries of a subcontent *)
-and entry :
- Caqti_lwt.connection ->
- Configuration.Ast.setting ->
- content ->
- entry Lwt.t S.decoder =
- fun db conf content ->
- let key_value key =
- if is_field content key then S.map (fun v -> `Prop v) (value db content key)
- else
- match
- Configuration.Ast.get_content conf (Model.Identifier.of_string key)
- with
- | Some subcontent -> S.map (fun v -> `Sub v) (sub_entries db subcontent)
- | None -> S.map (fun v -> `Prop v) (value db content key)
- in
- S.map
- (fun pairs ->
- let props =
- List.filter_map pairs ~f:(function `Prop v -> Some v | `Sub _ -> None)
- in
- let subs =
- List.filter_map pairs ~f:(function `Sub v -> Some v | `Prop _ -> None)
- in
- let* properties = seq_lwt props in
- let* subs = seq_lwt subs in
- Lwt.return (properties, subs))
- (S.key_value_pairs_seq key_value)
-
-(** The entries of a subcontent: a list of plain properties, one per row *)
-and sub_entries :
- Caqti_lwt.connection -> content -> content_to_insert Lwt.t S.decoder =
- fun db content ->
- S.map
- (fun entries ->
- let* entries = seq_lwt entries in
- Lwt.return (content, entries))
- (S.list (properties db content))
-
-(** Decode one content of the data document: its id and its "values" entries *)
-and content_entry :
- Caqti_lwt.connection ->
- Configuration.Ast.setting ->
- (content * entry list) Lwt.t S.decoder =
- fun db conf ->
- let** name = S.field "id" S.string in
- let content =
- Configuration.Ast.get_content conf (Model.Identifier.of_string name)
- |> Option.get
- in
- S.map
- (fun entries ->
- let* entries = seq_lwt entries in
- Lwt.return (content, entries))
- (S.field_opt_or ~default:[] "values" (S.list (entry db conf content)))
-
-(** Decode the whole data document *)
-and data_document :
- Caqti_lwt.connection ->
- Configuration.Ast.setting ->
- (content * entry list) list Lwt.t S.decoder =
- fun db conf ->
- S.map seq_lwt (S.field "content" (S.list (content_entry db conf)))
-
-let exec : string list -> t =
- fun _ ->
- (* The same document holds both the configuration and the data to insert:
- the configuration reader ignores the "values" keys, the data reader
- ignores the "section" keys *)
- let stdin = Common.read_stdin () in
- let ( let** ) = Result.bind in
- let** configuration = Configuration.Read_conf.read_string_conf stdin in
- Common.with_db configuration (fun db ->
- match S.decode_string (data_document db configuration) stdin with
- | Error err -> Lwt.return_error err
- | Ok inserts ->
- let* inserts = inserts in
- let* insertions =
- Lwt_list.fold_left_s
- (fun acc (content, entries) ->
- Lwt_list.fold_left_s
- (fun acc (entry, subcontents) ->
- (* Insert the main content *)
- let* acc, parent = insert_one db content entry acc in
- (* Insert the subcontents associated *)
- Lwt_list.fold_left_s
- (fun acc (subcontent, subentries) ->
- Lwt_list.fold_left_s
- (fun acc entry ->
- let* acc, _ =
- insert_one db ~parent subcontent entry acc
- in
- Lwt.return acc)
- acc subentries)
- acc subcontents)
- acc entries)
- [] inserts
+ Lwt.return ((content_id, id, json) :: List.rev ext)
+
+let exec : string list -> t = function
+ | [ content_id ] ->
+ Common.exec_with_conf (fun configuration db ->
+ let content =
+ Option.get
+ (Configuration.Ast.get_content configuration
+ (Model.Identifier.of_string content_id))
in
- Lwt.return_ok (List.rev insertions))
+ let ( let* ) = Lwt.bind in
+ let* lines1 = insert_one db content 1L in
+ let* lines2 = insert_one db content 2L in
+ Lwt.return (lines1 @ lines2))
+ | _ -> failwith "Invalid arguments"
diff --git a/test/insert_list.ml b/test/insert_list.ml
index a2425ae..88e17db 100644
--- a/test/insert_list.ml
+++ b/test/insert_list.ml
@@ -13,93 +13,37 @@ let pp : Format.formatter -> t -> unit =
Format.pp_print_string)
let ( let* ) = Lwt.bind
-let ( let+ ) opt cont = Result.map cont opt
-(** Build a request returning each row of the list table as a json object.
- Reference columns are followed by a [<field>_table] literal naming the
- targeted table, so that the output is self-describing. *)
-let list_as_json : Model.list -> (unit, string, _) Caqti.Template.Request.t =
- fun list ->
- let column 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 _ -> [] (* in a dedicated table *)
- | Model.Typ.MonoReference reference ->
- [
- (key, Model.Identifier.quote name);
- ( key ^ "_table",
- Croquette_common.Sql.quote_literal
- (Common.target_table reference.Model.Typ.t) );
- ]
- | _ -> [ (key, Model.Identifier.quote name) ]
- in
- let columns =
- ("id", "\"id\"") :: List.concat_map list.Model.DataList.fields ~f:column
+(** Insert two rows in the given list, with a generated provider *)
+let insert_rows db (list : Model.list) =
+ let fields = list.Model.DataList.fields in
+ let* _id1 =
+ Query.insert_list db list (Common.provider_for_fields fields 1L)
in
- let open Caqti.Templater in
- direct T.(unit -->* string)
- (Common.json_select_sql list.Model.DataList.id columns)
-
-let external_table_as_json :
- Model.Identifier.t ->
- Model.Identifier.t ->
- (unit, string, _) Caqti.Template.Request.t =
- fun table target_table ->
- let open Caqti.Templater in
- direct T.(unit -->* string)
- (Common.json_select_sql table
- [
- ("parent", "\"parent\"");
- ("target", "\"target\"");
- ( "target_table",
- Croquette_common.Sql.quote_literal
- (Model.Identifier.to_string target_table) );
- ])
-
-(** A provider giving a value for each field of the list, depending on its type.
- The references point to the row of id 1 of their target list. *)
-let provider_for (list : Model.list) (row : Int64.t) : string -> Value.value =
- fun field_name ->
- let field =
- List.find list.Model.DataList.fields ~f:(fun field ->
- String.equal field_name
- (Model.Identifier.to_string (Model.Field.name field)))
+ let* _id2 =
+ Query.insert_list db list (Common.provider_for_fields fields 2L)
in
- let (Model.Field.T ty) = Model.Field.type_ field in
- match ty with
- | Model.Typ.Text ->
- Value.single (Text, Some (field_name ^ "-" ^ Int64.to_string row))
- | Model.Typ.Integer -> Value.single (Integer, Some row)
- | Model.Typ.MonoReference reference ->
- Value.single (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 (MultiReference reference, List.to_seq values)
-
-let insert_rows db (list : Model.list) =
- let* _id1 = Query.insert_list db list (provider_for list 1L) in
- let* _id2 = Query.insert_list db list (provider_for list 2L) in
Lwt.return_unit
let exec : string list -> t =
fun _args ->
- let stdin = Common.read_stdin () in
- let+ configuration = Configuration.Read_conf.read_string_conf stdin in
- Common.with_db configuration (fun db ->
- let module Db = (val db) in
+ Common.exec_with_conf (fun configuration db ->
let lists = Configuration.Ast.lists configuration in
(* Insert two rows per list, in identifier order *)
let* () = Lwt_list.iter_s (insert_rows db) lists in
- (* Display the list tables, each under a header naming the table *)
+ (* Display the list tables, each under a header naming the table. The
+ reference columns are followed by a [<field>_table] literal naming the
+ targeted table, so that the output is self-describing. *)
let* rows =
Lwt_list.map_s
(fun list ->
let table = Model.Identifier.to_string list.Model.DataList.id in
- let* res = Db.collect_list (list_as_json list) () in
- let* json = Caqti_lwt.or_fail res in
+ let columns =
+ ("id", Model.Identifier.quote Model.Identifier.id)
+ :: Common.field_columns ~annotate_references:true
+ list.Model.DataList.fields
+ in
+ let* json = Common.collect_json db list.Model.DataList.id columns in
Lwt.return ((table ^ ":") :: json))
lists
in
@@ -122,10 +66,16 @@ let exec : string list -> t =
Lwt_list.map_s
(fun (table, target) ->
let name = Model.Identifier.to_string table in
- let* res =
- Db.collect_list (external_table_as_json table target) ()
+ let columns =
+ [
+ ("parent", Model.Identifier.quote Model.Identifier.parent);
+ ("target", Model.Identifier.quote Model.Identifier.target);
+ ( "target_table",
+ Croquette_common.Sql.quote_literal
+ (Model.Identifier.to_string target) );
+ ]
in
- let* json = Caqti_lwt.or_fail res in
+ let* json = Common.collect_json db table columns in
Lwt.return ((name ^ ":") :: json))
external_tables
in
diff --git a/test/load.ml b/test/load.ml
new file mode 100644
index 0000000..8f4d2fe
--- /dev/null
+++ b/test/load.ml
@@ -0,0 +1,37 @@
+let name = __FILE__
+
+let description =
+ "Insert two rows of the given content, load them as values with [Value.load] \
+ and print them with [Value.pp]. The configuration is read on the standard \
+ input"
+
+let speclist () = []
+
+type t = (string list, Configuration.Read_conf.error) result
+
+let pp : Format.formatter -> t -> unit =
+ Format.pp_print_result ~error:Configuration.Read_conf.pp_error
+ ~ok:
+ (Format.pp_print_list ~pp_sep:Format.pp_print_cut Format.pp_print_string)
+
+let exec : string list -> t = function
+ | [ content_id ] ->
+ Common.exec_with_conf (fun configuration db ->
+ let content =
+ Option.get
+ (Configuration.Ast.get_content configuration
+ (Model.Identifier.of_string content_id))
+ in
+ let ( let* ) = Lwt.bind in
+ let* id1 = Query.insert db content (Common.provider_for content 1L) in
+ let* id2 = Query.insert db content (Common.provider_for content 2L) in
+ let load_one id =
+ let v = Value.v content id in
+ let* () = Value.load db v in
+ Lwt.return
+ [ Format.sprintf "row %Ld:" id; Format.asprintf "%a" Value.pp v ]
+ in
+ let* lines1 = load_one id1 in
+ let* lines2 = load_one id2 in
+ Lwt.return (lines1 @ lines2))
+ | _ -> failwith "Invalid arguments"
diff --git a/test/select.ml b/test/select.ml
new file mode 100644
index 0000000..d185772
--- /dev/null
+++ b/test/select.ml
@@ -0,0 +1,46 @@
+open StdLabels
+
+let name = __FILE__
+
+let description =
+ "Insert two rows of the given content, then read them back with \
+ [Query.select]. The configuration is read on the standard input"
+
+let speclist () = []
+
+type t = (string list, Configuration.Read_conf.error) result
+
+let pp : Format.formatter -> t -> unit =
+ Format.pp_print_result ~error:Configuration.Read_conf.pp_error
+ ~ok:
+ (Format.pp_print_list ~pp_sep:Format.pp_print_cut Format.pp_print_string)
+
+let exec : string list -> t = function
+ | [ content_id ] ->
+ Common.exec_with_conf (fun configuration db ->
+ let content =
+ Option.get
+ (Configuration.Ast.get_content configuration
+ (Model.Identifier.of_string content_id))
+ in
+ let ( let* ) = Lwt.bind in
+ let* id1 = Query.insert db content (Common.provider_for content 1L) in
+ let* id2 = Query.insert db content (Common.provider_for content 2L) in
+ let select_one id =
+ let acc = ref [] in
+ let* () =
+ Query.select db id content
+ {
+ f =
+ (fun (named, data) ->
+ acc := Common.show_field named data :: !acc);
+ }
+ in
+ Lwt.return (List.rev !acc)
+ in
+ let* lines1 = select_one id1 in
+ let* lines2 = select_one id2 in
+ Lwt.return
+ (Format.sprintf "row %Ld:" id1
+ :: (lines1 @ (Format.sprintf "row %Ld:" id2 :: lines2))))
+ | _ -> failwith "Invalid arguments"
diff --git a/test/tests.ml b/test/tests.ml
index c6ab006..06f4451 100644
--- a/test/tests.ml
+++ b/test/tests.ml
@@ -38,6 +38,8 @@ let () =
register (module Path);
register (module Update);
register (module Identifier);
+ register (module Select);
+ register (module Load);
]
let args = ref []
diff --git a/test/update.ml b/test/update.ml
index 18c3821..502ad05 100644
--- a/test/update.ml
+++ b/test/update.ml
@@ -67,7 +67,10 @@ let configuration =
let speclist () =
let (lazy configuration) = configuration in
- let content = Configuration.Ast.get_content configuration (Model.Identifier.of_string "entity") in
+ let content =
+ Configuration.Ast.get_content configuration
+ (Model.Identifier.of_string "entity")
+ in
let fields = Model.Content.fields (Option.get content) in
Seq.fold_left
(fun acc feature ->
@@ -81,7 +84,10 @@ let speclist () =
Arg.String
(fun value ->
actions :=
- Text (Model.Identifier.to_string (Model.Field.name feature), value) :: !actions),
+ Text
+ ( Model.Identifier.to_string (Model.Field.name feature),
+ value )
+ :: !actions),
"<v> Set field with the value <v>" )
| Model.Typ.Integer ->
Some
@@ -89,7 +95,10 @@ let speclist () =
Arg.Int
(fun value ->
actions :=
- Integer (Model.Identifier.to_string (Model.Field.name feature), value) :: !actions),
+ Integer
+ ( Model.Identifier.to_string (Model.Field.name feature),
+ value )
+ :: !actions),
"<n> Set field with the value <n>" )
| Model.Typ.MonoReference target ->
Some
@@ -97,7 +106,10 @@ let speclist () =
Arg.Int
(fun value ->
actions :=
- Reference (target, Model.Identifier.to_string (Model.Field.name feature), value)
+ Reference
+ ( target,
+ Model.Identifier.to_string (Model.Field.name feature),
+ value )
:: !actions),
"<id> Set field with the id" )
| Model.Typ.MultiReference target ->
@@ -112,7 +124,10 @@ let speclist () =
in
actions :=
- References (target, Model.Identifier.to_string (Model.Field.name feature), ids)
+ References
+ ( target,
+ Model.Identifier.to_string (Model.Field.name feature),
+ ids )
:: !actions),
"<id,..> Set this list of values" )
in
@@ -133,7 +148,9 @@ let exec : string list -> t =
fun _ ->
let (lazy configuration) = configuration in
let content =
- Configuration.Ast.get_content configuration (Model.Identifier.of_string "entity") |> Option.get
+ Configuration.Ast.get_content configuration
+ (Model.Identifier.of_string "entity")
+ |> Option.get
in
Lwt_main.run
@@ -141,12 +158,7 @@ let exec : string list -> t =
let* db = Common.connect configuration in
let module Db = (val db) in
(* Insert a content without any value *)
- let* inserted = Query.insert db content (fun _ -> Value.none) in
- let id =
- match Value.get inserted Value.id_ref with
- | Some id -> id
- | None -> assert false
- in
+ let* id = Query.insert db content (fun _ -> Value.none) in
let group_commit : actions Seq.t Seq.t =
List.rev !actions |> List.to_seq
@@ -167,7 +179,9 @@ let exec : string list -> t =
let field =
Model.Content.fields content
|> Seq.find (fun f ->
- String.equal key (Model.Identifier.to_string (Model.Field.name f)))
+ String.equal key
+ (Model.Identifier.to_string
+ (Model.Field.name f)))
|> Option.get
in
let (Model.Field.T t) = Model.Field.type_ field in
@@ -182,13 +196,17 @@ let exec : string list -> t =
| Text (k, v) when String.equal k key ->
Some (Value.single (Model.Typ.Text, Some v))
| Integer (k, v) when String.equal k key ->
- Some (Value.single (Model.Typ.Integer, Some (Int64.of_int v)))
+ Some
+ (Value.single
+ (Model.Typ.Integer, Some (Int64.of_int v)))
| Reference (target, k, v) when String.equal k key ->
Some
(Value.single
- (Model.Typ.MonoReference target, Some (Int64.of_int v)))
+ ( Model.Typ.MonoReference target,
+ Some (Int64.of_int v) ))
| References (target, k, v) when String.equal k key ->
- Some (Value.single (Model.Typ.MultiReference target, v))
+ Some
+ (Value.single (Model.Typ.MultiReference target, v))
| _ -> None)
actions
in