do the same with eta data

This commit is contained in:
2025-01-21 15:37:28 -08:00
parent d617d22b6d
commit 82a69ca464
2 changed files with 26 additions and 22 deletions

View File

@@ -1,4 +1,4 @@
import { IOrderedStop, IRoute, IShuttle, IStop, ISystem } from "../src/entities/entities"; import { IEta, IOrderedStop, IRoute, IShuttle, IStop, ISystem } from "../src/entities/entities";
// Use a single set of generators in case any of the // Use a single set of generators in case any of the
// interfaces change in the future // interfaces change in the future
@@ -42,4 +42,12 @@ export function generateMockOrderedStops(): IOrderedStop[] {
{ stopId: "st2", routeId: "r1", position: 3 }, { stopId: "st2", routeId: "r1", position: 3 },
{ stopId: "st2", routeId: "r2", position: 4 }, { stopId: "st2", routeId: "r2", position: 4 },
]; ];
}
export function generateMockEtas(): IEta[] {
return [
{ shuttleId: "sh1", stopId: "st1", secondsRemaining: 120 },
{ shuttleId: "sh1", stopId: "st2", secondsRemaining: 180 },
{ shuttleId: "sh2", stopId: "st3", secondsRemaining: 240 },
];
} }

View File

@@ -1,6 +1,7 @@
import { beforeEach, describe, expect, test } from "@jest/globals"; import { beforeEach, describe, expect, test } from "@jest/globals";
import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository"; import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository";
import { import {
generateMockEtas,
generateMockOrderedStops, generateMockOrderedStops,
generateMockRoutes, generateMockRoutes,
generateMockShuttles, generateMockShuttles,
@@ -176,11 +177,7 @@ describe("UnoptimizedInMemoryRepository", () => {
describe("getEtasForShuttleId", () => { describe("getEtasForShuttleId", () => {
test("gets ETAs for a specific shuttle ID", async () => { test("gets ETAs for a specific shuttle ID", async () => {
const mockEtas = [ const mockEtas = generateMockEtas();
{ shuttleId: "sh1", stopId: "st1", secondsRemaining: 120 },
{ shuttleId: "sh1", stopId: "st2", secondsRemaining: 180 },
{ shuttleId: "sh2", stopId: "st3", secondsRemaining: 240 },
];
for (const eta of mockEtas) { for (const eta of mockEtas) {
await repository.addOrUpdateEta(eta); await repository.addOrUpdateEta(eta);
} }
@@ -197,11 +194,7 @@ describe("UnoptimizedInMemoryRepository", () => {
describe("getEtasForStopId", () => { describe("getEtasForStopId", () => {
test("gets ETAs for a specific stop ID", async () => { test("gets ETAs for a specific stop ID", async () => {
const mockEtas = [ const mockEtas = generateMockEtas();
{ shuttleId: "s1", stopId: "st1", secondsRemaining: 120 },
{ shuttleId: "s2", stopId: "st1", secondsRemaining: 180 },
{ shuttleId: "s3", stopId: "st2", secondsRemaining: 240 },
];
for (const eta of mockEtas) { for (const eta of mockEtas) {
await repository.addOrUpdateEta(eta); await repository.addOrUpdateEta(eta);
} }
@@ -218,7 +211,8 @@ describe("UnoptimizedInMemoryRepository", () => {
describe("getEtaForShuttleAndStopId", () => { describe("getEtaForShuttleAndStopId", () => {
test("gets a single ETA for a specific shuttle and stop ID", async () => { test("gets a single ETA for a specific shuttle and stop ID", async () => {
const mockEta = { shuttleId: "sh1", stopId: "st1", secondsRemaining: 120 }; const mockEtas = generateMockEtas();
const mockEta = mockEtas[0];
await repository.addOrUpdateEta(mockEta); await repository.addOrUpdateEta(mockEta);
const result = await repository.getEtaForShuttleAndStopId("sh1", "st1"); const result = await repository.getEtaForShuttleAndStopId("sh1", "st1");
@@ -412,22 +406,25 @@ describe("UnoptimizedInMemoryRepository", () => {
describe("addOrUpdateEta", () => { describe("addOrUpdateEta", () => {
test("adds a new ETA if nonexistent", async () => { test("adds a new ETA if nonexistent", async () => {
const newEta = { shuttleId: "shuttle1", stopId: "stop1", secondsRemaining: 120 }; const mockEtas = generateMockEtas();
const newEta = mockEtas[0];
await repository.addOrUpdateEta(newEta); await repository.addOrUpdateEta(newEta);
const result = await repository.getEtasForShuttleId("shuttle1"); const result = await repository.getEtasForShuttleId(newEta.shuttleId);
expect(result).toEqual([newEta]); expect(result).toEqual([newEta]);
}); });
test("updates an existing ETA if it exists", async () => { test("updates an existing ETA if it exists", async () => {
const existingEta = { shuttleId: "shuttle1", stopId: "stop1", secondsRemaining: 120 }; const mockEtas = generateMockEtas();
const updatedEta = { shuttleId: "shuttle1", stopId: "stop1", secondsRemaining: 60 }; const existingEta = mockEtas[0];
const updatedEta = structuredClone(existingEta);
updatedEta.secondsRemaining = existingEta.secondsRemaining + 60;
await repository.addOrUpdateEta(existingEta); await repository.addOrUpdateEta(existingEta);
await repository.addOrUpdateEta(updatedEta); await repository.addOrUpdateEta(updatedEta);
const result = await repository.getEtasForShuttleId("shuttle1"); const result = await repository.getEtasForShuttleId(existingEta.shuttleId);
expect(result).toEqual([updatedEta]); expect(result).toEqual([updatedEta]);
}); });
}); });
@@ -473,11 +470,10 @@ describe("UnoptimizedInMemoryRepository", () => {
describe("clearEtaData", () => { describe("clearEtaData", () => {
test("clears all ETAs from the repository", async () => { test("clears all ETAs from the repository", async () => {
const eta1 = { shuttleId: "shuttle1", stopId: "stop1", secondsRemaining: 120 }; const mockEtas = generateMockEtas();
const eta2 = { shuttleId: "shuttle2", stopId: "stop2", secondsRemaining: 150 }; for (const eta of mockEtas) {
await repository.addOrUpdateEta(eta);
await repository.addOrUpdateEta(eta1); }
await repository.addOrUpdateEta(eta2);
await repository.clearEtaData(); await repository.clearEtaData();