engine.spec.ts 14 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
/*
 * Copyright (C) 2015 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 { Engine } from "./engine";
24
import { Metric } from "./metric";
25 26
import { Filter, FilterOperator } from "./filter";
import { Clause } from "./clause";
27
import { View } from "./view";
28
import { engineScenario } from "../../test/scenario";
29 30

describe("engine class", () => {
31
    const engine = new Engine();
32

33 34 35
    const met = engineScenario.metrics;
    const dim = engineScenario.dimensions;
    const subdim = engineScenario.subDimensions;
36
    const views = engineScenario.views;
37

38 39 40
    met.forEach((item) => engine.addMetric(item));
    dim.forEach((item) => engine.addDimension(item));
    subdim.forEach((item) => engine.addDimension(item));
41

42 43
    views.forEach((view) => engine.addView(view));

44
    it("should be create a fill that cover all metrics and dimensions", () => {
45
        let query = {
46 47
            metrics : met.slice(0)
            , dimensions : dim.slice(0)
48
        };
49 50 51 52 53 54 55 56
        let optimalView = engine.query(query);
        expect(optimalView).to.be.an("object");
        expect(optimalView).to.have.property("metrics");
        expect(optimalView).to.have.property("dimensions");
        expect(optimalView).to.have.property("childViews");
        expect(optimalView.metrics).to.be.an("array");
        expect(optimalView.dimensions).to.be.an("array");
        expect(optimalView.childViews).to.be.an("array");
57
        expect(optimalView.metrics).to.have.length(12);
58
        expect(optimalView.dimensions).to.have.length(13);
59 60 61 62
    });
    it("should throw an exception, query with non-existent metric", () => {
        let error: boolean = false;
        try {
63
            engine.query({metrics: [engineScenario.wrongMet], dimensions: [dim[0]]});
64 65 66 67 68 69 70 71 72 73 74 75
        }
        catch (e){
            error = true;
            expect(e.message).to.be.equal("Engine views cannot cover the query");

        }
        expect(error);
    });

    it("should throw an exception, query with non-existent dimension", () => {
        let error: boolean = false;
        try {
76
            engine.query({metrics: [met[0]], dimensions: [engineScenario.wrongDim]});
77 78 79 80 81 82 83 84
        }
        catch (e){
            error = true;
            expect(e.message).to.be.equal("Engine views cannot cover the query");

        }
        expect(error);
    });
85 86 87

    it("should be create a fill that cover the query, that match perfectly with a existent view", () => {
        let query = {
88
            metrics : [met[0], met[1], met[2], met[10]]
89
            , dimensions : [dim[0], dim[7]]
90 91 92 93 94 95 96 97 98
        };
        let optimalView = engine.query(query);
        expect(optimalView).to.be.an("object");
        expect(optimalView).to.have.property("metrics");
        expect(optimalView).to.have.property("dimensions");
        expect(optimalView).to.have.property("childViews");
        expect(optimalView.metrics).to.be.an("array");
        expect(optimalView.dimensions).to.be.an("array");
        expect(optimalView.childViews).to.be.an("array");
99
        expect(optimalView.metrics).to.have.length(4);
100 101
        expect(optimalView.dimensions).to.have.length(2);
        expect(optimalView.childViews).to.have.length(0);
102

103
        expect(optimalView.id).to.be.equal(views[0].id);
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
    it("should be create a fill that cover the query, that match perfectly with a existent view, with clauses", () => {
        const clause = new Clause({
            filters: [new Filter({
                target: dim[2],
                operator: FilterOperator.NOTEQUAL,
                value: "1"
            })]
        });
        let query = {
            metrics : [met[5], met[6], met[7]]
            , dimensions : [dim[2]]
            , clauses: [clause]
        };
        let optimalView = engine.query(query);
        expect(optimalView).to.be.an("object");
        expect(optimalView).to.have.property("metrics");
        expect(optimalView).to.have.property("dimensions");
        expect(optimalView).to.have.property("childViews");
        expect(optimalView).to.have.property("materialized");
        expect(optimalView.metrics).to.be.an("array");
        expect(optimalView.dimensions).to.be.an("array");
        expect(optimalView.childViews).to.be.an("array");
        expect(optimalView.metrics).to.have.length(3);
        expect(optimalView.dimensions).to.have.length(1);
        expect(optimalView.childViews).to.have.length(0);
        expect(optimalView.materialized).to.be.true;

        expect(optimalView.id).to.be.equal(views[9].id);
    });

136 137 138 139
    it("should be create a fill that cover the query, using sub dimensions", () => {
        let emptyMetrics: Metric[] = [];
        let query = {
            metrics : emptyMetrics
140
            , dimensions : subdim.slice(0, 2)
141 142 143 144 145 146 147 148 149
        };
        let optimalView = engine.query(query);
        expect(optimalView).to.be.an("object");
        expect(optimalView).to.have.property("metrics");
        expect(optimalView).to.have.property("dimensions");
        expect(optimalView).to.have.property("childViews");
        expect(optimalView.metrics).to.be.an("array");
        expect(optimalView.dimensions).to.be.an("array");
        expect(optimalView.childViews).to.be.an("array");
150 151
        expect(optimalView.metrics).to.have.length(0);
        expect(optimalView.dimensions).to.have.length(2);
152
        expect(optimalView.childViews).to.have.length(3);
153

154
        expect(optimalView).satisfy((optView: View) => {
155
            return optView.dimensions.some((item) => item.name === subdim[0].name);
156 157
        });
        expect(optimalView).satisfy((optView: View) => {
158
            return optView.dimensions.some((item) => item.name === subdim[1].name);
159
        });
160 161 162 163 164 165
    });

    it("should be create a fill that cover the query, using the parents of sub dimensions", () => {
        let emptyMetrics: Metric[] = [];
        let query = {
            metrics : emptyMetrics
166
            , dimensions : [subdim[2], subdim[4]]
167 168 169 170 171 172 173 174 175
        };
        let optimalView = engine.query(query);
        expect(optimalView).to.be.an("object");
        expect(optimalView).to.have.property("metrics");
        expect(optimalView).to.have.property("dimensions");
        expect(optimalView).to.have.property("childViews");
        expect(optimalView.metrics).to.be.an("array");
        expect(optimalView.dimensions).to.be.an("array");
        expect(optimalView.childViews).to.be.an("array");
176 177
        expect(optimalView.metrics).to.have.length(0);
        expect(optimalView.dimensions).to.have.length(2);
178
        expect(optimalView.childViews).to.have.length(3);
179

180
        expect(optimalView).satisfy((optView: View) => {
181
            return optView.dimensions.some((item) => item.name === subdim[2].name);
182 183
        });
        expect(optimalView).satisfy((optView: View) => {
184
            return optView.dimensions.some((item) => item.name === subdim[4].name);
185
        });
186 187 188 189 190
    });

    it("should throw an exception, sub-dimension with non-existent parent", () => {
        let error: boolean = false;
        try {
191
            engine.query({metrics: [met[0]], dimensions: [subdim[3]]});
192 193 194
        }
        catch (e){
            error = true;
195
            expect(e.message).to.be.equal("Engine views cannot cover the query");
196 197

        }
198
        expect(error).to.be.true;
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

    it("should parse a clause, with target as dimension and operator as ==", () => {
        const strFilter = "dim:0==0";
        const clause = engine.parseClause(strFilter);
        expect(clause).to.be.an("object");
        expect(clause).to.have.property("filters");
        expect(clause).to.have.property("id");
        expect(clause.filters).to.be.an("array");
        expect(clause.filters).to.have.length(1);
        expect(clause.filters[0]).to.have.property("id");
        expect(clause.filters[0]).to.have.property("target");
        expect(clause.filters[0]).to.have.property("value");
        expect(clause.filters[0]).to.have.property("operator");
        expect(clause.filters[0].target).to.be.equal(dim[0]);
        expect(clause.filters[0].value).to.be.equal("0");
        expect(clause.filters[0].operator).to.be.equal(FilterOperator.EQUAL);
    });
    it("should parse a clause, with target as metric and operator as !=", () => {
        const strFilter = "met:0!=0";
        const clause = engine.parseClause(strFilter);
        expect(clause).to.be.an("object");
        expect(clause).to.have.property("filters");
        expect(clause).to.have.property("id");
        expect(clause.filters).to.be.an("array");
        expect(clause.filters).to.have.length(1);
        expect(clause.filters[0]).to.have.property("id");
        expect(clause.filters[0]).to.have.property("target");
        expect(clause.filters[0]).to.have.property("operator");
        expect(clause.filters[0]).to.have.property("value");
        expect(clause.filters[0].target).to.be.equal(met[0]);
        expect(clause.filters[0].value).to.be.equal("0");
        expect(clause.filters[0].operator).to.be.equal(FilterOperator.NOTEQUAL);
    });
    it("should throw an exception, when a dimension is not found", () => {
        let error: boolean = false;
        const strFilter = "dim:-1==0";
        const exeption = "Filter could not be created: \"dim:-1\" was not found";
        try {
            engine.parseClause(strFilter);
        }
        catch (e){
            error = true;
            expect(e.message).to.be.equal(exeption);

        }
        expect(error).to.be.true;
    });
    it("should throw an exception, when a metric is not found", () => {
        let error: boolean = false;
        const strFilter = "met:-1==0";
        const exeption = "Filter could not be created: \"met:-1\" was not found";
        try {
            engine.parseClause(strFilter);
        }
        catch (e){
            error = true;
            expect(e.message).to.be.equal(exeption);

        }
        expect(error).to.be.true;
    });
    it("should throw an exception, when a operator is not found", () => {
        let error: boolean = false;
        let strFilter = "met:-1=?0";
        let exeption = "Filter could not be created: Operator on \"" + strFilter + "\" could not be extracted";
        try {
            engine.parseClause(strFilter);
        }
        catch (e){
            error = true;
            expect(e.message).to.be.equal(exeption);

        }
        expect(error).to.be.true;
        error = false;
        strFilter = "met:-1!?0";
        exeption = "Filter could not be created: Operator on \"" + strFilter + "\" could not be extracted";
        try {
            engine.parseClause(strFilter);
        }
        catch (e){
            error = true;
            expect(e.message).to.be.equal(exeption);

        }
        expect(error).to.be.true;
    });
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

    it("should throw an exception, when a operator does not suit", () => {
        const operators = [">", "<", "<=", ">="];
        for (let i = 0; i < operators.length; ++i) {
            let error: boolean = false;
            let strFilter = "dim:3" + operators[i] + "joao";
            let exeption = "Filter could not be created: Operator \"" + operators[i] + "\" is invalid for target \"dim:3\"";
            try {
                engine.parseClause(strFilter);
            }
            catch (e){
                error = true;
                expect(e.message).to.be.equal(exeption);

            }
        }
    });

    it("should parse clauses with several operators for dates and integers", () => {
        const operators: {[key: string]: FilterOperator} = {
            ">": FilterOperator.GREATER,
            "<": FilterOperator.LOWER,
            "<=": FilterOperator.LOWEREQ,
            ">=": FilterOperator.GREATEREQ,
            "==": FilterOperator.EQUAL,
            "!=": FilterOperator.NOTEQUAL
        };
314
        for (let op of Object.keys(operators)) {
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
            const strFilter = "dim:0" + op + "0";
            const clause = engine.parseClause(strFilter);
            expect(clause).to.be.an("object");
            expect(clause).to.have.property("filters");
            expect(clause).to.have.property("id");
            expect(clause.filters).to.be.an("array");
            expect(clause.filters).to.have.length(1);
            expect(clause.filters[0]).to.have.property("id");
            expect(clause.filters[0]).to.have.property("target");
            expect(clause.filters[0]).to.have.property("operator");
            expect(clause.filters[0]).to.have.property("value");
            expect(clause.filters[0].target).to.be.equal(dim[0]);
            expect(clause.filters[0].value).to.be.equal("0");
            expect(clause.filters[0].operator).to.be.equal(operators[op]);
        }

331
        for (let op of Object.keys(operators)) {
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
            const strFilter = "dim:2" + op + "0";
            const clause = engine.parseClause(strFilter);
            expect(clause).to.be.an("object");
            expect(clause).to.have.property("filters");
            expect(clause).to.have.property("id");
            expect(clause.filters).to.be.an("array");
            expect(clause.filters).to.have.length(1);
            expect(clause.filters[0]).to.have.property("id");
            expect(clause.filters[0]).to.have.property("target");
            expect(clause.filters[0]).to.have.property("operator");
            expect(clause.filters[0]).to.have.property("value");
            expect(clause.filters[0].target).to.be.equal(dim[2]);
            expect(clause.filters[0].value).to.be.equal("0");
            expect(clause.filters[0].operator).to.be.equal(operators[op]);
        }
    });
348
});