list test fix

v1.18.x
barisusakli 10 years ago
parent 9750a36dee
commit c62e7ed641

@ -43,7 +43,7 @@ module.exports = function(db, module) {
if (!key) {
return callback();
}
module.getListRange(key, -1, 0, function(err, value) {
module.getListRange(key, -1, -1, function(err, value) {
if (err) {
return callback(err);
}
@ -86,43 +86,17 @@ module.exports = function(db, module) {
if (!key) {
return callback();
}
var skip = start,
limit = stop - start + 1,
splice = false;
if((start < 0 && stop >= 0) || (start >= 0 && stop < 0)) {
skip = 0;
limit = Math.pow(2, 31) - 2;
splice = true;
} else if (start > stop) {
return callback(null, []);
}
db.collection('objects').findOne({_key:key}, { array: { $slice: [skip, limit] }}, function(err, data) {
db.collection('objects').findOne({_key:key}, { array: 1}, function(err, data) {
if(err || !(data && data.array)) {
return callback(err, []);
}
if(splice) {
if(start < 0) {
start = data.array.length - Math.abs(start);
}
if(stop < 0) {
stop = data.array.length - Math.abs(stop);
}
if(start > stop) {
return callback(null, []);
}
var howMany = stop - start + 1;
if(start !== 0 || howMany !== data.array.length) {
data.array = data.array.splice(start, howMany);
}
if (stop === -1) {
data.array = data.array.slice(start);
} else {
data.array = data.array.slice(start, stop + 1);
}
callback(null, data.array);
});
};

@ -83,14 +83,15 @@ describe('List methods', function() {
});
describe('listRemoveAll()', function() {
it('should remove all the elements of list', function(done) {
db.listRemoveAll('testList2', function(err) {
it('should remove all the matching elements of list', function(done) {
db.listRemoveAll('testList2', '1', function(err) {
assert.equal(err, null, 'db.listRemoveAll error');
assert.equal(arguments.length, 1, 'arguments.length error');
db.getListRange('testList2', 0, -1, function(err, list) {
assert.equal(Array.isArray(list), true, 'list is not an array');
assert.equal(list.length, 0, 'list is not empty');
assert.equal(list.length, 1, 'element not removed');
assert.equal(list.indexOf('1'), -1, 'element not removed');
done();
});
});

Loading…
Cancel
Save