update remove implementation and tests

This commit is contained in:
2025-04-06 09:55:23 -07:00
parent 695fec1fce
commit a144657fa8
2 changed files with 18 additions and 48 deletions

View File

@@ -20,10 +20,6 @@ export class UnoptimizedInMemoryShuttleRepository implements ShuttleGetterSetter
return this.system; return this.system;
} }
public async getSystemById(systemId: string) {
return this.findEntityById(systemId, this.systems);
}
public async getStopsBySystemId(systemId: string) { public async getStopsBySystemId(systemId: string) {
return this.stops.filter(stop => stop.systemId === systemId); return this.stops.filter(stop => stop.systemId === systemId);
} }
@@ -100,12 +96,7 @@ export class UnoptimizedInMemoryShuttleRepository implements ShuttleGetterSetter
} }
public async updateSystem(system: ISystem): Promise<void> { public async updateSystem(system: ISystem): Promise<void> {
const index = this.systems.findIndex((s) => s.id === system.id); this.system = system;
if (index !== -1) {
this.systems[index] = system; // Update existing
} else {
this.systems.push(system); // Add new
}
} }
public async addOrUpdateRoute(route: IRoute): Promise<void> { public async addOrUpdateRoute(route: IRoute): Promise<void> {
@@ -175,8 +166,14 @@ export class UnoptimizedInMemoryShuttleRepository implements ShuttleGetterSetter
return await this.removeEntityByMatcherIfExists((value) => value.id === entityId, arrayToSearchIn); return await this.removeEntityByMatcherIfExists((value) => value.id === entityId, arrayToSearchIn);
} }
public async removeSystemIfExists(systemId: string): Promise<ISystem | null> { public async removeSystemIfExists(): Promise<ISystem | null> {
return await this.removeEntityByIdIfExists(systemId, this.systems); if (this.system !== null) {
const system = this.system;
this.system = null;
return system;
}
return null;
} }
public async removeRouteIfExists(routeId: string): Promise<IRoute | null> { public async removeRouteIfExists(routeId: string): Promise<IRoute | null> {
@@ -206,7 +203,7 @@ export class UnoptimizedInMemoryShuttleRepository implements ShuttleGetterSetter
} }
public async clearSystemData() { public async clearSystemData() {
this.systems = []; this.system = null;
} }
public async clearShuttleData(): Promise<void> { public async clearShuttleData(): Promise<void> {

View File

@@ -21,7 +21,7 @@ describe("UnoptimizedInMemoryRepository", () => {
repository = new UnoptimizedInMemoryShuttleRepository(); repository = new UnoptimizedInMemoryShuttleRepository();
}); });
describe("getSystems", () => { describe("getSystem", () => {
test("gets the system stored in the repository", async () => { test("gets the system stored in the repository", async () => {
const mockSystems = generateMockSystems(); const mockSystems = generateMockSystems();
await repository.updateSystem(mockSystems[0]); await repository.updateSystem(mockSystems[0]);
@@ -38,25 +38,6 @@ describe("UnoptimizedInMemoryRepository", () => {
}); });
}); });
describe("getSystemById", () => {
test("gets a system by the ID if it exists", async () => {
const mockSystems = generateMockSystems();
for (const system of mockSystems) {
await repository.updateSystem(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", () => { describe("getStopsBySystemId", () => {
test("gets stops by system ID", async () => { test("gets stops by system ID", async () => {
const mockStops = generateMockStops(); const mockStops = generateMockStops();
@@ -475,27 +456,19 @@ describe("UnoptimizedInMemoryRepository", () => {
describe("removeSystemIfExists", () => { describe("removeSystemIfExists", () => {
test("removes system given ID", async () => { test("removes system given ID", async () => {
const mockSystems = generateMockSystems(); const mockSystems = generateMockSystems();
await Promise.all(mockSystems.map(async (system) => { await repository.updateSystem(mockSystems[0]);
await repository.updateSystem(system);
}));
const systemToRemove = mockSystems[0]; await repository.removeSystemIfExists();
await repository.removeSystemIfExists(systemToRemove.id);
const remainingSystems = await repository.getSystemIfExists(); const expectedSystem = await repository.getSystemIfExists();
expect(remainingSystems).toHaveLength(mockSystems.length - 1); expect(expectedSystem).toBeNull();
}); });
test("does nothing if system doesn't exist", async () => { test("does nothing if system doesn't exist", async () => {
const mockSystems = generateMockSystems(); await repository.removeSystemIfExists();
await Promise.all(mockSystems.map(async (system) => {
await repository.updateSystem(system);
}));
await repository.removeSystemIfExists("nonexistent-id"); const expectedSystem = await repository.getSystemIfExists();
expect(expectedSystem).toBeNull();
const remainingSystems = await repository.getSystemIfExists();
expect(remainingSystems).toHaveLength(mockSystems.length);
}); });
}); });