Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring file in src/batch.js #77

Open
wants to merge 13 commits into
base: f24
Choose a base branch
from
Binary file added dump.rdb
Binary file not shown.
16 changes: 14 additions & 2 deletions src/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@ const DEFAULT_BATCH_SIZE = 100;

const sleep = util.promisify(setTimeout);

exports.processSortedSet = async function (setKey, process, options) {
options = options || {};
// I will reduce the complexity of the code by making helper functions outside the main function
// where the errors show

// helper functions:
// fixing error 1:
function processIsFunction(process) {
if (typeof process !== 'function') {
throw new Error('[[error:process-not-a-function]]');
}
}


exports.processSortedSet = async function (setKey, process, options) {
console.log('Hakaabi: Refactored code is running!');
options = options || {};

processIsFunction(process);

// Progress bar handling (upgrade scripts)
if (options.progress) {
Expand Down Expand Up @@ -68,6 +79,7 @@ exports.processSortedSet = async function (setKey, process, options) {
stop = start + options.batch - 1;
}
};
console.log('Hakaabi: Refactored code stopped running!');

exports.processArray = async function (array, process, options) {
options = options || {};
Expand Down
38 changes: 38 additions & 0 deletions test/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,44 @@ describe('batch', () => {
db.sortedSetAdd('processMe', scores, values, done);
});

it('should throw error if process is not a function in processSortedSet', async () => {
await assert.rejects(
batch.processSortedSet('processMe', 'notAFunction'),
new Error('[[error:process-not-a-function]]')
);
});

it('should throw error if process is not a function in processArray', async () => {
await assert.rejects(
batch.processArray(scores, 'notAFunction'),
new Error('[[error:process-not-a-function]]')
);
});

it('should correctly set options.progress.total if options.progress is defined', async () => {
const originalSortedSetCard = db.sortedSetCard;
db.sortedSetCard = () => Promise.resolve(100);

const options = { progress: {} };
await batch.processSortedSet('testSet', () => {}, options);

assert.strictEqual(options.progress.total, 100);

db.sortedSetCard = originalSortedSetCard;
});

it('should use db.processSortedSet if conditions are met', async () => {
const originalProcessSortedSet = db.processSortedSet;
db.processSortedSet = () => Promise.resolve('done');

const options = { doneIf: null, alwaysStartAt: undefined };
const result = await batch.processSortedSet('testSet', () => {}, options);

assert.strictEqual(result, 'done');

db.processSortedSet = originalProcessSortedSet;
});

it('should process sorted set with callbacks', (done) => {
let total = 0;
batch.processSortedSet('processMe', (items, next) => {
Expand Down