Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add database option support to DB.cas

  • Loading branch information...
commit 2d0088684b7cc12ea29648389effe4524bf7b54f 1 parent 48aee77
@wezm authored
Showing with 36 additions and 15 deletions.
  1. +7 −6 src/db.coffee
  2. +29 −9 test/db_test.coffee
View
13 src/db.coffee
@@ -244,18 +244,19 @@ class DB
cas: (key, oval, nval, args...) ->
switch args.length
- when 1 then callback = args[0]
+ when 1
+ options = {}
+ callback = args[0]
when 2
- database = args[0]
+ options = args[0]
callback = args[1]
else
throw new Error("Invalid number of arguments (#{args.length}) to cas");
- rpc_args =
- key: key
- oval: oval
+ rpc_args = this._initRpcArgs options
+ rpc_args.key = key
+ rpc_args.oval = oval if oval?
rpc_args.nval = nval if nval?
- rpc_args.DB = database if database?
@rpcClient.call 'cas', rpc_args, (error, status, output) ->
if error?
callback error, output
View
38 test/db_test.coffee
@@ -270,32 +270,52 @@ module.exports =
'sets the new value when the old value matches': (test) ->
test.expect 2
- db.set 'test', 'old', testDb, ->
- db.cas 'test', 'old', 'new', testDb, (error, output) ->
+ db.set 'test', 'old', ->
+ db.cas 'test', 'old', 'new', (error, output) ->
test.ifError error
- db.get 'test', testDb, (error, value) ->
+ db.get 'test', (error, value) ->
test.equal value, 'new'
test.done()
'doesn\'t set the new value when the old value differs': (test) ->
test.expect 3
- db.set 'test', 'old', testDb, ->
- db.cas 'test', 'not old', 'new', testDb, (error, output) ->
+ db.set 'test', 'old', ->
+ db.cas 'test', 'not old', 'new', (error, output) ->
test.ok error?
test.equal error.message, "Record has changed"
- db.get 'test', testDb, (error, value) ->
+ db.get 'test', (error, value) ->
test.equal value, 'old'
test.done()
'removes the record when the new value is null': (test) ->
test.expect 2
- db.set 'test', 'old', testDb, ->
- db.cas 'test', 'old', null, testDb, (error, output) ->
+ db.set 'test', 'old', ->
+ db.cas 'test', 'old', null, (error, output) ->
test.ifError error
- db.get 'test', testDb, (error, value) ->
+ db.get 'test', (error, value) ->
test.equal value, null
test.done()
+ 'allows an initial value to be set with an old value of null': (test) ->
+ test.expect 2
+ db.cas 'test', null, 'value', (error, output) ->
+ test.ifError error
+ db.get 'test', (error, value) ->
+ test.equal value, 'value'
+ test.done()
+
+ 'allows the database to be specified': (test) ->
+ test.expect 2
+ options = {database: 'test2.kct'}
+ db.add 'test', 'old', options, ->
+ db.cas 'test', 'old', 'new', options, (error, output) ->
+ test.ifError error
+
+ # Check that the value wasn't set on the default db
+ db.get 'test', (error, value) ->
+ test.ok value == null
+ test.done()
+
remove: testCase
setUp: dbClear
Please sign in to comment.
Something went wrong with that request. Please try again.