Skip to content

Commit

Permalink
Added tests for combinedPromises
Browse files Browse the repository at this point in the history
- Minor changes to unifiedDataLoader: adjusted logInfo, exports
- Wrote test cases for combinedPromises
  • Loading branch information
the-bay-kay committed Oct 24, 2023
1 parent 1674d46 commit 7dd8ffb
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 23 deletions.
95 changes: 77 additions & 18 deletions www/__tests__/unifiedDataLoader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,95 @@ import { ServerDataPoint } from '../js/types/diaryTypes';

mockLogger();

describe('combineWithDedup can', () => {
const testOne : ServerDataPoint = {
data: '',
metadata: {
key: '',
platform: '',
write_ts: 1, // the only value checked by combineWithDedup
time_zone: '',
write_fmt_time: '',
write_local_dt: null,
},
};
const testTwo = JSON.parse(JSON.stringify(testOne));
testTwo.metadata.write_ts = 2;
const testThree = JSON.parse(JSON.stringify(testOne));
testThree.metadata.write_ts = 3;
const testOne : ServerDataPoint = {
data: '',
metadata: {
key: '',
platform: '',
write_ts: 1, // the only value checked by combineWithDedup
time_zone: '',
write_fmt_time: '',
write_local_dt: null,
},
};

const testTwo = JSON.parse(JSON.stringify(testOne));
testTwo.metadata.write_ts = 2;
const testThree = JSON.parse(JSON.stringify(testOne));
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]);
});
it('Can work with arrays of len 1', () => {
it('work with arrays of len 1', () => {
expect(combineWithDedup([testOne], [testOne])).toEqual([testOne]);
expect(combineWithDedup([testOne], [testTwo])).toEqual([testOne, testTwo]);
});
it('Can work with arrays of len > 1', () => {
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]);
});
});

// combinedPromises tests
const promiseGenerator = (values: Array<ServerDataPoint>) => {
return Promise.resolve(values);
};

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

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);

expect(testResultOne).toEqual([testOne]);
expect(testResultTwo).toEqual([testOne, testTwo]);
});

it('works with arrays of len 2', async () => {
const promiseArrayOne = [promiseGenerator([testOne]), promiseGenerator([testTwo])];
const promiseArrayTwo = [promiseGenerator([testOne, testTwo]), promiseGenerator([testThree])];
const promiseArrayThree = [promiseGenerator([testOne]), promiseGenerator([testTwo, testThree])];
const promiseArrayFour = [promiseGenerator([testOne, testTwo]), promiseGenerator([testThree, testFour])];
const promiseArrayFive = [promiseGenerator([testOne, testTwo]), 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);

expect(testResultOne).toEqual([testOne, testTwo]);
expect(testResultTwo).toEqual([testOne, testTwo, testThree]);
expect(testResultThree).toEqual([testOne, testTwo, testThree]);
expect(testResultFour).toEqual([testOne, testTwo, testThree, testFour]);
expect(testResultFive).toEqual([testOne, testTwo, testThree]);
});

it('works with arrays of len >= 2', async () => {
const promiseArrayOne = [promiseGenerator([testOne]), promiseGenerator([testTwo]), promiseGenerator([testThree])];
const promiseArrayTwo = [promiseGenerator([testOne]), promiseGenerator([testTwo]), promiseGenerator([testTwo])];
const promiseArrayThree = [promiseGenerator([testOne]), promiseGenerator([testTwo]), promiseGenerator([testThree, testFour])];
const promiseArrayFour = [promiseGenerator([testOne]), promiseGenerator([testTwo, testThree]), 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);

expect(testResultOne).toEqual([testOne, testTwo, testThree]);
expect(testResultTwo).toEqual([testOne, testTwo]);
expect(testResultThree).toEqual([testOne, testTwo, testThree, testFour]);
expect(testResultFour).toEqual([testOne, testTwo, testThree, testFour]);
});
13 changes: 8 additions & 5 deletions www/js/unifiedDataLoader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logInfo } from './plugin/logger'
import { logDebug } from './plugin/logger'
import { getRawEntries } from './commHelper';
import { ServerDataPoint, ServerData, TimeQuery } from './types/diaryTypes'

Expand All @@ -8,7 +8,7 @@ import { ServerDataPoint, ServerData, TimeQuery } from './types/diaryTypes'
* @param list2 same as list1
* @returns a dedup array generated from the input lists
*/
const combineWithDedup = function(list1: Array<ServerDataPoint>, list2: Array<ServerDataPoint>) {
export const combineWithDedup = function(list1: Array<ServerDataPoint>, list2: Array<ServerDataPoint>) {
const combinedList = list1.concat(list2);
return combinedList.filter(function(value, i, array) {
const firstIndexOfValue = array.findIndex(function(element) {
Expand All @@ -24,8 +24,11 @@ const combineWithDedup = function(list1: Array<ServerDataPoint>, list2: Array<Se
* @param combiner a function that takes two arrays and joins them
* @returns A promise which evaluates to a combined list of values or errors
*/
const combinedPromises = function(promiseList: Array<Promise<any>>,
export const combinedPromises = function(promiseList: Array<Promise<any>>,
combiner: (list1: Array<any>, list2: Array<any>) => Array<any> ) {
if (promiseList.length === 0) {
throw new RangeError('combinedPromises needs input array.length >= 1');
}
return new Promise(function(resolve, reject) {
var firstResult = [];
var firstError = null;
Expand All @@ -41,11 +44,11 @@ const combinedPromises = function(promiseList: Array<Promise<any>>,
if (firstError && nextError) {
reject([firstError, nextError]);
} else {
logInfo("About to dedup localResult = "+firstResult.length
logDebug("About to dedup localResult = "+firstResult.length
+"remoteResult = "+nextResult.length);

const dedupedList = combiner(firstResult, nextResult);
logInfo("Deduped list = "+dedupedList.length);
logDebug("Deduped list = "+dedupedList.length);
resolve(dedupedList);
}
}
Expand Down

0 comments on commit 7dd8ffb

Please sign in to comment.