You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-07 13:22:56 +03:00
chore: refactor codebase to promises
This commit is contained in:
@@ -29,46 +29,42 @@ describe('returnBuffers', () => {
|
||||
assert.strictEqual(msg, 'WARNING: You activated returnBuffers and detectBuffers at the same time. The return value is always going to be a buffer.')
|
||||
end()
|
||||
})
|
||||
client.once('error', done)
|
||||
client.once('connect', () => {
|
||||
client.flushdb((err) => {
|
||||
client.hmset('hash key 2', 'key 1', 'val 1', 'key 2', 'val 2')
|
||||
client.set('string key 1', 'string value')
|
||||
end(err)
|
||||
})
|
||||
})
|
||||
client.flushdb()
|
||||
client.hmset('hash key 2', 'key 1', 'val 1', 'key 2', 'val 2')
|
||||
client.set('string key 1', 'string value').then(end)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
client.end(true)
|
||||
})
|
||||
|
||||
describe('get', () => {
|
||||
describe('first argument is a string', () => {
|
||||
it('returns a buffer', (done) => {
|
||||
client.get('string key 1', (err, reply) => {
|
||||
it('returns a buffer', () => {
|
||||
return client.get('string key 1').then((reply) => {
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply))
|
||||
assert.strictEqual('<Buffer 73 74 72 69 6e 67 20 76 61 6c 75 65>', reply.inspect())
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
|
||||
it('returns a bufffer when executed as part of transaction', (done) => {
|
||||
client.multi().get('string key 1').exec((err, reply) => {
|
||||
it('returns a buffer when executed as part of transaction', () => {
|
||||
return client.multi().get('string key 1').exec().then((reply) => {
|
||||
assert.strictEqual(1, reply.length)
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply[0]))
|
||||
assert.strictEqual('<Buffer 73 74 72 69 6e 67 20 76 61 6c 75 65>', reply[0].inspect())
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('multi.hget', () => {
|
||||
it('returns buffers', (done) => {
|
||||
client.multi()
|
||||
it('returns buffers', () => {
|
||||
return client.multi()
|
||||
.hget('hash key 2', 'key 1')
|
||||
.hget(Buffer.from('hash key 2'), 'key 1')
|
||||
.hget('hash key 2', Buffer.from('key 2'))
|
||||
.hget('hash key 2', 'key 2')
|
||||
.exec((err, reply) => {
|
||||
assert.strictEqual(true, Array.isArray(reply))
|
||||
.exec().then((reply) => {
|
||||
assert.strictEqual(4, reply.length)
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0].inspect())
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply[1]))
|
||||
@@ -77,20 +73,18 @@ describe('returnBuffers', () => {
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[2].inspect())
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply[3]))
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[3].inspect())
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('batch.hget', () => {
|
||||
it('returns buffers', (done) => {
|
||||
client.batch()
|
||||
it('returns buffers', () => {
|
||||
return client.batch()
|
||||
.hget('hash key 2', 'key 1')
|
||||
.hget(Buffer.from('hash key 2'), 'key 1')
|
||||
.hget('hash key 2', Buffer.from('key 2'))
|
||||
.hget('hash key 2', 'key 2')
|
||||
.exec((err, reply) => {
|
||||
assert.strictEqual(true, Array.isArray(reply))
|
||||
.exec().then((reply) => {
|
||||
assert.strictEqual(4, reply.length)
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0].inspect())
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply[1]))
|
||||
@@ -99,117 +93,103 @@ describe('returnBuffers', () => {
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[2].inspect())
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply[3]))
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[3].inspect())
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('hmget', () => {
|
||||
describe('first argument is a string', () => {
|
||||
it('handles array of strings with undefined values in transaction (repro #344)', (done) => {
|
||||
client.multi().hmget('hash key 2', 'key 3', 'key 4').exec((err, reply) => {
|
||||
assert.strictEqual(true, Array.isArray(reply))
|
||||
it('handles array of strings with undefined values in transaction (repro #344)', () => {
|
||||
return client.multi().hmget('hash key 2', 'key 3', 'key 4').exec().then((reply) => {
|
||||
assert.strictEqual(1, reply.length)
|
||||
assert.strictEqual(2, reply[0].length)
|
||||
assert.strictEqual(null, reply[0][0])
|
||||
assert.strictEqual(null, reply[0][1])
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('first argument is a buffer', () => {
|
||||
it('returns buffers for keys requested', (done) => {
|
||||
client.hmget(Buffer.from('hash key 2'), 'key 1', 'key 2', (err, reply) => {
|
||||
assert.strictEqual(true, Array.isArray(reply))
|
||||
it('returns buffers for keys requested', () => {
|
||||
return client.hmget(Buffer.from('hash key 2'), 'key 1', 'key 2').then((reply) => {
|
||||
assert.strictEqual(2, reply.length)
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply[0]))
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply[1]))
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0].inspect())
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[1].inspect())
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
|
||||
it('returns buffers for keys requested in transaction', (done) => {
|
||||
client.multi().hmget(Buffer.from('hash key 2'), 'key 1', 'key 2').exec((err, reply) => {
|
||||
assert.strictEqual(true, Array.isArray(reply))
|
||||
it('returns buffers for keys requested in transaction', () => {
|
||||
return client.multi().hmget(Buffer.from('hash key 2'), 'key 1', 'key 2').exec().then((reply) => {
|
||||
assert.strictEqual(1, reply.length)
|
||||
assert.strictEqual(2, reply[0].length)
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply[0][0]))
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply[0][1]))
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0][0].inspect())
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[0][1].inspect())
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
|
||||
it('returns buffers for keys requested in .batch', (done) => {
|
||||
client.batch().hmget(Buffer.from('hash key 2'), 'key 1', 'key 2').exec((err, reply) => {
|
||||
assert.strictEqual(true, Array.isArray(reply))
|
||||
it('returns buffers for keys requested in .batch', () => {
|
||||
return client.batch().hmget(Buffer.from('hash key 2'), 'key 1', 'key 2').exec().then((reply) => {
|
||||
assert.strictEqual(1, reply.length)
|
||||
assert.strictEqual(2, reply[0].length)
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply[0][0]))
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply[0][1]))
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0][0].inspect())
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[0][1].inspect())
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('hgetall', (done) => {
|
||||
describe('hgetall', () => {
|
||||
describe('first argument is a string', () => {
|
||||
it('returns buffer values', (done) => {
|
||||
client.hgetall('hash key 2', (err, reply) => {
|
||||
it('returns buffer values', () => {
|
||||
return client.hgetall('hash key 2').then((reply) => {
|
||||
assert.strictEqual('object', typeof reply)
|
||||
assert.strictEqual(2, Object.keys(reply).length)
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply['key 1'].inspect())
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply['key 2'].inspect())
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
|
||||
it('returns buffer values when executed in transaction', (done) => {
|
||||
client.multi().hgetall('hash key 2').exec((err, reply) => {
|
||||
it('returns buffer values when executed in transaction', () => {
|
||||
return client.multi().hgetall('hash key 2').exec().then((reply) => {
|
||||
assert.strictEqual(1, reply.length)
|
||||
assert.strictEqual('object', typeof reply[0])
|
||||
assert.strictEqual(2, Object.keys(reply[0]).length)
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0]['key 1'].inspect())
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[0]['key 2'].inspect())
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
|
||||
it('returns buffer values when executed in .batch', (done) => {
|
||||
client.batch().hgetall('hash key 2').exec((err, reply) => {
|
||||
it('returns buffer values when executed in .batch', () => {
|
||||
return client.batch().hgetall('hash key 2').exec().then((reply) => {
|
||||
assert.strictEqual(1, reply.length)
|
||||
assert.strictEqual('object', typeof reply[0])
|
||||
assert.strictEqual(2, Object.keys(reply[0]).length)
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0]['key 1'].inspect())
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[0]['key 2'].inspect())
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('first argument is a buffer', () => {
|
||||
it('returns buffer values', (done) => {
|
||||
client.hgetall(Buffer.from('hash key 2'), (err, reply) => {
|
||||
assert.strictEqual(null, err)
|
||||
it('returns buffer values', () => {
|
||||
return client.hgetall(Buffer.from('hash key 2')).then((reply) => {
|
||||
assert.strictEqual('object', typeof reply)
|
||||
assert.strictEqual(2, Object.keys(reply).length)
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply['key 1']))
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply['key 2']))
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply['key 1'].inspect())
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply['key 2'].inspect())
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
|
||||
it('returns buffer values when executed in transaction', (done) => {
|
||||
client.multi().hgetall(Buffer.from('hash key 2')).exec((err, reply) => {
|
||||
it('returns buffer values when executed in transaction', () => {
|
||||
return client.multi().hgetall(Buffer.from('hash key 2')).exec().then((reply) => {
|
||||
assert.strictEqual(1, reply.length)
|
||||
assert.strictEqual('object', typeof reply[0])
|
||||
assert.strictEqual(2, Object.keys(reply[0]).length)
|
||||
@@ -217,12 +197,11 @@ describe('returnBuffers', () => {
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply[0]['key 2']))
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0]['key 1'].inspect())
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[0]['key 2'].inspect())
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
|
||||
it('returns buffer values when executed in .batch', (done) => {
|
||||
client.batch().hgetall(Buffer.from('hash key 2')).exec((err, reply) => {
|
||||
it('returns buffer values when executed in .batch', () => {
|
||||
return client.batch().hgetall(Buffer.from('hash key 2')).exec().then((reply) => {
|
||||
assert.strictEqual(1, reply.length)
|
||||
assert.strictEqual('object', typeof reply[0])
|
||||
assert.strictEqual(2, Object.keys(reply[0]).length)
|
||||
@@ -230,15 +209,15 @@ describe('returnBuffers', () => {
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply[0]['key 2']))
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 31>', reply[0]['key 1'].inspect())
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[0]['key 2'].inspect())
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('publish/subscribe', (done) => {
|
||||
describe('publish/subscribe', () => {
|
||||
let pub
|
||||
let sub
|
||||
|
||||
const channel = 'test channel'
|
||||
const message = Buffer.from('test message')
|
||||
|
||||
@@ -247,25 +226,12 @@ describe('returnBuffers', () => {
|
||||
})
|
||||
|
||||
beforeEach((done) => {
|
||||
let pubConnected
|
||||
let subConnected
|
||||
const end = helper.callFuncAfter(done, 2)
|
||||
|
||||
pub = redis.createClient.apply(redis.createClient, basicArgs)
|
||||
sub = redis.createClient.apply(null, args)
|
||||
pub.once('connect', () => {
|
||||
pub.flushdb(() => {
|
||||
pubConnected = true
|
||||
if (subConnected) {
|
||||
done()
|
||||
}
|
||||
})
|
||||
})
|
||||
sub.once('connect', () => {
|
||||
subConnected = true
|
||||
if (pubConnected) {
|
||||
done()
|
||||
}
|
||||
})
|
||||
pub.flushdb().then(end)
|
||||
sub.once('connect', end)
|
||||
})
|
||||
|
||||
it('receives buffer messages', (done) => {
|
||||
@@ -276,7 +242,7 @@ describe('returnBuffers', () => {
|
||||
sub.on('message', (chnl, msg) => {
|
||||
assert.strictEqual(true, Buffer.isBuffer(msg))
|
||||
assert.strictEqual('<Buffer 74 65 73 74 20 6d 65 73 73 61 67 65>', msg.inspect())
|
||||
return done()
|
||||
done()
|
||||
})
|
||||
|
||||
sub.subscribe(channel)
|
||||
|
Reference in New Issue
Block a user