From 6b377719c10d5ab3343fd5221f99a4a21008e25a Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Thu, 14 Mar 2024 08:26:58 +0100 Subject: Initial commit --- tests/confLoader.ml | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 tests/confLoader.ml (limited to 'tests/confLoader.ml') diff --git a/tests/confLoader.ml b/tests/confLoader.ml new file mode 100644 index 0000000..266ff33 --- /dev/null +++ b/tests/confLoader.ml @@ -0,0 +1,128 @@ +open StdLabels + +(** Read the configuration in toml and return the internal representation *) +let load : string -> ImportConf.Syntax.t = + fun content -> + Otoml.Parser.from_string content |> ImportConf.t_of_toml |> Result.get_ok + +let conf = + load + {|version = 1 + +[source] + file = "source.xlsx" + name = "source" + +[externals.other] + intern_key = ":A" + file = "other.xlsx" + extern_key = ":C" + allow_missing = false + +[externals.last_file] + intern_key = ":other.A" + file = "last.xlsx" + extern_key = ":C" + allow_missing = true + +[sheet] + columns = [ + ":A ^ '_'", + ":B", + ":last_file.E", + ]|} + +let external_table_source = + ImportDataTypes.Table.{ file = "source.xlsx"; tab = 1; name = "source" } + +let external_table_other = + ImportDataTypes.Table.{ file = "other.xlsx"; tab = 1; name = "other" } + +let external_other = + ImportConf.Syntax. + { + intern_key = Path { alias = None; column = 1 }; + target = external_table_other; + extern_key = Path 3; + allow_missing = false; + match_rule = None; + } + +let external_table_last = + ImportDataTypes.Table.{ file = "last.xlsx"; tab = 1; name = "last_file" } + +let external_last = + ImportConf.Syntax. + { + intern_key = Path { alias = Some "other"; column = 1 }; + target = external_table_last; + extern_key = Path 3; + allow_missing = true; + match_rule = None; + } + +let show_source (source : ImportDataTypes.Table.t) = + Printf.sprintf "%s:%d" source.ImportDataTypes.Table.file + source.ImportDataTypes.Table.tab + +(* + * Compare two external sources + *) + +let show_sources sources = + let b = Buffer.create 16 in + Buffer.add_string b "["; + List.iter sources ~f:(fun source -> + Buffer.add_string b (show_source source); + Buffer.add_string b ","); + + let len = Buffer.length b in + if len > 1 then Buffer.truncate b (len - 1); + Buffer.add_string b "]"; + + Buffer.contents b + +and cmp_source : ImportDataTypes.Table.t -> ImportDataTypes.Table.t -> bool = + fun s1 s2 -> + String.equal s1.ImportDataTypes.Table.name s2.ImportDataTypes.Table.name + && String.equal s1.ImportDataTypes.Table.file s2.ImportDataTypes.Table.file + && s1.ImportDataTypes.Table.tab = s2.ImportDataTypes.Table.tab + +let cmp_list : ('a -> 'a -> bool) -> 'a list -> 'a list -> bool = + fun cmp elems1 elems2 -> List.for_all2 ~f:cmp elems1 elems2 + +(* + * Compare keys in the dependencies + *) + +let key_printer : ImportAnalyser.Dependency.key -> string = + fun { name; expression; _ } -> + let path_name = + let buffer = Buffer.create 16 in + ImportExpression.Headers.headers_of_expression buffer + (fun col buffer -> + Buffer.add_string buffer (ImportCSV.Csv.column_to_string col)) + expression; + Buffer.contents buffer + in + Printf.sprintf "%s, %s" name path_name + +and key_cmp a b = + 0 + = ImportExpression.T.cmp + (fun a b -> a - b) + a.ImportAnalyser.Dependency.expression + b.ImportAnalyser.Dependency.expression + +let keys_printer : ImportAnalyser.Dependency.key list -> string = + fun contents -> + let b = Buffer.create 16 in + List.iter contents ~f:(fun v -> Buffer.add_string b (key_printer v)); + Buffer.contents b + +(* + * Represents externals + *) + +let pp_externals : ImportConf.Syntax.extern list -> string = + fun ext -> ImportConf.Syntax.toml_of_externs ext |> Otoml.Printer.to_string -- cgit v1.2.3