aboutsummaryrefslogtreecommitdiff
path: root/tests/analyser_query_test.ml
blob: fd8914bd2c353a76c76d23c9cfc8590dc2bc939e (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
open StdLabels
module A = ImportAnalyser.Dependency
module Q = ImportAnalyser.Query
module Syntax = ImporterSyntax
module Expr = Expression_builder
open Test_migration

(** This is sample configuration used in the tests *)
let conf =
  Syntax.
    {
      ConfLoader.conf with
      columns =
        [
          Concat [ Path { alias = None; column = 1 }; Literal "_"; Empty ];
          Path { alias = None; column = 2 };
          Path { alias = Some "last_file"; column = 5 };
        ];
    }

let create_table =
  "Create table" >:: fun _ ->
  let out = A.get_process_order conf in

  let query = Q.create_table (List.hd out) in

  Alcotest.check Alcotest.string ""
    "CREATE TABLE 'last' (id INTEGER PRIMARY KEY,'key_last_file','col_5')" query

let select =
  "Select" >:: fun _ ->
  let query, _ = Q.select conf in
  let expected_query =
    {|SELECT COALESCE('source'.'col_1','') || ? || '' AS result_0,
'source'.'col_2' AS result_1,
'last_file'.'col_5' AS result_2
FROM 'source' AS 'source'
LEFT JOIN 'other' AS 'other' ON rtrim(upper('source'.'col_1')) = 'other'.'key_other'
LEFT JOIN 'last' AS 'last_file' ON rtrim(upper('other'.'col_1')) = 'last_file'.'key_last_file'|}
  in

  Alcotest.check Alcotest.string "" expected_query query.q

let check_externals =
  "Check external" >:: fun _ ->
  let query = Q.check_external conf (List.hd conf.externals) in

  let expected_query =
    "SELECT 'source'.'id', 'source'.'col_1'\n\
     FROM 'source' AS 'source'\n\
     LEFT JOIN 'other' AS 'other' ON rtrim(upper('source'.'col_1')) = \
     'other'.'key_other' WHERE 'other'.'key_other' IS NULL AND \
     'source'.'col_1' IS NOT NULL AND 'source'.'col_1' <> ''"
  in

  Alcotest.check Alcotest.string "" expected_query query.q

let previous =
  "Test window previous" >:: fun _ ->
  (* This is sample configuration used in the tests *)
  let conf =
    Syntax.
      {
        version = 1;
        locale = None;
        source = { file = "source.xlsx"; tab = 1; name = "previous" };
        externals = [];
        columns =
          [
            Window
              ( Previous (Path { alias = None; column = 5 }),
                [ Path { alias = None; column = 1 } ],
                [ Path { alias = None; column = 3 } ] );
          ];
        filters = [];
        sort = [];
        uniq = [];
      }
  in

  let query, _ = ImportAnalyser.Query.select conf in
  let expected_query =
    "SELECT LAG('previous'.'col_5') OVER (PARTITION BY 'previous'.'col_1' \
     ORDER BY 'previous'.'col_3') AS result_0\n\
     FROM 'source' AS 'previous'"
  in
  Alcotest.check Alcotest.string "" expected_query query.q

let sum =
  "Test window sum" >:: fun _ ->
  (* This is sample configuration used in the tests *)
  let conf =
    Syntax.
      {
        version = 1;
        locale = None;
        source = { file = "source.xlsx"; tab = 1; name = "previous" };
        externals = [];
        columns =
          [
            Window
              ( Sum (Path { alias = None; column = 5 }),
                [ Path { alias = None; column = 1 } ],
                [] );
          ];
        filters = [];
        sort = [];
        uniq = [];
      }
  in

  let query, _ = ImportAnalyser.Query.select conf in
  let expected_query =
    "SELECT SUM('previous'.'col_5') OVER (PARTITION BY 'previous'.'col_1') AS \
     result_0\n\
     FROM 'source' AS 'previous'"
  in
  Alcotest.check Alcotest.string "" expected_query query.q

let sum_total =
  "Test sum over the whole range" >:: fun _ ->
  (* This is sample configuration used in the tests *)
  let conf =
    Syntax.
      {
        version = 1;
        locale = None;
        source = { file = "source.xlsx"; tab = 1; name = "previous" };
        externals = [];
        columns = [ Window (Sum (Path { alias = None; column = 5 }), [], []) ];
        filters = [];
        sort = [];
        uniq = [];
      }
  in

  let query, _ = ImportAnalyser.Query.select conf in
  let expected_query =
    "SELECT SUM('previous'.'col_5') AS result_0\nFROM 'source' AS 'previous'"
  in
  Alcotest.check Alcotest.string "" expected_query query.q

let sum_unfiltered =
  "Test sum over the whole range" >:: fun _ ->
  (* This is sample configuration used in the tests *)
  let conf =
    Syntax.
      {
        version = 1;
        locale = None;
        source = { file = "source.xlsx"; tab = 1; name = "previous" };
        externals = [];
        columns =
          [
            Window
              ( Sum (Path { alias = None; column = 5 }),
                [],
                [ Path { alias = None; column = 1 } ] );
          ];
        filters = [];
        sort = [];
        uniq = [];
      }
  in

  let query, _ = ImportAnalyser.Query.select conf in
  let expected_query =
    "SELECT SUM('previous'.'col_5') OVER (ORDER BY 'previous'.'col_1') AS \
     result_0\n\
     FROM 'source' AS 'previous'"
  in
  Alcotest.check Alcotest.string "" expected_query query.q

let prepare_insert =
  "Test prepare_insert" >:: fun _ ->
  let key =
    ImportAnalyser.Dependency.
      {
        name = "key_test";
        expression = Concat [ Path 1; Literal "_"; Empty ];
        columns = lazy (ImportContainers.IntSet.singleton 1);
      }
  in

  let buffer = Buffer.create 16 in
  let () = ImportAnalyser.Query.build_key_insert buffer key in
  let contents = Buffer.contents buffer in

  let expected = "rtrim(upper(COALESCE(:col_1,'') || '_' || ''))" in

  Alcotest.check Alcotest.string "" expected contents

(** Test a request with a group in a filter.

    This generate a CTE expression in order to evaluate the group before loading
    the results from the query. *)
let filter_group =
  "Test filter_group" >:: fun _ ->
  let c col = Expr.path ImportDataTypes.Path.{ alias = None; column = col } in
  let conf =
    {
      conf with
      columns = [ c 1 ];
      filters = [ Expr.(max (c 3) [ c 1 ] [ c 1 ]) ];
    }
  in
  let contents, _ = ImportAnalyser.Query.select conf in

  let expected =
    {|WITH filter0 AS (SELECT source.id, LAST_VALUE('source'.'col_3') OVER (PARTITION BY 'source'.'col_1' ORDER BY 'source'.'col_1' RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS group_function
FROM 'source' AS 'source'
LEFT JOIN 'other' AS 'other' ON rtrim(upper('source'.'col_1')) = 'other'.'key_other'
LEFT JOIN 'last' AS 'last_file' ON rtrim(upper('other'.'col_1')) = 'last_file'.'key_last_file')
SELECT 'source'.'col_1' AS result_0
FROM 'source' AS 'source'
LEFT JOIN 'other' AS 'other' ON rtrim(upper('source'.'col_1')) = 'other'.'key_other'
LEFT JOIN 'last' AS 'last_file' ON rtrim(upper('other'.'col_1')) = 'last_file'.'key_last_file'
INNER JOIN 'filter0' ON filter0.id = source.id
WHERE filter0.group_function|}
  in

  Alcotest.check Alcotest.string "" expected contents.q

(** Test a request with a group in a filter.

    This generate a CTE expression in order to evaluate the group before loading
    the results from the query. *)
let filter_group2 =
  "Test filter_group" >:: fun _ ->
  let c col = Expr.path ImportDataTypes.Path.{ alias = None; column = col } in
  let conf =
    {
      conf with
      columns = [ c 1 ];
      filters =
        [ Expr.(max (c 3) [ c 1 ] [ c 1 ]); Expr.equal (c 3) Expr.integer_zero ];
    }
  in
  let contents, _ = ImportAnalyser.Query.select conf in

  let expected =
    {|WITH filter0 AS (SELECT source.id, LAST_VALUE('source'.'col_3') OVER (PARTITION BY 'source'.'col_1' ORDER BY 'source'.'col_1' RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS group_function
FROM 'source' AS 'source'
LEFT JOIN 'other' AS 'other' ON rtrim(upper('source'.'col_1')) = 'other'.'key_other'
LEFT JOIN 'last' AS 'last_file' ON rtrim(upper('other'.'col_1')) = 'last_file'.'key_last_file')
SELECT 'source'.'col_1' AS result_0
FROM 'source' AS 'source'
LEFT JOIN 'other' AS 'other' ON rtrim(upper('source'.'col_1')) = 'other'.'key_other'
LEFT JOIN 'last' AS 'last_file' ON rtrim(upper('other'.'col_1')) = 'last_file'.'key_last_file'
INNER JOIN 'filter0' ON filter0.id = source.id
WHERE COALESCE('source'.'col_3',0)=0 AND filter0.group_function|}
  in

  Alcotest.check Alcotest.string "" expected contents.q

let test_suit =
  [
    create_table;
    select;
    check_externals;
    previous;
    sum;
    sum_total;
    sum_unfiltered;
    prepare_insert;
    filter_group;
    filter_group2;
  ]

let tests = "analyser_query_test" >::: test_suit