From f442c6b279d069cad98c78f4ec5bfccfd4b95e90 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Tue, 7 Jan 2025 20:07:05 -0800 Subject: [PATCH] Revert "change caching and repository api to include system id initializer" This reverts commit e7e915a8b19f244ebd0abb1d1f7839bb412e5b10. --- src/repositories/ApiBasedRepository.ts | 9 ++-- .../ApiBasedRepositoryTests.test.ts | 42 ++++++++++--------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/repositories/ApiBasedRepository.ts b/src/repositories/ApiBasedRepository.ts index dda591a..18a562e 100644 --- a/src/repositories/ApiBasedRepository.ts +++ b/src/repositories/ApiBasedRepository.ts @@ -8,11 +8,11 @@ export interface ApiBasedRepositoryCache { etasForStopId?: { [stopId: string]: IEta[], }, - stopsByStopId?: { - [stopId: string]: IStop, + stopsBySystemId?: { + [systemId: string]: IStop[], }, - shuttleByShuttleId?: { - [shuttleId: string]: IShuttle, + shuttlesByShuttleId?: { + [shuttleId: string]: IShuttle[], }, // To speed things up, implement caches for other data later } @@ -34,7 +34,6 @@ const defaultTtls: ApiBasedRepositoryMillisecondTTLs = { export class ApiBasedRepository implements GetterRepository { constructor( - private systemId: string, private initialCache: ApiBasedRepositoryCache | undefined = emptyCache, private ttls: ApiBasedRepositoryMillisecondTTLs = defaultTtls, ) { diff --git a/test/repositories/ApiBasedRepositoryTests.test.ts b/test/repositories/ApiBasedRepositoryTests.test.ts index 52947a7..26e749e 100644 --- a/test/repositories/ApiBasedRepositoryTests.test.ts +++ b/test/repositories/ApiBasedRepositoryTests.test.ts @@ -149,7 +149,7 @@ describe("getEtaForShuttleAndStopId", () => { test("getEtaForShuttleAndStopId returns correct ETA data", async () => { updateGlobalFetchMockJson(genericEtaDataByStopId); - const repository = new ApiBasedRepository("1"); + const repository = new ApiBasedRepository(); const result = await repository.getEtaForShuttleAndStopId("5577", "177666"); expect(result?.secondsRemaining).toEqual(587); @@ -157,7 +157,7 @@ describe("getEtaForShuttleAndStopId", () => { }); test("getEtaForShuttleAndStopId returns null if API call is invalid and cache is empty", async () => { - const repository = new ApiBasedRepository("1"); + const repository = new ApiBasedRepository(); const result = await repository.getEtaForShuttleAndStopId("5577", "177666"); expect(result).toEqual(null); @@ -190,7 +190,7 @@ describe("getEtasForShuttleId", () => { etasForStopId: 100000, }; - const repository = new ApiBasedRepository("1", initialCache, ttls); + const repository = new ApiBasedRepository(initialCache, ttls); repository.updateEtasForSystemIfTTL = jest.fn(async () => { }); const result = await repository.getEtasForShuttleId("5577"); @@ -199,7 +199,7 @@ describe("getEtasForShuttleId", () => { }); test("getEtasForShuttleId returns empty array if no data available", async () => { - const repository = new ApiBasedRepository("1"); + const repository = new ApiBasedRepository(); repository.updateEtasForSystemIfTTL = jest.fn(async () => { }); const result = await repository.getEtasForShuttleId("5577"); @@ -234,7 +234,7 @@ describe("getEtasForStopId", () => { etasForStopId: 100000, }; - const repository = new ApiBasedRepository("1", initialCache, ttls); + const repository = new ApiBasedRepository(initialCache, ttls); repository.updateEtasForSystemIfTTL = jest.fn(async () => { }); const result = await repository.getEtasForStopId("177666"); @@ -243,7 +243,7 @@ describe("getEtasForStopId", () => { }); test("getEtasForStopId returns empty array if no data available", async () => { - const repository = new ApiBasedRepository("1"); + const repository = new ApiBasedRepository(); repository.updateEtasForSystemIfTTL = jest.fn(async () => { }); const result = await repository.getEtasForShuttleId("5577"); @@ -275,18 +275,20 @@ describe("updateEtasForSystemIfTTL", () => { expectedEta, ], }, - stopsByStopId: { - "1": { - systemId: "1", - millisecondsSinceEpoch: Date.now() - 1000, - name: "Chapman Court", - id: "177666", - coordinates: { - latitude: 33.796796, - longitude: -117.889293 - }, - } - } + stopsBySystemId: { + "1": [ + { + systemId: "1", + millisecondsSinceEpoch: Date.now() - 1000, + name: "Chapman Court", + id: "177666", + coordinates: { + latitude: 33.796796, + longitude: -117.889293 + }, + } + ], + }, }; const ttls: ApiBasedRepositoryMillisecondTTLs = { @@ -294,7 +296,7 @@ describe("updateEtasForSystemIfTTL", () => { etasForStopId: 100000, }; - const repository = new ApiBasedRepository("1", initialCache, ttls); + const repository = new ApiBasedRepository(initialCache, ttls); await repository.updateEtasForSystemIfTTL("1"); const updatedResult = await repository.getEtaForShuttleAndStopId( @@ -307,7 +309,7 @@ describe("updateEtasForSystemIfTTL", () => { test("updateEtasForSystemIfTTL updates all ETA data if data is TTL", async () => { updateGlobalFetchMockJson(genericEtaDataByStopId); - const repository = new ApiBasedRepository("1"); + const repository = new ApiBasedRepository(); repository.getStopsBySystemId = jest.fn(async () => { return [ {