Skip to content

Commit

Permalink
Improve geometry & collection testing
Browse files Browse the repository at this point in the history
  • Loading branch information
reilem committed Oct 28, 2024
1 parent ffecfbb commit 1d4af9f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
9 changes: 7 additions & 2 deletions examples/geometry/geometry_collection.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { GeoJSON2DGeometryCollection, GeoJSON3DGeometryCollection } from "../../src";
import { GeoJSON2DGeometryCollection, GeoJSON3DGeometryCollection, GeoJSONGeometryCollection } from "../../src";
import { geoJsonLineString3D } from "./line_string";
import { multiGeoJsonMultiLineString2D } from "./multi_line_string";
import { geoJsonMultiPoint2D } from "./multi_point";
import { singleGeoJsonMultiPolygon3D } from "./multi_polygon";
import { geoJsonPoint2D, geoJsonPoint2DWithBbox, geoJsonPoint3D } from "./point";
import { geoJsonPoint2D, geoJsonPoint2DWithBbox, geoJsonPoint3D, geoJsonPoint6D } from "./point";
import { geoJsonPolygon2D } from "./polygon";

export const singleGeoJsonGeometryCollection2D: GeoJSON2DGeometryCollection = {
Expand Down Expand Up @@ -35,3 +35,8 @@ export const multiGeoJsonGeometryCollection3DWithBbox: GeoJSON3DGeometryCollecti
...multiGeoJsonGeometryCollection3D,
bbox: [0.0, 0.0, 0.0, 20.0, 10.0, 10.0],
};

export const singleGeoJsonGeometryCollection6D: GeoJSONGeometryCollection = {
type: "GeometryCollection",
geometries: [geoJsonPoint6D],
};
15 changes: 9 additions & 6 deletions tests/geometry/geometry.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { describe, it } from "@jest/globals";
import { singleGeoJsonGeometryCollection2D } from "../../examples/geometry/geometry_collection";
import { geoJsonPoint2D } from "../../examples/geometry/point";
import { GeoJSON2DGeometry, GeoJSON3DGeometry, GeoJSONGeometry, GeoJSONGeometrySchema } from "../../src";
import { describe, expect, it } from "@jest/globals";
import { GeoJSON2DGeometry, GeoJSON3DGeometry, GeoJSONGeometry } from "../../src";
import { passGeoJSONGeometrySchemaTest } from "./_helpers";

function passGeoJSONGeometryTest(value: unknown): void {
passGeoJSONGeometrySchemaTest([], value);
}

describe("GeoJSONGeometry", () => {
it("allows a valid simple geometry", () => {
expect(GeoJSONGeometrySchema.parse(geoJsonPoint2D)).toEqual(geoJsonPoint2D);
passGeoJSONGeometryTest(geoJsonPoint2D);
});

it("allows a valid geometry collection", () => {
expect(GeoJSONGeometrySchema.parse(singleGeoJsonGeometryCollection2D)).toEqual(
singleGeoJsonGeometryCollection2D,
);
passGeoJSONGeometryTest(singleGeoJsonGeometryCollection2D);
});
});

Expand Down
37 changes: 29 additions & 8 deletions tests/geometry/geometry_collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
multiGeoJsonGeometryCollection3DWithBbox,
singleGeoJsonGeometryCollection2D,
singleGeoJsonGeometryCollection2DWithBbox,
singleGeoJsonGeometryCollection6D,
} from "../../examples/geometry/geometry_collection";
import { geoJsonLineString3D } from "../../examples/geometry/line_string";
import { geoJsonMultiPoint2D } from "../../examples/geometry/multi_point";
Expand All @@ -22,31 +23,45 @@ import {
import { failGeoJSONGeometrySchemaTest, passGeoJSONGeometrySchemaTest } from "./_helpers";

function passGeoJSONGeometryCollectionTest(value: unknown): void {
passGeoJSONGeometrySchemaTest(GeoJSONGeometryCollectionSchema, value);
passGeoJSONGeometrySchemaTest([GeoJSONGeometryCollectionSchema], value);
}

function passGeoJSON2DGeometryCollectionTest(value: unknown): void {
passGeoJSONGeometrySchemaTest([GeoJSONGeometryCollectionSchema, GeoJSON2DGeometryCollectionSchema], value);
}

function passGeoJSON3DGeometryCollectionTest(value: unknown): void {
passGeoJSONGeometrySchemaTest([GeoJSONGeometryCollectionSchema, GeoJSON3DGeometryCollectionSchema], value);
}

function failGeoJSONGeometryCollectionTest(value: unknown): void {
failGeoJSONGeometrySchemaTest(GeoJSONGeometryCollectionSchema, value);
failGeoJSONGeometrySchemaTest(
[GeoJSONGeometryCollectionSchema, GeoJSON2DGeometryCollectionSchema, GeoJSON3DGeometryCollectionSchema],
value,
);
}

describe("GeoJSONGeometryCollection", () => {
it("allows a geometry collection with one 2D geometry", () => {
passGeoJSONGeometryCollectionTest(singleGeoJsonGeometryCollection2D);
passGeoJSON2DGeometryCollectionTest(singleGeoJsonGeometryCollection2D);
});
it("allows a geometry collection with multiple 2D geometries", () => {
passGeoJSONGeometryCollectionTest(multiGeoJsonGeometryCollection2D);
passGeoJSON2DGeometryCollectionTest(multiGeoJsonGeometryCollection2D);
});
it("allows a geometry collection with multiple 3D geometries", () => {
passGeoJSONGeometryCollectionTest(multiGeoJsonGeometryCollection3D);
passGeoJSON3DGeometryCollectionTest(multiGeoJsonGeometryCollection3D);
});
it("allows a geometry collection with one 6D geometry", () => {
passGeoJSONGeometryCollectionTest(singleGeoJsonGeometryCollection6D);
});
it("allows a geometry collection with one 2D geometry and valid bbox", () => {
passGeoJSONGeometryCollectionTest(singleGeoJsonGeometryCollection2DWithBbox);
passGeoJSON2DGeometryCollectionTest(singleGeoJsonGeometryCollection2DWithBbox);
});
it("allows a geometry collection with multiple 2D geometries and valid bbox", () => {
passGeoJSONGeometryCollectionTest(multiGeoJsonGeometryCollection2DWithBbox);
passGeoJSON2DGeometryCollectionTest(multiGeoJsonGeometryCollection2DWithBbox);
});
it("allows a geometry collection with multiple 3D geometries and valid bbox", () => {
passGeoJSONGeometryCollectionTest(multiGeoJsonGeometryCollection3DWithBbox);
passGeoJSON3DGeometryCollectionTest(multiGeoJsonGeometryCollection3DWithBbox);
});
it("allows a geometry collection and preserves extra keys", () => {
passGeoJSONGeometryCollectionTest({
Expand Down Expand Up @@ -168,6 +183,9 @@ describe("GeoJSONGeometryCollection", () => {
it("does not allow a 3D geometry collection", () => {
expect(() => GeoJSON2DGeometryCollectionSchema.parse(multiGeoJsonGeometryCollection3D)).toThrow(ZodError);
});
it("does not allow a 6D geometry collection", () => {
expect(() => GeoJSON2DGeometryCollectionSchema.parse(singleGeoJsonGeometryCollection6D)).toThrow(ZodError);
});
});

describe("3D", () => {
Expand All @@ -179,6 +197,9 @@ describe("GeoJSONGeometryCollection", () => {
it("does not allow a 2D geometry collection", () => {
expect(() => GeoJSON3DGeometryCollectionSchema.parse(multiGeoJsonGeometryCollection2D)).toThrow(ZodError);
});
it("does not allow a 6D geometry collection", () => {
expect(() => GeoJSON3DGeometryCollectionSchema.parse(singleGeoJsonGeometryCollection6D)).toThrow(ZodError);
});
});
});

Expand Down

0 comments on commit 1d4af9f

Please sign in to comment.