postgres.spec.ts 11.2 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
/*
 * 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";
23
import { MonetAdapter, MonetConfig } from "./monet";
24
import { Adapter } from "../core/adapter";
25
26
import { Fixture as FixPostgres } from "../../test/postgres/fixture";
import { Fixture as FixMonet } from "../../test/monet/fixture";
27
28
29
import { ConfigParser } from "../util/configParser";
import { adapterScenario } from "../../test/scenario";

30
describe("Sql adapter", () => {
31
32
33
34
35

    // Initializing
    let config: any;
    let adapter: Adapter;
    let fixture;
36
    before(function (done): void {
37
        // Arrow function not used to get acces to this and skip the test
38
39
        const configPath =  process.env.BLENDB_SCHEMA_FILE;
        config = ConfigParser.parse(configPath);
40
41
        if (config.adapter === "postgres") {
            fixture = new FixPostgres(config.connection);
42
            fixture.load(config.loadViews, (err) => {
43
44
45
46
47
48
49
50
51
                if (err) {
                    throw err;
                }
                adapter = new PostgresAdapter(config.connection);
                done();
            });
        }
        else if (config.adapter === "monet") {
            fixture = new FixMonet(config.connection);
52
            fixture.load(config.loadViews, (err) => {
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
                if (err) {
                    throw err;
                }
                let parsedConfig: MonetConfig = {
                    user: config.connection.user,
                    dbname: config.connection.database,
                    password: config.connection.password,
                    host: config.connection.host,
                    port: config.connection.port
                };
                adapter = new MonetAdapter(parsedConfig);
                done();
            });
        }
        else {
            this.skip();
        }
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    });
    // 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();
        });
    });
88
89
    it("should sort data from join of two views", (done) => {
        let view = adapterScenario.sortView;
90
91
92
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
93
            expect(result).to.have.length(4);
94
95
96
97
98
99
100
101
102
            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();
103
       });
104
105
106
107
108
109
    });
    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");
110
            expect(result).to.have.length(5);
111
112
113
114
115
116
117
118
119
120
121
            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();
        });
    });
122
123
    it("should get data from join of one view", (done) => {
        let view = adapterScenario.joinWithOneView;
124
125
126
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
127
            expect(result).to.have.length(5);
128
129
130
131
132
133
134
135
136
137
138
            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();
        });
    });
139
140
141
    it("should get data from view with a single clause", (done) => {
    let view = adapterScenario.filterWithEqual;
    adapter.getDataFromView(view, (err, result) => {
142
143
144
145
146
147
148
149
150
151
152
153
154
155
            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();
        });
    });
156
157
    it("should get data from reduce with the same attributes of view", (done) => {
        let view = adapterScenario.reduceAsView;
158
159
160
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
161
            expect(result).to.have.length(5);
162
163
164
165
166
167
168
169
170
171
172
            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();
        });
    });
173
174
    it("should get data from join into reduce with a single clause", (done) => {
        let view = adapterScenario.filterAverageBought;
175
176
177
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
178
            expect(result).to.have.length(3);
179
180
181
182
183
184
185
186
187
188
189
190
            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) => {
191
        let view = adapterScenario.multipleClause;
192
193
194
195
196
197
198
199
200
201
202
203
        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);
            });
204
205
206
            done();
        });
    });
207
208
    it("should get data from view with single clause, with more than one filter", (done) => {
        let view = adapterScenario.singleClause;
209
210
211
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
212
            expect(result).to.have.length(4);
213
214
215
216
217
218
219
220
221
222
223
            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();
        });
    });
224
225
    it("should get data from view with no metrics", (done) => {
        let view = adapterScenario.joinWithNoMetrics;
226
227
228
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
229
            expect(result).to.have.length(5);
230
231
232
233
234
235
236
237
238
239
240
            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();
        });
    });
241
242
    it("should get data from join with one metric and one view", (done) => {
        let view = adapterScenario.growOneView;
243
244
245
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
246
            expect(result).to.have.length(5);
247
248
249
250
251
252
253
254
255
256
257
            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();
        });
    });
258
259
    it("should get data from join with unrelated dimension", (done) => {
        let view = adapterScenario.JoinWithAncestors;
260
261
262
        adapter.getDataFromView(view, (err, result) => {
            expect(err).to.be.a("null");
            expect(result).to.be.an("array");
263
            expect(result).to.have.length(5);
264
265
266
267
268
269
270
271
272
273
274
            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();
        });
    });
275
});