aboutsummaryrefslogtreecommitdiff
path: root/tests/sql_db.ml
blob: 75b829361789211da01917118405cded5a7d7328 (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
(** Test the behavior of the sqlite with a in-memory database *)

open StdLabels
open Test_migration

let result = Alcotest.(result Test_migration.csv_result reject)
let check = Alcotest.check result

let ( let* ) res cont =
  match res with
  | Ok value -> cont value
  | Error e -> Error e

(** Test a process with a simple configuration in-memory. Only one table is
    handlded *)
let run_test ~configuration ~input ~expected name =
  name >:: fun () ->
  (* We expect a valid configuration *)
  let conf = ConfLoader.load configuration in

  let exec db =
    let table = List.hd @@ ImportAnalyser.Dependency.get_process_order conf in
    let* () = ImportSQL.Db.create_table db table in

    (* Prepare the statement in order to import data *)
    let* stmt = ImportSQL.Db.prepare_insert db table in

    (* Inject some data into the table *)
    let result, _ =
      List.fold_left ~init:(Ok (), 0) input ~f:(fun (_, i) data ->
          let result =
            let* () = ImportSQL.Db.insert ~id:i db stmt data in
            let* () = ImportSQL.Db.reset stmt in
            Ok ()
          in
          (result, i + 1))
    in
    let* () = result in
    let* () = ImportSQL.Db.finalize stmt in

    (* Collect the data *)
    let data = ref [] in
    let* () =
      ImportSQL.Db.query db conf ~f:(fun rows ->
          let values = Array.map ~f:snd rows in
          data := values :: !data)
    in

    Ok (List.rev !data)
  in

  (* Use a magic keyword for in-memory database *)
  let result = ImportSQL.Db.with_db ":memory:" exec in
  check name expected result

(** Simple test used to check the process *)
let simple_extraction =
  run_test "simple_extraction"
    ~configuration:
      {|[source]
name = "source_name"
file = "source_file"

[sheet]
columns = [
    ":A ^ '_'", 
    ":B",
    ":E"]|}
    ~input:
      ImportCSV.DataType.
        [ [ (0, Integer 123); (1, Integer 2); (4, Integer 5) ] ]
    ~expected:
      (Ok ImportCSV.DataType.[ [| Content "123_"; Integer 2; Integer 5 |] ])

(** Ensure the behavior of the sum function when a filter is given. It is
    expected to accumulate the values over each line *)
let sum_sort =
  run_test "sum_sort"
    ~configuration:
      {|[source]
name = "source_name"
file = "source_file"

[sheet]
columns = [
    ":A", 
    "sum(:C, [:B], [:A])",
]|}
    ~input:
      ImportCSV.DataType.
        [
          [ (0, Integer 1); (1, Content "A"); (2, Integer 100) ];
          [ (0, Integer 2); (1, Content "A"); (2, Integer 100) ];
          [ (0, Integer 3); (1, Content "A"); (2, Integer 100) ];
        ]
    ~expected:
      (Ok
         ImportCSV.DataType.
           [
             [| Integer 1; Integer 100 |];
             [| Integer 2; Integer 200 |];
             [| Integer 3; Integer 300 |];
           ])

let sum_total =
  run_test "sum_total"
    ~configuration:
      {|[source]
name = "source_name"
file = "source_file"

[sheet]
columns = [
    ":A", 
    "sum(:C, [], [])",
]|}
    ~input:
      ImportCSV.DataType.
        [
          [ (0, Integer 1); (2, Integer 100) ];
          [ (0, Integer 2); (2, Integer 100) ];
        ]
    ~expected:
      (Ok [ [| ImportCSV.DataType.Integer 1; ImportCSV.DataType.Integer 200 |] ])

(** Ensure the behavior of the sum function when no filter is given. It is
    expected to get the total sum for each line *)
let sum_unfiltered =
  run_test "sum_unfiltered"
    ~configuration:
      {|[source]
name = "source_name"
file = "source_file"

[sheet]
columns = [
    ":A", 
    "sum(:C, [], [:A])",
]|}
    ~input:
      ImportCSV.DataType.
        [
          [ (0, Integer 1); (2, Integer 100) ];
          [ (0, Integer 2); (2, Integer 100) ];
        ]
    ~expected:
      (Ok [ [| ImportCSV.DataType.Integer 1; ImportCSV.DataType.Integer 200 |] ])

let sum_group =
  run_test "sum_group"
    ~configuration:
      {|[source]
name = "source_name"
file = "source_file"

[sheet]
columns = [
    ":A", 
    "sum(:C, [:A], [])",
]|}
    ~input:
      ImportCSV.DataType.
        [
          [ (0, Integer 1); (2, Integer 100) ];
          [ (0, Integer 1); (2, Integer 100) ];
          [ (0, Integer 2); (2, Integer 100) ];
        ]
    ~expected:
      (Ok
         ImportCSV.DataType.
           [
             [| Integer 1; Integer 200 |];
             [| Integer 1; Integer 200 |];
             [| Integer 2; Integer 100 |];
           ])

(** Adding a uniq filter on column A does not change the sum of the values *)
let sum_group_uniq =
  run_test "sum_group_uniq"
    ~configuration:
      {|[source]
name = "source_name"
file = "source_file"

[sheet]
uniq = [":A"]
columns = [
    ":A", 
    "sum(:C, [:A], [])",
]|}
    ~input:
      ImportCSV.DataType.
        [
          [ (0, Integer 1); (2, Integer 100) ];
          [ (0, Integer 1); (2, Integer 100) ];
          [ (0, Integer 2); (2, Integer 100) ];
        ]
    ~expected:
      (Ok
         ImportCSV.DataType.
           [ [| Integer 1; Integer 200 |]; [| Integer 2; Integer 100 |] ])

let test_suit =
  [
    simple_extraction;
    sum_sort;
    sum_total;
    sum_unfiltered;
    sum_group;
    sum_group_uniq;
  ]

let tests = "sql_db" >::: test_suit