mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
update repository loader and tests to support only one system
This commit is contained in:
@@ -15,7 +15,7 @@ export class ApiResponseError extends Error {
|
|||||||
* which inherit from `IEntityWithId`.
|
* which inherit from `IEntityWithId`.
|
||||||
*/
|
*/
|
||||||
export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader {
|
export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader {
|
||||||
supportedSystemIds = ["263"];
|
systemId = "263";
|
||||||
baseUrl = "https://passiogo.com/mapGetData.php";
|
baseUrl = "https://passiogo.com/mapGetData.php";
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@@ -38,9 +38,7 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
|
|||||||
};
|
};
|
||||||
const query = new URLSearchParams(params).toString();
|
const query = new URLSearchParams(params).toString();
|
||||||
|
|
||||||
const systemIds = await this.constructExistingEntityIdSet(async () => {
|
const system = await this.repository.getSystemIfExists();
|
||||||
return await this.repository.getSystemIfExists();
|
|
||||||
})
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${this.baseUrl}?${query}`);
|
const response = await fetch(`${this.baseUrl}?${query}`);
|
||||||
@@ -52,34 +50,28 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
|
|||||||
|
|
||||||
if (typeof json.all === "object") {
|
if (typeof json.all === "object") {
|
||||||
// filter down to supported systems
|
// filter down to supported systems
|
||||||
const filteredSystems = json.all.filter((jsonSystem: any) => this.supportedSystemIds.includes(jsonSystem.id));
|
const filteredSystem = json.all.find((jsonSystem: any) => jsonSystem.id === this.systemId);
|
||||||
await Promise.all(filteredSystems.map(async (system: any) => {
|
if (filteredSystem !== undefined) {
|
||||||
const constructedSystem: ISystem = {
|
const constructedSystem: ISystem = {
|
||||||
id: system.id,
|
id: filteredSystem.id,
|
||||||
name: system.fullname,
|
name: filteredSystem.fullname,
|
||||||
};
|
};
|
||||||
|
|
||||||
await this.repository.updateSystem(constructedSystem);
|
await this.repository.updateSystem(constructedSystem);
|
||||||
systemIds.delete(constructedSystem.id);
|
}
|
||||||
}));
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Received JSON object does not contain `all` field")
|
throw new Error("Received JSON object does not contain `all` field")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prune systems
|
|
||||||
await Promise.all(Array.from(systemIds).map(async (systemId) => {
|
|
||||||
await this.repository.removeSystemIfExists(systemId);
|
|
||||||
}));
|
|
||||||
} catch(e: any) {
|
} catch(e: any) {
|
||||||
throw new ApiResponseError(e.message);
|
throw new ApiResponseError(e.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchAndUpdateRouteDataForExistingSystemsInRepository() {
|
public async fetchAndUpdateRouteDataForExistingSystemInRepository() {
|
||||||
const systems = await this.repository.getSystemIfExists();
|
const system = await this.repository.getSystemIfExists();
|
||||||
await Promise.all(systems.map(async (system) => {
|
if (system !== null) {
|
||||||
await this.fetchAndUpdateRouteDataForSystemId(system.id);
|
await this.fetchAndUpdateRouteDataForSystemId(system.id);
|
||||||
}));
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchAndUpdateRouteDataForSystemId(systemId: string) {
|
public async fetchAndUpdateRouteDataForSystemId(systemId: string) {
|
||||||
@@ -131,11 +123,11 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemsInRepository() {
|
public async fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemInRepository() {
|
||||||
const systems = await this.repository.getSystemIfExists();
|
const system = await this.repository.getSystemIfExists();
|
||||||
await Promise.all(systems.map(async (system: ISystem) => {
|
if (system !== null) {
|
||||||
await this.fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId(system.id);
|
await this.fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId(system.id);
|
||||||
}));
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId(systemId: string) {
|
public async fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId(systemId: string) {
|
||||||
@@ -177,12 +169,13 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchAndUpdateShuttleDataForExistingSystemsInRepository() {
|
public async fetchAndUpdateShuttleDataForExistingSystemInRepository() {
|
||||||
const systems = await this.repository.getSystemIfExists();
|
const system = await this.repository.getSystemIfExists();
|
||||||
await Promise.all(systems.map(async (system: ISystem) => {
|
|
||||||
|
if (system !== null) {
|
||||||
const systemId = system.id;
|
const systemId = system.id;
|
||||||
await this.fetchAndUpdateShuttleDataForSystemId(systemId);
|
await this.fetchAndUpdateShuttleDataForSystemId(systemId);
|
||||||
}));
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchAndUpdateShuttleDataForSystemId(systemId: string) {
|
public async fetchAndUpdateShuttleDataForSystemId(systemId: string) {
|
||||||
@@ -243,12 +236,12 @@ export class ApiBasedShuttleRepositoryLoader implements ShuttleRepositoryLoader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchAndUpdateEtaDataForExistingStopsForSystemsInRepository() {
|
public async fetchAndUpdateEtaDataForExistingStopsForSystemInRepository() {
|
||||||
const systems = await this.repository.getSystemIfExists()
|
const system = await this.repository.getSystemIfExists()
|
||||||
await Promise.all(systems.map(async (system: ISystem) => {
|
if (system !== null) {
|
||||||
const systemId = system.id;
|
const systemId = system.id;
|
||||||
await this.fetchAndUpdateEtaDataForExistingStopsForSystemId(systemId);
|
await this.fetchAndUpdateEtaDataForExistingStopsForSystemId(systemId);
|
||||||
}))
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchAndUpdateEtaDataForExistingStopsForSystemId(systemId: string) {
|
public async fetchAndUpdateEtaDataForExistingStopsForSystemId(systemId: string) {
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
export interface ShuttleRepositoryLoader {
|
export interface ShuttleRepositoryLoader {
|
||||||
fetchAndUpdateSystemData(): Promise<void>;
|
fetchAndUpdateSystemData(): Promise<void>;
|
||||||
fetchAndUpdateRouteDataForExistingSystemsInRepository(): Promise<void>;
|
fetchAndUpdateRouteDataForExistingSystemInRepository(): Promise<void>;
|
||||||
fetchAndUpdateRouteDataForSystemId(systemId: string): Promise<void>;
|
fetchAndUpdateRouteDataForSystemId(systemId: string): Promise<void>;
|
||||||
fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemsInRepository(): Promise<void>;
|
fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemInRepository(): Promise<void>;
|
||||||
fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId(systemId: string): Promise<void>;
|
fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId(systemId: string): Promise<void>;
|
||||||
fetchAndUpdateShuttleDataForExistingSystemsInRepository(): Promise<void>;
|
fetchAndUpdateShuttleDataForExistingSystemInRepository(): Promise<void>;
|
||||||
fetchAndUpdateShuttleDataForSystemId(systemId: string): Promise<void>;
|
fetchAndUpdateShuttleDataForSystemId(systemId: string): Promise<void>;
|
||||||
fetchAndUpdateEtaDataForExistingStopsForSystemsInRepository(): Promise<void>;
|
fetchAndUpdateEtaDataForExistingStopsForSystemInRepository(): Promise<void>;
|
||||||
fetchAndUpdateEtaDataForExistingStopsForSystemId(systemId: string): Promise<void>;
|
fetchAndUpdateEtaDataForExistingStopsForSystemId(systemId: string): Promise<void>;
|
||||||
fetchAndUpdateEtaDataForStopId(stopId: string): Promise<void>;
|
fetchAndUpdateEtaDataForStopId(stopId: string): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,14 +47,14 @@ export class TimedApiBasedShuttleRepositoryLoader extends ApiBasedShuttleReposit
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await this.fetchAndUpdateSystemData();
|
await this.fetchAndUpdateSystemData();
|
||||||
await this.fetchAndUpdateRouteDataForExistingSystemsInRepository();
|
await this.fetchAndUpdateRouteDataForExistingSystemInRepository();
|
||||||
await this.fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemsInRepository();
|
await this.fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemInRepository();
|
||||||
await this.fetchAndUpdateShuttleDataForExistingSystemsInRepository();
|
await this.fetchAndUpdateShuttleDataForExistingSystemInRepository();
|
||||||
|
|
||||||
// Because ETA method doesn't support pruning yet,
|
// Because ETA method doesn't support pruning yet,
|
||||||
// add a call to the clear method here
|
// add a call to the clear method here
|
||||||
await this.repository.clearEtaData();
|
await this.repository.clearEtaData();
|
||||||
await this.fetchAndUpdateEtaDataForExistingStopsForSystemsInRepository();
|
await this.fetchAndUpdateEtaDataForExistingStopsForSystemInRepository();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,12 +45,8 @@ describe("ApiBasedRepositoryLoader", () => {
|
|||||||
await loader.fetchAndUpdateSystemData();
|
await loader.fetchAndUpdateSystemData();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
const systems = await loader.repository.getSystemIfExists();
|
const system = await loader.repository.getSystemIfExists();
|
||||||
if (loader.supportedSystemIds.length < numberOfSystemsInResponse) {
|
expect(system).not.toBeNull();
|
||||||
expect(systems).toHaveLength(loader.supportedSystemIds.length);
|
|
||||||
} else {
|
|
||||||
expect(systems).toHaveLength(numberOfSystemsInResponse);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("throws the correct error if the API response contains no data", async () => {
|
it("throws the correct error if the API response contains no data", async () => {
|
||||||
@@ -72,18 +68,17 @@ describe("ApiBasedRepositoryLoader", () => {
|
|||||||
|
|
||||||
|
|
||||||
describe("fetchAndUpdateRouteDataForExistingSystemsInRepository", () => {
|
describe("fetchAndUpdateRouteDataForExistingSystemsInRepository", () => {
|
||||||
test("calls fetchAndUpdateRouteDataForSystemId for all systems in repository", async () => {
|
test("calls fetchAndUpdateRouteDataForSystemId for system in repository", async () => {
|
||||||
const spy = jest.spyOn(loader, "fetchAndUpdateRouteDataForSystemId");
|
const spy = jest.spyOn(loader, "fetchAndUpdateRouteDataForSystemId");
|
||||||
|
|
||||||
const systems = generateMockSystems();
|
const systems = generateMockSystems();
|
||||||
|
|
||||||
await Promise.all(systems.map(async (system) => {
|
await loader.repository.updateSystem(systems[0]);
|
||||||
await loader.repository.updateSystem(system);
|
|
||||||
}));
|
|
||||||
|
|
||||||
await loader.fetchAndUpdateRouteDataForExistingSystemsInRepository();
|
await loader.fetchAndUpdateRouteDataForExistingSystemInRepository();
|
||||||
|
|
||||||
expect(spy.mock.calls.length).toBe(systems.length);
|
expect(spy.mock.calls.length).toBe(1);
|
||||||
|
expect(spy).toHaveBeenCalledWith(systems[0].id)
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -122,18 +117,17 @@ describe("ApiBasedRepositoryLoader", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemsInRepository", () => {
|
describe("fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemsInRepository", () => {
|
||||||
it("calls fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId for every system", async () => {
|
it("calls fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId for system", async () => {
|
||||||
const spy = jest.spyOn(loader, "fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId");
|
const spy = jest.spyOn(loader, "fetchAndUpdateStopAndPolylineDataForRoutesWithSystemId");
|
||||||
|
|
||||||
const systems = generateMockSystems();
|
const systems = generateMockSystems();
|
||||||
|
|
||||||
await Promise.all(systems.map(async (system) => {
|
await loader.repository.updateSystem(systems[0]);
|
||||||
await loader.repository.updateSystem(system);
|
|
||||||
}));
|
|
||||||
|
|
||||||
await loader.fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemsInRepository();
|
await loader.fetchAndUpdateStopAndPolylineDataForRoutesInExistingSystemInRepository();
|
||||||
|
|
||||||
expect(spy.mock.calls.length).toBe(systems.length);
|
expect(spy.mock.calls.length).toBe(1);
|
||||||
|
expect(spy).toHaveBeenCalledWith(systems[0].id)
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -182,13 +176,12 @@ describe("ApiBasedRepositoryLoader", () => {
|
|||||||
const spy = jest.spyOn(loader, "fetchAndUpdateShuttleDataForSystemId");
|
const spy = jest.spyOn(loader, "fetchAndUpdateShuttleDataForSystemId");
|
||||||
|
|
||||||
const systems = generateMockSystems();
|
const systems = generateMockSystems();
|
||||||
await Promise.all(systems.map(async (system) => {
|
await loader.repository.updateSystem(systems[0]);
|
||||||
await loader.repository.updateSystem(system);
|
|
||||||
}))
|
|
||||||
|
|
||||||
await loader.fetchAndUpdateShuttleDataForExistingSystemsInRepository();
|
await loader.fetchAndUpdateShuttleDataForExistingSystemInRepository();
|
||||||
|
|
||||||
expect(spy.mock.calls.length).toBe(systems.length);
|
expect(spy.mock.calls.length).toBe(1);
|
||||||
|
expect(spy).toHaveBeenCalledWith(systems[0].id)
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -225,13 +218,12 @@ describe("ApiBasedRepositoryLoader", () => {
|
|||||||
const spy = jest.spyOn(loader, "fetchAndUpdateEtaDataForExistingStopsForSystemId");
|
const spy = jest.spyOn(loader, "fetchAndUpdateEtaDataForExistingStopsForSystemId");
|
||||||
|
|
||||||
const systems = generateMockSystems();
|
const systems = generateMockSystems();
|
||||||
await Promise.all(systems.map(async (system) => {
|
await loader.repository.updateSystem(systems[0]);
|
||||||
await loader.repository.updateSystem(system);
|
|
||||||
}));
|
|
||||||
|
|
||||||
await loader.fetchAndUpdateEtaDataForExistingStopsForSystemsInRepository();
|
await loader.fetchAndUpdateEtaDataForExistingStopsForSystemInRepository();
|
||||||
|
|
||||||
expect(spy.mock.calls.length).toBe(systems.length);
|
expect(spy.mock.calls.length).toBe(1);
|
||||||
|
expect(spy).toHaveBeenCalledWith(systems[0].id)
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user