diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-11-20 16:23:53 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-11-20 16:23:53 +0100 |
| commit | b05e2d2be7be28ee6c577671002797cf9abaf177 (patch) | |
| tree | 0474684d619721024f0a6cb132cf2f4d2e84c344 /test | |
| parent | c4ca180fd7c2085e3ce3f8e32aa0c2cb724f4c53 (diff) | |
New module for working with Values
Diffstat (limited to 'test')
| -rw-r--r-- | test/dune | 3 | ||||
| -rw-r--r-- | test/insert.ml | 35 | ||||
| -rw-r--r-- | test/insert_list.ml | 10 | ||||
| -rw-r--r-- | test/update.ml | 23 |
4 files changed, 37 insertions, 34 deletions
@@ -14,4 +14,5 @@ configuration model html - query)) + query + value)) diff --git a/test/insert.ml b/test/insert.ml index 217fc83..ae5748e 100644 --- a/test/insert.ml +++ b/test/insert.ml @@ -15,7 +15,7 @@ let pp : Format.formatter -> t -> unit = Format.fprintf formatter "@[<v 2>%s:%Ld@;%s@]" content_id id content)) type content = Model.content -type value = Model.value +type value = Value.value type properties = (string * value) list (* A subcontent to insert, with one entry (properties) per row *) @@ -31,17 +31,18 @@ type entry = properties * content_to_insert list [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:int64 -> + ?parent:Model.content Value.t -> Caqti_lwt.connection -> content -> properties -> (string * int64 * string) Lwt.t = fun ?parent db content entry -> let ( let* ) = Lwt.bind in - let* id = + let* inserted = Query.insert ?parent db content (fun v -> - match List.assoc_opt v entry with Some v -> v | None -> Model.Typ.None) + match List.assoc_opt v entry with Some v -> v | None -> Value.none) in + let id = Value.id inserted 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 @@ -49,19 +50,19 @@ let insert_and_get_result : 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 id of the new row. *) + multivalued references. Return the accumulator and the inserted value. *) let insert_one : Caqti_lwt.connection -> - ?parent:int64 -> + ?parent:Model.content Value.t -> content -> properties -> (string * Int64.t * string) list -> - ((string * Int64.t * string) list * Int64.t) Lwt.t = + ((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), id) + Lwt.return (List.append ext ((content_id, id, json) :: acc), Value.v content id) module S = Configuration.Json5.Decode @@ -96,14 +97,14 @@ let rec value : ( "numeric", S.map (fun v -> - Lwt.return (key, Model.Typ.(V (Integer, Some (Int64.of_int v))))) + Lwt.return (key, Value.single (Model.Typ.Integer, Some (Int64.of_int v)))) S.int ); ( "text", - S.map (fun v -> Lwt.return (key, Model.Typ.(V (Text, Some v)))) S.string + 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, Model.Typ.None))); + ("other", S.succeed (Lwt.return (key, Value.none))); ] (** Decode a reference field: single value or list of values, depending on @@ -131,7 +132,7 @@ and reference_value : (fun ids -> Lwt.return ( key, - Model.Typ.Seq + Value.multi ( Model.Typ.MultiReference reference, List.to_seq (List.map ~f:Int64.of_int ids) ) )) (S.list S.int) @@ -140,7 +141,7 @@ and reference_value : (fun v -> Lwt.return ( key, - Model.Typ.V + Value.single (Model.Typ.MonoReference reference, Some (Int64.of_int v)) )) S.int @@ -159,7 +160,7 @@ and reference_value : in Lwt.return ( key, - Model.Typ.Seq + Value.multi (Model.Typ.MultiReference reference, List.to_seq ids) )) else let** props = properties db target in @@ -167,7 +168,7 @@ and reference_value : (let* props = props in let* _, id, _ = insert_and_get_result db target props in Lwt.return - (key, Model.Typ.V (Model.Typ.MonoReference reference, Some id))) + (key, Value.single (Model.Typ.MonoReference reference, Some id))) ) and properties : Caqti_lwt.connection -> content -> properties Lwt.t S.decoder = @@ -257,14 +258,14 @@ let exec : string list -> t = Lwt_list.fold_left_s (fun acc (entry, subcontents) -> (* Insert the main content *) - let* acc, id = insert_one db content entry acc in + 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:id subcontent entry acc + insert_one db ~parent subcontent entry acc in Lwt.return acc) acc subentries) diff --git a/test/insert_list.ml b/test/insert_list.ml index 9d4018f..ffc4169 100644 --- a/test/insert_list.ml +++ b/test/insert_list.ml @@ -60,7 +60,7 @@ let external_table_as_json : (** 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 -> Model.value = +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 -> @@ -70,14 +70,14 @@ let provider_for (list : Model.list) (row : Int64.t) : string -> Model.value = let (Model.Field.T ty) = Model.Field.type_ field in match ty with | Model.Typ.Text -> - Model.Typ.V (Text, Some (field_name ^ "-" ^ Int64.to_string row)) - | Model.Typ.Integer -> Model.Typ.V (Integer, Some row) + Value.single (Text, Some (field_name ^ "-" ^ Int64.to_string row)) + | Model.Typ.Integer -> Value.single (Integer, Some row) | Model.Typ.MonoReference reference -> - Model.Typ.V (MonoReference reference, Some 1L) + 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 - Model.Typ.Seq (MultiReference reference, List.to_seq values) + Value.multi (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 diff --git a/test/update.ml b/test/update.ml index 74208de..ec9bf91 100644 --- a/test/update.ml +++ b/test/update.ml @@ -141,7 +141,8 @@ let exec : string list -> t = let* db = Common.connect configuration in let module Db = (val db) in (* Insert a content without any value *) - let* id = Query.insert db content (fun _ -> Model.Typ.None) in + let* inserted = Query.insert db content (fun _ -> Value.none) in + let id = Value.id inserted in let group_commit : actions Seq.t Seq.t = List.rev !actions |> List.to_seq @@ -168,26 +169,26 @@ let exec : string list -> t = let (Model.Field.T t) = Model.Field.type_ field in begin match t with | Model.Typ.MultiReference _ as e -> - Some Model.Typ.(Seq (e, Seq.empty)) - | Integer as e -> Some Model.Typ.(V (e, None)) - | Text as e -> Some Model.Typ.(V (e, None)) + Some (Value.multi (e, Seq.empty)) + | Integer as e -> Some (Value.single (e, None)) + | Text as e -> Some (Value.single (e, None)) | MonoReference _ as e -> - Some Model.Typ.(V (e, None)) + Some (Value.single (e, None)) end | Text (k, v) when String.equal k key -> - Some Model.Typ.(V (Text, Some v)) + Some (Value.single (Model.Typ.Text, Some v)) | Integer (k, v) when String.equal k key -> - Some Model.Typ.(V (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 - Model.Typ.( - V (MonoReference target, Some (Int64.of_int v))) + (Value.single + (Model.Typ.MonoReference target, Some (Int64.of_int v))) | References (target, k, v) when String.equal k key -> - Some Model.Typ.(Seq (MultiReference target, v)) + Some (Value.multi (Model.Typ.MultiReference target, v)) | _ -> None) actions in - match value with Some v -> v | None -> Model.Typ.None) + match value with Some v -> v | None -> Value.none) in (* Get the values from the content registered in the database *) let* json_result = Db.find (Common.content_as_json content) id in |
