mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
143 lines
5.1 KiB
TypeScript
143 lines
5.1 KiB
TypeScript
import { beforeEach, describe, expect, test } from "@jest/globals";
|
|
import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository";
|
|
|
|
// 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/67901f14-c4ac-800d-8bcd-d2e622c522bf
|
|
|
|
describe("UnoptimizedInMemoryRepository", () => {
|
|
let repository: UnoptimizedInMemoryRepository;
|
|
|
|
beforeEach(() => {
|
|
repository = new UnoptimizedInMemoryRepository();
|
|
});
|
|
|
|
describe("getSystems", () => {
|
|
test("gets the systems stored in the repository", async () => {
|
|
const mockSystems = [
|
|
{ id: "1", name: "System A" },
|
|
{ id: "2", name: "System B" },
|
|
];
|
|
await repository.addOrUpdateSystem(mockSystems[0]);
|
|
await repository.addOrUpdateSystem(mockSystems[1]);
|
|
|
|
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 = [
|
|
{ id: "1", name: "System A" },
|
|
{ id: "2", name: "System B" },
|
|
{ id: "3", name: "System C" },
|
|
];
|
|
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("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("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("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();
|
|
});
|
|
});
|
|
}); |