You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
chore: refactor codebase to promises
This commit is contained in:
@@ -12,16 +12,13 @@ describe('detectBuffers', () => {
|
||||
detectBuffers: true
|
||||
})
|
||||
|
||||
beforeEach((done) => {
|
||||
beforeEach(() => {
|
||||
client = redis.createClient.apply(null, args)
|
||||
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')
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
return Promise.all([
|
||||
client.flushdb(),
|
||||
client.hmset('hash key 2', 'key 1', 'val 1', 'key 2', 'val 2'),
|
||||
client.set('string key 1', 'string value')
|
||||
])
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
@@ -30,45 +27,41 @@ describe('detectBuffers', () => {
|
||||
|
||||
describe('get', () => {
|
||||
describe('first argument is a string', () => {
|
||||
it('returns a string', (done) => {
|
||||
client.get('string key 1', helper.isString('string value', done))
|
||||
it('returns a string', () => {
|
||||
return client.get('string key 1').then(helper.isString('string value'))
|
||||
})
|
||||
|
||||
it('returns a string when executed as part of transaction', (done) => {
|
||||
client.multi().get('string key 1').exec((err, res) => {
|
||||
helper.isString('string value', done)(err, res[0])
|
||||
})
|
||||
it('returns a string when executed as part of transaction', () => {
|
||||
return client.multi().get('string key 1').exec().then(helper.isString('string value'))
|
||||
})
|
||||
})
|
||||
|
||||
describe('first argument is a buffer', () => {
|
||||
it('returns a buffer', (done) => {
|
||||
client.get(Buffer.from('string key 1'), (err, reply) => {
|
||||
it('returns a buffer', () => {
|
||||
return client.get(Buffer.from('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(Buffer.from('string key 1')).exec((err, reply) => {
|
||||
it('returns a buffer when executed as part of transaction', () => {
|
||||
return client.multi().get(Buffer.from('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('can interleave string and buffer results', (done) => {
|
||||
client.multi()
|
||||
it('can interleave string and buffer results', () => {
|
||||
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) => {
|
||||
.exec().then((reply) => {
|
||||
assert.strictEqual(true, Array.isArray(reply))
|
||||
assert.strictEqual(4, reply.length)
|
||||
assert.strictEqual('val 1', reply[0])
|
||||
@@ -77,19 +70,18 @@ describe('detectBuffers', () => {
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply[2]))
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[2].inspect())
|
||||
assert.strictEqual('val 2', reply[3])
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('batch.hget', () => {
|
||||
it('can interleave string and buffer results', (done) => {
|
||||
client.batch()
|
||||
it('can interleave string and buffer results', () => {
|
||||
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) => {
|
||||
.exec().then((reply) => {
|
||||
assert.strictEqual(true, Array.isArray(reply))
|
||||
assert.strictEqual(4, reply.length)
|
||||
assert.strictEqual('val 1', reply[0])
|
||||
@@ -98,71 +90,56 @@ describe('detectBuffers', () => {
|
||||
assert.strictEqual(true, Buffer.isBuffer(reply[2]))
|
||||
assert.strictEqual('<Buffer 76 61 6c 20 32>', reply[2].inspect())
|
||||
assert.strictEqual('val 2', reply[3])
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('hmget', () => {
|
||||
describe('first argument is a string', () => {
|
||||
it('returns strings for keys requested', (done) => {
|
||||
client.hmget('hash key 2', 'key 1', 'key 2', (err, reply) => {
|
||||
it('returns strings for keys requested', () => {
|
||||
return client.hmget('hash key 2', 'key 1', 'key 2').then((reply) => {
|
||||
assert.strictEqual(true, Array.isArray(reply))
|
||||
assert.strictEqual(2, reply.length)
|
||||
assert.strictEqual('val 1', reply[0])
|
||||
assert.strictEqual('val 2', reply[1])
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
|
||||
it('returns strings for keys requested in transaction', (done) => {
|
||||
client.multi().hmget('hash key 2', 'key 1', 'key 2').exec((err, reply) => {
|
||||
it('returns strings for keys requested in transaction', () => {
|
||||
return client.multi().hmget('hash key 2', 'key 1', 'key 2').exec().then((reply) => {
|
||||
assert.strictEqual(true, Array.isArray(reply))
|
||||
assert.strictEqual(1, reply.length)
|
||||
assert.strictEqual(2, reply[0].length)
|
||||
assert.strictEqual('val 1', reply[0][0])
|
||||
assert.strictEqual('val 2', reply[0][1])
|
||||
return done(err)
|
||||
})
|
||||
})
|
||||
|
||||
it('handles array of strings with undefined values (repro #344)', (done) => {
|
||||
client.hmget('hash key 2', 'key 3', 'key 4', (err, reply) => {
|
||||
assert.strictEqual(true, Array.isArray(reply))
|
||||
assert.strictEqual(2, reply.length)
|
||||
assert.strictEqual(null, reply[0])
|
||||
assert.strictEqual(null, reply[1])
|
||||
return done(err)
|
||||
})
|
||||
it('handles array of strings with undefined values (repro #344)', () => {
|
||||
return client.hmget('hash key 2', 'key 3', 'key 4')
|
||||
.then(helper.isDeepEqual([null, null]))
|
||||
})
|
||||
|
||||
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))
|
||||
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)
|
||||
})
|
||||
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(helper.isDeepEqual([[null, null]]))
|
||||
})
|
||||
})
|
||||
|
||||
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) => {
|
||||
it('returns buffers for keys requested', () => {
|
||||
return client.hmget(Buffer.from('hash key 2'), 'key 1', 'key 2').then((reply) => {
|
||||
assert.strictEqual(true, Array.isArray(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) => {
|
||||
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(true, Array.isArray(reply))
|
||||
assert.strictEqual(1, reply.length)
|
||||
assert.strictEqual(2, reply[0].length)
|
||||
@@ -170,12 +147,11 @@ describe('detectBuffers', () => {
|
||||
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) => {
|
||||
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(true, Array.isArray(reply))
|
||||
assert.strictEqual(1, reply.length)
|
||||
assert.strictEqual(2, reply[0].length)
|
||||
@@ -183,63 +159,49 @@ describe('detectBuffers', () => {
|
||||
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 string values', (done) => {
|
||||
client.hgetall('hash key 2', (err, reply) => {
|
||||
assert.strictEqual('object', typeof reply)
|
||||
assert.strictEqual(2, Object.keys(reply).length)
|
||||
assert.strictEqual('val 1', reply['key 1'])
|
||||
assert.strictEqual('val 2', reply['key 2'])
|
||||
return done(err)
|
||||
})
|
||||
it('returns string values', () => {
|
||||
return client.hgetall('hash key 2').then(helper.isDeepEqual({
|
||||
'key 1': 'val 1',
|
||||
'key 2': 'val 2'
|
||||
}))
|
||||
})
|
||||
|
||||
it('returns string values when executed in transaction', (done) => {
|
||||
client.multi().hgetall('hash key 2').exec((err, reply) => {
|
||||
assert.strictEqual(1, reply.length)
|
||||
assert.strictEqual('object', typeof reply[0])
|
||||
assert.strictEqual(2, Object.keys(reply[0]).length)
|
||||
assert.strictEqual('val 1', reply[0]['key 1'])
|
||||
assert.strictEqual('val 2', reply[0]['key 2'])
|
||||
return done(err)
|
||||
})
|
||||
it('returns string values when executed in transaction', () => {
|
||||
return client.multi().hgetall('hash key 2').exec().then(helper.isDeepEqual([{
|
||||
'key 1': 'val 1',
|
||||
'key 2': 'val 2'
|
||||
}]))
|
||||
})
|
||||
|
||||
it('returns string values when executed in .batch', (done) => {
|
||||
client.batch().hgetall('hash key 2').exec((err, reply) => {
|
||||
assert.strictEqual(1, reply.length)
|
||||
assert.strictEqual('object', typeof reply[0])
|
||||
assert.strictEqual(2, Object.keys(reply[0]).length)
|
||||
assert.strictEqual('val 1', reply[0]['key 1'])
|
||||
assert.strictEqual('val 2', reply[0]['key 2'])
|
||||
return done(err)
|
||||
})
|
||||
it('returns string values when executed in .batch', () => {
|
||||
return client.batch().hgetall('hash key 2').exec().then(helper.isDeepEqual([{
|
||||
'key 1': 'val 1',
|
||||
'key 2': 'val 2'
|
||||
}]))
|
||||
})
|
||||
})
|
||||
|
||||
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)
|
||||
@@ -247,12 +209,11 @@ describe('detectBuffers', () => {
|
||||
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)
|
||||
@@ -260,7 +221,6 @@ describe('detectBuffers', () => {
|
||||
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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user