// (C) 2007-2021 GoodData Corporation import * as request from "supertest"; import * as HttpStatusCodes from "http-status-codes"; import { ISchema } from "../../../schema/model/Schema"; import { dateDataSets } from "../dateDataSets"; import { schema } from "../../../schema/fixtures/dummySchema"; import { createEndpoint } from "../../../utils/tests"; import * as dateDataSetsErrors from "../../../constants/DateDataSetsErrors"; import { ILoadDateDatasetsRequest, ILoadDateDatasetsResponse, } from "../../../model/endPoints/LoadDateDatasets"; describe("loadDateDatasetsMocks endPoint", () => { const testSchema: ISchema = { project: { title: "My title", }, groups: [ { attributes: [ { title: "Customer", elements: ["John Doe", "Jane Doe"], }, ], metrics: [ { title: "Beer", expression: "Bar", }, { title: "Amount", expression: "Foo", }, ], facts: [ { title: "Amount", values: [1, 2, 3], }, ], dateDataSets: [{ title: "Date" }], }, { dateDataSets: [{ title: "Activity" }], }, { dateDataSets: [{ title: "Closed" }], meta: { type: "csv", identifier: "my-uploaded-csv", }, }, ], }; const app = createEndpoint(dateDataSets, testSchema); const LOAD_DATE_DATASETS_URI = "/gdc/internal/projects/mockproject/loadDateDataSets"; it("should return all date datasets", () => { return request(app) .post(LOAD_DATE_DATASETS_URI) .set("Content-Type", "application/json") .send({ dateDataSetsRequest: {}, }) .then(res => { expect(res.body).toMatchSnapshot(); }); }); it("should return available date datasets", () => { return request(app) .post(LOAD_DATE_DATASETS_URI) .set("Content-Type", "application/json") .send({ dateDataSetsRequest: { bucketItems: ["/gdc/md/mockproject/obj/attr.customer"], }, }) .then(res => { expect(res.body).toMatchSnapshot(); }); }); it("should return date datasets from CSV", () => { return request(app) .post(LOAD_DATE_DATASETS_URI) .set("Content-Type", "application/json") .send({ dateDataSetsRequest: { requiredDataSets: { type: "CUSTOM", customIdentifiers: ["my-uploaded-csv"], }, }, }) .then(res => { expect(res.body).toMatchSnapshot(); }); }); it("should return available date datasets with specified granularities", () => { return request(app) .post(LOAD_DATE_DATASETS_URI) .set("Content-Type", "application/json") .send({ dateDataSetsRequest: { includeDateGranularities: [ "GDC.time.day_in_quarter", "GDC.time.week_in_quarter", "GDC.time.month_in_quarter", ], }, }) .then(res => { expect(res.body).toMatchSnapshot(); }); }); it("should return custom dateDatasets from project schema", async () => { const requestObject: ILoadDateDatasetsRequest = { dateDataSetsRequest: { includeUnavailableDateDataSetsCount: true, bucketItems: [], requiredDataSets: { type: "PRODUCTION", }, }, }; const app = createEndpoint(dateDataSets, schema); const response = await request(app) .post(LOAD_DATE_DATASETS_URI) .set("Content-Type", "application/json") .send(requestObject); const expectedResponse: ILoadDateDatasetsResponse = { dateDataSetsResponse: { dateDataSets: [], unavailableDateDataSetsCount: 0, }, }; expect(response.body).toEqual(expectedResponse); }); it("should validate more than one root element", () => { return request(app) .post(LOAD_DATE_DATASETS_URI) .set("Content-Type", "application/json") .send({ a: ["b"], c: ["d"], e: ["f"], }) .expect(HttpStatusCodes.BAD_REQUEST) .then(res => { expect(res.body).toEqual(dateDataSetsErrors.WRONG_STRUCTURE); }); }); it("should validate invalid element key", () => { return request(app) .post(LOAD_DATE_DATASETS_URI) .set("Content-Type", "application/json") .send({ dateDataSetsRequestMustBeHere: {}, }) .expect(HttpStatusCodes.BAD_REQUEST) .then(res => { expect(res.body).toEqual(dateDataSetsErrors.STRUCTURE_NOT_DEFINED); }); }); it("should validate invalid element type", () => { return request(app) .post(LOAD_DATE_DATASETS_URI) .set("Content-Type", "application/json") .send({ dateDataSetsRequest: [], }) .expect(HttpStatusCodes.BAD_REQUEST) .then(res => { expect(res.body).toEqual(dateDataSetsErrors.INVALID_STRUCTURE); }); }); });