mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
change caching and repository api to include system id initializer
This commit is contained in:
@@ -8,11 +8,11 @@ export interface ApiBasedRepositoryCache {
|
|||||||
etasForStopId?: {
|
etasForStopId?: {
|
||||||
[stopId: string]: IEta[],
|
[stopId: string]: IEta[],
|
||||||
},
|
},
|
||||||
stopsBySystemId?: {
|
stopsByStopId?: {
|
||||||
[systemId: string]: IStop[],
|
[stopId: string]: IStop,
|
||||||
},
|
},
|
||||||
shuttlesByShuttleId?: {
|
shuttleByShuttleId?: {
|
||||||
[shuttleId: string]: IShuttle[],
|
[shuttleId: string]: IShuttle,
|
||||||
},
|
},
|
||||||
// To speed things up, implement caches for other data later
|
// To speed things up, implement caches for other data later
|
||||||
}
|
}
|
||||||
@@ -34,6 +34,7 @@ const defaultTtls: ApiBasedRepositoryMillisecondTTLs = {
|
|||||||
|
|
||||||
export class ApiBasedRepository implements GetterRepository {
|
export class ApiBasedRepository implements GetterRepository {
|
||||||
constructor(
|
constructor(
|
||||||
|
private systemId: string,
|
||||||
private initialCache: ApiBasedRepositoryCache | undefined = emptyCache,
|
private initialCache: ApiBasedRepositoryCache | undefined = emptyCache,
|
||||||
private ttls: ApiBasedRepositoryMillisecondTTLs = defaultTtls,
|
private ttls: ApiBasedRepositoryMillisecondTTLs = defaultTtls,
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -149,7 +149,7 @@ describe("getEtaForShuttleAndStopId", () => {
|
|||||||
test("getEtaForShuttleAndStopId returns correct ETA data", async () => {
|
test("getEtaForShuttleAndStopId returns correct ETA data", async () => {
|
||||||
updateGlobalFetchMockJson(genericEtaDataByStopId);
|
updateGlobalFetchMockJson(genericEtaDataByStopId);
|
||||||
|
|
||||||
const repository = new ApiBasedRepository();
|
const repository = new ApiBasedRepository("1");
|
||||||
const result = await repository.getEtaForShuttleAndStopId("5577", "177666");
|
const result = await repository.getEtaForShuttleAndStopId("5577", "177666");
|
||||||
|
|
||||||
expect(result?.secondsRemaining).toEqual(587);
|
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 () => {
|
test("getEtaForShuttleAndStopId returns null if API call is invalid and cache is empty", async () => {
|
||||||
const repository = new ApiBasedRepository();
|
const repository = new ApiBasedRepository("1");
|
||||||
const result = await repository.getEtaForShuttleAndStopId("5577", "177666");
|
const result = await repository.getEtaForShuttleAndStopId("5577", "177666");
|
||||||
|
|
||||||
expect(result).toEqual(null);
|
expect(result).toEqual(null);
|
||||||
@@ -190,7 +190,7 @@ describe("getEtasForShuttleId", () => {
|
|||||||
etasForStopId: 100000,
|
etasForStopId: 100000,
|
||||||
};
|
};
|
||||||
|
|
||||||
const repository = new ApiBasedRepository(initialCache, ttls);
|
const repository = new ApiBasedRepository("1", initialCache, ttls);
|
||||||
repository.updateEtasForSystemIfTTL = jest.fn(async () => {
|
repository.updateEtasForSystemIfTTL = jest.fn(async () => {
|
||||||
});
|
});
|
||||||
const result = await repository.getEtasForShuttleId("5577");
|
const result = await repository.getEtasForShuttleId("5577");
|
||||||
@@ -199,7 +199,7 @@ describe("getEtasForShuttleId", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("getEtasForShuttleId returns empty array if no data available", async () => {
|
test("getEtasForShuttleId returns empty array if no data available", async () => {
|
||||||
const repository = new ApiBasedRepository();
|
const repository = new ApiBasedRepository("1");
|
||||||
repository.updateEtasForSystemIfTTL = jest.fn(async () => {
|
repository.updateEtasForSystemIfTTL = jest.fn(async () => {
|
||||||
});
|
});
|
||||||
const result = await repository.getEtasForShuttleId("5577");
|
const result = await repository.getEtasForShuttleId("5577");
|
||||||
@@ -234,7 +234,7 @@ describe("getEtasForStopId", () => {
|
|||||||
etasForStopId: 100000,
|
etasForStopId: 100000,
|
||||||
};
|
};
|
||||||
|
|
||||||
const repository = new ApiBasedRepository(initialCache, ttls);
|
const repository = new ApiBasedRepository("1", initialCache, ttls);
|
||||||
repository.updateEtasForSystemIfTTL = jest.fn(async () => {
|
repository.updateEtasForSystemIfTTL = jest.fn(async () => {
|
||||||
});
|
});
|
||||||
const result = await repository.getEtasForStopId("177666");
|
const result = await repository.getEtasForStopId("177666");
|
||||||
@@ -243,7 +243,7 @@ describe("getEtasForStopId", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("getEtasForStopId returns empty array if no data available", async () => {
|
test("getEtasForStopId returns empty array if no data available", async () => {
|
||||||
const repository = new ApiBasedRepository();
|
const repository = new ApiBasedRepository("1");
|
||||||
repository.updateEtasForSystemIfTTL = jest.fn(async () => {
|
repository.updateEtasForSystemIfTTL = jest.fn(async () => {
|
||||||
});
|
});
|
||||||
const result = await repository.getEtasForShuttleId("5577");
|
const result = await repository.getEtasForShuttleId("5577");
|
||||||
@@ -275,20 +275,18 @@ describe("updateEtasForSystemIfTTL", () => {
|
|||||||
expectedEta,
|
expectedEta,
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
stopsBySystemId: {
|
stopsByStopId: {
|
||||||
"1": [
|
"1": {
|
||||||
{
|
systemId: "1",
|
||||||
systemId: "1",
|
millisecondsSinceEpoch: Date.now() - 1000,
|
||||||
millisecondsSinceEpoch: Date.now() - 1000,
|
name: "Chapman Court",
|
||||||
name: "Chapman Court",
|
id: "177666",
|
||||||
id: "177666",
|
coordinates: {
|
||||||
coordinates: {
|
latitude: 33.796796,
|
||||||
latitude: 33.796796,
|
longitude: -117.889293
|
||||||
longitude: -117.889293
|
},
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
],
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const ttls: ApiBasedRepositoryMillisecondTTLs = {
|
const ttls: ApiBasedRepositoryMillisecondTTLs = {
|
||||||
@@ -296,7 +294,7 @@ describe("updateEtasForSystemIfTTL", () => {
|
|||||||
etasForStopId: 100000,
|
etasForStopId: 100000,
|
||||||
};
|
};
|
||||||
|
|
||||||
const repository = new ApiBasedRepository(initialCache, ttls);
|
const repository = new ApiBasedRepository("1", initialCache, ttls);
|
||||||
await repository.updateEtasForSystemIfTTL("1");
|
await repository.updateEtasForSystemIfTTL("1");
|
||||||
|
|
||||||
const updatedResult = await repository.getEtaForShuttleAndStopId(
|
const updatedResult = await repository.getEtaForShuttleAndStopId(
|
||||||
@@ -309,7 +307,7 @@ describe("updateEtasForSystemIfTTL", () => {
|
|||||||
test("updateEtasForSystemIfTTL updates all ETA data if data is TTL", async () => {
|
test("updateEtasForSystemIfTTL updates all ETA data if data is TTL", async () => {
|
||||||
updateGlobalFetchMockJson(genericEtaDataByStopId);
|
updateGlobalFetchMockJson(genericEtaDataByStopId);
|
||||||
|
|
||||||
const repository = new ApiBasedRepository();
|
const repository = new ApiBasedRepository("1");
|
||||||
repository.getStopsBySystemId = jest.fn(async () => {
|
repository.getStopsBySystemId = jest.fn(async () => {
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user