@seasketch/geoprocessing
Version:
Geoprocessing and reporting framework for SeaSketch 2.0
108 lines • 3.75 kB
JavaScript
import { test, expect } from "vitest";
import { PreprocessingHandler } from "./PreprocessingHandler.js";
import { ValidationError } from "../types/index.js";
import deepEqual from "fast-deep-equal";
const feature = {
type: "Feature",
properties: {},
geometry: {
type: "Polygon",
coordinates: [
[
[-119.878_177_642_822_25, 34.377_870_792_354_564],
[-119.843_845_367_431_64, 34.377_870_792_354_564],
[-119.843_845_367_431_64, 34.412_291_363_453_07],
[-119.878_177_642_822_25, 34.412_291_363_453_07],
[-119.878_177_642_822_25, 34.377_870_792_354_564],
],
],
},
};
const simpleHandler = new PreprocessingHandler(async (feature) => {
return feature;
}, {
title: "handler",
description: "description",
timeout: 2,
requiresProperties: [],
});
const extraParamHandler = new PreprocessingHandler(async (feature, extraParams) => {
// Put extra params on the feature properties for testing
const result = {
...feature,
properties: extraParams,
};
return result;
}, {
title: "handler",
description: "description",
timeout: 2,
requiresProperties: [],
});
test("Makes options available as an instance var", () => {
expect(simpleHandler.options).toBeInstanceOf(Object);
expect(simpleHandler.options.title).toBe("handler");
});
test("Returns successful output as geojson", async () => {
const results = await simpleHandler.lambdaHandler({ body: JSON.stringify({ feature }), headers: {} }, {});
expect(results.statusCode).toBe(200);
const body = JSON.parse(results.body);
expect(body.data.type).toBe("Feature");
});
test("Preprocessor can accept extraParams", async () => {
const extraParams = { geography: "nearshore" };
const results = await extraParamHandler.lambdaHandler({
body: JSON.stringify({ feature, extraParams }),
headers: {},
}, {});
expect(results.statusCode).toBe(200);
const body = JSON.parse(results.body);
expect(body.data.type).toBe("Feature");
expect(deepEqual(body.data.properties, extraParams)).toBe(true);
});
test("Rejects misshapen requests", async () => {
const results = await simpleHandler.lambdaHandler({
body: JSON.stringify({ data: feature }),
headers: {},
}, {});
expect(results.statusCode).toBe(500);
const body = JSON.parse(results.body);
expect(body.status).toBe("error");
});
test("Returns validation errors", async () => {
const handler = new PreprocessingHandler(async () => {
throw new ValidationError("Out of bounds");
}, {
title: "handler",
description: "description",
timeout: 2,
requiresProperties: [],
});
const results = await handler.lambdaHandler({
body: JSON.stringify({ feature }),
headers: {},
}, {});
expect(results.statusCode).toBe(200);
const body = JSON.parse(results.body);
expect(body.status).toBe("validationError");
expect(body.error).toBe("Out of bounds");
});
test("500 errors are returned to clients", async () => {
const handler = new PreprocessingHandler(async () => {
throw new Error("I/O Error");
}, {
title: "handler",
description: "description",
timeout: 2,
requiresProperties: [],
});
const results = await handler.lambdaHandler({
body: JSON.stringify({ feature }),
headers: {},
}, {});
expect(results.statusCode).toBe(500);
const body = JSON.parse(results.body);
expect(body.status).toBe("error");
expect(body.error).toBe("I/O Error");
});
//# sourceMappingURL=PreprocessingHandler.test.js.map