Files
NodeBB/test/database/list.js

204 lines
5.0 KiB
JavaScript
Raw Normal View History

2014-12-29 16:20:35 -05:00
'use strict';
2014-12-29 16:20:35 -05:00
2017-02-17 20:20:42 -07:00
var async = require('async');
var assert = require('assert');
var db = require('../mocks/databasemock');
2014-12-29 16:20:35 -05:00
describe('List methods', function () {
describe('listAppend()', function () {
it('should append to a list', function (done) {
db.listAppend('testList1', 5, function (err) {
2017-05-19 20:24:54 -04:00
assert.ifError(err);
2015-01-15 14:02:51 -05:00
assert.equal(arguments.length, 1);
2014-12-29 16:20:35 -05:00
done();
});
});
2017-05-19 20:24:54 -04:00
it('should not add anyhing if key is falsy', function (done) {
db.listAppend(null, 3, function (err) {
assert.ifError(err);
done();
});
});
2014-12-29 16:20:35 -05:00
});
describe('listPrepend()', function () {
it('should prepend to a list', function (done) {
db.listPrepend('testList2', 3, function (err) {
2015-01-15 14:02:51 -05:00
assert.equal(err, null);
assert.equal(arguments.length, 1);
2014-12-29 16:20:35 -05:00
done();
});
});
it('should prepend 2 more elements to a list', function (done) {
2014-12-29 16:20:35 -05:00
async.series([
function (next) {
2014-12-29 16:20:35 -05:00
db.listPrepend('testList2', 2, next);
},
function (next) {
2014-12-29 16:20:35 -05:00
db.listPrepend('testList2', 1, next);
2017-02-17 19:31:21 -07:00
},
], function (err) {
2015-01-15 14:02:51 -05:00
assert.equal(err, null);
2014-12-29 16:20:35 -05:00
done();
});
});
2017-05-19 20:24:54 -04:00
it('should not add anyhing if key is falsy', function (done) {
db.listPrepend(null, 3, function (err) {
assert.ifError(err);
done();
});
});
2014-12-29 16:20:35 -05:00
});
describe('getListRange()', function () {
before(function (done) {
2015-01-15 14:02:51 -05:00
async.series([
function (next) {
2015-01-15 14:02:51 -05:00
db.listAppend('testList3', 7, next);
},
function (next) {
2015-01-15 14:02:51 -05:00
db.listPrepend('testList3', 3, next);
},
function (next) {
2015-01-15 14:02:51 -05:00
db.listAppend('testList4', 5, next);
2017-02-17 19:31:21 -07:00
},
2015-01-15 14:02:51 -05:00
], done);
});
it('should return an empty list', function (done) {
db.getListRange('doesnotexist', 0, -1, function (err, list) {
2015-01-15 14:02:51 -05:00
assert.equal(err, null);
assert.equal(arguments.length, 2);
assert.equal(Array.isArray(list), true);
assert.equal(list.length, 0);
2014-12-29 16:20:35 -05:00
done();
});
});
it('should return a list with one element', function (done) {
db.getListRange('testList4', 0, 0, function (err, list) {
2015-01-15 14:02:51 -05:00
assert.equal(err, null);
2015-01-15 15:06:02 -05:00
assert.equal(Array.isArray(list), true);
2015-01-15 14:02:51 -05:00
assert.equal(list[0], 5);
2014-12-29 16:20:35 -05:00
done();
});
});
it('should return a list with 2 elements 3, 7', function (done) {
db.getListRange('testList3', 0, -1, function (err, list) {
2015-01-15 14:02:51 -05:00
assert.equal(err, null);
assert.equal(Array.isArray(list), true);
assert.equal(list.length, 2);
assert.deepEqual(list, ['3', '7']);
2014-12-29 16:20:35 -05:00
done();
});
});
2017-05-19 20:24:54 -04:00
2017-05-19 20:32:31 -04:00
it('should not get anything if key is falsy', function (done) {
2017-05-19 20:24:54 -04:00
db.getListRange(null, 0, -1, function (err, data) {
assert.ifError(err);
assert.equal(data, undefined);
done();
});
});
2014-12-29 16:20:35 -05:00
});
describe('listRemoveLast()', function () {
before(function (done) {
2015-01-15 14:02:51 -05:00
async.series([
function (next) {
2015-01-15 14:02:51 -05:00
db.listAppend('testList4', 12, next);
},
function (next) {
2015-01-15 14:02:51 -05:00
db.listPrepend('testList4', 9, next);
2017-02-17 19:31:21 -07:00
},
2015-01-15 14:02:51 -05:00
], done);
});
it('should remove the last element of list and return it', function (done) {
db.listRemoveLast('testList4', function (err, lastElement) {
2015-01-15 14:02:51 -05:00
assert.equal(err, null);
assert.equal(arguments.length, 2);
assert.equal(lastElement, '12');
2014-12-29 16:20:35 -05:00
done();
});
});
2017-05-19 20:24:54 -04:00
it('should not remove anyhing if key is falsy', function (done) {
db.listRemoveLast(null, function (err) {
assert.ifError(err);
done();
});
});
2014-12-29 16:20:35 -05:00
});
describe('listRemoveAll()', function () {
before(function (done) {
2015-01-15 14:02:51 -05:00
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),
2017-02-17 19:31:21 -07:00
async.apply(db.listAppend, 'testList5', 5),
2015-01-15 14:02:51 -05:00
], done);
});
it('should remove all the matching elements of list', function (done) {
db.listRemoveAll('testList5', '1', function (err) {
2015-01-15 14:02:51 -05:00
assert.equal(err, null);
assert.equal(arguments.length, 1);
2014-12-29 16:20:35 -05:00
db.getListRange('testList5', 0, -1, function (err, list) {
2016-08-16 19:46:59 +02:00
assert.equal(err, null);
2015-01-15 14:02:51 -05:00
assert.equal(Array.isArray(list), true);
assert.equal(list.length, 2);
assert.equal(list.indexOf('1'), -1);
2014-12-29 16:20:35 -05:00
done();
});
});
});
2017-05-19 20:24:54 -04:00
it('should not remove anyhing if key is falsy', function (done) {
db.listRemoveAll(null, 3, function (err) {
assert.ifError(err);
done();
});
});
2014-12-29 16:20:35 -05:00
});
describe('listTrim()', function () {
it('should trim list to a certain range', function (done) {
2014-12-29 16:20:35 -05:00
var list = ['1', '2', '3', '4', '5'];
async.eachSeries(list, function (value, next) {
2015-01-15 14:02:51 -05:00
db.listAppend('testList6', value, next);
}, function (err) {
2014-12-29 16:20:35 -05:00
if (err) {
return done(err);
}
db.listTrim('testList6', 0, 2, function (err) {
2015-01-15 15:33:22 -05:00
assert.equal(err, null);
assert.equal(arguments.length, 1);
db.getListRange('testList6', 0, -1, function (err, list) {
2016-08-16 19:46:59 +02:00
assert.equal(err, null);
2015-01-15 15:33:22 -05:00
assert.equal(list.length, 3);
assert.deepEqual(list, ['1', '2', '3']);
2014-12-29 17:34:56 -05:00
done();
2014-12-29 16:20:35 -05:00
});
});
});
});
2017-05-19 20:24:54 -04:00
it('should not add anyhing if key is falsy', function (done) {
db.listTrim(null, 0, 3, function (err) {
assert.ifError(err);
done();
});
});
2014-12-29 16:20:35 -05:00
});
});