Skip to content

Commit

Permalink
ImplementDb.clear and call at start of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wezm committed Jan 29, 2011
1 parent 5b841e5 commit c2fc8ea
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 70 deletions.
142 changes: 73 additions & 69 deletions spec/kyoto-client_test.coffee
Expand Up @@ -11,83 +11,87 @@ vows.describe('kyoto-client').addBatch(
'after successfully opening':
topic: (db) ->
db.open 'localhost', 1978

'getting a non-existent value':
topic: (db) ->
db.get 'not-here', this.callback
undefined

'returns null': (error, value) ->
assert.isNull value

'setting a value':
topic: (db) ->
db.set 'test', "Test Value", (error) =>
db.get 'test', this.callback
undefined

'allows the value to be retrieved': (error, value) ->
assert.equal value.toString('utf8'), "Test Value"

'getBulk':
topic: (db) ->
db.set 'bulk1', "Bulk Value 1", (error) =>
db.set 'bulk2', "Bulk Value 2", (error) =>
db.getBulk ['bulk1', 'bulk2', 'missing'], this.callback
undefined

'allows multiple values to be retrieved at once': (error, results) ->
db.clear(this.callback)
undefined

'and clearing':
topic: (error, db) ->
db

'getting a non-existent value':
topic: (db) ->
db.get 'not-here', this.callback
undefined

'returns null': (error, value) ->
assert.isNull value

'setting a value':
topic: (db) ->
db.set 'test', "Test Value", (error) =>
db.get 'test', this.callback
undefined

'allows the value to be retrieved': (error, value) ->
assert.equal value.toString('utf8'), "Test Value"

'getBulk':
topic: (db) ->
db.set 'bulk1', "Bulk Value 1", (error) =>
db.set 'bulk2', "Bulk Value 2", (error) =>
db.getBulk ['bulk1', 'bulk2', 'missing'], this.callback
undefined

'allows multiple values to be retrieved at once': (error, results) ->
assert.equal results.bulk1, "Bulk Value 1"
assert.equal results.bulk2, "Bulk Value 2"
assert.isUndefined results.missing

'getBulk with escaped values':
topic: (db) ->
db.set 'bulk3', "Bulk\tValue", (error) =>
db.set 'bulk4', "Bulk Value 2", (error) =>
db.getBulk ['bulk3', 'bulk4'], this.callback
undefined
'getBulk with escaped values':
topic: (db) ->
db.set 'bulk3', "Bulk\tValue", (error) =>
db.set 'bulk4', "Bulk Value 2", (error) =>
db.getBulk ['bulk3', 'bulk4'], this.callback
undefined

'allows multiple values to be retrieved at once': (error, results) ->
'allows multiple values to be retrieved at once': (error, results) ->
assert.equal results.bulk3, "Bulk\tValue"
assert.equal results.bulk4, "Bulk Value 2"
assert.isUndefined results.missing


'Cursor with starting key':
topic: (db) ->
# Add a value for the cursor to retrieve
db.set 'cursor-test', "Cursor\tValue", (error) =>
db.getCursor 'cursor-test', this.callback
undefined

'current value can be retrieved': (error, cursor) ->
cursor.get (error, key, value) ->
assert.equal key.toString('utf8'), "cursor-test"
assert.equal value.toString('utf8'), "Cursor\tValue"

'can be enumerated': (error, cursor) ->
count = 0
cursor.each (error, key, value) ->
assert.isUndefined error
if key?
count++
else
assert.ok count > 0, "count is greater than zero"

'Cursor with no start key':
topic: (db) ->
db.getCursor this.callback
undefined

'can be enumerated': (error, cursor) ->
count = 0
cursor.each (error, key, value) ->
assert.isUndefined error
if key?
count++
else
assert.ok count > 0, "count is greater than zero"

'Cursor with starting key':
topic: (db) ->
# Add a value for the cursor to retrieve
db.set 'cursor-test', "Cursor\tValue", (error) =>
db.getCursor 'cursor-test', this.callback
undefined

'current value can be retrieved': (error, cursor) ->
cursor.get (error, key, value) ->
assert.equal key.toString('utf8'), "cursor-test"
assert.equal value.toString('utf8'), "Cursor\tValue"

'can be enumerated': (error, cursor) ->
count = 0
cursor.each (error, key, value) ->
assert.isUndefined error
if key?
count++
else
assert.ok count > 0, "count is greater than zero"

'Cursor with no start key':
topic: (db) ->
db.getCursor this.callback
undefined

'can be enumerated': (error, cursor) ->
count = 0
cursor.each (error, key, value) ->
assert.isUndefined error
if key?
count++
else
assert.ok count > 0, "count is greater than zero"

).export(module)
11 changes: 10 additions & 1 deletion src/db.coffee
Expand Up @@ -121,8 +121,17 @@ class DB
when 200 then callback undefined, data
else callback new Error("Unexpected response from server: #{response.statusCode}");

# Remove all values
clear: (callback) ->
request = @client.request 'GET', '/rpc/clear',
'Connection': 'keep-alive'
request.end()

clear: (error) ->
request.on 'response', (response) ->
response.on 'end', ->
switch response.statusCode
when 200 then callback()
else callback new Error("Unexpected response from server: #{response.statusCode}");

# TODO: Add optional database arg
# Note: value can be a string or Buffer for utf-8 strings it should be a Buffer
Expand Down

0 comments on commit c2fc8ea

Please sign in to comment.