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.

257 lines
6.6 KiB
JavaScript

10 years ago
'use strict';
10 years ago
const async = require('async');
const assert = require('assert');
const db = require('../mocks/databasemock');
10 years ago
describe('List methods', () => {
describe('listAppend()', () => {
it('should append to a list', (done) => {
db.listAppend('testList1', 5, function (err) {
8 years ago
assert.ifError(err);
assert.equal(arguments.length, 1);
10 years ago
done();
});
});
8 years ago
it('should not add anyhing if key is falsy', (done) => {
db.listAppend(null, 3, (err) => {
8 years ago
assert.ifError(err);
done();
});
});
it('should append each element to list', async () => {
await db.listAppend('arrayListAppend', ['a', 'b', 'c']);
let values = await db.getListRange('arrayListAppend', 0, -1);
assert.deepStrictEqual(values, ['a', 'b', 'c']);
await db.listAppend('arrayListAppend', ['d', 'e']);
values = await db.getListRange('arrayListAppend', 0, -1);
assert.deepStrictEqual(values, ['a', 'b', 'c', 'd', 'e']);
});
10 years ago
});
describe('listPrepend()', () => {
it('should prepend to a list', (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', (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);
},
], (err) => {
assert.equal(err, null);
10 years ago
done();
});
});
8 years ago
it('should not add anyhing if key is falsy', (done) => {
db.listPrepend(null, 3, (err) => {
8 years ago
assert.ifError(err);
done();
});
});
it('should prepend each element to list', async () => {
await db.listPrepend('arrayListPrepend', ['a', 'b', 'c']);
let values = await db.getListRange('arrayListPrepend', 0, -1);
assert.deepStrictEqual(values, ['c', 'b', 'a']);
await db.listPrepend('arrayListPrepend', ['d', 'e']);
values = await db.getListRange('arrayListPrepend', 0, -1);
assert.deepStrictEqual(values, ['e', 'd', 'c', 'b', 'a']);
});
10 years ago
});
describe('getListRange()', () => {
before((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', (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', (done) => {
db.getListRange('testList4', 0, 0, (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', (done) => {
db.getListRange('testList3', 0, -1, (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();
});
});
8 years ago
it('should not get anything if key is falsy', (done) => {
db.getListRange(null, 0, -1, (err, data) => {
8 years ago
assert.ifError(err);
assert.equal(data, undefined);
done();
});
});
10 years ago
});
describe('listRemoveLast()', () => {
before((done) => {
async.series([
function (next) {
db.listAppend('testList7', 12, next);
},
function (next) {
db.listPrepend('testList7', 9, next);
},
], done);
});
it('should remove the last element of list and return it', (done) => {
db.listRemoveLast('testList7', function (err, lastElement) {
assert.equal(err, null);
assert.equal(arguments.length, 2);
assert.equal(lastElement, '12');
10 years ago
done();
});
});
8 years ago
it('should not remove anyhing if key is falsy', (done) => {
db.listRemoveLast(null, (err) => {
8 years ago
assert.ifError(err);
done();
});
});
10 years ago
});
describe('listRemoveAll()', () => {
before((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', (done) => {
db.listRemoveAll('testList5', '1', function (err) {
assert.equal(err, null);
assert.equal(arguments.length, 1);
10 years ago
db.getListRange('testList5', 0, -1, (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();
});
});
});
8 years ago
it('should not remove anyhing if key is falsy', (done) => {
db.listRemoveAll(null, 3, (err) => {
8 years ago
assert.ifError(err);
done();
});
});
it('should remove multiple elements from list', async () => {
await db.listAppend('multiRemoveList', ['a', 'b', 'c', 'd', 'e']);
const initial = await db.getListRange('multiRemoveList', 0, -1);
assert.deepStrictEqual(initial, ['a', 'b', 'c', 'd', 'e']);
await db.listRemoveAll('multiRemoveList', ['b', 'd']);
const values = await db.getListRange('multiRemoveList', 0, -1);
assert.deepStrictEqual(values, ['a', 'c', 'e']);
});
10 years ago
});
describe('listTrim()', () => {
it('should trim list to a certain range', (done) => {
const list = ['1', '2', '3', '4', '5'];
async.eachSeries(list, (value, next) => {
db.listAppend('testList6', value, next);
}, (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, (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
});
});
});
});
8 years ago
it('should not add anyhing if key is falsy', (done) => {
db.listTrim(null, 0, 3, (err) => {
8 years ago
assert.ifError(err);
done();
});
});
10 years ago
});
7 years ago
describe('listLength', () => {
it('should get the length of a list', (done) => {
db.listAppend('getLengthList', 1, (err) => {
7 years ago
assert.ifError(err);
db.listAppend('getLengthList', 2, (err) => {
7 years ago
assert.ifError(err);
db.listLength('getLengthList', (err, length) => {
Async refactor in place (#7736) * feat: allow both callback&and await * feat: ignore async key * feat: callbackify and promisify in same file * Revert "feat: callbackify and promisify in same file" This reverts commit cea206a9b8e6d8295310074b18cc82a504487862. * feat: no need to store .callbackify * feat: change getTopics to async * feat: remove .async * fix: byScore * feat: rewrite topics/index and social with async/await * fix: rewrite topics/data.js fix issue with async.waterfall, only pass result if its not undefined * feat: add callbackify to redis/psql * feat: psql use await * fix: redis :volcano: * feat: less returns * feat: more await rewrite * fix: redis tests * feat: convert sortedSetAdd rewrite psql transaction to async/await * feat: :dog: * feat: test * feat: log client and query * feat: log bind * feat: more logs * feat: more logs * feat: check perform * feat: dont callbackify transaction * feat: remove logs * fix: main functions * feat: more logs * fix: increment * fix: rename * feat: remove cls * fix: remove console.log * feat: add deprecation message to .async usage * feat: update more dbal methods * fix: redis :voodoo: * feat: fix redis zrem, convert setObject * feat: upgrade getObject methods * fix: psql getObjectField * fix: redis tests * feat: getObjectKeys * feat: getObjectValues * feat: isObjectField * fix: add missing return * feat: delObjectField * feat: incrObjectField * fix: add missing await * feat: remove exposed helpers * feat: list methods * feat: flush/empty * feat: delete * fix: redis delete all * feat: get/set * feat: incr/rename * feat: type * feat: expire * feat: setAdd * feat: setRemove * feat: isSetMember * feat: getSetMembers * feat: setCount, setRemoveRandom * feat: zcard,zcount * feat: sortedSetRank * feat: isSortedSetMember * feat: zincrby * feat: sortedSetLex * feat: processSortedSet * fix: add mising await * feat: debug psql * fix: psql test * fix: test * fix: another test * fix: test fix * fix: psql tests * feat: remove logs * feat: user arrow func use builtin async promises * feat: topic bookmarks * feat: topic.delete * feat: topic.restore * feat: topics.purge * feat: merge * feat: suggested * feat: topics/user.js * feat: topics modules * feat: topics/follow * fix: deprecation msg * feat: fork * feat: topics/posts * feat: sorted/recent * feat: topic/teaser * feat: topics/tools * feat: topics/unread * feat: add back node versions disable deprecation notice wrap async controllers in try/catch * feat: use db directly * feat: promisify in place * fix: redis/psql * feat: deprecation message logs for psql * feat: more logs * feat: more logs * feat: logs again * feat: more logs * fix: call release * feat: restore travis, remove logs * fix: loops * feat: remove .async. usage
6 years ago
assert.ifError(err);
assert.equal(length, 2);
done();
});
7 years ago
});
});
});
Async refactor in place (#7736) * feat: allow both callback&and await * feat: ignore async key * feat: callbackify and promisify in same file * Revert "feat: callbackify and promisify in same file" This reverts commit cea206a9b8e6d8295310074b18cc82a504487862. * feat: no need to store .callbackify * feat: change getTopics to async * feat: remove .async * fix: byScore * feat: rewrite topics/index and social with async/await * fix: rewrite topics/data.js fix issue with async.waterfall, only pass result if its not undefined * feat: add callbackify to redis/psql * feat: psql use await * fix: redis :volcano: * feat: less returns * feat: more await rewrite * fix: redis tests * feat: convert sortedSetAdd rewrite psql transaction to async/await * feat: :dog: * feat: test * feat: log client and query * feat: log bind * feat: more logs * feat: more logs * feat: check perform * feat: dont callbackify transaction * feat: remove logs * fix: main functions * feat: more logs * fix: increment * fix: rename * feat: remove cls * fix: remove console.log * feat: add deprecation message to .async usage * feat: update more dbal methods * fix: redis :voodoo: * feat: fix redis zrem, convert setObject * feat: upgrade getObject methods * fix: psql getObjectField * fix: redis tests * feat: getObjectKeys * feat: getObjectValues * feat: isObjectField * fix: add missing return * feat: delObjectField * feat: incrObjectField * fix: add missing await * feat: remove exposed helpers * feat: list methods * feat: flush/empty * feat: delete * fix: redis delete all * feat: get/set * feat: incr/rename * feat: type * feat: expire * feat: setAdd * feat: setRemove * feat: isSetMember * feat: getSetMembers * feat: setCount, setRemoveRandom * feat: zcard,zcount * feat: sortedSetRank * feat: isSortedSetMember * feat: zincrby * feat: sortedSetLex * feat: processSortedSet * fix: add mising await * feat: debug psql * fix: psql test * fix: test * fix: another test * fix: test fix * fix: psql tests * feat: remove logs * feat: user arrow func use builtin async promises * feat: topic bookmarks * feat: topic.delete * feat: topic.restore * feat: topics.purge * feat: merge * feat: suggested * feat: topics/user.js * feat: topics modules * feat: topics/follow * fix: deprecation msg * feat: fork * feat: topics/posts * feat: sorted/recent * feat: topic/teaser * feat: topics/tools * feat: topics/unread * feat: add back node versions disable deprecation notice wrap async controllers in try/catch * feat: use db directly * feat: promisify in place * fix: redis/psql * feat: deprecation message logs for psql * feat: more logs * feat: more logs * feat: logs again * feat: more logs * fix: call release * feat: restore travis, remove logs * fix: loops * feat: remove .async. usage
6 years ago
it('should return 0 if list does not have any elements', (done) => {
db.listLength('doesnotexist', (err, length) => {
Async refactor in place (#7736) * feat: allow both callback&and await * feat: ignore async key * feat: callbackify and promisify in same file * Revert "feat: callbackify and promisify in same file" This reverts commit cea206a9b8e6d8295310074b18cc82a504487862. * feat: no need to store .callbackify * feat: change getTopics to async * feat: remove .async * fix: byScore * feat: rewrite topics/index and social with async/await * fix: rewrite topics/data.js fix issue with async.waterfall, only pass result if its not undefined * feat: add callbackify to redis/psql * feat: psql use await * fix: redis :volcano: * feat: less returns * feat: more await rewrite * fix: redis tests * feat: convert sortedSetAdd rewrite psql transaction to async/await * feat: :dog: * feat: test * feat: log client and query * feat: log bind * feat: more logs * feat: more logs * feat: check perform * feat: dont callbackify transaction * feat: remove logs * fix: main functions * feat: more logs * fix: increment * fix: rename * feat: remove cls * fix: remove console.log * feat: add deprecation message to .async usage * feat: update more dbal methods * fix: redis :voodoo: * feat: fix redis zrem, convert setObject * feat: upgrade getObject methods * fix: psql getObjectField * fix: redis tests * feat: getObjectKeys * feat: getObjectValues * feat: isObjectField * fix: add missing return * feat: delObjectField * feat: incrObjectField * fix: add missing await * feat: remove exposed helpers * feat: list methods * feat: flush/empty * feat: delete * fix: redis delete all * feat: get/set * feat: incr/rename * feat: type * feat: expire * feat: setAdd * feat: setRemove * feat: isSetMember * feat: getSetMembers * feat: setCount, setRemoveRandom * feat: zcard,zcount * feat: sortedSetRank * feat: isSortedSetMember * feat: zincrby * feat: sortedSetLex * feat: processSortedSet * fix: add mising await * feat: debug psql * fix: psql test * fix: test * fix: another test * fix: test fix * fix: psql tests * feat: remove logs * feat: user arrow func use builtin async promises * feat: topic bookmarks * feat: topic.delete * feat: topic.restore * feat: topics.purge * feat: merge * feat: suggested * feat: topics/user.js * feat: topics modules * feat: topics/follow * fix: deprecation msg * feat: fork * feat: topics/posts * feat: sorted/recent * feat: topic/teaser * feat: topics/tools * feat: topics/unread * feat: add back node versions disable deprecation notice wrap async controllers in try/catch * feat: use db directly * feat: promisify in place * fix: redis/psql * feat: deprecation message logs for psql * feat: more logs * feat: more logs * feat: logs again * feat: more logs * fix: call release * feat: restore travis, remove logs * fix: loops * feat: remove .async. usage
6 years ago
assert.ifError(err);
assert.strictEqual(length, 0);
done();
});
});
7 years ago
});
10 years ago
});