diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-03-27 08:48:05 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-03-27 08:48:05 +0100 |
| commit | 830e7c5d27d161b91402414e6ecd49f1ed818bfb (patch) | |
| tree | 448864635d29b1ea66424cbb9e0816fbcf1e1907 /test | |
Initial commit
Diffstat (limited to 'test')
| -rw-r--r-- | test/common.ml | 13 | ||||
| -rw-r--r-- | test/configuration_.ml | 27 | ||||
| -rw-r--r-- | test/dune | 20 | ||||
| -rw-r--r-- | test/hash.ml | 20 | ||||
| -rw-r--r-- | test/insert.ml | 186 | ||||
| -rw-r--r-- | test/intf.ml | 9 | ||||
| -rw-r--r-- | test/parent.ml | 42 | ||||
| -rw-r--r-- | test/printer.ml | 22 | ||||
| -rw-r--r-- | test/schema.ml | 32 | ||||
| -rw-r--r-- | test/splay_tree.ml | 28 | ||||
| -rw-r--r-- | test/tests.ml | 50 |
11 files changed, 449 insertions, 0 deletions
diff --git a/test/common.ml b/test/common.ml new file mode 100644 index 0000000..807289a --- /dev/null +++ b/test/common.ml @@ -0,0 +1,13 @@ +(** Common fuctions shared between the tests *) + +let ( let* ) = Lwt.bind + +(** Connect to the database in memory and create a schema matching the given + configuration *) +let connect : Configuration.Ast.setting -> Caqti_lwt.connection Lwt.t = + fun conf -> + let* conn = Caqti_lwt_unix.connect (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 + Lwt.return db diff --git a/test/configuration_.ml b/test/configuration_.ml new file mode 100644 index 0000000..0fd42fb --- /dev/null +++ b/test/configuration_.ml @@ -0,0 +1,27 @@ +let name = "configuration" +let description = "Load the configuration file and report errors" +let printer = ref (fun formatter _ -> Format.pp_print_nothing formatter ()) + +let speclist = + [ + ( "-tree", + Arg.Unit + (fun () -> + printer := + fun formater conf -> + Configuration.Graph_tree.pp formater (Configuration.Ast.tree conf)), + "Print the depenncy tree between root contents" ); + ] + +type t = (Configuration.Ast.setting, Configuration.Read_conf.error) result + +let pp : Format.formatter -> t -> unit = + Format.pp_print_result ~error:Configuration.Read_conf.pp_error + ~ok:(fun formater conf -> !printer formater conf) + +let exec : string list -> t = + fun _ -> + let stdin = In_channel.input_all stdin in + let ( let+ ) opt cont = Result.map cont opt in + let+ configuration = Configuration.Read_conf.read_string_conf stdin in + configuration diff --git a/test/dune b/test/dune new file mode 100644 index 0000000..51fa57a --- /dev/null +++ b/test/dune @@ -0,0 +1,20 @@ +(executable + (name tests) + (libraries + lwt + lwt.unix + dream + dwr_dream + dwr_cohttp + services_impl + caqti + caqti-lwt + caqti-lwt.unix + caqti-driver-sqlite3 + decoders + uri + splay + configuration + model + html + query)) diff --git a/test/hash.ml b/test/hash.ml new file mode 100644 index 0000000..83f7d5c --- /dev/null +++ b/test/hash.ml @@ -0,0 +1,20 @@ +(** Just a proof of concept. + + Display the hash of the configuration *) + +let name = __FILE__ +let description = "Print the hash of the configuration" +let speclist = [] + +type t = (int, 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_int + +let exec : string list -> t = + fun _ -> + let stdin = In_channel.input_all stdin in + let ( let+ ) opt cont = Result.map cont opt in + let+ configuration = Configuration.Read_conf.read_string_conf stdin in + Hashtbl.hash configuration diff --git a/test/insert.ml b/test/insert.ml new file mode 100644 index 0000000..eaa132f --- /dev/null +++ b/test/insert.ml @@ -0,0 +1,186 @@ +open StdLabels + +let name = __FILE__ +let description = "Insert data and check the result" +let speclist = [] + +type t = + ((string * Int64.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 + (fun formatter (content_id, id, content) -> + Format.fprintf formatter "@[<v 2>%s:%Ld@;%s@]" content_id id content)) + +(** Read the values to insert in the configuration *) +module Make (S : Decoders.Decode.S) = struct + let ( let* ) = S.( let* ) + let ( let+ ) = S.( let+ ) + + type value = Model.Reference.t Model.Typ.value + + (** The values are encoded in a very simple way, depending of the nature of + the field in the json, we create a property in for text or numeric *) + let value : value S.decoder = + S.one_of + [ + ( "numeric", + S.map (fun v -> Model.Typ.(V (Integer, Int64.of_int v))) S.int ); + ("text", S.map (fun v -> Model.Typ.(V (Text, v))) S.string); + ("other", S.map (fun _ -> Model.Typ.None) S.value); + ] + + type content = Model.Reference.t Model.Content.t + type properties = (string * value) list + type content_to_insert = content * properties list + + let rec properties : + Configuration.Ast.setting -> + string -> + (content * (properties * content_to_insert list) list) S.decoder = + fun conf name -> + (* We are loading the same configuration template, the content is supposed + to exists by definition and will never be None *) + let content = Configuration.Ast.get_content conf name |> Option.get in + let* properties = + S.field_opt_or ~default:[] "values" (S.list (S.key_value_pairs value)) + in + let* subcontents = + S.field_opt_or ~default:[] "values" (S.list (subcontents conf)) + in + let to_insert = List.combine properties subcontents in + S.succeed (content, to_insert) + + and subcontents : + Configuration.Ast.setting -> content_to_insert list S.decoder = + fun conf -> + let* properties = + S.key_value_pairs_seq (fun key -> + S.one_of + [ + ( "subcontent", + fun v -> + match Configuration.Ast.get_content conf key with + | None -> Error (Decoders.Error.make "no content") + | Some subcontent -> + let res = S.list (S.key_value_pairs value) v in + let v = Result.map (fun v -> Some (subcontent, v)) res in + v ); + ("other", fun _ -> Ok None); + ]) + in + let properties = List.filter_map ~f:(fun v -> v) properties in + S.succeed properties + + let content : + Configuration.Ast.setting -> + (content * (properties * content_to_insert list) list) S.decoder = + fun conf -> + let* name = S.field "id" S.string in + properties conf name + + let configuration : + Configuration.Ast.setting -> + (content * (properties * content_to_insert list) list) list S.decoder = + fun conf -> S.field "content" (S.list (content conf)) +end + +module D = Make (Configuration.Json5.Decode) + +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 + +let query table = + 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 insert_and_get_result ?parent db content entry = + let ( let* ) = Lwt.bind in + let* id = + Query.insert ?parent db content (fun v -> + match List.assoc_opt v entry with Some v -> v | None -> Model.Typ.None) + 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 = Caqti_lwt.or_fail json_result in + Lwt.return (content_id, id, json) + +let exec : string list -> t = + fun _ -> + let stdin = In_channel.input_all stdin in + let ( let* ) = Result.bind in + let* configuration = Configuration.Read_conf.read_string_conf stdin in + let* inserts = + Configuration.Json5.Decode.decode_string + (D.configuration configuration) + stdin + in + Lwt_main.run + (let ( let* ) = Lwt.bind in + let* db = Common.connect configuration in + let module Db = (val db) in + let* insertions = + Lwt_list.fold_left_s + (fun acc (content, entries) -> + Lwt_list.fold_left_s + (fun acc (entry, subcontents) -> + let* content_id, id, json = + insert_and_get_result db content entry + in + + let acc = (content_id, id, json) :: acc in + + (* Insert the subcontent associated *) + Lwt_list.fold_left_s + (fun acc (subcontent, entries) -> + let* acc = + Lwt_list.fold_left_s + (fun acc entry -> + let* subcontent_id, id, json = + insert_and_get_result ~parent:id db subcontent entry + in + Lwt.return ((subcontent_id, id, json) :: acc)) + acc entries + in + + Lwt.return acc) + acc subcontents) + acc entries) + [] inserts + in + Lwt.return_ok (List.rev insertions)) diff --git a/test/intf.ml b/test/intf.ml new file mode 100644 index 0000000..a68fa88 --- /dev/null +++ b/test/intf.ml @@ -0,0 +1,9 @@ +module type T = sig + type t + + val exec : string list -> t + val pp : Format.formatter -> t -> unit + val speclist : (string * Arg.spec * string) list + val name : string + val description : string +end diff --git a/test/parent.ml b/test/parent.ml new file mode 100644 index 0000000..4a15bfa --- /dev/null +++ b/test/parent.ml @@ -0,0 +1,42 @@ +let name = __FILE__ + +let description = + "Return the parent of the given content. The configuration is read on the \ + standard input" + +let parent = ref Model.Content.parent + +let speclist = + [ + ( "-root", + Arg.Unit + (fun () -> parent := fun content -> Some (Model.Content.root content)), + "Return the root element instead of the parent" ); + ] + +type t = (string option option, 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_option ~none:(fun f () -> + Format.pp_print_string f "[Content not found]") + @@ Format.pp_print_option ~none:(fun f () -> + Format.pp_print_string f "[No parent]") + @@ fun f -> Format.fprintf f "\"%s\"") + +let exec : string list -> t = function + | [ subcontent_id ] -> + let parent_id = + let stdin = In_channel.input_all stdin in + let ( let+ ) opt cont = Result.map cont opt in + let+ configuration = Configuration.Read_conf.read_string_conf stdin in + let ( let+ ) opt cont = Option.map cont opt in + let+ subcontent = + Configuration.Ast.get_content configuration subcontent_id + in + let+ v = !parent subcontent in + Model.Content.id v + in + parent_id + | _ -> failwith "Invalid arguments" diff --git a/test/printer.ml b/test/printer.ml new file mode 100644 index 0000000..5469d5e --- /dev/null +++ b/test/printer.ml @@ -0,0 +1,22 @@ +let name = __FILE__ +let description = "Print the configuration in json format" +let speclist = [] + +type t = (Configuration.Ast.setting, Configuration.Read_conf.error) result + +module TomlConf = Configuration.Read_conf.Make (Configuration.Toml.Decode) + +let read_conf : + string -> + (Configuration.Ast.setting, Configuration.Toml.Decode.error) result = + fun file -> Configuration.Toml.Decode.decode_file TomlConf.t file + +module ToJson = Configuration.Read_conf.Make (Configuration.Json5.Encode) + +let print_conf : + Configuration.Ast.setting -> Configuration.Json5.Json_encodeable.value = + fun _ -> raise Not_found + +let pp : Format.formatter -> t -> unit = + Format.pp_print_result ~error:Configuration.Read_conf.pp_error + ~ok:Format.pp_print_int diff --git a/test/schema.ml b/test/schema.ml new file mode 100644 index 0000000..0dc386d --- /dev/null +++ b/test/schema.ml @@ -0,0 +1,32 @@ +let name = __FILE__ +let description = "Print the schema for the given tables" +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_newline + Format.pp_print_string) + +let ( let* ) = Lwt.bind +let ( let+ ) opt cont = Result.map cont opt + +let schema_query = + Caqti_request.Infix.(Caqti_type.(string) ->! Caqti_type.string) + "SELECT sql from sqlite_schema where name=?" + +let get_schema : Caqti_lwt.connection -> string -> string Lwt.t = + fun db table_name -> + let module Db = (val db) in + let* result = Db.find schema_query table_name in + Caqti_lwt.or_fail result + +let exec : string list -> t = + fun tables -> + let stdin = In_channel.input_all stdin in + let+ configuration = Configuration.Read_conf.read_string_conf stdin in + Lwt_main.run + (let* db = Common.connect configuration in + Lwt_list.map_s (get_schema db) (List.rev tables)) diff --git a/test/splay_tree.ml b/test/splay_tree.ml new file mode 100644 index 0000000..318fc96 --- /dev/null +++ b/test/splay_tree.ml @@ -0,0 +1,28 @@ +let name = __FILE__ +let description = "Display the tree content after operations" + +module T = Splay.Make (struct + include Int + + let pp = Format.pp_print_int +end) + +type t = unit T.t + +let pp : Format.formatter -> t -> unit = T.pp +let tree : unit T.t ref = ref (T.empty ()) + +let speclist = + [ + ( "-add", + Arg.Int (fun n -> tree := T.add n () !tree), + "Add an element in the tree" ); + ( "-del", + Arg.Int (fun n -> tree := T.remove n !tree), + "Remove an element from the tree" ); + ( "-get", + Arg.Int (fun n -> ignore @@ T.find n !tree), + "Get an element from the tree" ); + ] + +let exec : string list -> t = fun _ -> !tree diff --git a/test/tests.ml b/test/tests.ml new file mode 100644 index 0000000..5e2bd59 --- /dev/null +++ b/test/tests.ml @@ -0,0 +1,50 @@ +(** Test library entry point. + + This library is a companion for the CRAM tests exopsing functions for the + test. + + Each module can be executed by giving its name as an argument to the binary. + The arguments expected by each test module are not specified *) + +let submodule = ref None +let speclist = ref [] + +let register : (module Intf.T) -> string * Arg.spec * string = + fun (module T) -> + let name = + T.name |> String.lowercase_ascii |> Filename.remove_extension + |> Filename.basename + in + ( "-" ^ name, + Arg.Unit + (fun () -> + speclist := T.speclist; + submodule := Some (module T : Intf.T)), + T.description ) + +(** Register all the tests in the library *) +let () = + speclist := + List.sort + (fun (n1, _, _) (n2, _, _) -> String.compare n1 n2) + [ + register (module Splay_tree); + register (module Parent); + register (module Schema); + register (module Hash); + register (module Configuration_); + register (module Insert); + ] + +let args = ref [] +let annon_fun arg = args := arg :: !args +let () = Arg.parse_dynamic speclist annon_fun "Start the tests" + +let () = + match !submodule with + | None -> + prerr_endline "Please select a test from the library to run"; + exit 1 + | Some (module SubCommand : Intf.T) -> + let result = SubCommand.exec !args in + Format.printf "@[<v 0>%a@]@." SubCommand.pp result |
