mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Update test cases to test updating shuttle data depending on proximity
This commit is contained in:
@@ -16,6 +16,7 @@ import {
|
||||
updateGlobalFetchMockJsonToThrowSyntaxError
|
||||
} from "../../../../testHelpers/fetchMockHelpers";
|
||||
import { assertAsyncCallbackThrowsApiResponseError } from "../../../../testHelpers/assertAsyncCallbackThrowsApiResponseError";
|
||||
import { IRoute } from "../../../entities/ShuttleRepositoryEntities";
|
||||
|
||||
describe("ApiBasedShuttleRepositoryLoader", () => {
|
||||
let loader: ApiBasedShuttleRepositoryLoader;
|
||||
@@ -125,47 +126,86 @@ describe("ApiBasedShuttleRepositoryLoader", () => {
|
||||
});
|
||||
|
||||
describe("updateShuttleDataForSystemBasedOnProximityToRoutes", () => {
|
||||
it("updates shuttle data in repository from API if shuttles close enough to route", async () => {
|
||||
const distance = 1;
|
||||
loader = new ApiBasedShuttleRepositoryLoader(
|
||||
"263",
|
||||
"1",
|
||||
new UnoptimizedInMemoryShuttleRepository(),
|
||||
distance,
|
||||
);
|
||||
|
||||
function generateMockRoutesWithPolylineCoordinates() {
|
||||
const routes = generateMockRoutes();
|
||||
routes[0].polylineCoordinates = [
|
||||
{ latitude: 33.78792, longitude: -117.86187 },
|
||||
{ latitude: 33.78792, longitude: -117.86200 },
|
||||
{ latitude: 33.78792, longitude: -117.86245 }
|
||||
{latitude: 33.78792, longitude: -117.86187},
|
||||
{latitude: 33.78792, longitude: -117.86200},
|
||||
{latitude: 33.78792, longitude: -117.86245}
|
||||
];
|
||||
return routes;
|
||||
}
|
||||
|
||||
await Promise.all(routes.map(async (route) => {
|
||||
await loader.repository.addOrUpdateRoute(route);
|
||||
}));
|
||||
|
||||
function getMockJsonResponseMatchingRouteAndCoordinates(route: IRoute, longitude: string, latitude: string) {
|
||||
const modifiedSuccessfulResponse = {
|
||||
...fetchShuttleDataSuccessfulResponse,
|
||||
};
|
||||
|
||||
Object.keys(modifiedSuccessfulResponse.buses).forEach((busId) => {
|
||||
const bus = (modifiedSuccessfulResponse.buses as any)[busId][0];
|
||||
bus.latitude = "33.78792";
|
||||
bus.longitude = "-117.86187";
|
||||
bus.routeId = routes[0].id;
|
||||
bus.latitude = latitude;
|
||||
bus.longitude = longitude;
|
||||
bus.routeId = route.id;
|
||||
});
|
||||
return modifiedSuccessfulResponse;
|
||||
}
|
||||
|
||||
async function addMockRoutes(routes: IRoute[]) {
|
||||
await Promise.all(routes.map(async (route) => {
|
||||
await loader.repository.addOrUpdateRoute(route);
|
||||
}));
|
||||
}
|
||||
|
||||
it("updates shuttle data in repository from API if shuttles close enough to route", async () => {
|
||||
const distanceMiles = 1;
|
||||
loader = new ApiBasedShuttleRepositoryLoader(
|
||||
"263",
|
||||
"1",
|
||||
new UnoptimizedInMemoryShuttleRepository(),
|
||||
distanceMiles,
|
||||
);
|
||||
|
||||
const routes = generateMockRoutesWithPolylineCoordinates();
|
||||
await addMockRoutes(routes);
|
||||
const modifiedSuccessfulResponse = getMockJsonResponseMatchingRouteAndCoordinates(
|
||||
routes[0],
|
||||
"-117.86187",
|
||||
"33.78792"
|
||||
);
|
||||
updateGlobalFetchMockJson(modifiedSuccessfulResponse);
|
||||
const busesInResponse = Object.values(modifiedSuccessfulResponse.buses);
|
||||
|
||||
await loader.updateShuttleDataForSystemBasedOnProximityToRoutes();
|
||||
|
||||
const shuttles = await loader.repository.getShuttles();
|
||||
|
||||
expect(shuttles.length).toEqual(busesInResponse.length);
|
||||
});
|
||||
|
||||
it("does not update shuttle data in repository from API if shuttles are not close enough to route", async () => {
|
||||
const distanceMiles = 1;
|
||||
loader = new ApiBasedShuttleRepositoryLoader(
|
||||
"263",
|
||||
"1",
|
||||
new UnoptimizedInMemoryShuttleRepository(),
|
||||
distanceMiles,
|
||||
);
|
||||
|
||||
const routes = generateMockRoutesWithPolylineCoordinates();
|
||||
await addMockRoutes(routes);
|
||||
|
||||
const modifiedSuccessfulResponse = getMockJsonResponseMatchingRouteAndCoordinates(
|
||||
routes[0],
|
||||
"-116.86187",
|
||||
"32.78792"
|
||||
);
|
||||
updateGlobalFetchMockJson(modifiedSuccessfulResponse);
|
||||
|
||||
await loader.updateShuttleDataForSystemBasedOnProximityToRoutes();
|
||||
|
||||
const shuttles = await loader.repository.getShuttles();
|
||||
expect(shuttles.length).toEqual(0);
|
||||
});
|
||||
|
||||
it("throws the correct error if the API response contains no data", async () => {
|
||||
updateGlobalFetchMockJsonToThrowSyntaxError();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user