Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Commit

Permalink
support values to be null/undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
kesla committed Oct 19, 2014
1 parent a46108d commit e77bbd4
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 48 deletions.
5 changes: 2 additions & 3 deletions lib/levelup.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,8 @@ LevelUP.prototype.put = function (key_, value_, options, callback) {

callback = getCallback(options, callback)

if (key_ === null || key_ === undefined
|| value_ === null || value_ === undefined)
return writeError(this, 'put() requires key and value arguments', callback)
if (key_ === null || key_ === undefined)
return writeError(this, 'put() requires a key argument', callback)

if (maybeError(this, options, callback))
return
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"xtend": "~3.0.0"
},
"devDependencies": {
"leveldown": "~0.10.0",
"leveldown": "~1.0.0",
"bustermove": "*",
"tap": "*",
"referee": "*",
Expand Down
8 changes: 1 addition & 7 deletions test/argument-checking-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,10 @@ buster.testCase('Argument checking', {

assert.exception(
db.put.bind(db)
, { name: 'WriteError', message: 'put() requires key and value arguments' }
, { name: 'WriteError', message: 'put() requires a key argument' }
, 'no-arg put() throws'
)

assert.exception(
db.put.bind(db, 'foo')
, { name: 'WriteError', message: 'put() requires key and value arguments' }
, 'callback-less, 1-arg put() throws'
)

done()
})
}
Expand Down
18 changes: 2 additions & 16 deletions test/batch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,23 +247,9 @@ buster.testCase('batch()', {

, 'test batch#put() with missing `value`': function () {
// value = undefined
assert.exception(this.batch.put.bind(this.batch, 'foo1'), function (err) {
console.log('err.name', err.name, 'err.message', err.message)
if (err.name != 'WriteError')
return false
if ('value cannot be `null` or `undefined`' != err.message)
return false
return true
})
this.batch.put('foo1')

// value = null
assert.exception(this.batch.put.bind(this.batch, 'foo1', null), function (err) {
if (err.name != 'WriteError')
return false
if ('value cannot be `null` or `undefined`' != err.message)
return false
return true
})
this.batch.put('foo1', null)
}

, 'test batch#put() with missing `key`': function () {
Expand Down
8 changes: 1 addition & 7 deletions test/get-put-del-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,10 @@ buster.testCase('get() / put() / del()', {

assert.exception(
db.put.bind(db)
, { name: 'WriteError', message: 'put() requires key and value arguments' }
, { name: 'WriteError', message: 'put() requires a key argument' }
, 'no-arg put() throws'
)

assert.exception(
db.put.bind(db, 'foo')
, { name: 'WriteError', message: 'put() requires key and value arguments' }
, 'callback-less, 1-arg put() throws'
)

done()
})
}
Expand Down
22 changes: 8 additions & 14 deletions test/null-and-undefined-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,36 +80,30 @@ buster.testCase('null & undefined keys & values', {
})
}

, 'put() with null value causes error': function (done) {
, 'put() with null value works': function (done) {
this.db.put('foo', null, function (err, value) {
refute(value)
assert.isInstanceOf(err, Error)
assert.isInstanceOf(err, errors.LevelUPError)
refute(err)
done()
})
}

, 'put() with undefined value causes error': function (done) {
, 'put() with undefined value works': function (done) {
this.db.put('foo', undefined, function (err, value) {
refute(value)
assert.isInstanceOf(err, Error)
assert.isInstanceOf(err, errors.LevelUPError)
refute(err)
done()
})
}
, 'batch() with undefined value causes error': function (done) {
, 'batch() with undefined value works': function (done) {
this.db.batch([{key: 'foo', value: undefined, type: 'put'}]
, function (err) {
assert.isInstanceOf(err, Error)
assert.isInstanceOf(err, errors.LevelUPError)
refute(err)
done()
})
}
, 'batch() with null value causes error': function (done) {
, 'batch() with null value works': function (done) {
this.db.batch([{key: 'foo', value: null, type: 'put'}]
, function (err) {
assert.isInstanceOf(err, Error)
assert.isInstanceOf(err, errors.LevelUPError)
refute(err)
done()
})
}
Expand Down

0 comments on commit e77bbd4

Please sign in to comment.