engine.spec.ts 13.5 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
        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.metrics).to.be.an("array");
        expect(optimalView.dimensions).to.be.an("array");
55
        expect(optimalView.metrics).to.have.length(12);
56
        expect(optimalView.dimensions).to.have.length(12);
57 58 59 60
    });
    it("should throw an exception, query with non-existent metric", () => {
        let error: boolean = false;
        try {
61
            engine.query({metrics: [engineScenario.wrongMet], dimensions: [dim[0]]});
62 63 64 65 66 67 68 69 70 71 72 73
        }
        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 {
74
            engine.query({metrics: [met[0]], dimensions: [engineScenario.wrongDim]});
75 76 77 78 79 80 81 82
        }
        catch (e){
            error = true;
            expect(e.message).to.be.equal("Engine views cannot cover the query");

        }
        expect(error);
    });
83 84 85

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

101
        expect(optimalView.id).to.be.equal(views[0].id);
102 103
    });

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    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");
121
        expect(optimalView).to.have.property("operation");
122 123
        expect(optimalView.metrics).to.be.an("array");
        expect(optimalView.dimensions).to.be.an("array");
124
        expect(optimalView.operation).to.be.an("object");
125 126
        expect(optimalView.metrics).to.have.length(3);
        expect(optimalView.dimensions).to.have.length(1);
127
        expect(optimalView.operation).to.have.property("opcode");
128 129 130 131

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

132 133 134 135
    it("should be create a fill that cover the query, using sub dimensions", () => {
        let emptyMetrics: Metric[] = [];
        let query = {
            metrics : emptyMetrics
136
            , dimensions : subdim.slice(0, 2)
137 138 139 140 141 142 143
        };
        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.metrics).to.be.an("array");
        expect(optimalView.dimensions).to.be.an("array");
144 145
        expect(optimalView.metrics).to.have.length(0);
        expect(optimalView.dimensions).to.have.length(2);
146

147
        expect(optimalView).satisfy((optView: View) => {
148
            return optView.dimensions.some((item) => item.name === subdim[0].name);
149 150
        });
        expect(optimalView).satisfy((optView: View) => {
151
            return optView.dimensions.some((item) => item.name === subdim[1].name);
152
        });
153 154 155 156 157 158
    });

    it("should be create a fill that cover the query, using the parents of sub dimensions", () => {
        let emptyMetrics: Metric[] = [];
        let query = {
            metrics : emptyMetrics
159
            , dimensions : [subdim[2], subdim[4]]
160 161 162 163 164 165 166
        };
        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.metrics).to.be.an("array");
        expect(optimalView.dimensions).to.be.an("array");
167 168
        expect(optimalView.metrics).to.have.length(0);
        expect(optimalView.dimensions).to.have.length(2);
169

170
        expect(optimalView).satisfy((optView: View) => {
171
            return optView.dimensions.some((item) => item.name === subdim[2].name);
172 173
        });
        expect(optimalView).satisfy((optView: View) => {
174
            return optView.dimensions.some((item) => item.name === subdim[4].name);
175
        });
176 177 178 179 180
    });

    it("should throw an exception, sub-dimension with non-existent parent", () => {
        let error: boolean = false;
        try {
181
            engine.query({metrics: [met[0]], dimensions: [subdim[3]]});
182 183 184
        }
        catch (e){
            error = true;
185
            expect(e.message).to.be.equal("Engine views cannot cover the query");
186 187

        }
188
        expect(error).to.be.true;
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 271 272 273 274 275 276

    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;
    });
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

    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
        };
304
        for (let op of Object.keys(operators)) {
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
            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]);
        }

321
        for (let op of Object.keys(operators)) {
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
            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]);
        }
    });
338
});