[Pkg-javascript-commits] [node-leveldown] 219/492: extended put()/get()/del() tests
Andrew Kelley
andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:14:01 UTC 2014
This is an automated email from the git hooks/post-receive script.
andrewrk-guest pushed a commit to annotated tag rocksdb-0.10.1
in repository node-leveldown.
commit b879b3123171ab874670db2afad1704716224839
Author: Rod Vagg <rod at vagg.org>
Date: Sun Feb 17 18:39:08 2013 +1100
extended put()/get()/del() tests
---
src/database.cc | 15 +++---
test/put-get-del-test.js | 136 +++++++++++++++++++++++++++++++++++++++++++++++
test/put-test.js | 134 ----------------------------------------------
3 files changed, 143 insertions(+), 142 deletions(-)
diff --git a/src/database.cc b/src/database.cc
index 3a4dec6..9c6ac32 100644
--- a/src/database.cc
+++ b/src/database.cc
@@ -239,7 +239,6 @@ v8::Handle<v8::Value> Database::Get (const v8::Arguments& args) {
METHOD_SETUP_COMMON(put, 1, 2)
CB_ERR_IF_NULL_OR_UNDEFINED(0, Key)
- CB_ERR_IF_NULL_OR_UNDEFINED(1, Value)
v8::Local<v8::Value> keyBufferV = args[0];
STRING_OR_BUFFER_TO_SLICE(key, keyBufferV, Key)
@@ -265,15 +264,15 @@ v8::Handle<v8::Value> Database::Get (const v8::Arguments& args) {
v8::Handle<v8::Value> Database::Delete (const v8::Arguments& args) {
v8::HandleScope scope;
- Database* database = node::ObjectWrap::Unwrap<Database>(args.This());
- v8::Persistent<v8::Function> callback =
- v8::Persistent<v8::Function>::New(v8::Local<v8::Function>::Cast(args[2]));
+ METHOD_SETUP_COMMON(put, 1, 2)
+
+ CB_ERR_IF_NULL_OR_UNDEFINED(0, Key)
+
+ v8::Local<v8::Value> keyBufferV = args[0];
+ STRING_OR_BUFFER_TO_SLICE(key, keyBufferV, Key)
- CB_ERR_IF_NULL_OR_UNDEFINED(0, "Key")
+ v8::Persistent<v8::Value> keyBuffer = v8::Persistent<v8::Value>::New(keyBufferV);
- v8::Persistent<v8::Value> keyBuffer = v8::Persistent<v8::Value>::New(args[0]);
- STRING_OR_BUFFER_TO_SLICE(key, keyBuffer, Key)
- v8::Local<v8::Object> optionsObj = v8::Local<v8::Object>::Cast(args[1]);
BOOLEAN_OPTION_VALUE(optionsObj, sync)
DeleteWorker* worker = new DeleteWorker(
diff --git a/test/put-get-del-test.js b/test/put-get-del-test.js
new file mode 100644
index 0000000..480da66
--- /dev/null
+++ b/test/put-get-del-test.js
@@ -0,0 +1,136 @@
+const fs = require('fs')
+ , path = require('path')
+ , test = require('tap').test
+ , testCommon = require('./common')
+ , leveldown = require('../')
+
+ , testBuffer = fs.readFileSync(path.join(__dirname, 'data/testdata.bin'))
+
+/**** SETUP & UTILITY STUFF ****/
+
+var db
+
+ , makeGetDelErrorTests = function (type, key, expectedError) {
+ test('get() with ' + type + ' causes error', function (t) {
+ db.get(key, function (err) {
+ t.ok(err, 'has error')
+ t.type(err, Error)
+ t.like(err.message, expectedError, 'correct error message')
+ t.end()
+ })
+ })
+
+ test('del() with ' + type + ' causes error', function (t) {
+ db.del(key, function (err) {
+ t.ok(err, 'has error')
+ t.type(err, Error)
+ t.like(err.message, expectedError, 'correct error message')
+ t.end()
+ })
+ })
+ }
+
+ , makePutErrorTest = function (type, key, value, expectedError) {
+ test('put() with ' + type + ' causes error', function (t) {
+ db.put(key, value, function (err) {
+ t.ok(err, 'has error')
+ t.type(err, Error)
+ t.like(err.message, expectedError, 'correct error message')
+ t.end()
+ })
+ })
+ }
+
+ , makePutGetDelSuccessfulTest = function (type, key, value) {
+ test('put()/get()/del() with ' + type, function (t) {
+ db.put(key, value, function (err) {
+ t.notOk(err, 'no error')
+ db.get(key, function (err, _value) {
+ t.notOk(err, 'no error, has key/value for `' + key + '`')
+ t.equals(_value.toString(), value.toString())
+ db.del(key, function (err) {
+ t.notOk(err, 'no error, deleted key/value for `' + key + '`')
+ db.get(key, function (err) {
+ t.ok(err, 'entry propertly deleted')
+ t.like(err.message, /NotFound/)
+ t.end()
+ })
+ })
+ })
+ })
+ })
+ }
+
+ , makeErrorKeyTest = function (type, key, expectedError) {
+ makeGetDelErrorTests(type, key, expectedError)
+ makePutErrorTest(type, key, 'foo', expectedError)
+ }
+
+/**** SETUP ENVIRONMENT ****/
+
+test('setUp', function (t) {
+ testCommon.setUp.apply(null, arguments)
+ db = leveldown(testCommon.location())
+ db.open(t.end.bind(t))
+})
+
+/**** TEST ERROR KEYS ****/
+
+makeErrorKeyTest('null key', null, /Key argument cannot be `null` or `undefined`/)
+makeErrorKeyTest('undefined key', undefined, /Key argument cannot be `null` or `undefined`/)
+makeErrorKeyTest('empty String key', '', /Key argument cannot be an empty String/)
+makeErrorKeyTest('empty Buffer key', new Buffer(0), /Key argument cannot be an empty Buffer/)
+makeErrorKeyTest('empty Array key', [], /Key argument cannot be an empty String/)
+
+/**** TEST NON-ERROR KEYS ****/
+
+// valid falsey keys
+makePutGetDelSuccessfulTest('`false` key', false, 'foo false')
+makePutGetDelSuccessfulTest('`0` key', 0, 'foo 0')
+makePutGetDelSuccessfulTest('`NaN` key', NaN, 'foo NaN')
+
+// standard String key
+makePutGetDelSuccessfulTest(
+ 'long String key'
+ , 'some long string that I\'m using as a key for this unit test, cross your fingers dude, we\'re going in!'
+ , 'foo'
+)
+
+// Buffer key
+makePutGetDelSuccessfulTest('Buffer key', testBuffer, 'foo')
+
+// non-empty Array as a key
+makePutGetDelSuccessfulTest('Array value', 'foo', [1,2,3,4])
+
+/**** TEST ERROR VALUES ****/
+
+makePutErrorTest('null value', 'foo', null, /Value argument cannot be `null` or `undefined`/)
+makePutErrorTest('undefined value', 'foo', undefined, /Value argument cannot be `null` or `undefined`/)
+makePutErrorTest('empty String value', 'foo', '', /Value argument cannot be an empty String/)
+makePutErrorTest('empty Buffer value', 'foo', new Buffer(0), /Value argument cannot be an empty Buffer/)
+makePutErrorTest('empty Array value', 'foo', [], /Value argument cannot be an empty String/)
+
+// valid falsey values
+makePutGetDelSuccessfulTest('`false` value', 'foo false', false)
+makePutGetDelSuccessfulTest('`0` value', 'foo 0', 0)
+makePutGetDelSuccessfulTest('`NaN` value', 'foo NaN', NaN)
+
+// standard String value
+makePutGetDelSuccessfulTest(
+ 'long String value'
+ , 'foo'
+ , 'some long string that I\'m using as a key for this unit test, cross your fingers dude, we\'re going in!'
+)
+
+// standard Buffer value
+makePutGetDelSuccessfulTest('Buffer key', 'foo', testBuffer)
+
+// non-empty Array as a value
+makePutGetDelSuccessfulTest('Array value', [1,2,3,4], 'foo')
+
+/**** CLEANUP ENVIRONMENT ****/
+
+test('tearDown', function (t) {
+ testCommon.tearDown.apply(null, arguments)
+ db.close(t.end.bind(t))
+})
\ No newline at end of file
diff --git a/test/put-test.js b/test/put-test.js
deleted file mode 100644
index c1a2d42..0000000
--- a/test/put-test.js
+++ /dev/null
@@ -1,134 +0,0 @@
-const fs = require('fs')
- , path = require('path')
- , test = require('tap').test
- , testCommon = require('./common')
- , leveldown = require('../')
-
-var db
-
- , makePutErrorTest = function (type, key, value, expectedError) {
- test('put() with ' + type + ' causes error', function (t) {
- db.put(key, value, function (err) {
- t.ok(err, 'has error')
- t.type(err, Error)
- t.like(err.message, expectedError, 'correct error message')
- t.end()
- })
- })
- }
-
- , makePutTest = function (t, key, value, callback) {
- db.put(key, value, function (err) {
- t.notOk(err, 'no error')
- db.get(key, function (err, _value) {
- t.notOk(err, 'no error, has key/value for `' + key + '`')
- t.equals(_value.toString(), value.toString())
- callback()
- })
- })
- }
-
-test('setUp', function (t) {
- testCommon.setUp.apply(null, arguments)
- db = leveldown(testCommon.location())
- db.open(t.end.bind(t))
-})
-
-makePutErrorTest('null key', null, 'foo', /Key argument cannot be `null` or `undefined`/)
-makePutErrorTest('undefined key', undefined, 'foo', /Key argument cannot be `null` or `undefined`/)
-makePutErrorTest('empty String key', '', 'foo', /Key argument cannot be an empty String/)
-makePutErrorTest('empty Buffer key', new Buffer(0), 'foo', /Key argument cannot be an empty Buffer/)
-makePutErrorTest('empty Array key', [], 'foo', /Key argument cannot be an empty String/)
-
-test('put() with other falsey keys works', function (t) {
- t.plan(9)
-
- var done = function () {
- if (++done.count == done.expected)
- return t.end()
- }
-
- , testPut = function (key, value) {
- done.expected++
- makePutTest(t, key, value, done)
- }
-
- testPut(false, 'foo false')
- testPut(0, 'foo 0')
- testPut(NaN, 'foo NaN')
-})
-
-test('put() with plain String key works', function (t) {
- makePutTest(
- t
- , 'some long string that I\'m using as a key for this unit test, cross your fingers dude, we\'re going in!'
- , 'foo'
- , t.end.bind(t)
- )
-})
-
-test('put() with plain Buffer key works', function (t) {
- fs.readFile(path.join(__dirname, 'data/testdata.bin'), function (err, data) {
- t.notOk(err, 'read test data file')
- t.type(data, Buffer)
-
- makePutTest(
- t
- , data
- , 'foo'
- , t.end.bind(t)
- )
- })
-})
-
-makePutErrorTest('null value', 'foo', null, /Value argument cannot be `null` or `undefined`/)
-makePutErrorTest('undefined value', 'foo', undefined, /Value argument cannot be `null` or `undefined`/)
-makePutErrorTest('empty String value', 'foo', '', /Value argument cannot be an empty String/)
-makePutErrorTest('empty Buffer value', 'foo', new Buffer(0), /Value argument cannot be an empty Buffer/)
-makePutErrorTest('empty Array value', 'foo', [], /Value argument cannot be an empty String/)
-
-test('put() with other falsey values works', function (t) {
- t.plan(9)
-
- var done = function () {
- if (++done.count == done.expected)
- return t.end()
- }
-
- , testPut = function (key, value) {
- done.expected++
- makePutTest(t, key, value, done)
- }
-
- testPut('foo false', false)
- testPut('foo 0', 0)
- testPut('foo NaN', NaN)
-})
-
-test('put() with plain String value works', function (t) {
- makePutTest(
- t
- , 'foo'
- , 'some long string that I\'m using as a value for this unit test, cross your fingers dude, we\'re going in!'
- , t.end.bind(t)
- )
-})
-
-test('put() with plain Buffer value works', function (t) {
- fs.readFile(path.join(__dirname, 'data/testdata.bin'), function (err, data) {
- t.notOk(err, 'read test data file')
- t.type(data, Buffer)
-
- makePutTest(
- t
- , 'foo'
- , data
- , t.end.bind(t)
- )
- })
-})
-
-test('tearDown', function (t) {
- testCommon.tearDown.apply(null, arguments)
- db.close(t.end.bind(t))
-})
\ No newline at end of file
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-leveldown.git
More information about the Pkg-javascript-commits
mailing list