2018-01-03 04:05:53 +00:00
|
|
|
// Copyright IBM Corp. 2016,2018. All Rights Reserved.
|
2016-11-21 08:48:43 +00:00
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const utils = require('../lib/utils');
|
|
|
|
const assert = require('assert');
|
2016-11-21 08:48:43 +00:00
|
|
|
|
|
|
|
describe('Utils', function() {
|
|
|
|
describe('uploadInChunks', function() {
|
|
|
|
it('calls process function for each chunk', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const largeArray = ['item1', 'item2', 'item3'];
|
|
|
|
const calls = [];
|
2016-11-21 08:48:43 +00:00
|
|
|
|
|
|
|
utils.uploadInChunks(largeArray, 1, function processFunction(array, cb) {
|
|
|
|
calls.push(array);
|
|
|
|
cb();
|
|
|
|
}, function finished(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assert.deepEqual(calls, [['item1'], ['item2'], ['item3']]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('calls process function only once when array is smaller than chunk size', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const largeArray = ['item1', 'item2'];
|
|
|
|
const calls = [];
|
2016-11-21 08:48:43 +00:00
|
|
|
|
|
|
|
utils.uploadInChunks(largeArray, 3, function processFunction(array, cb) {
|
|
|
|
calls.push(array);
|
|
|
|
cb();
|
|
|
|
}, function finished(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assert.deepEqual(calls, [['item1', 'item2']]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('concats results from each call to the process function', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const largeArray = ['item1', 'item2', 'item3', 'item4'];
|
2016-11-21 08:48:43 +00:00
|
|
|
|
|
|
|
utils.uploadInChunks(largeArray, 2, function processFunction(array, cb) {
|
|
|
|
cb(null, array);
|
|
|
|
}, function finished(err, results) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assert.deepEqual(results, ['item1', 'item2', 'item3', 'item4']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('downloadInChunks', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
let largeArray, calls, chunkSize, skip;
|
2016-11-21 08:48:43 +00:00
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
largeArray = ['item1', 'item2', 'item3'];
|
|
|
|
calls = [];
|
|
|
|
chunkSize = 2;
|
|
|
|
skip = 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
function processFunction(filter, cb) {
|
|
|
|
calls.push(Object.assign({}, filter));
|
2019-10-07 09:45:34 +00:00
|
|
|
const results = [];
|
2016-11-21 08:48:43 +00:00
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
for (let i = 0; i < chunkSize; i++) {
|
2016-11-21 08:48:43 +00:00
|
|
|
if (largeArray[skip + i]) {
|
|
|
|
results.push(largeArray[skip + i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
skip += chunkSize;
|
|
|
|
cb(null, results);
|
|
|
|
}
|
|
|
|
|
|
|
|
it('calls process function with the correct filter', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const expectedFilters = [{skip: 0, limit: chunkSize}, {skip: chunkSize, limit: chunkSize}];
|
2016-11-21 08:48:43 +00:00
|
|
|
utils.downloadInChunks({}, chunkSize, processFunction, function finished(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assert.deepEqual(calls, expectedFilters);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('concats the results of all calls of the process function', function(done) {
|
|
|
|
utils.downloadInChunks({}, chunkSize, processFunction, function finished(err, results) {
|
|
|
|
if (err) return done(err);
|
|
|
|
assert.deepEqual(results, largeArray);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('concatResults', function() {
|
|
|
|
it('concats regular arrays', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const array1 = ['item1', 'item2'];
|
|
|
|
const array2 = ['item3', 'item4'];
|
2016-11-21 08:48:43 +00:00
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const concatResults = utils.concatResults(array1, array2);
|
2016-11-21 08:48:43 +00:00
|
|
|
assert.deepEqual(concatResults, ['item1', 'item2', 'item3', 'item4']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('concats objects containing arrays', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const object1 = {deltas: [{change: 'change 1'}], conflict: []};
|
|
|
|
const object2 = {deltas: [{change: 'change 2'}], conflict: [{conflict: 'conflict 1'}]};
|
|
|
|
const expectedResults = {
|
2016-11-21 08:48:43 +00:00
|
|
|
deltas: [{change: 'change 1'}, {change: 'change 2'}],
|
|
|
|
conflict: [{conflict: 'conflict 1'}],
|
|
|
|
};
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const concatResults = utils.concatResults(object1, object2);
|
2016-11-21 08:48:43 +00:00
|
|
|
assert.deepEqual(concatResults, expectedResults);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|