// (C) 2007-2019 GoodData Corporation import * as request from "supertest"; import { organizedCatalog } from "../organized_catalog"; import { createEndpoint } from "../../../utils/tests"; import { ISchema } from "../../../schema/model/Schema"; describe("Organized catalog API when it is not defined in the mock schema", () => { const schema: ISchema = { project: { title: "My title", identifier: "thisproject", }, groups: [ { attributes: [ { title: "Account", elements: ["1", "2", "3"], }, ], metrics: [ { title: "Beer", expression: "Bar", }, ], facts: [ { title: "Amount", values: [1, 2, 3], }, ], dateDataSets: [ { title: "Date", }, ], }, ], }; const app = createEndpoint(organizedCatalog, schema); describe("catalog/groups API", () => { it("should return empty groups", async () => { const response = await request(app) .get("/gdc/internal/projects/thisproject/catalog/groups?production=1") .expect(200); expect(response.body).toEqual({ catalogGroups: [], }); }); }); describe("catalog/items API", () => { it("should return all catalog items for production dataset", async () => { const response = await request(app) .get("/gdc/internal/projects/thisproject/catalog/items?limit=500&offset=0&production=1") .expect(200); const expectedBody = { catalogItems: { items: [ { expression: "Bar", identifier: "beer", links: { self: "/gdc/md/thisproject/obj/beer", }, title: "Beer", type: "metric", }, { identifier: "fact.amount", links: { self: "/gdc/md/thisproject/obj/fact.amount", }, title: "Amount", type: "fact", }, { identifier: "attr.account", links: { defaultDisplayForm: "/gdc/md/thisproject/obj/attr.account.df", self: "/gdc/md/thisproject/obj/attr.account", }, title: "Account", type: "attribute", }, ], paging: { count: 3, offset: 0, }, }, }; expect(response.body).toEqual(expectedBody); }); it("should return all attributes when required types are only attribute", async () => { const response = await request(app) .get("/gdc/internal/projects/thisproject/catalog/items?limit=500&offset=0&types=attribute") .expect(200); const expectedBody = { catalogItems: { items: [ { identifier: "attr.account", links: { defaultDisplayForm: "/gdc/md/thisproject/obj/attr.account.df", self: "/gdc/md/thisproject/obj/attr.account", }, title: "Account", type: "attribute", }, ], paging: { count: 1, offset: 0, }, }, }; expect(response.body).toEqual(expectedBody); }); }); describe("catalog/query API", () => { it("should return all catalog item as available when types are not specified", async () => { const response = await request(app) .post("/gdc/internal/projects/thisproject/catalog/query") .set("Content-Type", "application/json") .send({ catalogQueryRequest: { bucketItems: [{ uri: "/gdc/md/thisproject/obj/fact.amount" }], }, }) .expect(200); const expectedBody = { catalogAvailableItems: { items: [ "/gdc/md/thisproject/obj/beer", "/gdc/md/thisproject/obj/fact.amount", "/gdc/md/thisproject/obj/attr.account", ], }, }; expect(response.body).toEqual(expectedBody); }); it("should return only attribute when requested attributes only", async () => { const response = await request(app) .post("/gdc/internal/projects/thisproject/catalog/query") .set("Content-Type", "application/json") .send({ catalogQueryRequest: { bucketItems: [{ uri: "/gdc/md/thisproject/obj/fact.amount" }], types: ["attribute"], }, }) .expect(200); const expectedBody = { catalogAvailableItems: { items: ["/gdc/md/thisproject/obj/attr.account"], }, }; expect(response.body).toEqual(expectedBody); }); }); }); describe("Organized catalog API when it is defined in the mock schema", () => { const schema: ISchema = { project: { title: "My title", identifier: "someproject", }, organizedCatalog: { groups: [ { query: { production: "1", }, response: ["production group"], }, ], items: [ { query: { limit: "500", offset: "123", production: "1", }, response: [ { expression: "measure_1_expression", format: "measure_1_format", identifier: "metric_1", type: "metric", title: "Measure1", }, { identifier: "attribute_1", type: "attribute", title: "Attribute1", }, { identifier: "fact_1", type: "fact", title: "Fact1", }, ], }, ], queryResponse: [ { body: { catalogQueryRequest: { bucketItems: ["cinemas"], }, }, response: ["cinemas", "sales", "viewers"], }, ], }, }; const app = createEndpoint(organizedCatalog, schema); describe("catalog/groups API", () => { it("should return groups specified for the particular params", async () => { const response = await request(app) .get("/gdc/internal/projects/someproject/catalog/groups?production=1") .expect(200); expect(response.body).toEqual({ catalogGroups: ["production group"], }); }); }); describe("catalog/items API", () => { it("should return generated items specified for the particular params with the correct project id in the URIs", async () => { const response = await request(app) .get("/gdc/internal/projects/someproject/catalog/items?limit=500&offset=123&production=1") .expect(200); const expectedBody = { catalogItems: { items: [ { expression: "measure_1_expression", format: "measure_1_format", identifier: "metric_1", links: { self: "/gdc/md/someproject/obj/metric_1", }, title: "Measure1", type: "metric", }, { identifier: "attribute_1", links: { self: "/gdc/md/someproject/obj/attribute_1", defaultDisplayForm: "/gdc/md/someproject/obj/attribute_1.df", }, title: "Attribute1", type: "attribute", }, { identifier: "fact_1", links: { self: "/gdc/md/someproject/obj/fact_1", }, title: "Fact1", type: "fact", }, ], paging: { count: 3, offset: 123, }, }, }; expect(response.body).toEqual(expectedBody); }); }); describe("catalog/query API", () => { it("should return generated URIs of the identifiers specified for the params with the correct project id in the URIs", async () => { const response = await request(app) .post("/gdc/internal/projects/someproject/catalog/query") .set("Content-Type", "application/json") .send({ catalogQueryRequest: { bucketItems: [{ uri: "/gdc/md/someproject/obj/cinemas" }], }, }) .expect(200); const expectedBody = { catalogAvailableItems: { items: [ "/gdc/md/someproject/obj/cinemas", "/gdc/md/someproject/obj/sales", "/gdc/md/someproject/obj/viewers", ], }, }; expect(response.body).toEqual(expectedBody); }); }); });