// (C) 2007-2019 GoodData Corporation import { AFMBuilder } from "../AFMBuilder"; import { schema } from "../../../schema/fixtures/dummySchema"; import { buildProject } from "../../../schema/builder"; import { ExecuteAFM as AFM } from "@gooddata/typings"; describe("AFMBuilder", () => { const getProject = () => buildProject(schema); it("should be able to build directly from schema", () => { const builder = AFMBuilder.buildFromSchema(schema); builder.addMeasure(); expect(builder.build()).toEqual({ execution: { afm: { attributes: [], measures: [ { definition: { measure: { item: { identifier: "simple_metric", }, }, }, localIdentifier: "m0", }, ], filters: [], }, resultSpec: {}, }, }); }); it("should be able to add measure", () => { const builder = new AFMBuilder(getProject()); builder.addMeasure(); expect(builder.build()).toEqual({ execution: { afm: { attributes: [], measures: [ { definition: { measure: { item: { identifier: "simple_metric", }, }, }, localIdentifier: "m0", }, ], filters: [], }, resultSpec: {}, }, }); }); it("should be able to add multiple measures", () => { const builder = new AFMBuilder(getProject()); builder.addMeasure().addMeasure(); expect(builder.build()).toEqual({ execution: { afm: { attributes: [], measures: [ { definition: { measure: { item: { identifier: "simple_metric", }, }, }, localIdentifier: "m0", }, { definition: { measure: { item: { identifier: "4321", }, }, }, localIdentifier: "m1", }, ], filters: [], }, resultSpec: {}, }, }); }); it("should be able to set measure custom format", () => { const builder = new AFMBuilder(getProject()); builder.addMeasure("#,##0.00").addMeasure("#,###0.000"); expect(builder.build()).toEqual({ execution: { afm: { attributes: [], measures: [ { definition: { measure: { item: { identifier: "simple_metric", }, }, }, format: "#,##0.00", localIdentifier: "m0", }, { definition: { measure: { item: { identifier: "4321", }, }, }, format: "#,###0.000", localIdentifier: "m1", }, ], filters: [], }, resultSpec: {}, }, }); }); it("should be able to set measure custom title (alias)", () => { const builder = new AFMBuilder(getProject()); builder.addMeasure(null, "Custom name").addMeasure(null, "Custom name 2"); expect(builder.build()).toEqual({ execution: { afm: { attributes: [], measures: [ { alias: "Custom name", definition: { measure: { item: { identifier: "simple_metric", }, }, }, localIdentifier: "m0", }, { alias: "Custom name 2", definition: { measure: { item: { identifier: "4321", }, }, }, localIdentifier: "m1", }, ], filters: [], }, resultSpec: {}, }, }); }); it("should be able to set measure custom title and measure title", () => { const builder = new AFMBuilder(getProject()); builder.addMeasure("#,##0.00", "Custom name"); expect(builder.build()).toEqual({ execution: { afm: { attributes: [], measures: [ { alias: "Custom name", definition: { measure: { item: { identifier: "simple_metric", }, }, }, format: "#,##0.00", localIdentifier: "m0", }, ], filters: [], }, resultSpec: {}, }, }); }); it("should be able to set attribute based measure", () => { const builder = new AFMBuilder(getProject()); builder.addAttributeBasedMeasure(); expect(builder.build()).toEqual({ execution: { afm: { attributes: [], measures: [ { alias: "Count of Employee", definition: { measure: { aggregation: "count", item: { identifier: "attr.employee.df", }, }, }, localIdentifier: "m0", format: "#,##0", }, ], filters: [], }, resultSpec: {}, }, }); }); it("should be able to add attribute", () => { const builder = new AFMBuilder(getProject()); builder.addAttribute(); expect(builder.build()).toEqual({ execution: { afm: { attributes: [ { displayForm: { identifier: "attr.employee.df", }, localIdentifier: "a0", }, ], measures: [], filters: [], }, resultSpec: {}, }, }); }); it("should be able to add attribute with alias", () => { const alias = "My custom alias"; const builder = new AFMBuilder(getProject()); builder.addAttribute(alias); expect(builder.build()).toEqual({ execution: { afm: { attributes: [ { displayForm: { identifier: "attr.employee.df", }, localIdentifier: "a0", alias, }, ], measures: [], filters: [], }, resultSpec: {}, }, }); }); it("should be able to add attribute by its name", () => { const builder = new AFMBuilder(getProject()); builder.addAttributeByTitle("Customer"); expect(builder.build()).toEqual({ execution: { afm: { attributes: [ { displayForm: { identifier: "1234.df", }, localIdentifier: "a0", }, ], measures: [], filters: [], }, resultSpec: {}, }, }); }); it("should be able to add multiple attributes", () => { const builder = new AFMBuilder(getProject()); builder.addAttribute().addAttribute(); expect(builder.build()).toEqual({ execution: { afm: { attributes: [ { displayForm: { identifier: "attr.employee.df", }, localIdentifier: "a0", }, { displayForm: { identifier: "1234.df", }, localIdentifier: "a1", }, ], measures: [], filters: [], }, resultSpec: {}, }, }); }); it("should be able to add result spec", () => { const builder = new AFMBuilder(getProject()); builder.addResultSpec({ dimensions: [], }); expect(builder.build()).toEqual({ execution: { afm: { attributes: [], measures: [], filters: [], }, resultSpec: { dimensions: [], }, }, }); }); it("should be able to add filter", () => { const builder = new AFMBuilder(getProject()); const filter: AFM.IPositiveAttributeFilter = { positiveAttributeFilter: { displayForm: { identifier: "attr.employee.df", }, in: ["/gdc/md/mockproject/obj/attr.employee/elements?id=1"], }, }; builder.addFilter(filter); expect(builder.build()).toEqual({ execution: { afm: { attributes: [], measures: [], filters: [filter], }, resultSpec: {}, }, }); }); it("should be able to add date attribute", () => { const builder = new AFMBuilder(getProject()); builder.addDateAttribute(); expect(builder.build()).toEqual({ execution: { afm: { attributes: [ { localIdentifier: "a0", displayForm: { identifier: "attr.date.year.df", }, }, ], measures: [], filters: [], }, resultSpec: {}, }, }); }); it("should be able to add native totals to afm", () => { const builder = new AFMBuilder(getProject()); builder .addAttribute() .addMeasure() .addResultSpec({ dimensions: [ { itemIdentifiers: ["a0"], }, ], }) .addTotals(["min", "nat"]); expect(builder.build()).toMatchObject({ execution: { afm: { nativeTotals: [ { measureIdentifier: "m0", attributeIdentifiers: [], }, ], }, }, }); }); it("should be able to add totals to result spec", () => { const builder = new AFMBuilder(getProject()); builder .addAttribute() .addMeasure() .addResultSpec({ dimensions: [ { itemIdentifiers: ["a0"], }, ], }) .addTotals(["min", "nat"]); expect(builder.build()).toMatchObject({ execution: { resultSpec: { dimensions: [ { itemIdentifiers: ["a0"], totals: [ { measureIdentifier: "m0", attributeIdentifier: "a0", type: "min", }, { measureIdentifier: "m0", attributeIdentifier: "a0", type: "nat", }, ], }, ], }, }, }); }); });