configParser.spec.ts 6.35 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";

23 24
import { ConfigParser, ViewParsingOptions } from "./configParser";
import { Dimension, DimensionStrOptions } from "../core/dimension";
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
import { RelationType } from "../common/types";

function strToRelationType (str: string): RelationType {
    switch (str) {
        case "day":
            return RelationType.DAY;
        case "month":
            return RelationType.MONTH;
        case "year":
            return RelationType.YEAR;
        case "dayofweek":
            return RelationType.DAYOFWEEK;
        default:
            return RelationType.NONE;
    }
}
41 42 43 44 45 46 47 48 49 50 51 52

describe("configParser utility library", () => {
    it("should throw expection for inexistent metric", () => {
        let opts: ViewParsingOptions =  {
            alias: "Test",
            data: "test",
            dimensions: [],
            metrics: ["met:-1"]
        };

        let error: boolean = false;
        try {
53
            ConfigParser.parseViewOpt(opts, {}, {});
54 55 56 57 58 59 60 61 62 63
        }
        catch (e) {
            error = true;
            expect(e.message).to.be
            .equal("[Parsing error] Non exist metric set to view " + opts.alias);
        }

        expect(error).to.be.true;
    });

64
    it("should throw expection for inexistent dimension", () => {
65 66 67 68 69 70 71 72 73
        let opts: ViewParsingOptions =  {
            alias: "Test",
            data: "test",
            dimensions: ["dim:-1"],
            metrics: []
        };

        let error: boolean = false;
        try {
74
            ConfigParser.parseViewOpt(opts, {}, {});
75 76 77 78 79 80 81 82 83 84
        }
        catch (e) {
            error = true;
            expect(e.message).to.be
            .equal("[Parsing error] Non exist dimension set to view " + opts.alias);
        }

        expect(error).to.be.true;
    });

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    it("should throw expection for inexistent parent for subdimension", () => {
        let opts: DimensionStrOptions =  {
            name: "dim:-1",
            dataType: "integer",
            parent: "dim:0",
            relation: "none"
        };

        let dims: Dimension[] = [];

        let error: boolean = false;
        try {
            ConfigParser.parseDimOpts(opts, dims);
        }
        catch (e) {
            error = true;
            expect(e.message).to.be
            .equal("[Parsing error] Parent for subdimension " + opts.name + "  not found");
        }

        expect(error).to.be.true;
    });

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
    it("should throw expection for bad formed clause, invalid operator", () => {
        let opts: ViewParsingOptions =  {
            alias: "thisMUSTgoesWrong",
            data: "someRandomPath",
            origin: true,
            dimensions: ["dim:0"],
            metrics: [],
            clauses: ["dim:0?=1"]
        };

        let dimMap: {[key: string]: Dimension} = {
            "dim:0" : new Dimension({name: "dim:0", dataType: "integer"})
        };

        let error: boolean = false;
        try {
            ConfigParser.parseViewOpt(opts, {}, dimMap);
        }
        catch (e) {
            error = true;
            expect(e.message).to.be
            .equal("[Parsing error] Bad formed clause: on view \"" + opts.alias +
                   "\" the clause \"dim:0?=1\" could not be created");
        }

        expect(error).to.be.true;
    });

    it("should throw expection for bad formed clause, invalid target", () => {
        let opts: ViewParsingOptions =  {
            alias: "thisMUSTgoesWrong",
            data: "someRandomPath",
            origin: true,
            dimensions: ["dim:0"],
            metrics: [],
            clauses: ["dim:1==1"]
        };

        let dimMap: {[key: string]: Dimension} = {
            "dim:0" : new Dimension({name: "dim:0", dataType: "integer"})
        };

        let error: boolean = false;
        try {
            ConfigParser.parseViewOpt(opts, {}, dimMap);
        }
        catch (e) {
            error = true;
            expect(e.message).to.be
            .equal("[Parsing error] Bad formed clause: on view \"" + opts.alias +
                   "\" the clause \"dim:1==1\" could not be created");
        }

        expect(error).to.be.true;
    });

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
    it("should parse subdimentions relations correctly", () => {
        let opts: DimensionStrOptions[] =  [
            {
                name: "dim:day",
                dataType: "integer",
                parent: "dim:0",
                relation: "day"
            },
            {
                name: "dim:month",
                dataType: "integer",
                parent: "dim:0",
                relation: "month"
            },
            {
                name: "dim:year",
                dataType: "integer",
                parent: "dim:0",
                relation: "year"
            },
            {
                name: "dim:dayofweek",
                dataType: "integer",
                parent: "dim:0",
                relation: "dayofweek"
            },
            {
                name: "dim:none",
                dataType: "integer",
                parent: "dim:0",
                relation: "none"
            },
        ];

        let dims: Dimension[] = [
            new Dimension({name: "dim:a", dataType: "date"}),
            new Dimension({name: "dim:0", dataType: "date"})
        ];

        for (let i = 0; i < opts.length; ++i) {
            let parsed = ConfigParser.parseDimOpts(opts[i], dims);
            expect(parsed.name).to.be.equal(opts[i].name);
            expect(parsed.dataType).to.be.equal(opts[i].dataType);
            expect(parsed.parent).to.be.equal(dims[1]);
            expect(parsed.relation).to.be.equal(strToRelationType(opts[i].relation));
        }
    });

212
});