Skip to content

Commit

Permalink
Refactored combinedPromises
Browse files Browse the repository at this point in the history
- combinedPromises now uses `Promise.allSettled()`
- rewrote tests to reflect change
  • Loading branch information
the-bay-kay committed Nov 6, 2023
1 parent 2a04319 commit 000f08d
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 177 deletions.
73 changes: 33 additions & 40 deletions www/__tests__/unifiedDataLoader.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mockLogger } from '../__mocks__/globalMocks';
import { combineWithDedup, combinedPromises } from '../js/services/unifiedDataLoader';
import { removeDup, combinedPromises } from '../js/services/unifiedDataLoader';
import { ServerData } from '../js/types/serverData';

mockLogger();
Expand All @@ -9,7 +9,7 @@ const testOne: ServerData<any> = {
metadata: {
key: '',
platform: '',
write_ts: 1, // the only value checked by combineWithDedup
write_ts: 1, // the only value checked by removeDup
time_zone: '',
write_fmt_time: '',
write_local_dt: null,
Expand All @@ -23,25 +23,21 @@ testThree.metadata.write_ts = 3;
const testFour = JSON.parse(JSON.stringify(testOne));
testFour.metadata.write_ts = 4;

describe('combineWithDedup can', () => {
it('work with empty arrays', () => {
expect(combineWithDedup([], [])).toEqual([]);
expect(combineWithDedup([], [testOne])).toEqual([testOne]);
expect(combineWithDedup([testOne, testTwo], [])).toEqual([testOne, testTwo]);
describe('removeDup can', () => {
it('work with an empty array', () => {
expect(removeDup([])).toEqual([]);
});
it('work with arrays of len 1', () => {
expect(combineWithDedup([testOne], [testOne])).toEqual([testOne]);
expect(combineWithDedup([testOne], [testTwo])).toEqual([testOne, testTwo]);

it('work with an array of len 1', () => {
expect(removeDup([testOne])).toEqual([testOne]);
});
it('work with arrays of len > 1', () => {
expect(combineWithDedup([testOne], [testOne, testTwo])).toEqual([testOne, testTwo]);
expect(combineWithDedup([testOne], [testTwo, testTwo])).toEqual([testOne, testTwo]);
expect(combineWithDedup([testOne, testTwo], [testTwo, testTwo])).toEqual([testOne, testTwo]);
expect(combineWithDedup([testOne, testTwo, testThree], [testOne, testTwo])).toEqual([
testOne,
testTwo,
testThree,
]);

it('work with an array of len >=1', () => {
expect(removeDup([testOne, testTwo])).toEqual([testOne, testTwo]);
expect(removeDup([testOne, testOne])).toEqual([testOne]);
expect(removeDup([testOne, testTwo, testThree])).toEqual([testOne, testTwo, testThree]);
expect(removeDup([testOne, testOne, testThree])).toEqual([testOne, testThree]);
expect(removeDup([testOne, testOne, testOne])).toEqual([testOne]);
});
});

Expand All @@ -55,38 +51,35 @@ const badPromiseGenerator = (input: string) => {

it('throws an error on an empty input', async () => {
expect(() => {
combinedPromises([], combineWithDedup);
combinedPromises([], removeDup);
}).toThrow();
});

it('catches when all promises fails', async () => {
expect(combinedPromises([badPromiseGenerator('')], combineWithDedup)).rejects.toEqual(['']);
expect(combinedPromises([badPromiseGenerator('')], removeDup)).rejects.toEqual(['']);
expect(
combinedPromises(
[badPromiseGenerator('bad'), badPromiseGenerator('promise')],
combineWithDedup,
),
combinedPromises([badPromiseGenerator('bad'), badPromiseGenerator('promise')], removeDup),
).rejects.toEqual(['bad', 'promise']);
expect(
combinedPromises(
[badPromiseGenerator('very'), badPromiseGenerator('bad'), badPromiseGenerator('promise')],
combineWithDedup,
removeDup,
),
).rejects.toEqual(['very', 'bad', 'promise']);

expect(
combinedPromises([badPromiseGenerator('bad'), promiseGenerator([testOne])], combineWithDedup),
combinedPromises([badPromiseGenerator('bad'), promiseGenerator([testOne])], removeDup),
).resolves.toEqual([testOne]);
expect(
combinedPromises([promiseGenerator([testOne]), badPromiseGenerator('bad')], combineWithDedup),
combinedPromises([promiseGenerator([testOne]), badPromiseGenerator('bad')], removeDup),
).resolves.toEqual([testOne]);
});

it('work with arrays of len 1', async () => {
const promiseArrayOne = [promiseGenerator([testOne])];
const promiseArrayTwo = [promiseGenerator([testOne, testTwo])];
const testResultOne = await combinedPromises(promiseArrayOne, combineWithDedup);
const testResultTwo = await combinedPromises(promiseArrayTwo, combineWithDedup);
const testResultOne = await combinedPromises(promiseArrayOne, removeDup);
const testResultTwo = await combinedPromises(promiseArrayTwo, removeDup);

expect(testResultOne).toEqual([testOne]);
expect(testResultTwo).toEqual([testOne, testTwo]);
Expand All @@ -105,11 +98,11 @@ it('works with arrays of len 2', async () => {
promiseGenerator([testTwo, testThree]),
];

const testResultOne = await combinedPromises(promiseArrayOne, combineWithDedup);
const testResultTwo = await combinedPromises(promiseArrayTwo, combineWithDedup);
const testResultThree = await combinedPromises(promiseArrayThree, combineWithDedup);
const testResultFour = await combinedPromises(promiseArrayFour, combineWithDedup);
const testResultFive = await combinedPromises(promiseArrayFive, combineWithDedup);
const testResultOne = await combinedPromises(promiseArrayOne, removeDup);
const testResultTwo = await combinedPromises(promiseArrayTwo, removeDup);
const testResultThree = await combinedPromises(promiseArrayThree, removeDup);
const testResultFour = await combinedPromises(promiseArrayFour, removeDup);
const testResultFive = await combinedPromises(promiseArrayFive, removeDup);

expect(testResultOne).toEqual([testOne, testTwo]);
expect(testResultTwo).toEqual([testOne, testTwo, testThree]);
Expand Down Expand Up @@ -140,10 +133,10 @@ it('works with arrays of len >= 2', async () => {
promiseGenerator([testFour]),
];

const testResultOne = await combinedPromises(promiseArrayOne, combineWithDedup);
const testResultTwo = await combinedPromises(promiseArrayTwo, combineWithDedup);
const testResultThree = await combinedPromises(promiseArrayThree, combineWithDedup);
const testResultFour = await combinedPromises(promiseArrayFour, combineWithDedup);
const testResultOne = await combinedPromises(promiseArrayOne, removeDup);
const testResultTwo = await combinedPromises(promiseArrayTwo, removeDup);
const testResultThree = await combinedPromises(promiseArrayThree, removeDup);
const testResultFour = await combinedPromises(promiseArrayFour, removeDup);

expect(testResultOne).toEqual([testOne, testTwo, testThree]);
expect(testResultTwo).toEqual([testOne, testTwo]);
Expand All @@ -154,4 +147,4 @@ it('works with arrays of len >= 2', async () => {
/*
TO-DO: Once getRawEnteries can be tested via end-to-end testing, we will be able to
test getUnifiedDataForInterval as well.
*/
*/
109 changes: 55 additions & 54 deletions www/js/diary/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ angular
$ionicPlatform,
$window,
$rootScope,
UnifiedDataLoader,
Logger,
$injector,
) {
Expand Down Expand Up @@ -268,64 +267,65 @@ angular
' -> ' +
moment.unix(tripEndTransition.data.ts).toString(),
);
return UnifiedDataLoader.getUnifiedSensorDataForInterval(
'background/filtered_location',
tq,
).then(function (locationList) {
if (locationList.length == 0) {
return undefined;
}
var sortedLocationList = locationList.sort(tsEntrySort);
var retainInRange = function (loc) {
return (
tripStartTransition.data.ts <= loc.data.ts && loc.data.ts <= tripEndTransition.data.ts
);
};
const getMethod = window['cordova'].plugins.BEMUserCache.getSensorDataForInterval;
return getUnifiedDataForInterval('background/filtered_location', tq, getMethod).then(
function (locationList) {
if (locationList.length == 0) {
return undefined;
}
var sortedLocationList = locationList.sort(tsEntrySort);
var retainInRange = function (loc) {
return (
tripStartTransition.data.ts <= loc.data.ts &&
loc.data.ts <= tripEndTransition.data.ts
);
};

var filteredLocationList = sortedLocationList.filter(retainInRange);
var filteredLocationList = sortedLocationList.filter(retainInRange);

// Fix for https://github.com/e-mission/e-mission-docs/issues/417
if (filteredLocationList.length == 0) {
return undefined;
}
// Fix for https://github.com/e-mission/e-mission-docs/issues/417
if (filteredLocationList.length == 0) {
return undefined;
}

var tripStartPoint = filteredLocationList[0];
var tripEndPoint = filteredLocationList[filteredLocationList.length - 1];
Logger.log(
'tripStartPoint = ' +
JSON.stringify(tripStartPoint) +
'tripEndPoint = ' +
JSON.stringify(tripEndPoint),
);
// if we get a list but our start and end are undefined
// let's print out the complete original list to get a clue
// this should help with debugging
// https://github.com/e-mission/e-mission-docs/issues/417
// if it ever occurs again
if (angular.isUndefined(tripStartPoint) || angular.isUndefined(tripEndPoint)) {
Logger.log('BUG 417 check: locationList = ' + JSON.stringify(locationList));
var tripStartPoint = filteredLocationList[0];
var tripEndPoint = filteredLocationList[filteredLocationList.length - 1];
Logger.log(
'transitions: start = ' +
JSON.stringify(tripStartTransition.data) +
' end = ' +
JSON.stringify(tripEndTransition.data.ts),
'tripStartPoint = ' +
JSON.stringify(tripStartPoint) +
'tripEndPoint = ' +
JSON.stringify(tripEndPoint),
);
}
// if we get a list but our start and end are undefined
// let's print out the complete original list to get a clue
// this should help with debugging
// https://github.com/e-mission/e-mission-docs/issues/417
// if it ever occurs again
if (angular.isUndefined(tripStartPoint) || angular.isUndefined(tripEndPoint)) {
Logger.log('BUG 417 check: locationList = ' + JSON.stringify(locationList));
Logger.log(
'transitions: start = ' +
JSON.stringify(tripStartTransition.data) +
' end = ' +
JSON.stringify(tripEndTransition.data.ts),
);
}

const tripProps = points2TripProps(filteredLocationList);

return {
...tripProps,
start_loc: {
type: 'Point',
coordinates: [tripStartPoint.data.longitude, tripStartPoint.data.latitude],
},
end_loc: {
type: 'Point',
coordinates: [tripEndPoint.data.longitude, tripEndPoint.data.latitude],
},
};
});
const tripProps = points2TripProps(filteredLocationList);

return {
...tripProps,
start_loc: {
type: 'Point',
coordinates: [tripStartPoint.data.longitude, tripStartPoint.data.latitude],
},
end_loc: {
type: 'Point',
coordinates: [tripEndPoint.data.longitude, tripEndPoint.data.latitude],
},
};
},
);
};

var linkTrips = function (trip1, trip2) {
Expand Down Expand Up @@ -354,7 +354,8 @@ angular
' -> ' +
moment.unix(tq.endTs).toString(),
);
return UnifiedDataLoader.getUnifiedMessagesForInterval('statemachine/transition', tq).then(
const getMethod = window['cordova'].plugins.BEMUserCache.getMessagesForInterval;
return getUnifiedDataForInterval('statemachine/transition', tq, getMethod).then(
function (transitionList) {
if (transitionList.length == 0) {
Logger.log('No unprocessed trips. yay!');
Expand Down
Loading

0 comments on commit 000f08d

Please sign in to comment.