From 02ebeb478277a02435619ab61de86f8070d57c79 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Tue, 21 Jan 2025 15:19:06 -0800 Subject: [PATCH] use generator method for shuttles --- test/generators.ts | 10 ++++- ...UnoptimizedInMemoryRepositoryTests.test.ts | 39 +++++++------------ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/test/generators.ts b/test/generators.ts index 964c0e7..9d323b1 100644 --- a/test/generators.ts +++ b/test/generators.ts @@ -1,4 +1,4 @@ -import { ISystem } from "../src/entities/entities"; +import { IShuttle, ISystem } from "../src/entities/entities"; export function generateMockSystems(): ISystem[] { @@ -7,4 +7,12 @@ export function generateMockSystems(): ISystem[] { { id: "2", name: "System B" }, { 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 } }, + ]; } \ No newline at end of file diff --git a/test/repositories/UnoptimizedInMemoryRepositoryTests.test.ts b/test/repositories/UnoptimizedInMemoryRepositoryTests.test.ts index 8035d50..0cb20d8 100644 --- a/test/repositories/UnoptimizedInMemoryRepositoryTests.test.ts +++ b/test/repositories/UnoptimizedInMemoryRepositoryTests.test.ts @@ -1,7 +1,6 @@ import { beforeEach, describe, expect, test } from "@jest/globals"; import { UnoptimizedInMemoryRepository } from "../../src/repositories/UnoptimizedInMemoryRepository"; -import { generateMockSystems } from "../generators"; -import { mock } from "node:test"; +import { generateMockShuttles, generateMockSystems } from "../generators"; // For repositories created in the future, reuse core testing // logic from here and differentiate setup (e.g. creating mocks) @@ -129,11 +128,7 @@ describe("UnoptimizedInMemoryRepository", () => { }); 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 } }, - ]; + const mockShuttles = generateMockShuttles(); for (const shuttle of mockShuttles) { await repository.addOrUpdateShuttle(shuttle); } @@ -150,11 +145,7 @@ describe("UnoptimizedInMemoryRepository", () => { 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 } }, - ]; + const mockShuttles = generateMockShuttles(); for (const shuttle of mockShuttles) { await repository.addOrUpdateShuttle(shuttle); } @@ -171,15 +162,12 @@ describe("UnoptimizedInMemoryRepository", () => { 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 } }, - ]; + const mockShuttles = generateMockShuttles(); for (const shuttle of mockShuttles) { await repository.addOrUpdateShuttle(shuttle); } - const result = await repository.getShuttleById("2"); + const result = await repository.getShuttleById("sh2"); expect(result).toEqual(mockShuttles[1]); }); @@ -356,7 +344,8 @@ describe("UnoptimizedInMemoryRepository", () => { 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" }; + const mockShuttles = generateMockShuttles(); + const newShuttle = mockShuttles[0]; await repository.addOrUpdateShuttle(newShuttle); @@ -365,8 +354,10 @@ describe("UnoptimizedInMemoryRepository", () => { }); 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" }; + const mockShuttles = generateMockShuttles(); + const existingShuttle = mockShuttles[0]; + const updatedShuttle = structuredClone(existingShuttle); + updatedShuttle.name = "Updated Shuttle"; await repository.addOrUpdateShuttle(existingShuttle); await repository.addOrUpdateShuttle(updatedShuttle); @@ -469,10 +460,10 @@ describe("UnoptimizedInMemoryRepository", () => { 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); + const mockShuttles = generateMockShuttles(); + for (const shuttle of mockShuttles) { + await repository.addOrUpdateShuttle(shuttle); + } await repository.clearShuttleData();