postgres.spec.ts 12.6 KB
Newer Older
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
/*
 * Copyright (C) 2017 Centro de Computacao Cientifica e Software Livre
 * Departamento de Informatica - Universidade Federal do Parana
 *
 * This file is part of blendb.
 *
 * blendb is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * blendb is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with blendb.  If not, see <http://www.gnu.org/licenses/>.
 */

import { expect } from "chai";

import { PostgresAdapter } from "./postgres";
import { Adapter } from "../core/adapter";
import { Fixture } from "../../test/postgres/fixture";

import { ConfigParser } from "../util/configParser";
import { adapterScenario } from "../../test/scenario";

describe("postgres adapter", () => {

    // Initializing
    let config: any;
    let adapter: Adapter;
    let fixture;
    before((done) => {
        config = ConfigParser.parse("config/test.yaml");
        fixture = new Fixture(config.connection);
        fixture.load(config.loadViews, config.struct.create, (err) => {
            if (err) {
                throw err;
            }
            adapter = new PostgresAdapter(config.connection);
            done();
        });
    });
    // Tests
    it("should get data from single materialized view", (done) => {
        let view = adapterScenario.materializedView;
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
            expect(result).to.have.length(5);
            let keys: string[] = [];
            keys = keys.concat(view.metrics.map((item) => item.name));
            keys = keys.concat(view.dimensions.map((item) => item.name));
            result.forEach((row) => {
                expect(row).to.be.an("object");
                expect(row).to.have.all.keys(keys);
            });
            done();
        });
    });
    it("should get data from join of 2 views (without selection)", (done) => {
        let view = adapterScenario.noSelectionView;
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
            expect(result).to.have.length(1);
            expect(result[0]).to.be.an("object");
            let keys: string[] = [];
            keys = keys.concat(view.metrics.map((item) => item.name));
            keys = keys.concat(view.dimensions.map((item) => item.name));
            result.forEach((row) => {
                expect(row).to.be.an("object");
                expect(row).to.have.all.keys(keys);
            });
            done();
        });
    });
    it("should get data from join of 2 views (with selection)", (done) => {
        let view = adapterScenario.withSelectionView;
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
            expect(result).to.have.length(5);
            expect(result[0]).to.be.an("object");
            let keys: string[] = [];
            keys = keys.concat(view.metrics.map((item) => item.name));
            keys = keys.concat(view.dimensions.map((item) => item.name));
            result.forEach((row) => {
                expect(row).to.be.an("object");
                expect(row).to.have.all.keys(keys);
            });
            done();
        });
    });
    it("should get data from single view (with sub-dimension)", (done) => {
        let view = adapterScenario.subDimensionView;
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
103
            expect(result).to.have.length(5);
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
            expect(result[0]).to.be.an("object");
            let keys: string[] = [];
            keys = keys.concat(view.metrics.map((item) => item.name));
            keys = keys.concat(view.dimensions.map((item) => item.name));
            result.forEach((row) => {
                expect(row).to.be.an("object");
                expect(row).to.have.all.keys(keys);
            });
            done();
        });
    });
    it("should get data from join of 4 views (with selection)", (done) => {
        let view = adapterScenario.join4View;
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
120
            expect(result).to.have.length(5);
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
            expect(result[0]).to.be.an("object");
            let keys: string[] = [];
            keys = keys.concat(view.metrics.map((item) => item.name));
            keys = keys.concat(view.dimensions.map((item) => item.name));
            result.forEach((row) => {
                expect(row).to.be.an("object");
                expect(row).to.have.all.keys(keys);
            });
            done();
        });
    });

    it("should get data from different sub dimensions with same parent", (done) => {
        let view = adapterScenario.dateView;
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
            expect(result).to.have.length(5);
            expect(result[0]).to.be.an("object");
            let keys: string[] = [];
            keys = keys.concat(view.metrics.map((item) => item.name));
            keys = keys.concat(view.dimensions.map((item) => item.name));
            result.forEach((row) => {
                expect(row).to.be.an("object");
                expect(row).to.have.all.keys(keys);
146
                expect(row).to.have.property("dim:0:year", 2017);
147
148
149
150
            });
            done();
        });
    });
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    it("should get data from view with all types of agreggation", (done) => {
        let view = adapterScenario.aggrView;
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
            expect(result).to.have.length(1);
            expect(result[0]).to.be.an("object");
            let keys: string[] = [];
            keys = keys.concat(view.metrics.map((item) => item.name));
            keys = keys.concat(view.dimensions.map((item) => item.name));
            result.forEach((row) => {
                expect(row).to.be.an("object");
                expect(row).to.have.all.keys(keys);
            });

            expect(parseInt(result[0]["met:0"], 10)).to.be.equal(15);
            expect(parseInt(result[0]["met:1"], 10)).to.be.equal(3);
            expect(parseInt(result[0]["met:6"], 10)).to.be.equal(5);
            done();
        });
    });
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

    it("should get data from view when a single clause exists", (done) => {
        let view = adapterScenario.clauseView;
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
            expect(result).to.have.length(1);
            expect(result[0]).to.be.an("object");
            let keys: string[] = [];
            keys = keys.concat(view.metrics.map((item) => item.name));
            keys = keys.concat(view.dimensions.map((item) => item.name));
            result.forEach((row) => {
                expect(row).to.be.an("object");
                expect(row).to.have.all.keys(keys);
            });

            expect(parseInt(result[0]["met:0"], 10)).to.be.equal(1);
            expect(parseInt(result[0]["met:1"], 10)).to.be.equal(1);
            expect(parseInt(result[0]["met:2"], 10)).to.be.equal(1);
            expect(result[0]["dim:0"].getDate()).to.be.equal(1);
            done();
        });
    });

    it("should get data from view with single clause,  with more than on filter", (done) => {
        let view = adapterScenario.multiFilterView;
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
            expect(result).to.have.length(2);
            expect(result[0]).to.be.an("object");
            let keys: string[] = [];
            keys = keys.concat(view.metrics.map((item) => item.name));
            keys = keys.concat(view.dimensions.map((item) => item.name));
            result.forEach((row) => {
                expect(row).to.be.an("object");
                expect(row).to.have.all.keys(keys);
            });

            done();
        });
    });

    it("should get data from view with multiple clauses", (done) => {
        let view = adapterScenario.multiClauseView;
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
            expect(result).to.have.length(0);

            done();
        });
    });

    it("should get data from view with a clause with not equal operator", (done) => {
        let view = adapterScenario.notEqualView;
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
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
            expect(result).to.have.length(2);
            expect(result[0]).to.be.an("object");
            let keys: string[] = [];
            keys = keys.concat(view.metrics.map((item) => item.name));
            keys = keys.concat(view.dimensions.map((item) => item.name));
            result.forEach((row) => {
                expect(row).to.be.an("object");
                expect(row).to.have.all.keys(keys);
            });

            done();
        });
    });

    it("should get data from view, using > AND < operators on filters", (done) => {
        let view = adapterScenario.gtltView;
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
            expect(result).to.have.length(1);
            expect(result[0]).to.be.an("object");
            let keys: string[] = [];
            keys = keys.concat(view.metrics.map((item) => item.name));
            keys = keys.concat(view.dimensions.map((item) => item.name));
            result.forEach((row) => {
                expect(row).to.be.an("object");
                expect(row).to.have.all.keys(keys);
            });

            done();
        });
    });

    it("should get data from view, using >= AND <= operators on filters", (done) => {
        let view = adapterScenario.geleView;
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
            expect(result).to.have.length(3);
270
271
272
273
274
275
276
277
278
279
280
281
            expect(result[0]).to.be.an("object");
            let keys: string[] = [];
            keys = keys.concat(view.metrics.map((item) => item.name));
            keys = keys.concat(view.dimensions.map((item) => item.name));
            result.forEach((row) => {
                expect(row).to.be.an("object");
                expect(row).to.have.all.keys(keys);
            });

            done();
        });
    });
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300

    it("should get data when a filter does not have any effect on the query", (done) => {
        let view = adapterScenario.notMatchFilterView;
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
            expect(result).to.have.length(5);
            expect(result[0]).to.be.an("object");
            let keys: string[] = [];
            keys = keys.concat(view.metrics.map((item) => item.name));
            keys = keys.concat(view.dimensions.map((item) => item.name));
            result.forEach((row) => {
                expect(row).to.be.an("object");
                expect(row).to.have.all.keys(keys);
            });

            done();
        });
    });
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

    it("should get data using COUNT when the view is not origin", (done) => {
        let view = adapterScenario.notOriginCount;
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
            expect(result).to.have.length(5);
            expect(result[0]).to.be.an("object");
            let keys: string[] = [];
            keys = keys.concat(view.metrics.map((item) => item.name));
            keys = keys.concat(view.dimensions.map((item) => item.name));
            result.forEach((row) => {
                expect(row).to.be.an("object");
                expect(row).to.have.all.keys(keys);
            });

            done();
        });
    });
320
});