summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/common.ml86
-rw-r--r--test/configuration_.ml2
-rw-r--r--test/dune1
-rw-r--r--test/hash.ml2
-rw-r--r--test/insert.ml93
-rw-r--r--test/intf.ml6
-rw-r--r--test/parent.ml2
-rw-r--r--test/schema.ml2
-rw-r--r--test/splay_tree.ml2
-rw-r--r--test/tests.ml3
-rw-r--r--test/update.ml207
11 files changed, 312 insertions, 94 deletions
diff --git a/test/common.ml b/test/common.ml
index 807289a..f06d431 100644
--- a/test/common.ml
+++ b/test/common.ml
@@ -11,3 +11,89 @@ let connect : Configuration.Ast.setting -> Caqti_lwt.connection Lwt.t =
let module Db = (val db) in
let* () = Query.create db conf in
Lwt.return db
+
+(** Load the given content and return the values as a json *)
+let content_as_json :
+ 'a Model.Content.t -> (int64, string, [ `One ]) Caqti_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)
+ in
+ let buff = Buffer.create 64 in
+ let () =
+ Seq.iter
+ (fun field ->
+ Buffer.add_char buff '\'';
+ Buffer.add_string buff (Model.Field.name field);
+
+ Buffer.add_string buff "',";
+ Buffer.add_string buff (Model.Field.name field);
+ Buffer.add_string buff ",")
+ fields
+ in
+
+ let () =
+ match Model.Content.parent content with
+ | None ->
+ let length = Buffer.length buff in
+ if length > 1 then Buffer.truncate buff (length - 1);
+ ()
+ | Some _ -> Buffer.add_string buff "'parent',parent"
+ in
+
+ let q = Buffer.contents buff in
+ Caqti_request.Infix.(Caqti_type.(int64) ->! Caqti_type.string)
+ ~oneshot:true
+ ("SELECT json_object(" ^ q ^ ") from " ^ Model.Content.id content
+ ^ " where id=?")
+
+let query_values (table : Model.Reference.t Model.Content.t) (feature : string)
+ =
+ Caqti_request.Infix.(Caqti_type.(int64) ->* Caqti_type.string)
+ ~oneshot:true
+ ("SELECT json_object('" ^ feature ^ "', target) from \""
+ ^ Model.Content.id table ^ "." ^ feature ^ "\" where parent=?")
+
+(** 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
+ content. *)
+let collect_references :
+ Caqti_lwt.connection ->
+ Model.Reference.t Model.Content.t ->
+ 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.Content.id content in
+
+ (* Only keep the multivaluated fields *)
+ let fields =
+ Seq.filter
+ (fun field ->
+ let (T ty) = Model.Field.type_ field in
+ match ty with Model.Typ.MultiReference _ -> true | _ -> false)
+ (Model.Content.fields content)
+ |> List.of_seq
+ in
+ let results =
+ 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
+ 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)
+ in
+ Lwt.return line)
+ [] fields
+ in
+
+ results
diff --git a/test/configuration_.ml b/test/configuration_.ml
index 0cc01da..a5079c3 100644
--- a/test/configuration_.ml
+++ b/test/configuration_.ml
@@ -2,7 +2,7 @@ let name = "configuration"
let description = "Load the configuration file and report errors"
let printer = ref (fun formatter _ -> Format.pp_print_nothing formatter ())
-let speclist =
+let speclist () =
[
( "--tree",
Arg.Unit
diff --git a/test/dune b/test/dune
index 51fa57a..cead409 100644
--- a/test/dune
+++ b/test/dune
@@ -13,6 +13,7 @@
caqti-driver-sqlite3
decoders
uri
+ croquette_common
splay
configuration
model
diff --git a/test/hash.ml b/test/hash.ml
index 83f7d5c..c91280b 100644
--- a/test/hash.ml
+++ b/test/hash.ml
@@ -4,7 +4,7 @@
let name = __FILE__
let description = "Print the hash of the configuration"
-let speclist = []
+let speclist () = []
type t = (int, Configuration.Read_conf.error) result
diff --git a/test/insert.ml b/test/insert.ml
index 70b98d6..bc65422 100644
--- a/test/insert.ml
+++ b/test/insert.ml
@@ -2,7 +2,7 @@ open StdLabels
let name = __FILE__
let description = "Insert data and check the result"
-let speclist = []
+let speclist () = []
type t =
((string * Int64.t * string) list, Configuration.Read_conf.error) result
@@ -14,53 +14,7 @@ let pp : Format.formatter -> t -> unit =
(fun formatter (content_id, id, content) ->
Format.fprintf formatter "@[<v 2>%s:%Ld@;%s@]" content_id id content))
-let query_columns : 'a Model.Content.t -> string =
- 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)
- in
- let buff = Buffer.create 64 in
- let () =
- Seq.iter
- (fun field ->
- Buffer.add_char buff '\'';
- Buffer.add_string buff (Model.Field.name field);
-
- Buffer.add_string buff "',";
- Buffer.add_string buff (Model.Field.name field);
- Buffer.add_string buff ",")
- fields
- in
-
- let () =
- match Model.Content.parent content with
- | None ->
- let length = Buffer.length buff in
- if length > 1 then Buffer.truncate buff (length - 1);
- ()
- | Some _ -> Buffer.add_string buff "'parent',parent"
- in
-
- Buffer.contents buff
-
type content = Model.Reference.t Model.Content.t
-
-let query (table : content) =
- Caqti_request.Infix.(Caqti_type.(int64) ->! Caqti_type.string)
- ~oneshot:true
- ("SELECT json_object(" ^ query_columns table ^ ") from "
- ^ Model.Content.id table ^ " where id=?")
-
-let query_values (table : content) (feature : string) =
- Caqti_request.Infix.(Caqti_type.(int64) ->* Caqti_type.string)
- ~oneshot:true
- ("SELECT json_object('" ^ feature ^ "', target) from \""
- ^ Model.Content.id table ^ "." ^ feature ^ "\" where parent=?")
-
type value = Model.Reference.t Model.Typ.value
type properties = (string * value) list
@@ -83,7 +37,7 @@ let insert_and_get_result :
in
let content_id = Model.Content.id content in
let module Db = (val db) in
- let* json_result = Db.find (query content) id 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)
@@ -258,43 +212,6 @@ end
module D = Make (Configuration.Json5.Decode)
-(** 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
- content. *)
-let collect_references :
- Caqti_lwt.connection -> content -> int64 -> 'a list Lwt.t =
- fun db content id ->
- let module Db = (val db) in
- let ( let* ) = Lwt.bind in
- let content_id = Model.Content.id content in
-
- (* Only keep the multivaluated fields *)
- let fields =
- Seq.filter
- (fun field ->
- let (T ty) = Model.Field.type_ field in
- match ty with Model.Typ.MultiReference _ -> true | _ -> false)
- (Model.Content.fields content)
- |> List.of_seq
- in
- let results =
- 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
- in
- let* json = Caqti_lwt.or_fail json_result in
- let line =
- List.fold_left ~init:acc json ~f:(fun acc json ->
- (content_id, id, json) :: acc)
- in
- Lwt.return line)
- [] fields
- in
-
- results
-
let exec : string list -> t =
fun _ ->
let stdin = In_channel.input_all stdin in
@@ -324,7 +241,7 @@ let exec : string list -> t =
let acc = (content_id, id, json) :: acc in
(* Also query the multivaluated references *)
- let* ext = collect_references db content id in
+ let* ext = Common.collect_references db content id in
let acc = List.append ext acc in
(* Insert the subcontent associated *)
Lwt_list.fold_left_s
@@ -339,7 +256,9 @@ let exec : string list -> t =
let acc = (subcontent_id, id, json) :: acc in
(* Also query the multivaluated references *)
- let* ext = collect_references db subcontent id in
+ let* ext =
+ Common.collect_references db subcontent id
+ in
let acc = List.append ext acc in
Lwt.return acc)
acc entries
diff --git a/test/intf.ml b/test/intf.ml
index a68fa88..5012ba2 100644
--- a/test/intf.ml
+++ b/test/intf.ml
@@ -2,8 +2,12 @@ module type T = sig
type t
val exec : string list -> t
+ (** [exec args] execute the test.
+
+ [args] is the list of the anonymous arguments given in the command line *)
+
val pp : Format.formatter -> t -> unit
- val speclist : (string * Arg.spec * string) list
+ val speclist : unit -> (string * Arg.spec * string) list
val name : string
val description : string
end
diff --git a/test/parent.ml b/test/parent.ml
index a13afda..232de34 100644
--- a/test/parent.ml
+++ b/test/parent.ml
@@ -6,7 +6,7 @@ let description =
let parent = ref Model.Content.parent
-let speclist =
+let speclist () =
[
( "--root",
Arg.Unit
diff --git a/test/schema.ml b/test/schema.ml
index 0dc386d..b063270 100644
--- a/test/schema.ml
+++ b/test/schema.ml
@@ -1,6 +1,6 @@
let name = __FILE__
let description = "Print the schema for the given tables"
-let speclist = []
+let speclist () = []
type t = (string list, Configuration.Read_conf.error) result
diff --git a/test/splay_tree.ml b/test/splay_tree.ml
index acd2f2b..7a9d2f7 100644
--- a/test/splay_tree.ml
+++ b/test/splay_tree.ml
@@ -12,7 +12,7 @@ type t = unit T.t
let pp : Format.formatter -> t -> unit = T.pp
let tree : unit T.t ref = ref (T.empty ())
-let speclist =
+let speclist () =
[
( "--add",
Arg.Int (fun n -> tree := T.add n () !tree),
diff --git a/test/tests.ml b/test/tests.ml
index 9242495..d396ac0 100644
--- a/test/tests.ml
+++ b/test/tests.ml
@@ -18,7 +18,7 @@ let register : (module Intf.T) -> string * Arg.spec * string =
( "--" ^ name,
Arg.Unit
(fun () ->
- speclist := T.speclist;
+ speclist := T.speclist ();
submodule := Some (module T : Intf.T)),
T.description )
@@ -34,6 +34,7 @@ let () =
register (module Hash);
register (module Configuration_);
register (module Insert);
+ register (module Update);
]
let args = ref []
diff --git a/test/update.ml b/test/update.ml
new file mode 100644
index 0000000..1e94fc2
--- /dev/null
+++ b/test/update.ml
@@ -0,0 +1,207 @@
+let name = __FILE__
+let description = "Update a content"
+
+type actions =
+ | Commit
+ | Unset of string
+ | Text of string * string
+ | Integer of string * int
+ | Reference of Model.Reference.t Model.Typ.reference * string * int
+ | References of Model.Reference.t Model.Typ.reference * string * int64 Seq.t
+
+let actions : actions list ref = ref []
+
+type t = string list
+
+let pp : Format.formatter -> t -> unit = Format.(pp_print_list pp_print_string)
+
+let configuration =
+ lazy
+ (let model =
+ {|
+ {
+ "content": [
+ {
+ "id": "entity",
+ "section": [
+ {
+ "field": [
+ {
+ "name": "references",
+ "label": {},
+ "type": {
+ "content": "entity",
+ "multivaluated": true
+ }
+ },
+ {
+ "name": "reference",
+ "label": {},
+ "type": {
+ "content": "entity",
+ "multivaluated": false
+ }
+ },
+ {
+ "name": "integer",
+ "label": {},
+ "type": "integer"
+ },
+ {
+ "name": "text",
+ "label": {},
+ "type": "text"
+ },
+ ]
+ }
+ ]
+ }
+ ]
+ }|}
+ in
+ Configuration.Read_conf.read_string_conf model
+ |> Result.map_error (fun e ->
+ Croquette_common.Format.format_string (fun fmt ->
+ Configuration.Read_conf.pp_error fmt e))
+ |> Result.get_ok')
+
+let speclist () =
+ let (lazy configuration) = configuration in
+ let content = Configuration.Ast.get_content configuration "entity" in
+ let fields = Model.Content.fields (Option.get content) in
+ Seq.fold_left
+ (fun acc feature ->
+ let key = "--" ^ Model.Field.name feature in
+ let (Model.Field.T type_) = Model.Field.type_ feature in
+ let argument =
+ match type_ with
+ | Model.Typ.Text ->
+ Some
+ ( key,
+ Arg.String
+ (fun value ->
+ actions :=
+ Text (Model.Field.name feature, value) :: !actions),
+ "<v> Set field with the value <v>" )
+ | Model.Typ.Integer ->
+ Some
+ ( key,
+ Arg.Int
+ (fun value ->
+ actions :=
+ Integer (Model.Field.name feature, value) :: !actions),
+ "<n> Set field with the value <n>" )
+ | Model.Typ.MonoReference target ->
+ Some
+ ( key,
+ Arg.Int
+ (fun value ->
+ actions :=
+ Reference (target, Model.Field.name feature, value)
+ :: !actions),
+ "<id> Set field with the id" )
+ | Model.Typ.MultiReference target ->
+ Some
+ ( key,
+ Arg.String
+ (fun value ->
+ let ids =
+ String.split_on_char ',' value
+ |> List.map (fun v -> Int64.of_string (String.trim v))
+ |> List.to_seq
+ in
+
+ actions :=
+ References (target, Model.Field.name feature, ids)
+ :: !actions),
+ "<id,..> Set this list of values" )
+ in
+
+ match argument with None -> acc | Some v -> v :: acc)
+ [
+ ( "--commit",
+ Arg.Unit (fun () -> actions := Commit :: !actions),
+ "\tCommit this group of update" );
+ ( "--clear",
+ Arg.String (fun feature -> actions := Unset feature :: !actions),
+ "<f> Clear the field <f>" );
+ ]
+ fields
+ |> Arg.align
+
+let exec : string list -> t =
+ fun _ ->
+ let (lazy configuration) = configuration in
+ let content =
+ Configuration.Ast.get_content configuration "entity" |> Option.get
+ in
+
+ Lwt_main.run
+ (let ( let* ) = Lwt.bind in
+ 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 group_commit : actions Seq.t Seq.t =
+ List.rev !actions |> List.to_seq
+ |> Seq.group (fun a b ->
+ match (a, b) with _, Commit -> false | _ -> true)
+ in
+ (* Update the values from the argument in the command line *)
+ let* printers =
+ Seq.fold_left
+ (fun acc actions ->
+ let* acc = acc in
+ let* () =
+ Query.update db id content (fun key ->
+ let value =
+ Seq.find_map
+ (function
+ | Unset k when String.equal k key ->
+ let field =
+ Model.Content.fields content
+ |> Seq.find (fun f ->
+ String.equal key (Model.Field.name f))
+ |> Option.get
+ in
+ 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))
+ | MonoReference _ as e ->
+ Some Model.Typ.(V (e, None))
+ end
+ | Text (k, v) when String.equal k key ->
+ Some Model.Typ.(V (Text, Some v))
+ | Integer (k, v) when String.equal k key ->
+ Some Model.Typ.(V (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)))
+ | References (target, k, v) when String.equal k key ->
+ Some Model.Typ.(Seq (MultiReference target, v))
+ | _ -> None)
+ actions
+ in
+ match value with Some v -> v | None -> Model.Typ.None)
+ in
+ (* Get the values from the content registered in the database *)
+ let* json_result = Db.find (Common.content_as_json content) id in
+ let* json = Caqti_lwt.or_fail json_result in
+
+ (* Also query for the referenoces *)
+ let* refs = Common.collect_references db content id in
+ let acc =
+ List.fold_left
+ (fun acc (_, _, ref) -> ref :: acc)
+ (json :: acc) (List.rev refs)
+ in
+
+ Lwt.return acc)
+ (Lwt.return []) group_commit
+ in
+ Lwt.return (List.rev printers))