Skip to content

Commit

Permalink
[TM-1452] touches boundary and multiple filters tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
roguenet committed Nov 20, 2024
1 parent 2a93aee commit 40ede90
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { createMock, DeepMocked } from "@golevelup/ts-jest";
import { Test, TestingModule } from "@nestjs/testing";
import { PolicyService } from "@terramatch-microservices/common";
import { BadRequestException, NotImplementedException, UnauthorizedException } from "@nestjs/common";
import { SitePolygonFactory } from "@terramatch-microservices/database/factories";
import { Resource } from "@terramatch-microservices/common/util";
import { SitePolygon } from "@terramatch-microservices/database/entities";
import { SitePolygonFactory } from "@terramatch-microservices/database/factories";

describe("SitePolygonsController", () => {
let controller: SitePolygonsController;
Expand Down Expand Up @@ -67,7 +67,7 @@ describe("SitePolygonsController", () => {

it("Returns a valid value if the request is valid", async () => {
policyService.authorize.mockResolvedValue(undefined);
const sitePolygon = await SitePolygonFactory.create();
const sitePolygon = await SitePolygonFactory.build();
mockQueryBuilder([sitePolygon]);
const result = await controller.findMany({});
expect(result.meta).not.toBe(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
IndicatorOutputTreeCountFactory,
IndicatorOutputTreeCoverFactory,
IndicatorOutputTreeCoverLossFactory,
POLYGON,
PolygonGeometryFactory,
ProjectFactory,
SiteFactory,
SitePolygonFactory,
Expand All @@ -18,6 +20,7 @@ import { Indicator, PolygonGeometry, SitePolygon, TreeSpecies } from "@terramatc
import { BadRequestException } from "@nestjs/common";
import { faker } from "@faker-js/faker";
import { DateTime } from "luxon";
import { IndicatorSlug } from "@terramatch-microservices/database/constants";

describe("SitePolygonsService", () => {
let service: SitePolygonsService;
Expand Down Expand Up @@ -117,8 +120,8 @@ describe("SitePolygonsService", () => {
expect(result.length).toBe(14);
});

it("Should throw when pageAfter polygon not found", () => {
expect(service.buildQuery(20, "asdfasdf")).rejects.toThrow(BadRequestException);
it("Should throw when pageAfter polygon not found", async () => {
await expect(service.buildQuery(20, "asdfasdf")).rejects.toThrow(BadRequestException);
});

it("Should return empty arrays from utility methods if no associated records exist", async () => {
Expand Down Expand Up @@ -266,4 +269,64 @@ describe("SitePolygonsService", () => {
expect(result.length).toBe(3);
expect(result.map(({ id }) => id).sort()).toEqual([poly1.id, poly2.id, poly3.id].sort());
});

it("throws when an indicator slug is invalid", async () => {
const query = await service.buildQuery(20);
expect(() => query.isMissingIndicators(["foo" as IndicatorSlug])).toThrow(BadRequestException);
});

it("filters polygons by boundary polygon", async () => {
await SitePolygon.truncate();
await PolygonGeometry.truncate();
const sitePoly1 = await SitePolygonFactory.create();
const poly2 = await PolygonGeometryFactory.create({
polygon: { ...POLYGON, coordinates: [POLYGON.coordinates[0].map(([lat, lng]) => [lat + 5, lng + 5])] }
});
const sitePoly2 = await SitePolygonFactory.create({ polygonUuid: poly2.uuid });

let query = await service.buildQuery(20);
await query.touchesBoundary(poly2.uuid);
let result = await query.execute();
expect(result.length).toBe(1);
expect(result[0].id).toBe(sitePoly2.id);

query = await service.buildQuery(20);
result = await query.execute();
expect(result.length).toBe(2);
expect(result.map(({ id }) => id).sort()).toEqual([sitePoly1.id, sitePoly2.id].sort());
});

it("throws when a boundary poly uuid doesn't exist", async () => {
const query = await service.buildQuery(20);
await expect(query.touchesBoundary("asdf")).rejects.toThrow(BadRequestException);
});

it("Can apply multiple filter types at once", async () => {
await SitePolygon.truncate();
const project1 = await ProjectFactory.create({ isTest: true });
const site1 = await SiteFactory.create({ projectId: project1.id });
const project2 = await ProjectFactory.create();
const site2 = await SiteFactory.create({ projectId: project2.id });
const draftPoly1 = await SitePolygonFactory.create({ siteUuid: site1.uuid, status: "draft" });
await IndicatorOutputHectaresFactory.create({
sitePolygonId: draftPoly1.id,
indicatorSlug: "restorationByStrategy"
});
const draftPoly2 = await SitePolygonFactory.create({ siteUuid: site2.uuid, status: "draft" });
await SitePolygonFactory.create({ siteUuid: site1.uuid, status: "approved" });
const approvedPoly2 = await SitePolygonFactory.create({ siteUuid: site2.uuid, status: "approved" });
await IndicatorOutputHectaresFactory.create({
sitePolygonId: approvedPoly2.id,
indicatorSlug: "restorationByStrategy"
});

const query = (await service.buildQuery(20))
.isMissingIndicators(["restorationByStrategy"])
.hasStatuses(["draft", "approved"]);
await query.filterProjectUuids([project2.uuid]);
await query.touchesBoundary(approvedPoly2.polygonUuid);
const result = await query.execute();
expect(result.length).toBe(1);
expect(result[0].id).toBe(draftPoly2.id);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PolygonGeometry } from "../entities";
import { UserFactory } from "./user.factory";

// The shortest polygon defined in the prod DB as of the writing of this test.
const POLYGON = {
export const POLYGON = {
type: "Polygon",
coordinates: [
[
Expand Down

0 comments on commit 40ede90

Please sign in to comment.