Files
project-inter-server/test/repositories/UnoptimizedInMemoryRepositoryTests.test.ts

543 lines
21 KiB
TypeScript

import { beforeEach, describe, expect, test } from "@jest/globals";
import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository";
import { generateMockSystems } from "../generators";
import { mock } from "node:test";
// For repositories created in the future, reuse core testing
// logic from here and differentiate setup (e.g. creating mocks)
// Do this by creating a function which takes a GetterRepository
// or GetterSetterRepository instance
// Full disclosure: most of this was generated by ChatGPT
// https://chatgpt.com/share/67902072-3adc-800d-bd22-06f7b3e6f4a4
describe("UnoptimizedInMemoryRepository", () => {
let repository: UnoptimizedInMemoryRepository;
beforeEach(() => {
repository = new UnoptimizedInMemoryRepository();
});
describe("getSystems", () => {
test("gets the systems stored in the repository", async () => {
const mockSystems = generateMockSystems();
for (const system of mockSystems) {
await repository.addOrUpdateSystem(system);
}
const result = await repository.getSystems();
expect(result).toEqual(mockSystems);
});
test("gets an empty list if there are no systems stored", async () => {
const result = await repository.getSystems();
expect(result).toEqual([]);
});
});
describe("getSystemById", () => {
test("gets a system by the ID if it exists", async () => {
const mockSystems = generateMockSystems();
for (const system of mockSystems) {
await repository.addOrUpdateSystem(system);
}
const result = await repository.getSystemById("2");
expect(result).toEqual(mockSystems[1]); // Ensure it retrieves the correct system
});
test("returns null if the system doesn't exist", async () => {
const result = await repository.getSystemById("nonexistent-id");
expect(result).toBeNull();
});
});
describe("getStopsBySystemId", () => {
test("gets stops by system ID", async () => {
const mockStops = [
{ id: "1", name: "Stop A", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 } },
{ id: "2", name: "Stop B", systemId: "sys1", coordinates: { latitude: 15, longitude: 25 } },
{ id: "3", name: "Stop C", systemId: "sys2", coordinates: { latitude: 30, longitude: 40 } },
];
for (const stop of mockStops) {
await repository.addOrUpdateStop(stop);
}
const result = await repository.getStopsBySystemId("sys1");
expect(result).toEqual(mockStops.filter((stop) => stop.systemId === "sys1"));
});
test("returns an empty list if there are no stops for the given system ID", async () => {
const result = await repository.getStopsBySystemId("nonexistent-system");
expect(result).toEqual([]);
});
});
describe("getStopById", () => {
test("gets a stop by ID if it exists", async () => {
const mockStop = { id: "st1", name: "Stop A", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 } };
await repository.addOrUpdateStop(mockStop);
const result = await repository.getStopById("st1");
expect(result).toEqual(mockStop);
});
test("returns null if the stop does not exist", async () => {
const result = await repository.getStopById("nonexistent-stop");
expect(result).toBeNull();
});
});
describe("getRoutesBySystemId", () => {
test("gets all routes for a specific system ID", async () => {
const mockRoutes = [
{ id: "r1", name: "Route 1", color: "red", systemId: "sys1", polylineCoordinates: [] },
{ id: "r2", name: "Route 2", color: "blue", systemId: "sys1", polylineCoordinates: [] },
{ id: "r3", name: "Route 3", color: "green", systemId: "sys2", polylineCoordinates: [] },
];
for (const route of mockRoutes) {
await repository.addOrUpdateRoute(route);
}
const result = await repository.getRoutesBySystemId("sys1");
expect(result).toEqual(mockRoutes.filter((route) => route.systemId === "sys1"));
});
test("returns an empty list if there are no routes for the system ID", async () => {
const result = await repository.getRoutesBySystemId("nonexistent-system");
expect(result).toEqual([]);
});
});
describe("getRouteById", () => {
test("gets a route by ID if it exists", async () => {
const mockRoute = { id: "r1", name: "Route 1", color: "red", systemId: "sys1", polylineCoordinates: [] };
await repository.addOrUpdateRoute(mockRoute);
const result = await repository.getRouteById("r1");
expect(result).toEqual(mockRoute);
});
test("returns null if the route does not exist", async () => {
const result = await repository.getRouteById("nonexistent-route");
expect(result).toBeNull();
});
});
describe("getShuttlesBySystemId", () => {
test("gets all shuttles for a specific system ID", async () => {
const mockShuttles = [
{ id: "sh1", name: "Shuttle A", routeId: "r1", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 } },
{ id: "sh2", name: "Shuttle B", routeId: "r2", systemId: "sys1", coordinates: { latitude: 15, longitude: 25 } },
{ id: "sh3", name: "Shuttle C", routeId: "r3", systemId: "sys2", coordinates: { latitude: 30, longitude: 40 } },
];
for (const shuttle of mockShuttles) {
await repository.addOrUpdateShuttle(shuttle);
}
const result = await repository.getShuttlesBySystemId("sys1");
expect(result).toEqual(mockShuttles.filter((sh) => sh.systemId === "sys1"));
});
test("returns an empty list if there are no shuttles for the system ID", async () => {
const result = await repository.getShuttlesBySystemId("nonexistent-system");
expect(result).toEqual([]);
});
});
describe("getShuttlesByRouteId", () => {
test("gets all shuttles for a specific route ID", async () => {
const mockShuttles = [
{ id: "sh1", name: "Shuttle A", routeId: "r1", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 } },
{ id: "sh2", name: "Shuttle B", routeId: "r1", systemId: "sys1", coordinates: { latitude: 15, longitude: 25 } },
{ id: "sh3", name: "Shuttle C", routeId: "r2", systemId: "sys2", coordinates: { latitude: 30, longitude: 40 } },
];
for (const shuttle of mockShuttles) {
await repository.addOrUpdateShuttle(shuttle);
}
const result = await repository.getShuttlesByRouteId("r1");
expect(result).toEqual(mockShuttles.filter((sh) => sh.routeId === "r1"));
});
test("returns an empty list if there are no shuttles for the route ID", async () => {
const result = await repository.getShuttlesByRouteId("nonexistent-route");
expect(result).toEqual([]);
});
});
describe("getShuttleById", () => {
test("gets a shuttle by ID if it exists", async () => {
const mockShuttles = [
{ id: "1", name: "Shuttle A", routeId: "r1", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 } },
{ id: "2", name: "Shuttle B", routeId: "r2", systemId: "sys1", coordinates: { latitude: 15, longitude: 25 } },
];
for (const shuttle of mockShuttles) {
await repository.addOrUpdateShuttle(shuttle);
}
const result = await repository.getShuttleById("2");
expect(result).toEqual(mockShuttles[1]);
});
test("returns null if the shuttle doesn't exist", async () => {
const result = await repository.getShuttleById("nonexistent-id");
expect(result).toBeNull();
});
});
describe("getEtasForShuttleId", () => {
test("gets ETAs for a specific shuttle ID", async () => {
const mockEtas = [
{ shuttleId: "sh1", stopId: "st1", secondsRemaining: 120 },
{ shuttleId: "sh1", stopId: "st2", secondsRemaining: 180 },
{ shuttleId: "sh2", stopId: "st3", secondsRemaining: 240 },
];
for (const eta of mockEtas) {
await repository.addOrUpdateEta(eta);
}
const result = await repository.getEtasForShuttleId("sh1");
expect(result).toEqual(mockEtas.filter((eta) => eta.shuttleId === "sh1"));
});
test("returns an empty list if there are no ETAs for the shuttle ID", async () => {
const result = await repository.getEtasForShuttleId("nonexistent-shuttle");
expect(result).toEqual([]);
});
});
describe("getEtasForStopId", () => {
test("gets ETAs for a specific stop ID", async () => {
const mockEtas = [
{ shuttleId: "s1", stopId: "st1", secondsRemaining: 120 },
{ shuttleId: "s2", stopId: "st1", secondsRemaining: 180 },
{ shuttleId: "s3", stopId: "st2", secondsRemaining: 240 },
];
for (const eta of mockEtas) {
await repository.addOrUpdateEta(eta);
}
const result = await repository.getEtasForStopId("st1");
expect(result).toEqual(mockEtas.filter((eta) => eta.stopId === "st1"));
});
test("returns an empty list if there are no ETAs for the stop ID", async () => {
const result = await repository.getEtasForStopId("nonexistent-stop");
expect(result).toEqual([]);
});
});
describe("getEtaForShuttleAndStopId", () => {
test("gets a single ETA for a specific shuttle and stop ID", async () => {
const mockEta = { shuttleId: "sh1", stopId: "st1", secondsRemaining: 120 };
await repository.addOrUpdateEta(mockEta);
const result = await repository.getEtaForShuttleAndStopId("sh1", "st1");
expect(result).toEqual(mockEta);
});
test("returns null if no ETA matches the shuttle and stop ID", async () => {
const result = await repository.getEtaForShuttleAndStopId("nonexistent-shuttle", "nonexistent-stop");
expect(result).toBeNull();
});
});
describe("getOrderedStopByRouteAndStopId", () => {
test("gets an ordered stop by route ID and stop ID", async () => {
const mockOrderedStop = {
routeId: "r1",
stopId: "st1",
position: 1,
};
await repository.addOrUpdateOrderedStop(mockOrderedStop);
const result = await repository.getOrderedStopByRouteAndStopId("r1", "st1");
expect(result).toEqual(mockOrderedStop);
});
test("returns null if no ordered stop matches the given route ID and stop ID", async () => {
const result = await repository.getOrderedStopByRouteAndStopId("nonexistent-route", "nonexistent-stop");
expect(result).toBeNull();
});
});
describe("getOrderedStopsByStopId", () => {
test("gets all ordered stops for a specific stop ID", async () => {
const mockOrderedStops = [
{ stopId: "st1", routeId: "r1", position: 1 },
{ stopId: "st1", routeId: "r2", position: 2 },
{ stopId: "st2", routeId: "r3", position: 3 },
];
for (const orderedStop of mockOrderedStops) {
await repository.addOrUpdateOrderedStop(orderedStop);
}
const result = await repository.getOrderedStopsByStopId("st1");
expect(result).toEqual(mockOrderedStops.filter((os) => os.stopId === "st1"));
});
test("returns an empty list if there are no ordered stops for the stop ID", async () => {
const result = await repository.getOrderedStopsByStopId("nonexistent-stop");
expect(result).toEqual([]);
});
});
describe("getOrderedStopsByRouteId", () => {
test("gets all ordered stops for a specific route ID", async () => {
const mockOrderedStops = [
{ stopId: "st1", routeId: "r1", position: 1 },
{ stopId: "st2", routeId: "r1", position: 2 },
{ stopId: "st3", routeId: "r2", position: 3 },
];
for (const orderedStop of mockOrderedStops) {
await repository.addOrUpdateOrderedStop(orderedStop);
}
const result = await repository.getOrderedStopsByRouteId("r1");
expect(result).toEqual(mockOrderedStops.filter((os) => os.routeId === "r1"));
});
test("returns an empty list if there are no ordered stops for the route ID", async () => {
const result = await repository.getOrderedStopsByRouteId("nonexistent-route");
expect(result).toEqual([]);
});
});
describe("addOrUpdateSystem", () => {
test("adds a new system if nonexistent", async () => {
const mockSystems = generateMockSystems();
const newSystem = mockSystems[0];
await repository.addOrUpdateSystem(newSystem);
const result = await repository.getSystems();
expect(result).toEqual([newSystem]);
});
test("updates an existing system if it exists", async () => {
const mockSystems = generateMockSystems();
const existingSystem = mockSystems[0];
const updatedSystem = structuredClone(existingSystem);
updatedSystem.name = "Updated System";
await repository.addOrUpdateSystem(existingSystem);
await repository.addOrUpdateSystem(updatedSystem);
const result = await repository.getSystems();
expect(result).toEqual([updatedSystem]);
});
});
describe("addOrUpdateRoute", () => {
test("adds a new route if nonexistent", async () => {
const newRoute = { id: "route1", name: "Route 1", color: "red", systemId: "sys1", polylineCoordinates: [] };
await repository.addOrUpdateRoute(newRoute);
const result = await repository.getRoutesBySystemId("sys1");
expect(result).toEqual([newRoute]);
});
test("updates an existing route if it exists", async () => {
const existingRoute = { id: "route1", name: "Route 1", color: "red", systemId: "sys1", polylineCoordinates: [] };
const updatedRoute = { id: "route1", name: "Updated Route 1", color: "blue", systemId: "sys1", polylineCoordinates: [] };
await repository.addOrUpdateRoute(existingRoute);
await repository.addOrUpdateRoute(updatedRoute);
const result = await repository.getRoutesBySystemId("sys1");
expect(result).toEqual([updatedRoute]);
});
});
describe("addOrUpdateShuttle", () => {
test("adds a new shuttle if nonexistent", async () => {
const newShuttle = { id: "shuttle1", name: "Shuttle A", coordinates: { latitude: 10, longitude: 20 }, routeId: "route1", systemId: "sys1" };
await repository.addOrUpdateShuttle(newShuttle);
const result = await repository.getShuttlesBySystemId("sys1");
expect(result).toEqual([newShuttle]);
});
test("updates an existing shuttle if it exists", async () => {
const existingShuttle = { id: "shuttle1", name: "Shuttle A", coordinates: { latitude: 10, longitude: 20 }, routeId: "route1", systemId: "sys1" };
const updatedShuttle = { id: "shuttle1", name: "Updated Shuttle A", coordinates: { latitude: 30, longitude: 40 }, routeId: "route1", systemId: "sys1" };
await repository.addOrUpdateShuttle(existingShuttle);
await repository.addOrUpdateShuttle(updatedShuttle);
const result = await repository.getShuttlesBySystemId("sys1");
expect(result).toEqual([updatedShuttle]);
});
});
describe("addOrUpdateStop", () => {
test("adds a new stop if nonexistent", async () => {
const newStop = { id: "stop1", name: "Stop A", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 } };
await repository.addOrUpdateStop(newStop);
const result = await repository.getStopsBySystemId("sys1");
expect(result).toEqual([newStop]);
});
test("updates an existing stop if it exists", async () => {
const existingStop = { id: "stop1", name: "Stop A", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 } };
const updatedStop = { id: "stop1", name: "Updated Stop A", systemId: "sys1", coordinates: { latitude: 30, longitude: 40 } };
await repository.addOrUpdateStop(existingStop);
await repository.addOrUpdateStop(updatedStop);
const result = await repository.getStopsBySystemId("sys1");
expect(result).toEqual([updatedStop]);
});
});
describe("addOrUpdateOrderedStop", () => {
test("adds a new ordered stop if nonexistent", async () => {
const newOrderedStop = { routeId: "route1", stopId: "stop1", position: 1 };
await repository.addOrUpdateOrderedStop(newOrderedStop);
const result = await repository.getOrderedStopsByRouteId("route1");
expect(result).toEqual([newOrderedStop]);
});
test("updates an existing ordered stop if it exists", async () => {
const existingOrderedStop = { routeId: "route1", stopId: "stop1", position: 1 };
const updatedOrderedStop = { routeId: "route1", stopId: "stop1", position: 2 };
await repository.addOrUpdateOrderedStop(existingOrderedStop);
await repository.addOrUpdateOrderedStop(updatedOrderedStop);
const result = await repository.getOrderedStopsByRouteId("route1");
expect(result).toEqual([updatedOrderedStop]);
});
});
describe("addOrUpdateEta", () => {
test("adds a new ETA if nonexistent", async () => {
const newEta = { shuttleId: "shuttle1", stopId: "stop1", secondsRemaining: 120 };
await repository.addOrUpdateEta(newEta);
const result = await repository.getEtasForShuttleId("shuttle1");
expect(result).toEqual([newEta]);
});
test("updates an existing ETA if it exists", async () => {
const existingEta = { shuttleId: "shuttle1", stopId: "stop1", secondsRemaining: 120 };
const updatedEta = { shuttleId: "shuttle1", stopId: "stop1", secondsRemaining: 60 };
await repository.addOrUpdateEta(existingEta);
await repository.addOrUpdateEta(updatedEta);
const result = await repository.getEtasForShuttleId("shuttle1");
expect(result).toEqual([updatedEta]);
});
});
describe("clearSystemData", () => {
test("clears all systems from the repository", async () => {
const mockSystems = generateMockSystems();
for (const system of mockSystems) {
await repository.addOrUpdateSystem(system);
}
await repository.clearSystemData();
const result = await repository.getSystems();
expect(result).toEqual([]);
});
test("clears system data when the repository has data", async () => {
const mockSystems = generateMockSystems();
const system = mockSystems[0];
await repository.addOrUpdateSystem(system);
await repository.clearSystemData();
const result = await repository.getSystems();
expect(result).toEqual([]);
});
});
describe("clearShuttleData", () => {
test("clears all shuttles from the repository", async () => {
const shuttle1 = { id: "shuttle1", name: "Shuttle A", systemId: "sys1", routeId: "route1", coordinates: { latitude: 10, longitude: 20 } };
const shuttle2 = { id: "shuttle2", name: "Shuttle B", systemId: "sys2", routeId: "route2", coordinates: { latitude: 15, longitude: 25 } };
await repository.addOrUpdateShuttle(shuttle1);
await repository.addOrUpdateShuttle(shuttle2);
await repository.clearShuttleData();
const result = await repository.getShuttlesBySystemId("sys1");
expect(result).toEqual([]);
});
});
describe("clearEtaData", () => {
test("clears all ETAs from the repository", async () => {
const eta1 = { shuttleId: "shuttle1", stopId: "stop1", secondsRemaining: 120 };
const eta2 = { shuttleId: "shuttle2", stopId: "stop2", secondsRemaining: 150 };
await repository.addOrUpdateEta(eta1);
await repository.addOrUpdateEta(eta2);
await repository.clearEtaData();
const result = await repository.getEtasForShuttleId("shuttle1");
expect(result).toEqual([]);
});
});
describe("clearOrderedStopData", () => {
test("clears all ordered stops from the repository", async () => {
const orderedStop1 = { routeId: "route1", stopId: "stop1", position: 1 };
const orderedStop2 = { routeId: "route2", stopId: "stop2", position: 2 };
await repository.addOrUpdateOrderedStop(orderedStop1);
await repository.addOrUpdateOrderedStop(orderedStop2);
await repository.clearOrderedStopData();
const result = await repository.getOrderedStopsByRouteId("route1");
expect(result).toEqual([]);
});
});
describe("clearRouteData", () => {
test("clears all routes from the repository", async () => {
const route1 = { id: "route1", name: "Route 1", systemId: "sys1", color: "#ffffff", polylineCoordinates: [] };
const route2 = { id: "route2", name: "Route 2", systemId: "sys2", color: "#ffffff", polylineCoordinates: [] };
await repository.addOrUpdateRoute(route1);
await repository.addOrUpdateRoute(route2);
await repository.clearRouteData();
const result = await repository.getRoutesBySystemId("sys1");
expect(result).toEqual([]);
});
});
describe("clearStopData", () => {
test("clears all stops from the repository", async () => {
const stop1 = { id: "stop1", name: "Stop A", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 } };
const stop2 = { id: "stop2", name: "Stop B", systemId: "sys2", coordinates: { latitude: 10, longitude: 20 } };
await repository.addOrUpdateStop(stop1);
await repository.addOrUpdateStop(stop2);
await repository.clearStopData();
const result = await repository.getStopsBySystemId("sys1");
expect(result).toEqual([]);
});
});
});