diff --git a/src/loaders/ApiBasedShuttleRepositoryLoader.ts b/src/loaders/ApiBasedShuttleRepositoryLoader.ts index b8a6285..30f033e 100644 --- a/src/loaders/ApiBasedShuttleRepositoryLoader.ts +++ b/src/loaders/ApiBasedShuttleRepositoryLoader.ts @@ -39,7 +39,7 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader const query = new URLSearchParams(params).toString(); const systemIds = await this.constructExistingEntityIdSet(async () => { - return await this.repository.getSystems(); + return await this.repository.getSystemIfExists(); }) try { @@ -76,7 +76,7 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader } public async fetchAndUpdateRouteDataForExistingSystemsInRepository() { - const systems = await this.repository.getSystems(); + const systems = await this.repository.getSystemIfExists(); await Promise.all(systems.map(async (system) => { await this.fetchAndUpdateRouteDataForSystemId(system.id); })); @@ -132,7 +132,7 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader } public async fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemsInRepository() { - const systems = await this.repository.getSystems(); + const systems = await this.repository.getSystemIfExists(); await Promise.all(systems.map(async (system: ISystem) => { await this.fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId(system.id); })); @@ -178,7 +178,7 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader } public async fetchAndUpdateShuttleDataForExistingSystemsInRepository() { - const systems = await this.repository.getSystems(); + const systems = await this.repository.getSystemIfExists(); await Promise.all(systems.map(async (system: ISystem) => { const systemId = system.id; await this.fetchAndUpdateShuttleDataForSystemId(systemId); @@ -244,7 +244,7 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader } public async fetchAndUpdateEtaDataForExistingStopsForSystemsInRepository() { - const systems = await this.repository.getSystems() + const systems = await this.repository.getSystemIfExists() await Promise.all(systems.map(async (system: ISystem) => { const systemId = system.id; await this.fetchAndUpdateEtaDataForExistingStopsForSystemId(systemId); diff --git a/src/repositories/ShuttleGetterRepository.ts b/src/repositories/ShuttleGetterRepository.ts index 664328f..28a9de2 100644 --- a/src/repositories/ShuttleGetterRepository.ts +++ b/src/repositories/ShuttleGetterRepository.ts @@ -1,7 +1,7 @@ import { IEta, IOrderedStop, IRoute, IShuttle, IStop, ISystem } from "../entities/entities"; export interface ShuttleGetterRepository { - getSystems(): Promise; + getSystemIfExists(): Promise; getSystemById(systemId: string): Promise; getStopsBySystemId(systemId: string): Promise; diff --git a/src/repositories/UnoptimizedInMemoryShuttleRepository.ts b/src/repositories/UnoptimizedInMemoryShuttleRepository.ts index 59afac9..d807556 100644 --- a/src/repositories/UnoptimizedInMemoryShuttleRepository.ts +++ b/src/repositories/UnoptimizedInMemoryShuttleRepository.ts @@ -16,7 +16,7 @@ export class UnoptimizedInMemoryShuttleRepository implements ShuttleGetterSetter private subscribers: ((eta: IEta) => void)[] = []; - public async getSystems() { + public async getSystemIfExists() { return this.systems; } diff --git a/src/resolvers/QueryResolvers.ts b/src/resolvers/QueryResolvers.ts index 06df1a9..1b316c8 100644 --- a/src/resolvers/QueryResolvers.ts +++ b/src/resolvers/QueryResolvers.ts @@ -4,7 +4,7 @@ import { Resolvers } from "../generated/graphql"; export const QueryResolvers: Resolvers = { Query: { systems: async (_parent, args, contextValue, _info) => { - return await contextValue.shuttleRepository.getSystems(); + return await contextValue.shuttleRepository.getSystemIfExists(); }, system: async (_parent, args, contextValue, _info) => { if (!args.id) return null; diff --git a/test/loaders/ApiBasedShuttleRepositoryLoaderTests.test.ts b/test/loaders/ApiBasedShuttleRepositoryLoaderTests.test.ts index cead5ec..5cf30f1 100644 --- a/test/loaders/ApiBasedShuttleRepositoryLoaderTests.test.ts +++ b/test/loaders/ApiBasedShuttleRepositoryLoaderTests.test.ts @@ -45,7 +45,7 @@ describe("ApiBasedRepositoryLoader", () => { await loader.fetchAndUpdateSystemData(); // Assert - const systems = await loader.repository.getSystems(); + const systems = await loader.repository.getSystemIfExists(); if (loader.supportedSystemIds.length < numberOfSystemsInResponse) { expect(systems).toHaveLength(loader.supportedSystemIds.length); } else { diff --git a/test/repositories/UnoptimizedInMemoryShuttleRepositoryTests.test.ts b/test/repositories/UnoptimizedInMemoryShuttleRepositoryTests.test.ts index 003f71f..9801037 100644 --- a/test/repositories/UnoptimizedInMemoryShuttleRepositoryTests.test.ts +++ b/test/repositories/UnoptimizedInMemoryShuttleRepositoryTests.test.ts @@ -22,21 +22,19 @@ describe("UnoptimizedInMemoryRepository", () => { }); describe("getSystems", () => { - test("gets the systems stored in the repository", async () => { + test("gets the system stored in the repository", async () => { const mockSystems = generateMockSystems(); - for (const system of mockSystems) { - await repository.addOrUpdateSystem(system); - } + await repository.addOrUpdateSystem(mockSystems[0]); - const result = await repository.getSystems(); + const result = await repository.getSystemIfExists(); - expect(result).toEqual(mockSystems); + expect(result).toEqual(mockSystems[0]); }); - test("gets an empty list if there are no systems stored", async () => { - const result = await repository.getSystems(); + test("gets null if there is no data associated with the system", async () => { + const result = await repository.getSystemIfExists(); - expect(result).toEqual([]); + expect(result).toEqual(null); }); }); @@ -331,7 +329,7 @@ describe("UnoptimizedInMemoryRepository", () => { await repository.addOrUpdateSystem(newSystem); - const result = await repository.getSystems(); + const result = await repository.getSystemIfExists(); expect(result).toEqual([newSystem]); }); @@ -344,7 +342,7 @@ describe("UnoptimizedInMemoryRepository", () => { await repository.addOrUpdateSystem(existingSystem); await repository.addOrUpdateSystem(updatedSystem); - const result = await repository.getSystems(); + const result = await repository.getSystemIfExists(); expect(result).toEqual([updatedSystem]); }); }); @@ -484,7 +482,7 @@ describe("UnoptimizedInMemoryRepository", () => { const systemToRemove = mockSystems[0]; await repository.removeSystemIfExists(systemToRemove.id); - const remainingSystems = await repository.getSystems(); + const remainingSystems = await repository.getSystemIfExists(); expect(remainingSystems).toHaveLength(mockSystems.length - 1); }); @@ -496,7 +494,7 @@ describe("UnoptimizedInMemoryRepository", () => { await repository.removeSystemIfExists("nonexistent-id"); - const remainingSystems = await repository.getSystems(); + const remainingSystems = await repository.getSystemIfExists(); expect(remainingSystems).toHaveLength(mockSystems.length); }); }); @@ -671,7 +669,7 @@ describe("UnoptimizedInMemoryRepository", () => { await repository.clearSystemData(); - const result = await repository.getSystems(); + const result = await repository.getSystemIfExists(); expect(result).toEqual([]); }); @@ -682,7 +680,7 @@ describe("UnoptimizedInMemoryRepository", () => { await repository.clearSystemData(); - const result = await repository.getSystems(); + const result = await repository.getSystemIfExists(); expect(result).toEqual([]); }); });