filterpidsbycid tests

v1.18.x
Baris Usakli 8 years ago
parent 816809ce3c
commit 0f05ae4ac1

@ -54,31 +54,36 @@ module.exports = function (Posts) {
Posts.filterPidsByCid = function (pids, cid, callback) {
if (!cid) {
return callback(null, pids);
return setImmediate(callback, null, pids);
}
if (!Array.isArray(cid) || cid.length === 1) {
// Single cid
db.isSortedSetMembers('cid:' + parseInt(cid, 10) + ':pids', pids, function (err, isMembers) {
if (err) {
return callback(err);
}
return filterPidsBySingleCid(pids, cid, callback);
}
async.waterfall([
function (next) {
async.map(cid, function (cid, next) {
Posts.filterPidsByCid(pids, cid, next);
}, next);
},
function (pidsArr, next) {
next(null, _.union.apply(_, pidsArr));
},
], callback);
};
function filterPidsBySingleCid(pids, cid, callback) {
async.waterfall([
function (next) {
db.isSortedSetMembers('cid:' + parseInt(cid, 10) + ':pids', pids, next);
},
function (isMembers, next) {
pids = pids.filter(function (pid, index) {
return pid && isMembers[index];
});
callback(null, pids);
});
} else {
// Multiple cids
async.map(cid, function (cid, next) {
Posts.filterPidsByCid(pids, cid, next);
}, function (err, pidsArr) {
if (err) {
return callback(err);
}
callback(null, _.union.apply(_, pidsArr));
});
}
};
next(null, pids);
},
], callback);
}
};

@ -714,6 +714,34 @@ describe('Post\'s', function () {
});
});
describe('filterPidsByCid', function () {
it('should return pids as is if cid is falsy', function (done) {
posts.filterPidsByCid([1, 2, 3], null, function (err, pids) {
assert.ifError(err);
assert.deepEqual([1, 2, 3], pids);
done();
});
});
it('should filter pids by single cid', function (done) {
posts.filterPidsByCid([postData.pid, 100, 101], cid, function (err, pids) {
assert.ifError(err);
assert.deepEqual([postData.pid], pids);
done();
});
});
it('should filter pids by multiple cids', function (done) {
posts.filterPidsByCid([postData.pid, 100, 101], [cid, 2, 3], function (err, pids) {
assert.ifError(err);
assert.deepEqual([postData.pid], pids);
done();
});
});
});
after(function (done) {
db.emptydb(done);
});

Loading…
Cancel
Save