list tests

v1.18.x
barisusakli 10 years ago
parent 8aa75b7af4
commit 3e8c29e5f9

@ -18,7 +18,9 @@ module.exports = function(db, module) {
}
if (exists) {
db.collection('objects').update({_key:key}, {$push: {array: {$each: [value], $position: 0}}}, {upsert:true, w:1 }, callback);
db.collection('objects').update({_key:key}, {$push: {array: {$each: [value], $position: 0}}}, {upsert:true, w:1 }, function(err, res) {
callback(err);
});
} else {
module.listAppend(key, value, callback);
}
@ -31,7 +33,9 @@ module.exports = function(db, module) {
return callback();
}
value = helpers.valueToString(value);
db.collection('objects').update({ _key: key }, { $push: { array: value } }, {upsert:true, w:1}, callback);
db.collection('objects').update({ _key: key }, { $push: { array: value } }, {upsert:true, w:1}, function(err, res) {
callback(err);
});
};
module.listRemoveLast = function(key, callback) {
@ -50,28 +54,32 @@ module.exports = function(db, module) {
});
};
module.listTrim = function(key, start, stop, callback) {
callback = callback || helpers.noop;
module.listRemoveAll = function(key, value, callback) {
callback = callback || helpers.noop;
if (!key) {
return callback();
}
module.getListRange(key, start, stop, function(err, value) {
if (err) {
return callback(err);
}
value = helpers.valueToString(value);
db.collection('objects').update({_key: key}, {$set: {array: value}}, callback);
db.collection('objects').update({_key: key }, { $pull: { array: value } }, function(err, res) {
callback(err);
});
};
module.listRemoveAll = function(key, value, callback) {
callback = callback || helpers.noop;
module.listTrim = function(key, start, stop, callback) {
callback = callback || helpers.noop;
if (!key) {
return callback();
}
value = helpers.valueToString(value);
module.getListRange(key, start, stop, function(err, value) {
if (err) {
return callback(err);
}
db.collection('objects').update({_key: key }, { $pull: { array: value } }, callback);
db.collection('objects').update({_key: key}, {$set: {array: value}}, function(err, res) {
callback(err);
});
});
};
module.getListRange = function(key, start, stop, callback) {

@ -2,11 +2,15 @@
module.exports = function(redisClient, module) {
module.listPrepend = function(key, value, callback) {
redisClient.lpush(key, value, callback);
redisClient.lpush(key, value, function(err, res) {
callback(err);
});
};
module.listAppend = function(key, value, callback) {
redisClient.rpush(key, value, callback);
redisClient.rpush(key, value, function(err, res) {
callback(err);
});
};
module.listRemoveLast = function(key, callback) {
@ -14,14 +18,18 @@ module.exports = function(redisClient, module) {
};
module.listRemoveAll = function(key, value, callback) {
redisClient.lrem(key, 0, value, callback);
redisClient.lrem(key, 0, value, function(err, res) {
callback(err);
});
};
module.getListRange = function(key, start, stop, callback) {
redisClient.lrange(key, start, stop, callback);
module.listTrim = function(key, start, stop, callback) {
redisClient.ltrim(key, start, stop, function(err, res) {
callback(err);
});
};
module.listTrim = function(key, start, stop, callback) {
redisClient.ltrim(key, start, stop, callback);
module.getListRange = function(key, start, stop, callback) {
redisClient.lrange(key, start, stop, callback);
};
};

@ -11,6 +11,7 @@ describe('Test database', function() {
});
require('./database/keys');
require('./database/list');
it('should not throw err', function(done) {
var objectKey = 'testObj';
@ -108,49 +109,6 @@ describe('Test database', function() {
});
});
it('should not throw err', function(done) {
function listAppend(callback) {
db.listAppend('myList5', 5, function(err, data) {
callback(err, {'listAppend': data});
});
}
function listPrepend(callback) {
db.listPrepend('myList5', 4, function(err, data) {
callback(err, {'listPrepend': data});
});
}
function listRemoveLast(callback) {
db.listRemoveLast('myList5', function(err, data) {
callback(err, {'listRemoveLast': data});
});
}
function getListRange(callback) {
db.getListRange('myList5', 0, -1, function(err, data) {
callback(err, {'getListRange': data});
});
}
var listTasks = [
listAppend,
listPrepend,
getListRange,
listRemoveLast,
getListRange
];
async.series(listTasks, function(err, results) {
assert.equal(err, null, 'error in list methods: ' + err);
assert.ok(results);
done();
});
});
it('should not throw err', function(done) {
function setAdd(callback) {

@ -1,3 +1,4 @@
'use strict';
var async = require('async'),
assert = require('assert'),

@ -0,0 +1,126 @@
'use strict';
var async = require('async'),
assert = require('assert'),
db = require('../mocks/databasemock');
describe('List methods', function() {
describe('listAppend()', function() {
it('should append to a list', function(done) {
db.listAppend('testList', 5, function(err) {
assert.equal(err, null, 'db.listAppend error');
assert.equal(arguments.length, 1, 'arguments.length error');
done();
});
});
});
describe('listPrepend()', function() {
it('should prepend to a list', function(done) {
db.listPrepend('testList2', 3, function(err) {
assert.equal(err, null, 'db.listPrepend error');
assert.equal(arguments.length, 1, 'arguments.length error');
done();
});
});
it('should prepend 2 more elements to a list', function(done) {
async.series([
function(next) {
db.listPrepend('testList2', 2, next);
},
function(next) {
db.listPrepend('testList2', 1, next);
}
], function(err) {
assert.equal(err, null, 'db.listPrepend error');
done();
});
});
});
describe('getListRange()', function() {
it('should return an empty list', function(done) {
db.getListRange('doesnotexist', 0, -1, function(err, list) {
assert.equal(err, null, 'db.getListRange error');
assert.equal(arguments.length, 2, 'arguments.length error');
assert.equal(Array.isArray(list), true, 'list is not an array');
assert.equal(list.length, 0, 'list not empty');
done();
});
});
it('should return a list with one element', function(done) {
db.getListRange('testList', 0, 0, function(err, list) {
assert.equal(err, null, 'db.getListRange error');
assert.equal(Array.isArray(list), true, 'list is not an array');
assert.equal(list[0], 5, 'list does not have value');
done();
});
});
it('should return a list with 3 elements 1,2,3', function(done) {
db.getListRange('testList2', 0, -1, function(err, list) {
assert.equal(err, null, 'db.getListRange error');
assert.equal(Array.isArray(list), true, 'list is not an array');
assert.equal(list.length, 3, 'list length is not 3');
assert.deepEqual(list, ['1', '2', '3'], 'lists not equal');
done();
});
});
});
describe('listRemoveLast()', function() {
it('should remove the last element of list', function(done) {
db.listRemoveLast('testList2', function(err, lastElement) {
assert.equal(err, null, 'db.listRemoveLast error');
assert.equal(arguments.length, 2, 'arguments.length error');
assert.equal(lastElement, '3', 'last element not correct');
done();
});
});
});
describe('listRemoveAll()', function() {
it('should remove all the elements of list', function(done) {
db.listRemoveAll('testList2', function(err) {
assert.equal(err, null, 'db.listRemoveAll error');
assert.equal(arguments.length, 1, 'arguments.length error');
db.getListRange('testList2', function(err, list) {
assert.equal(Array.isArray(list), true, 'list is not an array');
assert.equal(list.length, 0, 'list is not empty');
done();
});
});
});
});
describe('listTrim()', function() {
it('should trim list to a certain range', function(done) {
var list = ['1', '2', '3', '4', '5'];
async.eachSeries(list, function(value, next) {
db.listAppend('testList2', value, next);
}, function(err) {
if (err) {
return done(err);
}
db.listTrim('testList2', 0, 2, function(err) {
assert.equal(err, null, 'db.listTrim error');
assert.equal(arguments.length, 1, 'arguments.length error');
db.getListRange('testList2', 0, -1, function(err, list) {
assert.equal(list.length, 3, 'list length is not 3');
assert.deepEqual(list, ['1', '2', '3'], 'lists not properly trimmed');
});
});
});
});
});
after(function() {
db.flushdb();
});
});
Loading…
Cancel
Save