aboutsummaryrefslogtreecommitdiff
path: root/tests/configuration_toml.ml
blob: 620a106c3c817b1eefa3da2a057016f6af94b0ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
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 external_filters () =
  let configuration =
    ConfLoader.load'
      {|[source]
name = ""
file = ""
tab = 0

[externals.other]
  intern_key = ":A"
  file = "other.xlsx"
  extern_key = ":C"
  filters = [":B = 1"]


[sheet]
columns = []|}
  and expected =
    Ok
      {
        ImporterSyntax.dummy_conf with
        externals =
          ConfLoader.
            [
              {
                external_other with
                filters = [ Expression_builder.(equal (path 2) integer_one) ];
              };
            ];
      }
  in
  Alcotest.(check (result Test_migration.syntax string))
    "Filters in external" 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 };
                      ] );
                filters = [];
                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);
    ("External with filter", `Quick, external_filters);
  ]

let tests = "configuration_toml" >::: test_suit