Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 174 lines (172 sloc) 5.855 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
require('./helpers')
_ = require('underscore')

describe 'redis-keyspace prefix for keys', () ->
  client = null
  client2 = null
  beforeEach () ->
    client = getClientWithPrefixAndDB keyspace_name
    client2 = getClientWithPrefixAndDB 'other_prefix'
    client.set('someKey', 'someValue', testError )
    client2.set('someKey', 'otherValue', testError )
  afterEach () ->
    client.FLUSHDB testError
    client.quit()
    client2.quit()
  it 'should delete keys is different keyspaces', () ->
    runBlock 'exists in keyspace A', (done) ->
      client.exists('someKey', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(1)
        done()
      )
    runBlock 'del in keyspace A', (done) ->
      client.del('someKey', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(1)
        done()
      )
    runBlock 'do not exists in keyspace A', (done) ->
      client.exists('someKey', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(0)
        done()
      )
  it 'should rename keys in different keyspaces', () ->
    runBlock 'rename in keyspace B', (done) ->
      client2.rename('someKey', 'renamedKey', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual('OK')
        done()
      )
    runBlock 'renamed exists in keyspace B', (done) ->
      client2.exists('renamedKey', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(1)
        done()
      )
    runBlock 'renamenx in keyspace A', (done) ->
      client.renamenx('someKey', 'otherRenamedKey', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(1)
        done()
      )
    runBlock 'renamednx exists in keyspace A', (done) ->
      client.exists('otherRenamedKey', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(1)
        done()
      )
  it 'should get the type of a key', () ->
    runBlock 'type of key', (done) ->
      client.type('someKey', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual('string')
        done()
      )
  it 'should manage keys expirations', () ->
    runBlock 'expire key', (done) ->
      client.expire('someKey', 10, testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(1)
        done()
      )
    runBlock 'ttl key', (done) ->
      client.ttl('someKey', testAsync (error,reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(10)
        done()
      )
    runBlock 'persist key', (done) ->
      client.persist('someKey', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(1)
        done()
      )
    runBlock 'expireat key', (done) ->
      client.expireat('someKey', +new Date() + 20000, testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(1)
        done()
      )
    runBlock 'ttl key after expireat', (done) ->
      client.ttl('someKey', testAsync (error,reply) ->
        expect(error).toBeNull()
        expect(reply).toBeGreaterThan(1000000000000)
        done()
      )
  it 'should move keys between db', () ->
    runBlock 'prepare client on other db', (done) ->
      @client3 = getClientWithPrefixAndDB keyspace_name
      @client3.select(14, testError)
      done()
    runBlock 'move key', (done) ->
      client.move('someKey', 14, testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(1)
        done()
      )
    runBlock 'exists moved key', (done) ->
      @client3.exists('someKey', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(1)
        done()
      )
    runBlock 'do not exists moved key', (done) ->
      client.exists('someKey', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(0)
        done()
      )
    runBlock 'del other db key', (done) ->
      @client3.del('someKey', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(1)
        done()
      )
    runBlock 'close other db client', (done) ->
      @client3.quit()
      done()
  it 'should get object in keyspace', () ->
    runBlock 'object refcount key', (done) ->
      client.object('REFCOUNT', 'someKey', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(1)
        done()
      )
    runBlock 'object encoding key', (done) ->
      client.object('ENCODING', 'someKey', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual('raw')
        done()
      )
    runBlock 'object idletime key', (done) ->
      client.object('IDLETIME', 'someKey', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(0)
        done()
      )
  it 'should get the keys by pattern', () ->
    runBlock 'keys*', (done) ->
      client.keys('*', testAsync (error, reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(['someKey'])
        done()
      )
    runBlock 'keys someKey', (done) ->
      client.keys('someKey', testAsync (error,reply) ->
        expect(error).toBeNull()
        expect(reply).toEqual(['someKey'])
        done()
      )
  it 'should get randomkey inside keyspace', () ->
    client.rename('someKey', 'r1', testError)
    client.set('r2', 'v2', testError)
    client.set('r3', 'v3', testError)
    client.set('r4', 'v4', testError)
    client.set('r5', 'v5', testError)
    runBlock 'randomkey', (done) ->
      client.randomkey(testAsync (error,reply) ->
        expect(error).toBeNull()
        expect(_.include(['r1','r2','r3','r4','r5'], reply)).toBeTruthy()
        done()
      )
    
Something went wrong with that request. Please try again.