UNPKG

@itwin/insights-client

Version:

Insights client for the iTwin platform

280 lines 15.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ const chaiAsPromised = require("chai-as-promised"); const chai_1 = require("chai"); require("reflect-metadata"); const GlobalSetup_1 = require("../utils/GlobalSetup"); const cross_fetch_1 = require("cross-fetch"); (0, chai_1.use)(chaiAsPromised); describe("Reports Client", () => { const mappingIds = []; const reportIds = []; const reportMappingIds = []; const reportAggregations = []; const aggregationIds = []; before(async () => { // create mappings for tests const newMapping = { mappingName: "Test1", iModelId: GlobalSetup_1.testIModel.id, }; let mapping = await GlobalSetup_1.mappingsClient.createMapping(GlobalSetup_1.accessToken, newMapping); mappingIds.push(mapping.id); newMapping.mappingName = "Test2"; mapping = await GlobalSetup_1.mappingsClient.createMapping(GlobalSetup_1.accessToken, newMapping); mappingIds.push(mapping.id); newMapping.mappingName = "Test3"; mapping = await GlobalSetup_1.mappingsClient.createMapping(GlobalSetup_1.accessToken, newMapping); mappingIds.push(mapping.id); // create reports for tests const newReport = { displayName: "Test1", projectId: GlobalSetup_1.iTwinId, }; let report = await GlobalSetup_1.reportsClient.createReport(GlobalSetup_1.accessToken, newReport); reportIds.push(report.id); newReport.displayName = "Test2"; report = await GlobalSetup_1.reportsClient.createReport(GlobalSetup_1.accessToken, newReport); reportIds.push(report.id); newReport.displayName = "Test3"; report = await GlobalSetup_1.reportsClient.createReport(GlobalSetup_1.accessToken, newReport); reportIds.push(report.id); // create deleted report newReport.displayName = "Test"; report = await GlobalSetup_1.reportsClient.createReport(GlobalSetup_1.accessToken, newReport); await GlobalSetup_1.reportsClient.deleteReport(GlobalSetup_1.accessToken, report.id); // create reportMappings for tests const newReportMapping = { mappingId: mappingIds[mappingIds.length - 3], imodelId: GlobalSetup_1.testIModel.id, }; let reportMapping = await GlobalSetup_1.reportsClient.createReportMapping(GlobalSetup_1.accessToken, reportIds[0], newReportMapping); reportMappingIds.push(reportMapping.mappingId); newReportMapping.mappingId = mappingIds[mappingIds.length - 2]; reportMapping = await GlobalSetup_1.reportsClient.createReportMapping(GlobalSetup_1.accessToken, reportIds[0], newReportMapping); reportMappingIds.push(reportMapping.mappingId); newReportMapping.mappingId = mappingIds[mappingIds.length - 1]; reportMapping = await GlobalSetup_1.reportsClient.createReportMapping(GlobalSetup_1.accessToken, reportIds[0], newReportMapping); reportMappingIds.push(reportMapping.mappingId); // create table sets for test const newAggregationTableSet = { tableSetName: "TableSet1", description: "AggregationTableSet for a mapping", datasourceId: mappingIds[mappingIds.length - 3], datasourceType: "IModelMapping", }; let tableSet = await GlobalSetup_1.aggregationsClient.createAggregationTableSet(GlobalSetup_1.accessToken, newAggregationTableSet); aggregationIds.push(tableSet.id); newAggregationTableSet.tableSetName = "TableSet2"; tableSet = await GlobalSetup_1.aggregationsClient.createAggregationTableSet(GlobalSetup_1.accessToken, newAggregationTableSet); aggregationIds.push(tableSet.id); newAggregationTableSet.tableSetName = "TableSet3"; tableSet = await GlobalSetup_1.aggregationsClient.createAggregationTableSet(GlobalSetup_1.accessToken, newAggregationTableSet); aggregationIds.push(tableSet.id); // create report aggregations for tests const newAggregation = { aggregationTableSetId: aggregationIds[aggregationIds.length - 3], }; let aggregation = await GlobalSetup_1.reportsClient.createReportAggregation(GlobalSetup_1.accessToken, reportIds[0], newAggregation); reportAggregations.push(aggregation.aggregationTableSetId); newAggregation.aggregationTableSetId = aggregationIds[aggregationIds.length - 2]; aggregation = await GlobalSetup_1.reportsClient.createReportAggregation(GlobalSetup_1.accessToken, reportIds[0], newAggregation); reportAggregations.push(aggregation.aggregationTableSetId); newAggregation.aggregationTableSetId = aggregationIds[aggregationIds.length - 1]; aggregation = await GlobalSetup_1.reportsClient.createReportAggregation(GlobalSetup_1.accessToken, reportIds[0], newAggregation); reportAggregations.push(aggregation.aggregationTableSetId); }); after(async () => { while (mappingIds.length > 0) { await GlobalSetup_1.mappingsClient.deleteMapping(GlobalSetup_1.accessToken, mappingIds.pop()); } while (reportIds.length > 0) { await GlobalSetup_1.reportsClient.deleteReport(GlobalSetup_1.accessToken, reportIds.pop()); } }); // reports tests it("Reports - Create and delete", async () => { const newReport = { displayName: "Test", projectId: GlobalSetup_1.iTwinId, }; const report = await GlobalSetup_1.reportsClient.createReport(GlobalSetup_1.accessToken, newReport); (0, chai_1.expect)(report).to.not.be.undefined; (0, chai_1.expect)(report.displayName).to.be.eq("Test"); const response = await GlobalSetup_1.reportsClient.deleteReport(GlobalSetup_1.accessToken, report.id); (0, chai_1.expect)(response.status).to.be.eq(204); }); it("Reports - Get", async () => { const report = await GlobalSetup_1.reportsClient.getReport(GlobalSetup_1.accessToken, reportIds[0]); (0, chai_1.expect)(report).to.not.be.undefined; (0, chai_1.expect)(report.displayName).to.be.eq("Test1"); const headers = { // eslint-disable-next-line @typescript-eslint/naming-convention Authorization: GlobalSetup_1.accessToken, }; const odataResult = await (0, cross_fetch_1.default)(report._links.odata.href, { headers }); (0, chai_1.expect)(odataResult.status).to.be.eq(200); }); it("Reports - Update", async () => { const reportUpdate = { description: "Updated", }; const report = await GlobalSetup_1.reportsClient.updateReport(GlobalSetup_1.accessToken, reportIds[0], reportUpdate); (0, chai_1.expect)(report).to.not.be.undefined; (0, chai_1.expect)(report.description).to.be.eq("Updated"); }); it("Reports - Get all including deleted", async () => { const reports = await GlobalSetup_1.reportsClient.getReports(GlobalSetup_1.accessToken, GlobalSetup_1.iTwinId, undefined, true); (0, chai_1.expect)(reports).to.not.be.undefined; (0, chai_1.expect)(reports.length).to.be.gt(3); (0, chai_1.expect)(reports.some((x) => x.deleted)).to.be.true; }); it("Reports - Get all", async () => { const reports = await GlobalSetup_1.reportsClient.getReports(GlobalSetup_1.accessToken, GlobalSetup_1.iTwinId); (0, chai_1.expect)(reports).to.not.be.undefined; (0, chai_1.expect)(reports.length).to.be.above(2); for (const report of reports) { (0, chai_1.expect)(["Test1", "Test2", "Test3"]).to.include(report.displayName); } }); it("Reports - Get with iterator", async () => { const reportsIt = GlobalSetup_1.reportsClient.getReportsIterator(GlobalSetup_1.accessToken, GlobalSetup_1.iTwinId, 2); let flag = false; for await (const report of reportsIt) { flag = true; (0, chai_1.expect)(report).to.not.be.undefined; (0, chai_1.expect)(["Test1", "Test2", "Test3"]).to.include(report.displayName); } (0, chai_1.expect)(flag).to.be.true; }); it("Reports - Get pages", async () => { const reportsIt = GlobalSetup_1.reportsClient.getReportsIterator(GlobalSetup_1.accessToken, GlobalSetup_1.iTwinId, 2); let elementCount = 0; let flag = false; for await (const reports of reportsIt.byPage()) { flag = true; (0, chai_1.expect)(reports).to.not.be.undefined; if (reports.length) { for (const report of reports) { (0, chai_1.expect)(["Test1", "Test2", "Test3"]).to.include(report.displayName); } elementCount += reports.length; } } (0, chai_1.expect)(flag).to.be.true; (0, chai_1.expect)(elementCount).to.not.be.eq(0); }); // report mapping tests it("Report mappings - Create and delete", async () => { const newMapping = { mappingName: "Test", iModelId: GlobalSetup_1.testIModel.id, }; const mapping = await GlobalSetup_1.mappingsClient.createMapping(GlobalSetup_1.accessToken, newMapping); (0, chai_1.expect)(mapping).to.not.be.undefined; mappingIds.push(mapping.id); const newReportMapping = { mappingId: mappingIds[mappingIds.length - 1], imodelId: GlobalSetup_1.testIModel.id, }; const reportMapping = await GlobalSetup_1.reportsClient.createReportMapping(GlobalSetup_1.accessToken, reportIds[0], newReportMapping); (0, chai_1.expect)(reportMapping).to.not.be.undefined; (0, chai_1.expect)(reportMapping.imodelId).to.be.eq(GlobalSetup_1.testIModel.id); reportMappingIds.push(reportMapping.mappingId); let response; response = await GlobalSetup_1.reportsClient.deleteReportMapping(GlobalSetup_1.accessToken, reportIds[0], reportMappingIds.pop()); (0, chai_1.expect)(response.status).to.be.eq(204); response = await GlobalSetup_1.mappingsClient.deleteMapping(GlobalSetup_1.accessToken, mappingIds.pop()); (0, chai_1.expect)(response.status).to.be.eq(204); }); it("Report mappings - Get all", async () => { const reportMappings = await GlobalSetup_1.reportsClient.getReportMappings(GlobalSetup_1.accessToken, reportIds[0]); (0, chai_1.expect)(reportMappings).to.not.be.undefined; (0, chai_1.expect)(reportMappings.length).to.be.above(2); (0, chai_1.expect)([...mappingIds]).to.include(reportMappings[0].mappingId); }); it("Report mappings - Get with iterator", async () => { const reportsIt = GlobalSetup_1.reportsClient.getReportMappingsIterator(GlobalSetup_1.accessToken, reportIds[0], 2); let flag = false; for await (const reportMapping of reportsIt) { flag = true; (0, chai_1.expect)(reportMapping).to.not.be.undefined; (0, chai_1.expect)([...mappingIds]).to.include(reportMapping.mappingId); } (0, chai_1.expect)(flag).to.be.true; }); it("Report mappings - Get pages with iterator", async () => { const reportsIt = GlobalSetup_1.reportsClient.getReportMappingsIterator(GlobalSetup_1.accessToken, reportIds[0], 2); let elementCount = 0; let flag = false; for await (const reportMappings of reportsIt.byPage()) { flag = true; (0, chai_1.expect)(reportMappings).to.not.be.undefined; if (reportMappings.length) { (0, chai_1.expect)([...mappingIds]).to.include(reportMappings[0].mappingId); elementCount += reportMappings.length; } } (0, chai_1.expect)(flag).to.be.true; (0, chai_1.expect)(elementCount).to.not.be.eq(0); }); // report aggregation tests it("Report aggregations - Create and delete", async () => { const newAggregationTableSet = { tableSetName: "TableSet1", description: "AggregationTableSet for a mapping", datasourceId: mappingIds[mappingIds.length - 3], datasourceType: "IModelMapping", }; const tableSet = await GlobalSetup_1.aggregationsClient.createAggregationTableSet(GlobalSetup_1.accessToken, newAggregationTableSet); aggregationIds.push(tableSet.id); const newAggregation = { aggregationTableSetId: aggregationIds[aggregationIds.length - 1], }; const aggregation = await GlobalSetup_1.reportsClient.createReportAggregation(GlobalSetup_1.accessToken, reportIds[0], newAggregation); (0, chai_1.expect)(aggregation).to.not.be.undefined; (0, chai_1.expect)(aggregation.aggregationTableSetId).to.be.eq(aggregationIds[aggregationIds.length - 1]); reportAggregations.push(aggregation.aggregationTableSetId); let response; response = await GlobalSetup_1.reportsClient.deleteReportAggregation(GlobalSetup_1.accessToken, reportIds[0], reportAggregations.pop()); (0, chai_1.expect)(response.status).to.be.eq(204); response = await GlobalSetup_1.aggregationsClient.deleteAggregationTableSet(GlobalSetup_1.accessToken, aggregationIds.pop()); (0, chai_1.expect)(response.status).to.be.eq(204); }); it("Report aggregations - Get all", async () => { const aggregations = await GlobalSetup_1.reportsClient.getReportAggregations(GlobalSetup_1.accessToken, reportIds[0]); (0, chai_1.expect)(aggregations).to.not.be.undefined; (0, chai_1.expect)(aggregations.length).to.be.above(2); (0, chai_1.expect)([...reportIds]).to.include(aggregations[0].reportId); }); it("Report aggregations - Get with iterator", async () => { const aggregationsIt = GlobalSetup_1.reportsClient.getReportAggregationsIterator(GlobalSetup_1.accessToken, reportIds[0], 2); let flag = false; for await (const aggregation of aggregationsIt) { flag = true; (0, chai_1.expect)(aggregation).to.not.be.undefined; (0, chai_1.expect)([...reportIds]).to.include(aggregation.reportId); } (0, chai_1.expect)(flag).to.be.true; }); it("Report aggregations - Get pages with iterator", async () => { const aggregationsIt = GlobalSetup_1.reportsClient.getReportAggregationsIterator(GlobalSetup_1.accessToken, reportIds[0], 2); let elementCount = 0; let flag = false; for await (const aggregations of aggregationsIt.byPage()) { flag = true; (0, chai_1.expect)(aggregations).to.not.be.undefined; if (aggregations.length) { (0, chai_1.expect)([...reportIds]).to.include(aggregations[0].reportId); elementCount += aggregations.length; } } (0, chai_1.expect)(flag).to.be.true; (0, chai_1.expect)(elementCount).to.not.be.eq(0); }); }); //# sourceMappingURL=ReportsClient.test.js.map