You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.1 KiB
JavaScript

10 years ago
'use strict';
10 years ago
/*global require, after, before*/
10 years ago
var async = require('async'),
assert = require('assert'),
db = require('../mocks/databasemock');
describe('List methods', function () {
10 years ago
describe('listAppend()', function () {
it('should append to a list', function (done) {
db.listAppend('testList1', 5, function (err) {
assert.equal(err, null);
assert.equal(arguments.length, 1);
10 years ago
done();
});
});
});
describe('listPrepend()', function () {
it('should prepend to a list', function (done) {
db.listPrepend('testList2', 3, function (err) {
assert.equal(err, null);
assert.equal(arguments.length, 1);
10 years ago
done();
});
});
it('should prepend 2 more elements to a list', function (done) {
10 years ago
async.series([
function (next) {
10 years ago
db.listPrepend('testList2', 2, next);
},
function (next) {
10 years ago
db.listPrepend('testList2', 1, next);
}
], function (err) {
assert.equal(err, null);
10 years ago
done();
});
});
});
describe('getListRange()', function () {
before(function (done) {
async.series([
function (next) {
db.listAppend('testList3', 7, next);
},
function (next) {
db.listPrepend('testList3', 3, next);
},
function (next) {
db.listAppend('testList4', 5, next);
}
], done);
});
it('should return an empty list', function (done) {
db.getListRange('doesnotexist', 0, -1, function (err, list) {
assert.equal(err, null);
assert.equal(arguments.length, 2);
assert.equal(Array.isArray(list), true);
assert.equal(list.length, 0);
10 years ago
done();
});
});
it('should return a list with one element', function (done) {
db.getListRange('testList4', 0, 0, function (err, list) {
assert.equal(err, null);
10 years ago
assert.equal(Array.isArray(list), true);
assert.equal(list[0], 5);
10 years ago
done();
});
});
it('should return a list with 2 elements 3, 7', function (done) {
db.getListRange('testList3', 0, -1, function (err, list) {
assert.equal(err, null);
assert.equal(Array.isArray(list), true);
assert.equal(list.length, 2);
assert.deepEqual(list, ['3', '7']);
10 years ago
done();
});
});
});
describe('listRemoveLast()', function () {
before(function (done) {
async.series([
function (next) {
db.listAppend('testList4', 12, next);
},
function (next) {
db.listPrepend('testList4', 9, next);
}
], done);
});
it('should remove the last element of list and return it', function (done) {
db.listRemoveLast('testList4', function (err, lastElement) {
assert.equal(err, null);
assert.equal(arguments.length, 2);
assert.equal(lastElement, '12');
10 years ago
done();
});
});
});
describe('listRemoveAll()', function () {
before(function (done) {
async.series([
async.apply(db.listAppend, 'testList5', 1),
async.apply(db.listAppend, 'testList5', 1),
async.apply(db.listAppend, 'testList5', 1),
async.apply(db.listAppend, 'testList5', 2),
async.apply(db.listAppend, 'testList5', 5)
], done);
});
it('should remove all the matching elements of list', function (done) {
db.listRemoveAll('testList5', '1', function (err) {
assert.equal(err, null);
assert.equal(arguments.length, 1);
10 years ago
db.getListRange('testList5', 0, -1, function (err, list) {
assert.equal(err, null);
assert.equal(Array.isArray(list), true);
assert.equal(list.length, 2);
assert.equal(list.indexOf('1'), -1);
10 years ago
done();
});
});
});
});
describe('listTrim()', function () {
it('should trim list to a certain range', function (done) {
10 years ago
var list = ['1', '2', '3', '4', '5'];
async.eachSeries(list, function (value, next) {
db.listAppend('testList6', value, next);
}, function (err) {
10 years ago
if (err) {
return done(err);
}
db.listTrim('testList6', 0, 2, function (err) {
10 years ago
assert.equal(err, null);
assert.equal(arguments.length, 1);
db.getListRange('testList6', 0, -1, function (err, list) {
assert.equal(err, null);
10 years ago
assert.equal(list.length, 3);
assert.deepEqual(list, ['1', '2', '3']);
10 years ago
done();
10 years ago
});
});
});
});
});
after(function (done) {
db.flushdb(done);
10 years ago
});
});