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.

354 lines
8.5 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('Key methods', () => {
beforeEach((done) => {
10 years ago
db.set('testKey', 'testValue', done);
});
10 years ago
it('should set a key without error', (done) => {
db.set('testKey', 'testValue', function (err) {
assert.ifError(err);
assert(arguments.length < 2);
10 years ago
done();
});
});
it('should get a key without error', (done) => {
db.get('testKey', function (err, value) {
assert.ifError(err);
assert.equal(arguments.length, 2);
assert.strictEqual(value, 'testValue');
10 years ago
done();
});
});
it('should return null if key does not exist', (done) => {
db.get('doesnotexist', (err, value) => {
7 years ago
assert.ifError(err);
assert.equal(value, null);
done();
});
});
it('should return true if key exist', (done) => {
db.exists('testKey', function (err, exists) {
assert.ifError(err);
assert.equal(arguments.length, 2);
assert.strictEqual(exists, true);
done();
});
});
it('should return false if key does not exist', (done) => {
db.exists('doesnotexist', function (err, exists) {
assert.ifError(err);
assert.equal(arguments.length, 2);
assert.strictEqual(exists, false);
10 years ago
done();
});
});
it('should work for an array of keys', (done) => {
db.exists(['testKey', 'doesnotexist'], (err, exists) => {
6 years ago
assert.ifError(err);
assert.deepStrictEqual(exists, [true, false]);
done();
});
});
describe('scan', () => {
it('should scan keys for pattern', async () => {
await db.sortedSetAdd('ip:123:uid', 1, 'a');
await db.sortedSetAdd('ip:123:uid', 2, 'b');
await db.sortedSetAdd('ip:124:uid', 2, 'b');
await db.sortedSetAdd('ip:1:uid', 1, 'a');
await db.sortedSetAdd('ip:23:uid', 1, 'a');
const data = await db.scan({ match: 'ip:1*' });
assert.equal(data.length, 3);
assert(data.includes('ip:123:uid'));
assert(data.includes('ip:124:uid'));
assert(data.includes('ip:1:uid'));
});
});
it('should delete a key without error', (done) => {
db.delete('testKey', function (err) {
assert.ifError(err);
assert(arguments.length < 2);
10 years ago
db.get('testKey', (err, value) => {
assert.ifError(err);
assert.equal(false, !!value);
10 years ago
done();
});
});
});
it('should return false if key was deleted', (done) => {
db.delete('testKey', function (err) {
assert.ifError(err);
assert(arguments.length < 2);
db.exists('testKey', (err, exists) => {
assert.ifError(err);
10 years ago
assert.strictEqual(exists, false);
done();
});
10 years ago
});
});
it('should delete all keys passed in', (done) => {
10 years ago
async.parallel([
function (next) {
10 years ago
db.set('key1', 'value1', next);
},
function (next) {
10 years ago
db.set('key2', 'value2', next);
},
], (err) => {
10 years ago
if (err) {
return done(err);
}
db.deleteAll(['key1', 'key2'], function (err) {
assert.ifError(err);
assert.equal(arguments.length, 1);
10 years ago
async.parallel({
key1exists: function (next) {
db.exists('key1', next);
},
key2exists: function (next) {
db.exists('key2', next);
},
}, (err, results) => {
assert.ifError(err);
assert.equal(results.key1exists, false);
assert.equal(results.key2exists, false);
10 years ago
done();
});
});
});
});
it('should delete all sorted set elements', (done) => {
async.parallel([
function (next) {
db.sortedSetAdd('deletezset', 1, 'value1', next);
},
function (next) {
db.sortedSetAdd('deletezset', 2, 'value2', next);
},
], (err) => {
if (err) {
return done(err);
}
db.delete('deletezset', (err) => {
assert.ifError(err);
async.parallel({
key1exists: function (next) {
db.isSortedSetMember('deletezset', 'value1', next);
},
key2exists: function (next) {
db.isSortedSetMember('deletezset', 'value2', next);
},
}, (err, results) => {
assert.ifError(err);
assert.equal(results.key1exists, false);
assert.equal(results.key2exists, false);
done();
});
});
});
});
describe('increment', () => {
it('should initialize key to 1', (done) => {
db.increment('keyToIncrement', (err, value) => {
assert.ifError(err);
assert.strictEqual(parseInt(value, 10), 1);
10 years ago
done();
});
});
it('should increment key to 2', (done) => {
db.increment('keyToIncrement', (err, value) => {
assert.ifError(err);
assert.strictEqual(parseInt(value, 10), 2);
10 years ago
done();
});
});
it('should set then increment a key', (done) => {
db.set('myIncrement', 1, (err) => {
assert.ifError(err);
db.increment('myIncrement', (err, value) => {
assert.ifError(err);
assert.equal(value, 2);
db.get('myIncrement', (err, value) => {
assert.ifError(err);
assert.equal(value, 2);
done();
});
});
});
});
it('should return the correct value', (done) => {
db.increment('testingCache', (err) => {
assert.ifError(err);
db.get('testingCache', (err, value) => {
assert.ifError(err);
assert.equal(value, 1);
db.increment('testingCache', (err) => {
assert.ifError(err);
db.get('testingCache', (err, value) => {
assert.ifError(err);
assert.equal(value, 2);
done();
});
});
});
});
});
10 years ago
});
describe('rename', () => {
it('should rename key to new name', (done) => {
db.set('keyOldName', 'renamedKeyValue', (err) => {
10 years ago
if (err) {
return done(err);
}
db.rename('keyOldName', 'keyNewName', function (err) {
assert.ifError(err);
assert(arguments.length < 2);
10 years ago
db.get('keyNewName', (err, value) => {
assert.ifError(err);
assert.equal(value, 'renamedKeyValue');
10 years ago
done();
});
});
});
});
it('should rename multiple keys', (done) => {
db.sortedSetAdd('zsettorename', [1, 2, 3], ['value1', 'value2', 'value3'], (err) => {
assert.ifError(err);
db.rename('zsettorename', 'newzsetname', (err) => {
assert.ifError(err);
db.exists('zsettorename', (err, exists) => {
assert.ifError(err);
assert(!exists);
db.getSortedSetRange('newzsetname', 0, -1, (err, values) => {
assert.ifError(err);
assert.deepEqual(['value1', 'value2', 'value3'], values);
done();
});
});
});
});
});
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 not error if old key does not exist', (done) => {
db.rename('doesnotexist', 'anotherdoesnotexist', (err) => {
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);
db.exists('anotherdoesnotexist', (err, exists) => {
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(!exists);
done();
});
});
});
10 years ago
});
7 years ago
describe('type', () => {
it('should return null if key does not exist', (done) => {
db.type('doesnotexist', (err, type) => {
7 years ago
assert.ifError(err);
assert.strictEqual(type, null);
done();
});
});
it('should return hash as type', (done) => {
db.setObject('typeHash', { foo: 1 }, (err) => {
7 years ago
assert.ifError(err);
db.type('typeHash', (err, type) => {
7 years ago
assert.ifError(err);
assert.equal(type, 'hash');
done();
});
});
});
it('should return zset as type', (done) => {
db.sortedSetAdd('typeZset', 123, 'value1', (err) => {
7 years ago
assert.ifError(err);
db.type('typeZset', (err, type) => {
7 years ago
assert.ifError(err);
assert.equal(type, 'zset');
done();
});
});
});
it('should return set as type', (done) => {
db.setAdd('typeSet', 'value1', (err) => {
7 years ago
assert.ifError(err);
db.type('typeSet', (err, type) => {
7 years ago
assert.ifError(err);
assert.equal(type, 'set');
done();
});
});
});
it('should return list as type', (done) => {
db.listAppend('typeList', 'value1', (err) => {
7 years ago
assert.ifError(err);
db.type('typeList', (err, type) => {
7 years ago
assert.ifError(err);
assert.equal(type, 'list');
done();
});
});
});
it('should return string as type', (done) => {
db.set('typeString', 'value1', (err) => {
7 years ago
assert.ifError(err);
db.type('typeString', (err, type) => {
7 years ago
assert.ifError(err);
assert.equal(type, 'string');
done();
});
});
});
4 years ago
it('should expire a key using seconds', (done) => {
db.expire('testKey', 86400, (err) => {
4 years ago
assert.ifError(err);
db.ttl('testKey', (err, ttl) => {
4 years ago
assert.ifError(err);
assert.equal(Math.round(86400 / 1000), Math.round(ttl / 1000));
done();
});
});
});
it('should expire a key using milliseconds', (done) => {
db.pexpire('testKey', 86400000, (err) => {
4 years ago
assert.ifError(err);
db.pttl('testKey', (err, pttl) => {
4 years ago
assert.ifError(err);
assert.equal(Math.round(86400000 / 1000000), Math.round(pttl / 1000000));
done();
});
});
});
7 years ago
});
10 years ago
});