use generator method for shuttles

This commit is contained in:
2025-01-21 15:19:06 -08:00
parent f71e589493
commit 02ebeb4782
2 changed files with 24 additions and 25 deletions

View File

@@ -1,4 +1,4 @@
import { ISystem } from "../src/entities/entities"; import { IShuttle, ISystem } from "../src/entities/entities";
export function generateMockSystems(): ISystem[] { export function generateMockSystems(): ISystem[] {
@@ -8,3 +8,11 @@ export function generateMockSystems(): ISystem[] {
{ id: "3", name: "System C" }, { id: "3", name: "System C" },
]; ];
} }
export function generateMockShuttles(): IShuttle[] {
return [
{ id: "sh1", name: "Shuttle A", routeId: "r1", systemId: "sys1", coordinates: { latitude: 10, longitude: 20 } },
{ id: "sh2", name: "Shuttle B", routeId: "r2", systemId: "sys2", coordinates: { latitude: 15, longitude: 25 } },
{ id: "sh3", name: "Shuttle C", routeId: "r3", systemId: "sys3", coordinates: { latitude: 30, longitude: 40 } },
];
}

View File

@@ -1,7 +1,6 @@
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 { generateMockSystems } from "../generators"; import { generateMockShuttles, generateMockSystems } from "../generators";
import { mock } from "node:test";
// For repositories created in the future, reuse core testing // For repositories created in the future, reuse core testing
// logic from here and differentiate setup (e.g. creating mocks) // logic from here and differentiate setup (e.g. creating mocks)
@@ -129,11 +128,7 @@ describe("UnoptimizedInMemoryRepository", () => {
}); });
describe("getShuttlesBySystemId", () => { describe("getShuttlesBySystemId", () => {
test("gets all shuttles for a specific system ID", async () => { test("gets all shuttles for a specific system ID", async () => {
const mockShuttles = [ const mockShuttles = generateMockShuttles();
{ 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) { for (const shuttle of mockShuttles) {
await repository.addOrUpdateShuttle(shuttle); await repository.addOrUpdateShuttle(shuttle);
} }
@@ -150,11 +145,7 @@ describe("UnoptimizedInMemoryRepository", () => {
describe("getShuttlesByRouteId", () => { describe("getShuttlesByRouteId", () => {
test("gets all shuttles for a specific route ID", async () => { test("gets all shuttles for a specific route ID", async () => {
const mockShuttles = [ const mockShuttles = generateMockShuttles();
{ 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) { for (const shuttle of mockShuttles) {
await repository.addOrUpdateShuttle(shuttle); await repository.addOrUpdateShuttle(shuttle);
} }
@@ -171,15 +162,12 @@ describe("UnoptimizedInMemoryRepository", () => {
describe("getShuttleById", () => { describe("getShuttleById", () => {
test("gets a shuttle by ID if it exists", async () => { test("gets a shuttle by ID if it exists", async () => {
const mockShuttles = [ const mockShuttles = generateMockShuttles();
{ 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) { for (const shuttle of mockShuttles) {
await repository.addOrUpdateShuttle(shuttle); await repository.addOrUpdateShuttle(shuttle);
} }
const result = await repository.getShuttleById("2"); const result = await repository.getShuttleById("sh2");
expect(result).toEqual(mockShuttles[1]); expect(result).toEqual(mockShuttles[1]);
}); });
@@ -356,7 +344,8 @@ describe("UnoptimizedInMemoryRepository", () => {
describe("addOrUpdateShuttle", () => { describe("addOrUpdateShuttle", () => {
test("adds a new shuttle if nonexistent", async () => { test("adds a new shuttle if nonexistent", async () => {
const newShuttle = { id: "shuttle1", name: "Shuttle A", coordinates: { latitude: 10, longitude: 20 }, routeId: "route1", systemId: "sys1" }; const mockShuttles = generateMockShuttles();
const newShuttle = mockShuttles[0];
await repository.addOrUpdateShuttle(newShuttle); await repository.addOrUpdateShuttle(newShuttle);
@@ -365,8 +354,10 @@ describe("UnoptimizedInMemoryRepository", () => {
}); });
test("updates an existing shuttle if it exists", async () => { 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 mockShuttles = generateMockShuttles();
const updatedShuttle = { id: "shuttle1", name: "Updated Shuttle A", coordinates: { latitude: 30, longitude: 40 }, routeId: "route1", systemId: "sys1" }; const existingShuttle = mockShuttles[0];
const updatedShuttle = structuredClone(existingShuttle);
updatedShuttle.name = "Updated Shuttle";
await repository.addOrUpdateShuttle(existingShuttle); await repository.addOrUpdateShuttle(existingShuttle);
await repository.addOrUpdateShuttle(updatedShuttle); await repository.addOrUpdateShuttle(updatedShuttle);
@@ -469,10 +460,10 @@ describe("UnoptimizedInMemoryRepository", () => {
describe("clearShuttleData", () => { describe("clearShuttleData", () => {
test("clears all shuttles from the repository", async () => { 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 mockShuttles = generateMockShuttles();
const shuttle2 = { id: "shuttle2", name: "Shuttle B", systemId: "sys2", routeId: "route2", coordinates: { latitude: 15, longitude: 25 } }; for (const shuttle of mockShuttles) {
await repository.addOrUpdateShuttle(shuttle1); await repository.addOrUpdateShuttle(shuttle);
await repository.addOrUpdateShuttle(shuttle2); }
await repository.clearShuttleData(); await repository.clearShuttleData();