module Expression = ImportExpression.T module Path = ImportDataTypes.Path open Test_migration let nested_group () = let expected = Error {|in field "sheet": in field "columns": while decoding a list: element 0: A group function cannot contains another group function, but got "max(:A, [counter([:A], [:A])], [])"|} and result = ConfLoader.load' {|[source] name = "source_name" file = "source_file" [sheet] columns = [ "max(:A, [counter([:A], [:A])], [])", ]|} in Alcotest.(check (result Test_migration.syntax Test_migration.trimed_string)) "duplicate" expected result (** Load a simple configuration *) let load_configuration () = let configuration = ConfLoader.load' {|[source] name = "" file = "" tab = 0 [sheet] columns = []|} and expected = Ok ImporterSyntax.dummy_conf in Alcotest.(check (result Test_migration.syntax string)) "Simple configuration" expected configuration let externals () = let configuration = ConfLoader.load' {|[source] name = "" file = "" tab = 0 [externals.other] intern_key = ":A" file = "other.xlsx" extern_key = ":C" allow_missing = false [sheet] columns = []|} and expected = Ok { ImporterSyntax.dummy_conf with externals = [ ConfLoader.external_other ]; } in Alcotest.(check (result Test_migration.syntax Test_migration.trimed_string)) "Simple external" expected configuration (** There is an error in this configuration the key [intern_key] is missing in the external *) let external_with_missing_key () = let configuration = ConfLoader.load' {|[source] name = "" file = "" [externals.other] file = "" extern_key = "" [sheet] columns = []|} and expected = Error {|in field "externals": Failed while decoding key-value pairs: Expected an object with an attribute "intern_key", but got file = "" extern_key = ""|} in Alcotest.(check (result Test_migration.syntax Test_migration.trimed_string)) "Missing key" expected configuration let sub_external () = let configuration = ConfLoader.load' {|[source] name = "" file = "" tab = 0 [externals.other-1] intern_key = ":A" file = "other.xlsx" extern_key = ":C" allow_missing = false [sheet] columns = []|} and expected = Ok { ImporterSyntax.dummy_conf with externals = ConfLoader. [ { external_other with target = { external_table_other with name = "other-1" }; }; ]; } in Alcotest.(check (result Test_migration.syntax string)) "external with path" expected configuration let sub_external_with_missing_key () = let configuration = ConfLoader.load' {|[source] name = "" file = "" [externals.other-1] file = "" extern_key = "" [sheet] columns = []|} and expected = Error {|in field "externals": Failed while decoding key-value pairs: Expected an object with an attribute "intern_key", but got file = "" extern_key = ""|} in Alcotest.(check (result Test_migration.syntax Test_migration.trimed_string)) "Missing intern_key" expected configuration (** The same configuration has external, and sub-element external *) let sub_external_mixed () = let configuration = ConfLoader.load' {|[source] name = "" file = "" tab = 0 [externals.other] intern_key = ":A" file = "other.xlsx" extern_key = ":C" allow_missing = false [externals.other-1] intern_key = ":A" file = "other.xlsx" extern_key = ":C" allow_missing = false [sheet] columns = []|} and expected = Ok { ImporterSyntax.dummy_conf with externals = ConfLoader. [ external_other; { external_other with target = { external_table_other with name = "other-1" }; }; ]; } in Alcotest.(check (result Test_migration.syntax string)) "external with path" expected configuration let missing_dataset () = let configuration = ConfLoader.load' {|[source] name = "" tab = 0 [sheet] columns = []|} and expected = Error {|in field "source": I tried the following decoders but they all failed: "file" decoder: Expected an object with an attribute "file", but got name = "" tab = 0 "dataset" decoder: No dataset declared, but got name = "" tab = 0|} in Alcotest.(check (result Test_migration.syntax Test_migration.trimed_string)) "No dataset provided" expected configuration let empty_dataset () = let configuration = ConfLoader.load' ~dataset:(fun _ -> Otoml.TomlArray []) {| dataset = "…" [source] name = "" [sheet] columns = []|} and expected = Error {|in field "dataset": Expected an object with an attribute "files", but got []|} in Alcotest.(check (result Test_migration.syntax Test_migration.trimed_string)) "Invalid Dataset" expected configuration let dataset_with_invalid_key () = let configuration = ConfLoader.load' ~dataset:(fun _ -> Otoml.( TomlTable [ ("files", TomlTable [ ("other-1", TomlString "other.xlsx") ]) ])) {| dataset = "…" [source] name = "" [sheet] columns = []|} and expected = Error {|in field "dataset": in field "files": Failed while decoding key-value pairs: Expected a key without '-', but got "other-1"|} in Alcotest.(check (result Test_migration.syntax Test_migration.trimed_string)) "Invalid Dataset: invalid key" expected configuration let external_dataset () = let configuration = ConfLoader.load' ~dataset:(fun _ -> Otoml.( TomlTable [ ("files", TomlTable [ ("other", TomlString "other.xlsx") ]) ])) {| dataset = "…" [source] name = "" file = "" tab = 0 [externals.other-1] # The file is not defined here # And in the dataset, there is no "other-1", just "other": the application # should be able to infer the information from "other" and apply it here. intern_key = ":A" extern_key = ":C" allow_missing = false [sheet] columns = []|} and expected = Ok { ImporterSyntax.dummy_conf with externals = ConfLoader. [ { external_other with target = { external_table_other with name = "other-1" }; }; ]; } in Alcotest.(check (result Test_migration.syntax string)) "Dataset with alias" expected configuration let test_suit = [ ( "parse_extern" >:: fun _ -> let toml = ConfLoader.load_from_file "configuration/simple.toml" in match toml with | Error s -> raise (Failure s) | Ok result -> let expected = ImporterSyntax.Extern. { target = { file = ""; tab = 1; name = "target" }; extern_key = Literal "_B"; intern_key = Function ( "concat", [ Path { alias = None; column = 1 }; Path { alias = None; column = 2 }; ] ); match_rule = None; allow_missing = true; } in ignore @@ Alcotest.check (Alcotest.list Test_migration.extern_testable) "" [ expected ] result.externals ); ( "parse_columns" >:: fun _ -> let toml = ConfLoader.load_from_file "configuration/simple.toml" in match toml with | Error s -> raise (Failure s) | Ok result -> let open Path in let open Expression in let expected = [ Function ( "if", [ Path { alias = Some "target"; column = 1 }; Path { alias = None; column = 2 }; Literal "free' text"; ] ); Window ( Counter, [ Path { alias = Some "target"; column = 1 } ], [ Path { alias = Some "target"; column = 1 } ] ); ] in Alcotest.check (Alcotest.list Test_migration.expression_testable) "" expected result.columns ); ( "parse_csv" >:: fun _ -> let toml = ConfLoader.load_from_file "configuration/example_csv.toml" in ignore toml ); ("nested group", `Quick, nested_group); ("Basic configuration", `Quick, load_configuration); ("Configuration with external", `Quick, externals); ("Faulty configuration", `Quick, external_with_missing_key); ("Sub external", `Quick, sub_external); ("Faulty configuration", `Quick, sub_external_with_missing_key); ("Mix in external and sub external", `Quick, sub_external_mixed); ("Missing dataset", `Quick, missing_dataset); ("Empty dataset", `Quick, empty_dataset); ("Dataset with invalid key", `Quick, dataset_with_invalid_key); ("External dataset", `Quick, external_dataset); ] let tests = "configuration_toml" >::: test_suit