// (C) 2007-2020 GoodData Corporation import { ExecuteAFM as AFM } from "@gooddata/typings"; import { isRelativelyEqual, relativizeMockedExecutionLocalIdentifiers, isILocalIdentifierQualifier, } from "../execution"; import * as mockedExecution from "./fixtures/measureValueFilter/mockedExecution.json"; import * as errorResponseExecution from "./fixtures/measureValueFilter/errorResponseExecution.json"; import { ISchemaAfmExecution } from "../../../../schema/model/SchemaAfmExecution"; describe("isRelativelyEqual", () => { it("should not equal if measures differ", () => { const execution: AFM.IExecution = { execution: { afm: { measures: [ { localIdentifier: "xyz313", definition: { measure: { item: { identifier: "xyz", }, }, }, }, ], }, }, }; const other: AFM.IExecution = { execution: { afm: { measures: [ { localIdentifier: "m0", definition: { measure: { item: { identifier: "sdlfjsdlf", }, }, }, }, ], }, }, }; expect(isRelativelyEqual(execution, other)).toBeFalsy(); }); it("should equal if measures are same but with different local identifiers and aliases", () => { const execution: AFM.IExecution = { execution: { afm: { measures: [ { localIdentifier: "xyz313", alias: "measure-name", definition: { measure: { item: { identifier: "xyz", }, }, }, }, ], }, }, }; const other: AFM.IExecution = { execution: { afm: { measures: [ { localIdentifier: "m0", alias: "different-measure-name", definition: { measure: { item: { identifier: "xyz", }, }, }, }, ], }, }, }; expect(isRelativelyEqual(execution, other)).toBeTruthy(); }); it("should not equal if attributes differ", () => { const execution: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "sdflsjdflj", displayForm: { identifier: "123", }, }, ], }, }, }; const other: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "a0", displayForm: { identifier: "sdfljl", }, }, ], }, }, }; expect(isRelativelyEqual(execution, other)).toBeFalsy(); }); it("should equal if attributes are the same but local identifiers and aliases are different", () => { const execution: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "sdflsjdflj", alias: "attribute-name", displayForm: { identifier: "123", }, }, ], }, }, }; const other: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "a0", alias: "different-attribute-name", displayForm: { identifier: "123", }, }, ], }, }, }; expect(isRelativelyEqual(execution, other)).toBeTruthy(); }); it("should not equal if sorts differ", () => { const execution: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "ssdlfjdl", displayForm: { identifier: "123", }, }, ], }, resultSpec: { sorts: [ { attributeSortItem: { attributeIdentifier: "ssdlfjdl", direction: "desc", }, }, ], }, }, }; const other: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "a0", displayForm: { identifier: "123", }, }, ], }, resultSpec: { sorts: [ { attributeSortItem: { attributeIdentifier: "a0", direction: "asc", }, }, ], }, }, }; expect(isRelativelyEqual(execution, other)).toBeFalsy(); }); it("should be equal if only identifiers differ", () => { const execution: AFM.IExecution = { execution: { afm: { measures: [ { localIdentifier: "xyz313", definition: { measure: { item: { identifier: "xyz", }, }, }, }, ], }, }, }; const other: AFM.IExecution = { execution: { afm: { measures: [ { localIdentifier: "m0", definition: { measure: { item: { identifier: "xyz", }, }, }, }, ], }, }, }; expect(isRelativelyEqual(execution, other)).toBeTruthy(); }); it("should correctly work with execution with previous period comparison", () => { const execution: AFM.IExecution = { execution: { afm: { measures: [ { localIdentifier: "c465143b93644beeaff09e63733421c6_previous_period_1", definition: { previousPeriodMeasure: { measureIdentifier: "c465143b93644beeaff09e63733421c6", dateDataSets: [ { dataSet: { uri: "/gdc/md/mockproject/obj/activity.dataset" }, periodsAgo: 1, }, ], }, }, alias: "Amount - previous period", }, { localIdentifier: "c465143b93644beeaff09e63733421c6", definition: { measure: { item: { uri: "/gdc/md/mockproject/obj/__amount", }, }, }, alias: "Amount", }, { localIdentifier: "09c21a863ea149808a1b9c5212de0269", definition: { measure: { item: { uri: "/gdc/md/mockproject/obj/__won", }, }, }, alias: "# Won", }, ], filters: [ { relativeDateFilter: { dataSet: { uri: "/gdc/md/mockproject/obj/activity.dataset", }, granularity: "GDC.time.month", from: 0, to: 0, }, }, ], }, resultSpec: { dimensions: [ { itemIdentifiers: ["measureGroup"], }, { itemIdentifiers: [], }, ], sorts: [], }, }, }; const otherExecution: AFM.IExecution = { execution: { afm: { measures: [ { localIdentifier: "m0", definition: { previousPeriodMeasure: { measureIdentifier: "m1", dateDataSets: [ { dataSet: { uri: "/gdc/md/mockproject/obj/activity.dataset", }, periodsAgo: 1, }, ], }, }, alias: "Amount - previous period", }, { localIdentifier: "m1", definition: { measure: { item: { uri: "/gdc/md/mockproject/obj/__amount", }, }, }, alias: "Amount", }, { localIdentifier: "m2", definition: { measure: { item: { uri: "/gdc/md/mockproject/obj/__won", }, }, }, alias: "# Won", }, ], filters: [ { relativeDateFilter: { dataSet: { uri: "/gdc/md/mockproject/obj/activity.dataset", }, granularity: "GDC.time.month", from: 0, to: 0, }, }, ], }, resultSpec: { dimensions: [ { itemIdentifiers: ["measureGroup"], }, { itemIdentifiers: [], }, ], sorts: [], }, }, }; expect(isRelativelyEqual(execution, otherExecution)).toBeTruthy(); }); it("should correctly work with execution with pop comparison", () => { const execution: AFM.IExecution = { execution: { afm: { measures: [ { localIdentifier: "72f8f22615884843bbcb852fe3811cd8_pop", definition: { popMeasure: { measureIdentifier: "72f8f22615884843bbcb852fe3811cd8", popAttribute: { uri: "/gdc/md/mockproject/obj/attr.activity.year", }, }, }, alias: "Amount - SP year ago", }, { localIdentifier: "72f8f22615884843bbcb852fe3811cd8", definition: { measure: { item: { uri: "/gdc/md/mockproject/obj/__amount", }, }, }, alias: "Amount", }, ], }, resultSpec: { sorts: [], dimensions: [ { itemIdentifiers: [], }, { itemIdentifiers: ["measureGroup"], }, ], }, }, }; const otherExecution: AFM.IExecution = { execution: { afm: { measures: [ { localIdentifier: "m1_pop", definition: { popMeasure: { measureIdentifier: "m1", popAttribute: { uri: "/gdc/md/mockproject/obj/attr.activity.year", }, }, }, alias: "Amount - SP year ago", }, { localIdentifier: "m1", definition: { measure: { item: { uri: "/gdc/md/mockproject/obj/__amount", }, }, }, alias: "Amount", }, ], }, resultSpec: { sorts: [], dimensions: [ { itemIdentifiers: [], }, { itemIdentifiers: ["measureGroup"], }, ], }, }, }; expect(isRelativelyEqual(execution, otherExecution)).toBeTruthy(); }); it("should correctly work with execution with arithmetic measure", () => { const execution: AFM.IExecution = { execution: { afm: { measures: [ { localIdentifier: "aa7f9f75ead44515bc428b5860949331", definition: { arithmeticMeasure: { measureIdentifiers: [ "edc0c486a50f43aebb925e7a059bbc8c", "edc0c486a50f43aebb925e7a059bbc8c", ], operator: "sum", }, }, alias: "Sum of Amount and Amount", }, { localIdentifier: "72f8f22615884843bbcb852fe3811cd8", definition: { measure: { item: { uri: "/gdc/md/mockproject/obj/__amount", }, }, }, alias: "Amount", }, ], }, resultSpec: { sorts: [], dimensions: [ { itemIdentifiers: [], }, { itemIdentifiers: ["measureGroup"], }, ], }, }, }; const otherExecution: AFM.IExecution = { execution: { afm: { measures: [ { localIdentifier: "am1", definition: { arithmeticMeasure: { measureIdentifiers: [ "edc0c486a50f43aebb925e7a059bbc8c", "edc0c486a50f43aebb925e7a059bbc8c", ], operator: "sum", }, }, alias: "Sum of Amount and Amount", }, { localIdentifier: "m1", definition: { measure: { item: { uri: "/gdc/md/mockproject/obj/__amount", }, }, }, alias: "Amount", }, ], }, resultSpec: { sorts: [], dimensions: [ { itemIdentifiers: [], }, { itemIdentifiers: ["measureGroup"], }, ], }, }, }; expect(isRelativelyEqual(execution, otherExecution)).toBeTruthy(); }); it("should equal if same total is defined for the same measure and attribute", () => { const execution: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "ssdlfjdl", displayForm: { identifier: "123", }, }, ], measures: [ { localIdentifier: "xyz313", definition: { measure: { item: { identifier: "xyz", }, }, }, }, ], }, resultSpec: { sorts: [ { attributeSortItem: { attributeIdentifier: "ssdlfjdl", direction: "desc", }, }, ], dimensions: [ { itemIdentifiers: ["ssdlfjdl"], totals: [ { type: "sum", measureIdentifier: "xyz313", attributeIdentifier: "ssdlfjdl", }, ], }, { itemIdentifiers: ["measureGroup"], }, ], }, }, }; const other: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "a0", displayForm: { identifier: "123", }, }, ], measures: [ { localIdentifier: "m0", definition: { measure: { item: { identifier: "xyz", }, }, }, }, ], }, resultSpec: { sorts: [ { attributeSortItem: { attributeIdentifier: "a0", direction: "desc", }, }, ], dimensions: [ { itemIdentifiers: ["a0"], totals: [ { type: "sum", measureIdentifier: "m0", attributeIdentifier: "a0", }, ], }, { itemIdentifiers: ["measureGroup"], }, ], }, }, }; expect(isRelativelyEqual(execution, other)).toBeTruthy(); }); it("should not equal if different total is defined for the same measure and attribute", () => { const execution: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "ssdlfjdl", displayForm: { identifier: "123", }, }, ], measures: [ { localIdentifier: "xyz313", definition: { measure: { item: { identifier: "xyz", }, }, }, }, ], }, resultSpec: { sorts: [ { attributeSortItem: { attributeIdentifier: "ssdlfjdl", direction: "desc", }, }, ], dimensions: [ { itemIdentifiers: ["ssdlfjdl"], totals: [ { type: "sum", measureIdentifier: "xyz313", attributeIdentifier: "ssdlfjdl", }, ], }, { itemIdentifiers: ["measureGroup"], }, ], }, }, }; const other: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "a0", displayForm: { identifier: "123", }, }, ], measures: [ { localIdentifier: "m0", definition: { measure: { item: { identifier: "xyz", }, }, }, }, ], }, resultSpec: { sorts: [ { attributeSortItem: { attributeIdentifier: "a0", direction: "desc", }, }, ], dimensions: [ { itemIdentifiers: ["a0"], totals: [ { type: "min", measureIdentifier: "m0", attributeIdentifier: "a0", }, ], }, { itemIdentifiers: ["measureGroup"], }, ], }, }, }; expect(isRelativelyEqual(execution, other)).toBeFalsy(); }); it("should equal when local measure value or ranking filter is applied on one execution", () => { const execution: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "ssdlfjdl", displayForm: { identifier: "123", }, }, ], measures: [ { localIdentifier: "xyz313", definition: { measure: { item: { identifier: "xyz", }, }, }, }, ], filters: [ { measureValueFilter: { measure: { localIdentifier: "xyz313", }, condition: { comparison: { operator: "EQUAL_TO", value: 42, }, }, }, }, { rankingFilter: { measures: [ { localIdentifier: "xyz313", }, ], operator: "TOP", value: 3, }, }, ], }, resultSpec: { sorts: [ { attributeSortItem: { attributeIdentifier: "ssdlfjdl", direction: "desc", }, }, ], dimensions: [ { itemIdentifiers: ["ssdlfjdl"], }, { itemIdentifiers: ["measureGroup"], }, ], }, }, }; const other: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "ssdlfjdl", displayForm: { identifier: "123", }, }, ], measures: [ { localIdentifier: "xyz313", definition: { measure: { item: { identifier: "xyz", }, }, }, }, ], }, resultSpec: { sorts: [ { attributeSortItem: { attributeIdentifier: "ssdlfjdl", direction: "desc", }, }, ], dimensions: [ { itemIdentifiers: ["ssdlfjdl"], }, { itemIdentifiers: ["measureGroup"], }, ], }, }, }; expect(isRelativelyEqual(execution, other)).toBe(true); }); it("should not equal when external measure value filter is applied on one execution", () => { const execution: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "ssdlfjdl", displayForm: { identifier: "123", }, }, ], measures: [ { localIdentifier: "xyz313", definition: { measure: { item: { identifier: "xyz", }, }, }, }, ], filters: [ { measureValueFilter: { measure: { uri: "/uri", }, condition: { comparison: { operator: "EQUAL_TO", value: 42, }, }, }, }, ], }, resultSpec: { sorts: [ { attributeSortItem: { attributeIdentifier: "ssdlfjdl", direction: "desc", }, }, ], dimensions: [ { itemIdentifiers: ["ssdlfjdl"], }, { itemIdentifiers: ["measureGroup"], }, ], }, }, }; const other: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "ssdlfjdl", displayForm: { identifier: "123", }, }, ], measures: [ { localIdentifier: "xyz313", definition: { measure: { item: { identifier: "xyz", }, }, }, }, ], }, resultSpec: { sorts: [ { attributeSortItem: { attributeIdentifier: "ssdlfjdl", direction: "desc", }, }, ], dimensions: [ { itemIdentifiers: ["ssdlfjdl"], }, { itemIdentifiers: ["measureGroup"], }, ], }, }, }; expect(isRelativelyEqual(execution, other)).toBe(false); }); it("should not equal when external ranking filter is applied on one execution", () => { const execution: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "ssdlfjdl", displayForm: { identifier: "123", }, }, ], measures: [ { localIdentifier: "xyz313", definition: { measure: { item: { identifier: "xyz", }, }, }, }, ], filters: [ { rankingFilter: { measures: [ { uri: "/uri", }, ], operator: "TOP", value: 3, }, }, ], }, resultSpec: { sorts: [ { attributeSortItem: { attributeIdentifier: "ssdlfjdl", direction: "desc", }, }, ], dimensions: [ { itemIdentifiers: ["ssdlfjdl"], }, { itemIdentifiers: ["measureGroup"], }, ], }, }, }; const other: AFM.IExecution = { execution: { afm: { attributes: [ { localIdentifier: "ssdlfjdl", displayForm: { identifier: "123", }, }, ], measures: [ { localIdentifier: "xyz313", definition: { measure: { item: { identifier: "xyz", }, }, }, }, ], }, resultSpec: { sorts: [ { attributeSortItem: { attributeIdentifier: "ssdlfjdl", direction: "desc", }, }, ], dimensions: [ { itemIdentifiers: ["ssdlfjdl"], }, { itemIdentifiers: ["measureGroup"], }, ], }, }, }; expect(isRelativelyEqual(execution, other)).toBe(false); }); }); describe("relativizeMockedExecutionLocalIdentifiers", () => { it("should relativize local identifiers of mocked execution", () => { const relativizedMock = relativizeMockedExecutionLocalIdentifiers( // @ts-ignore - this is necessary to convert JSON fixture module to target type mockedExecution as ISchemaAfmExecution, ); expect(relativizedMock).toMatchSnapshot(); }); it("should handle error response", () => { const relativizedMock = relativizeMockedExecutionLocalIdentifiers( // @ts-ignore - this is necessary to convert JSON fixture module to target type errorResponseExecution as ISchemaAfmExecution, ); expect(relativizedMock).toMatchSnapshot(); }); }); describe("isILocalIdentifierQualifier", () => { it("should return true if local identifier qualifier is provided", () => { expect(isILocalIdentifierQualifier({ localIdentifier: "m1" })).toBe(true); }); it("should return false if URI qualifier is provided", () => { expect(isILocalIdentifierQualifier({ uri: "/uri" })).toBe(false); }); it("should return false if identifier qualifier is provided", () => { expect(isILocalIdentifierQualifier({ identifier: "id" })).toBe(false); }); });