// (C) 2007-2019 GoodData Corporation import { ExecuteAFM as AFM } from "@gooddata/typings"; import { cloneDeep } from "lodash"; import { IRandomDataResult } from "../dataResult"; import { getTotalsFromExecution, getTotalsTypesFromExecution, TotalsResultRandomGenerator, getTotals, getTotalsData, } from "../totals"; const EXECUTION_WITH_TOTALS: AFM.IExecution = { execution: { afm: { nativeTotals: [ { attributeIdentifiers: [], measureIdentifier: "m", }, ], }, resultSpec: { dimensions: [ { itemIdentifiers: [], totals: [ { measureIdentifier: "m", attributeIdentifier: "a", type: "sum", }, { measureIdentifier: "m", attributeIdentifier: "a", type: "nat", }, ], }, ], }, }, }; const EXECUTION_WITHOUT_TOTALS: AFM.IExecution = { execution: { afm: {}, resultSpec: { dimensions: [ { itemIdentifiers: [], }, ], }, }, }; const dataResultWithoutTotals: IRandomDataResult = { dims: [], isEmpty: false, data: [], headerItems: [ [ [ { attributeHeaderItem: { uri: "ahu1", name: "ahn1", }, }, { attributeHeaderItem: { uri: "ahu2", name: "ahn2", }, }, { attributeHeaderItem: { uri: "ahu3", name: "ahn3", }, }, ], ], [ [ { measureHeaderItem: { name: "mn1", order: 0 } }, { measureHeaderItem: { name: "mn2", order: 1 } }, ], ], ], measureHeaderItems: [], }; const EXECUTION_WITH_MULTIPLE_TOTALS: AFM.IExecution = { execution: { afm: { measures: [ { localIdentifier: "id1", definition: { measure: { item: { uri: "u1" } } }, }, { localIdentifier: "id2", definition: { measure: { item: { uri: "u2" } } }, }, ], }, resultSpec: { dimensions: [ { itemIdentifiers: ["a1"], totals: [ { type: "sum", measureIdentifier: "id1", attributeIdentifier: "a1", }, { type: "sum", measureIdentifier: "id2", attributeIdentifier: "a1", }, { type: "max", measureIdentifier: "id2", attributeIdentifier: "a1", }, ], }, { itemIdentifiers: ["m1"] }, ], }, }, }; const dataResultWithMultipleTotals: IRandomDataResult = { dims: [], isEmpty: false, data: [], headerItems: [ [ [ { attributeHeaderItem: { uri: "ahu1", name: "ahn1", }, }, { attributeHeaderItem: { uri: "ahu2", name: "ahn2", }, }, { attributeHeaderItem: { uri: "ahu3", name: "ahn3", }, }, ], ], [ [ { measureHeaderItem: { name: "mn1", order: 0 } }, { measureHeaderItem: { name: "mn2", order: 1 } }, ], ], ], measureHeaderItems: [], }; const createRandomGenerator: () => TotalsResultRandomGenerator = () => { let n = 1; return () => String(n++); }; describe("getTotalFromExecution", () => { it("should get totals from first dimension when dimension and totals present", () => { const totals = getTotalsFromExecution(EXECUTION_WITH_TOTALS); expect(totals).toEqual([ { measureIdentifier: "m", attributeIdentifier: "a", type: "sum", }, { measureIdentifier: "m", attributeIdentifier: "a", type: "nat", }, ]); }); it("should get empty totals when no totals present", () => { const totals = getTotalsTypesFromExecution(EXECUTION_WITHOUT_TOTALS); expect(totals).toEqual([]); }); it("should get empty totals when no result spec", () => { const totals = getTotalsTypesFromExecution({ execution: { afm: {}, }, }); expect(totals).toEqual([]); }); }); describe("getTotalsTypesFromExecution", () => { it("should return totals types list when present in result spec", () => { const totals = getTotalsTypesFromExecution(EXECUTION_WITH_TOTALS); expect(totals).toEqual(["sum", "nat"]); }); it("should return empty totals types list when no result spec", () => { const totals = getTotalsTypesFromExecution(EXECUTION_WITHOUT_TOTALS); expect(totals).toEqual([]); }); }); describe("getTotalsData", () => { it("should return empty totals", () => { const totalsResult = getTotalsData( EXECUTION_WITHOUT_TOTALS, dataResultWithoutTotals, createRandomGenerator(), ); expect(totalsResult).toEqual([[], []]); }); it("should return totals results", () => { const totalsResult = getTotalsData( EXECUTION_WITH_MULTIPLE_TOTALS, dataResultWithMultipleTotals, createRandomGenerator(), ); expect(totalsResult).toEqual([[["1", "2"], [null, "3"]], []]); }); }); describe("getTotals", () => { it("should return totals types and data when execution request is valid", () => { const totalsResponse = getTotals( EXECUTION_WITH_TOTALS, dataResultWithMultipleTotals, createRandomGenerator(), ); expect(totalsResponse).toHaveProperty("data"); expect(totalsResponse).toHaveProperty("types"); }); it("should return error and data when execution request has invalid nativetotals definition", () => { const executionObject = cloneDeep(EXECUTION_WITH_TOTALS); delete executionObject.execution.afm.nativeTotals; const totalsResponse = getTotals( executionObject, dataResultWithMultipleTotals, createRandomGenerator(), ); expect(totalsResponse).toHaveProperty("error"); }); });