graph.spec.ts 19.1 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
/*
 * 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 { Metric } from "../core/metric";
import { Dimension } from "../core/dimension";
import { View } from "../core/view";
26 27
import { Filter, FilterOperator } from "../core/filter";
import { Clause } from "../core/clause";
28
import { Graph } from "./graph";
29
import { AggregationType, RelationType, DataType } from "../common/types";
30
import { Query } from "../common/query";
31 32 33 34 35

describe("graph class", () => {

    it("should not create 2 vertices with the same dimension", () => {
        let g = new Graph();
36
        let dim = new Dimension({name: "dim:test", dataType: DataType.STRING});
37 38 39 40 41 42 43 44 45 46

        expect(g.addDimension(dim)).to.be.true;
        expect(g.addDimension(dim)).to.be.false;
    });

    it("should not create 2 vertices with same metric", () => {
        let g = new Graph();
        let met = new Metric({
            name: "met:test",
            aggregation: AggregationType.SUM,
47
            dataType: DataType.STRING
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        });

        expect(g.addMetric(met)).to.be.true;
        expect(g.addMetric(met)).to.be.false;
    });

    it("should not create a vertex with a invalid dimension", () => {
        let g = new Graph();
        let dim: Dimension;

        expect(g.addDimension(dim)).to.be.false;
    });

    it("should not create a vertex with a invalid metric", () => {
        let g = new Graph();
        let met: Metric;

        expect(g.addMetric(met)).to.be.false;
    });

    it("should not create a vertex with a subdimension when parent is not a vertex", () => {
        let g = new Graph();
70
        let dim = new Dimension({name: "dim:test", dataType: DataType.STRING});
71 72
        let subdim = new Dimension({
            name: "dim:sub_test",
73
            dataType: DataType.STRING,
74 75 76 77 78 79 80 81 82
            relation: RelationType.MONTH,
            parent: dim
        });

        expect(g.addDimension(subdim)).to.be.false;
    });

    it("should add a set of views", () => {
        let dims = [
83 84 85 86
            new Dimension({name: "dim:0", dataType: DataType.STRING}),
            new Dimension({name: "dim:1", dataType: DataType.STRING}),
            new Dimension({name: "dim:2", dataType: DataType.STRING}),
            new Dimension({name: "dim:3", dataType: DataType.STRING})
87 88 89 90 91 92 93 94 95 96 97 98
        ];

        let g = new Graph();

        for (let i = 0; i < dims.length; ++i) {
            expect(g.addDimension(dims[i])).to.be.true;
        }

        let views = [
            new View({
                metrics: [],
                dimensions: [dims[0], dims[1]],
99
                origin: true
100 101 102 103
            }),
            new View({
                metrics: [],
                dimensions: [dims[2], dims[3]],
104
                origin: true
105 106 107 108
            }),
            new View({
                metrics: [],
                dimensions: dims,
109
                origin: true
110 111 112 113 114 115 116 117 118 119
            })
        ];

        for (let i = 0; i < views.length; ++i) {
            expect(g.addView(views[i])).to.be.true;
        }
    });

    it("should not add a view twice", () => {
        let dims = [
120 121
            new Dimension({name: "dim:0", dataType: DataType.STRING}),
            new Dimension({name: "dim:1", dataType: DataType.STRING}),
122 123 124 125 126 127 128 129 130 131 132
        ];

        let g = new Graph();

        for (let i = 0; i < dims.length; ++i) {
            expect(g.addDimension(dims[i])).to.be.true;
        }

        let view = new View({
            metrics: [],
            dimensions: [dims[0], dims[1]],
133
            origin: true
134 135 136 137 138 139 140 141
        });

        expect(g.addView(view)).to.be.true;
        expect(g.addView(view)).to.be.false;
    });

    it("should not add views when metrics and dimensions are not vertices", () => {
        let dims = [
142 143 144
            new Dimension({name: "dim:0", dataType: DataType.STRING}),
            new Dimension({name: "dim:1", dataType: DataType.STRING}),
            new Dimension({name: "dim:2", dataType: DataType.STRING})
145 146 147 148 149 150 151 152 153
        ];

        let g = new Graph();

        expect(g.addDimension(dims[0])).to.be.true;

        let view = new View({
            metrics: [],
            dimensions: dims,
154
            origin: true
155 156 157 158 159 160 161 162 163 164 165
        });

        expect(g.addView(view)).to.be.false;
    });

    it("should not add invalid views", () => {
        let g = new Graph();
        expect(g.addView(null)).to.be.false;
    });

    it("should add only once a view with only one vertex", () => {
166
        let dim = new Dimension({name: "dim:0", dataType: DataType.STRING});
167 168 169 170 171 172 173 174

        let g = new Graph();

        expect(g.addDimension(dim)).to.be.true;

        let view = new View({
            metrics: [],
            dimensions: [dim],
175
            origin: true
176 177 178 179 180 181 182
        });

        expect(g.addView(view)).to.be.true;
        expect(g.addView(view)).to.be.false;
    });

    it("should create a cover for a single vertex", () => {
183
        let dim = new Dimension({name: "dim:0", dataType: DataType.STRING});
184 185 186 187 188 189 190 191

        let g = new Graph();

        expect(g.addDimension(dim)).to.be.true;

        let view = new View({
            metrics: [],
            dimensions: [dim],
192
            origin: true
193 194 195
        });

        expect(g.addView(view)).to.be.true;
196 197
        const query: Query = { metrics: [], dimensions: [dim] };
        let children = g.cover(query);
198 199
        expect(children).to.be.an("array");
        expect(children).to.have.length(1);
200
        expect(children[0].id).to.be.equal(view.id);
201 202 203 204
    });

    it("should create a cover for several vertices", () => {
        let dims = [
205 206 207
            new Dimension({name: "dim:0", dataType: DataType.STRING}),
            new Dimension({name: "dim:1", dataType: DataType.STRING}),
            new Dimension({name: "dim:2", dataType: DataType.STRING})
208 209 210
        ];

        let mets = [
211 212 213
            new Metric({name: "met:0", dataType: DataType.INTEGER, aggregation: AggregationType.SUM}),
            new Metric({name: "met:1", dataType: DataType.INTEGER, aggregation: AggregationType.AVG}),
            new Metric({name: "met:2", dataType: DataType.INTEGER, aggregation: AggregationType.AVG})
214 215 216 217 218 219 220 221 222 223 224 225 226
        ];

        let g = new Graph();

        for (let i = 0; i < 3; ++i) {
            expect(g.addDimension(dims[i])).to.be.true;
            expect(g.addMetric(mets[i])).to.be.true;
        }

        let views = [
            new View({
                metrics: [mets[0]],
                dimensions: [dims[0]],
227
                origin: true
228 229 230 231
            }),
            new View({
                metrics: [mets[1]],
                dimensions: [dims[1]],
232
                origin: true
233 234 235 236
            }),
            new View({
                metrics: [mets[2]],
                dimensions: [dims[2]],
237
                origin: true
238 239 240 241
            }),
            new View({
                metrics: [],
                dimensions: dims,
242
                origin: true
243 244 245 246
            }),
            new View({
                metrics: mets,
                dimensions: dims,
247
                origin: true
248 249 250 251
            }),
            new View({
                metrics: [mets[0], mets[1]],
                dimensions: [dims[0], dims[1]],
252
                origin: true
253 254 255 256 257 258 259
            }),
        ];

        for (let i = 0; i < views.length; ++i) {
            expect(g.addView(views[i])).to.be.true;
        }

260 261
        const query: Query = { metrics: [mets[0], mets[1]], dimensions: [dims[0], dims[1]] };
        let children = g.cover(query);
262 263
        expect(children).to.be.an("array");
        expect(children).to.have.length(1);
264
        expect(children[0].id).to.be.equal(views[views.length - 1].id);
265 266 267
    });

    it("should create a cover with sub dimensions", () => {
268
        let dim = new Dimension({name: "dim:0", dataType: DataType.DATE});
269 270 271 272
        let dims = [
            dim,
            new Dimension({
                name: "subdim:0",
273
                dataType: DataType.STRING,
274 275 276 277 278
                parent: dim,
                relation: RelationType.MONTH
            }),
            new Dimension({
                name: "subdim:1",
279
                dataType: DataType.STRING,
280 281 282 283 284 285 286 287 288 289 290 291 292 293
                parent: dim,
                relation: RelationType.DAY
            }),
        ];

        let g = new Graph();

        for (let i = 0; i < 3; ++i) {
            expect(g.addDimension(dims[i])).to.be.true;
        }

        let view = new View({
            metrics: [],
            dimensions: [dims[0]],
294
            origin: true
295 296 297 298
        });

        expect(g.addView(view)).to.be.true;

299 300
        const query: Query = { metrics: [], dimensions: [dims[1], dims[2]] };
        let children = g.cover(query);
301 302
        expect(children).to.be.an("array");
        expect(children).to.have.length(1);
303
        expect(children[0].id).to.be.equal(view.id);
304 305 306
    });

    it("should return empty when try to cover a empty list", () => {
307
        let dim = new Dimension({name: "dim:0", dataType: DataType.DATE});
308 309 310 311
        let dims = [
            dim,
            new Dimension({
                name: "subdim:0",
312
                dataType: DataType.STRING,
313 314 315 316 317
                parent: dim,
                relation: RelationType.MONTH
            }),
            new Dimension({
                name: "subdim:1",
318
                dataType: DataType.STRING,
319 320 321 322 323 324 325 326 327 328 329 330 331 332
                parent: dim,
                relation: RelationType.DAY
            }),
        ];

        let g = new Graph();

        for (let i = 0; i < 3; ++i) {
            expect(g.addDimension(dims[i])).to.be.true;
        }

        let view = new View({
            metrics: [],
            dimensions: [dims[0]],
333
            origin: true
334 335 336 337
        });

        expect(g.addView(view)).to.be.true;

338 339
        const query: Query = { metrics: [], dimensions: [] };
        let children = g.cover(query);
340 341 342
        expect(children).to.be.an("array");
        expect(children).to.be.empty;
    });
343 344 345

    it("should cover the graph, even when a constraint edge can not be used", () => {
        let dims = [
346 347 348
            new Dimension({name: "dim:0", dataType: DataType.DATE}),
            new Dimension({name: "dim:1", dataType: DataType.DATE}),
            new Dimension({name: "dim:2", dataType: DataType.DATE}),
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
        ];

        let filter1 = new Filter({
            target: dims[0],
            operator: FilterOperator.EQUAL,
            value: "01/01/01"
        });
        let filter2 = new Filter({
            target: dims[0],
            operator: FilterOperator.EQUAL,
            value: "01/01/02"
        });
        let clause1 = new Clause({filters: [filter1]});
        let clause2 = new Clause({filters: [filter2]});

        let g = new Graph();

        for (let i = 0; i < 3; ++i) {
            expect(g.addDimension(dims[i])).to.be.true;
        }

        let view1 = new View({
            metrics: [],
            dimensions: [dims[0], dims[1]],
373
            origin: true
374 375 376 377 378
        });

        let view2 = new View({
            metrics: [],
            dimensions: [dims[1], dims[2]],
379
            origin: true
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
        });

        let view3 = new View({
            metrics: [],
            dimensions: dims,
            origin: false,
            clauses: [clause1]
        });

        expect(g.addView(view1)).to.be.true;
        expect(g.addView(view2)).to.be.true;
        expect(g.addView(view3)).to.be.true;

        const query: Query = { metrics: [], dimensions: dims, clauses: [clause2] };
        let children = g.cover(query);
        expect(children).to.be.an("array");
        expect(children).to.have.length(2);
        expect(children.every((item) => item.id !==  view3.id)).to.be.true;
    });

400 401
    it("should cover the query, using filters of intervals", () => {
        let dims = [
402 403 404
            new Dimension({name: "dim:0", dataType: DataType.FLOAT}),
            new Dimension({name: "dim:1", dataType: DataType.FLOAT}),
            new Dimension({name: "dim:2", dataType: DataType.DATE}),
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
        ];

        const filters = [
            new Filter({
                target: dims[0],
                operator: FilterOperator.GREATEREQ,
                value: "0"
            }),
            new Filter({
                target: dims[0],
                operator: FilterOperator.LOWER,
                value: "10"
            }),
            new Filter({
                target: dims[1],
                operator: FilterOperator.GREATER,
                value: "0"
            }),
            new Filter({
                target: dims[1],
                operator: FilterOperator.LOWEREQ,
                value: "10"
            }),
            new Filter({
                target: dims[2],
                operator: FilterOperator.GREATEREQ,
                value: "0"
            }),
            new Filter({
                target: dims[2],
                operator: FilterOperator.LOWER,
                value: "10"
            })
        ];

        const clauses = [
            new Clause({filters: [filters[0]]}),
            new Clause({filters: [filters[1]]}),
            new Clause({filters: [filters[2]]}),
            new Clause({filters: [filters[3]]}),
            new Clause({filters: [filters[4]]}),
            new Clause({filters: [filters[5]]}),
        ];

        const filtersTest = [
            new Filter({
                target: dims[0],
                operator: FilterOperator.GREATER,
                value: "1"
            }),
            new Filter({
                target: dims[0],
                operator: FilterOperator.LOWEREQ,
                value: "9"
            }),
            new Filter({
                target: dims[1],
                operator: FilterOperator.GREATEREQ,
                value: "1"
            }),
            new Filter({
                target: dims[1],
                operator: FilterOperator.LOWER,
                value: "9"
            }),
            new Filter({
                target: dims[2],
                operator: FilterOperator.GREATER,
                value: "1"
            }),
            new Filter({
                target: dims[2],
                operator: FilterOperator.LOWEREQ,
                value: "9"
            })
        ];

        const testClauses = [
            new Clause({filters: [filtersTest[0]]}),
            new Clause({filters: [filtersTest[1]]}),
            new Clause({filters: [filtersTest[2]]}),
            new Clause({filters: [filtersTest[3]]}),
            new Clause({filters: [filtersTest[4]]}),
            new Clause({filters: [filtersTest[5]]}),
        ];

        let g = new Graph();

        for (let i = 0; i < 3; ++i) {
            expect(g.addDimension(dims[i])).to.be.true;
        }

        let view0 = new View({
            metrics: [],
            dimensions: dims,
            origin: false,
            clauses: clauses
        });

        expect(g.addView(view0)).to.be.true;

        const query: Query = { metrics: [], dimensions: dims, clauses: testClauses };
        let children = g.cover(query);
        expect(children).to.have.length(1);
        expect(children[0].id === view0.id).to.be.true;
    });

    it("should cover the query, not using filters of intervals", () => {
        let dims = [
514 515 516
            new Dimension({name: "dim:0", dataType: DataType.FLOAT}),
            new Dimension({name: "dim:1", dataType: DataType.FLOAT}),
            new Dimension({name: "dim:2", dataType: DataType.DATE}),
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
        ];

        const filters = [
            new Filter({
                target: dims[0],
                operator: FilterOperator.GREATEREQ,
                value: "10"
            }),
            new Filter({
                target: dims[0],
                operator: FilterOperator.LOWER,
                value: "0"
            }),
            new Filter({
                target: dims[1],
                operator: FilterOperator.GREATER,
                value: "10"
            }),
            new Filter({
                target: dims[1],
                operator: FilterOperator.LOWEREQ,
                value: "0"
            }),
            new Filter({
                target: dims[2],
                operator: FilterOperator.GREATEREQ,
                value: "10"
            }),
            new Filter({
                target: dims[2],
                operator: FilterOperator.LOWER,
                value: "0"
            })
        ];

        const clauses = [
            new Clause({filters: [filters[0]]}),
            new Clause({filters: [filters[1]]}),
            new Clause({filters: [filters[2]]}),
            new Clause({filters: [filters[3]]}),
            new Clause({filters: [filters[4]]}),
            new Clause({filters: [filters[5]]}),
        ];

        const filtersTest = [
            new Filter({
                target: dims[0],
                operator: FilterOperator.GREATER,
                value: "0"
            }),
            new Filter({
                target: dims[0],
                operator: FilterOperator.LOWEREQ,
                value: "10"
            }),
            new Filter({
                target: dims[1],
                operator: FilterOperator.GREATEREQ,
                value: "0"
            }),
            new Filter({
                target: dims[1],
                operator: FilterOperator.LOWER,
                value: "11"
            }),
            new Filter({
                target: dims[2],
                operator: FilterOperator.GREATER,
                value: "0"
            }),
            new Filter({
                target: dims[2],
                operator: FilterOperator.LOWEREQ,
                value: "10"
            })
        ];

        const testClauses = [
            new Clause({filters: [filtersTest[0]]}),
            new Clause({filters: [filtersTest[1]]}),
            new Clause({filters: [filtersTest[2]]}),
            new Clause({filters: [filtersTest[3]]}),
            new Clause({filters: [filtersTest[4]]}),
            new Clause({filters: [filtersTest[5]]}),
        ];

        let g = new Graph();

        for (let i = 0; i < 3; ++i) {
            expect(g.addDimension(dims[i])).to.be.true;
        }

        let views = [
            new View({
                metrics: [],
                dimensions: dims,
613
                origin: false
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
            })
        ];

        for (let  i = 0; i < testClauses.length; ++i) {
            views.push(new View({
                metrics: [],
                dimensions: dims,
                origin: false,
                clauses: [clauses[i]]
            }));
        }

        for (let  i = 0; i < views.length; ++i) {
            expect(g.addView(views[i])).to.be.true;
        }

        const query: Query = { metrics: [], dimensions: dims, clauses: testClauses };
        let children = g.cover(query);
        expect(children).to.have.length(1);
        expect(children[0].id === views[0].id).to.be.true;
    });

636
});