Permalink
Browse files

Support database option in DB.add

  • Loading branch information...
1 parent c344f6e commit e868735331a94964494a5ea4a97e561391885a14 @wezm committed Apr 4, 2011
Showing with 21 additions and 7 deletions.
  1. +7 −6 src/db.coffee
  2. +14 −1 test/db_test.coffee
View
@@ -124,17 +124,18 @@ class DB
# Add a record if it doesn't already exist
add: (key, value, 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 add");
- rpc_args =
- key: key
- value: value
- rpc_args.DB = database if database?
+ rpc_args = this._initRpcArgs options
+ rpc_args.key = key
+ rpc_args.value = value
@rpcClient.call 'add', rpc_args, (error, status, output) ->
if error?
callback error, output
View
@@ -7,8 +7,11 @@ db = new kt.Db testDb
db.open('localhost', 1979)
dbClear = (callback) ->
+ # Clear default db (tests that behaviour)
db.clear (error, output) ->
- callback()
+ # Clear alternate db (tests specifying the db)
+ db.clear {database: 'test2.kct'}, (error, output) ->
+ callback()
module.exports =
defaultDatabase: testCase
@@ -120,6 +123,16 @@ module.exports =
test.equal error.message, "Record exists"
test.done()
+ 'allows the database to be specified': (test) ->
+ test.expect 2
+ db.add 'test', 'other db', {database: 'test2.kct'}, (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()
+
replace: testCase
setUp: dbClear

0 comments on commit e868735

Please sign in to comment.