1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +03:00
Files
node-redis/test/commands/hset.spec.js
2017-05-19 06:14:29 +02:00

68 lines
2.3 KiB
JavaScript

'use strict'
const Buffer = require('safe-buffer').Buffer
const assert = require('assert')
const config = require('../lib/config')
const helper = require('../helper')
const redis = config.redis
describe('The \'hset\' method', () => {
helper.allTests((ip, args) => {
describe(`using ${ip}`, () => {
let client
const hash = 'test hash'
beforeEach(() => {
client = redis.createClient.apply(null, args)
return client.flushdb()
})
it('allows a value to be set in a hash', () => {
const field = Buffer.from('0123456789')
const value = Buffer.from('abcdefghij')
client.hset(hash, field, value).then(helper.isNumber(1))
return client.hget(hash, field).then(helper.isString(value.toString()))
})
it('handles an empty value', () => {
const field = Buffer.from('0123456789')
const value = Buffer.from('')
client.hset(hash, field, value).then(helper.isNumber(1))
return client.hget([hash, field]).then(helper.isString(''))
})
it('handles empty key and value', () => {
const field = Buffer.from('')
const value = Buffer.from('')
client.hset([hash, field, value]).then(helper.isNumber(1))
return client.hset(hash, field, value).then(helper.isNumber(0))
})
it('warns if someone passed a array either as field or as value', () => {
const hash = 'test hash'
const field = 'array'
// This would be converted to "array contents" but if you use more than one entry,
// it'll result in e.g. "array contents,second content" and this is not supported and considered harmful
const value = ['array contents']
return client.hmset(hash, field, value).then(assert, helper.isError())
})
it('does not error when a buffer and date are set as values on the same hash', () => {
return client.hmset('test hash', 'buffer', Buffer.from('abcdefghij'), 'data', new Date())
.then(helper.isString('OK'))
})
it('does not error when a buffer and date are set as fields on the same hash', () => {
return client.hmset('test hash', Buffer.from('abcdefghij'), 'buffer', new Date(), 'date')
.then(helper.isString('OK'))
})
afterEach(() => {
client.end(true)
})
})
})
})