Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jjspace committed Jan 23, 2025
1 parent be5f5a7 commit 0b91045
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/engine/Source/Scene/ITwinData.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import RuntimeError from "../Core/RuntimeError.js";
import Check from "../Core/Check.js";
import KmlDataSource from "../DataSources/KmlDataSource.js";
import GeoJsonDataSource from "../DataSources/GeoJsonDataSource.js";
import DeveloperError from "../Core/DeveloperError.js";

/**
* Methods for loading iTwin platform data into CesiumJS
Expand Down Expand Up @@ -205,6 +206,17 @@ ITwinData.createDataSourceForRealityDataId = async function (
return KmlDataSource.load(tilesetAccessUrl);
};

/**
* Load data from the Geospatial Features API as GeoJSON.
*
* NOTE: this function does not implement the full WFS schema yet so the limit
* should be larger than the entire dataset to retrieve everything
*
* @param {string} iTwinId The id of the iTwin to load data from
* @param {string} collectionId The id of the data collection to load
* @param {number} [limit=10000] number of items per page, must be between 1 and 10,000 inclusive
* @returns {Promise<GeoJsonDataSource>}
*/
ITwinData.loadGeospatialFeatures = async function (
iTwinId,
collectionId,
Expand All @@ -218,11 +230,14 @@ ITwinData.loadGeospatialFeatures = async function (
Check.typeOf.number.lessThanOrEquals("limit", limit, 10000);
Check.typeOf.number.greaterThanOrEquals("limit", limit, 1);
}
if (!defined(ITwinPlatform.defaultAccessToken)) {
throw new DeveloperError("Must set ITwinPlatform.defaultAccessToken first");
}
//>>includeEnd('debug')

const pageLimit = limit ?? 10000;

const tilesetUrl = `${ITwinPlatform.apiEndpoint}/geospatial-features/itwins/${iTwinId}/ogc/collections/${collectionId}/items`;
const tilesetUrl = `${ITwinPlatform.apiEndpoint}geospatial-features/itwins/${iTwinId}/ogc/collections/${collectionId}/items`;

const resource = new Resource({
url: tilesetUrl,
Expand Down
63 changes: 63 additions & 0 deletions packages/engine/Specs/Scene/ITwinDataSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,67 @@ describe("ITwinData", () => {
expect(geojsonSpy).not.toHaveBeenCalled();
});
});

describe("loadGeospatialFeatures", () => {
let geojsonSpy;
beforeEach(() => {
geojsonSpy = spyOn(GeoJsonDataSource, "load");
});

it("rejects with no iTwinId", async () => {
await expectAsync(
// @ts-expect-error
ITwinData.loadGeospatialFeatures(undefined),
).toBeRejectedWithDeveloperError(
"Expected iTwinId to be typeof string, actual typeof was undefined",
);
});

it("rejects with no collectionId", async () => {
await expectAsync(
// @ts-expect-error
ITwinData.loadGeospatialFeatures("itwin-id-1", undefined),
).toBeRejectedWithDeveloperError(
"Expected collectionId to be typeof string, actual typeof was undefined",
);
});

it("rejects with limit < 1", async () => {
await expectAsync(
ITwinData.loadGeospatialFeatures("itwin-id-1", "collection-id-1", 0),
).toBeRejectedWithDeveloperError(
"Expected limit to be greater than or equal to 1, actual value was 0",
);
});

it("rejects with limit > 10000", async () => {
await expectAsync(
ITwinData.loadGeospatialFeatures(
"itwin-id-1",
"collection-id-1",
20000,
),
).toBeRejectedWithDeveloperError(
"Expected limit to be less than or equal to 10000, actual value was 20000",
);
});

it("rejects with no default access token set", async () => {
ITwinPlatform.defaultAccessToken = undefined;
await expectAsync(
ITwinData.loadGeospatialFeatures("itwin-id-1", "collection-id-1"),
).toBeRejectedWithDeveloperError(
"Must set ITwinPlatform.defaultAccessToken first",
);
});

it("creates a GeoJsonDataSource from the constructed blob url if the type is GeoJSON", async () => {
await ITwinData.loadGeospatialFeatures("itwin-id-1", "collection-id-1");

expect(geojsonSpy).toHaveBeenCalledTimes(1);
expect(geojsonSpy.calls.mostRecent().args[0].url).toEqual(
"https://api.bentley.com/geospatial-features/itwins/itwin-id-1/ogc/collections/collection-id-1/items?limit=10000&client=CesiumJS",
);
});
});
});

0 comments on commit 0b91045

Please sign in to comment.