[Pkg-javascript-commits] [node-leveldown] 220/492: tests for basic put/get/del, more args checking

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 b1577260a3eb6860d5d0db56ba9831f5fdc5a9f2
Author: Rod Vagg <rod at vagg.org>
Date:   Sun Feb 17 20:41:13 2013 +1100

    tests for basic put/get/del, more args checking
---
 src/leveldown.h          |  8 ++++----
 test/common.js           |  4 ++--
 test/del-test.js         | 44 ++++++++++++++++++++++++++++++++++++++++++++
 test/get-test.js         | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 test/open-test.js        |  6 ++++++
 test/put-get-del-test.js | 14 ++++++--------
 test/put-test.js         | 45 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 155 insertions(+), 14 deletions(-)

diff --git a/src/leveldown.h b/src/leveldown.h
index e1c886d..7fb093e 100644
--- a/src/leveldown.h
+++ b/src/leveldown.h
@@ -104,7 +104,7 @@
  */
 #define METHOD_SETUP_COMMON(name, optionPos, callbackPos) \
   if (args.Length() == 0) { \
-    THROW_RETURN(name() requires a callback argument 2) \
+    THROW_RETURN(name() requires a callback argument) \
   } \
   Database* database = ObjectWrap::Unwrap<Database>(args.This()); \
   v8::Local<v8::Object> optionsObj; \
@@ -117,18 +117,18 @@
     callback = v8::Persistent<v8::Function>::New( \
       v8::Local<v8::Function>::Cast(args[callbackPos - 1]) \
     ); \
-  } else if (args[optionPos]->IsObject()) { \
+  } else if (args[optionPos]->IsObject() && args[callbackPos]->IsFunction()) { \
     optionsObj = v8::Local<v8::Object>::Cast(args[optionPos]); \
     callback = v8::Persistent<v8::Function>::New( \
       v8::Local<v8::Function>::Cast(args[callbackPos]) \
     ); \
   } else { \
-    THROW_RETURN(name() requires a callback argument 3) \
+    THROW_RETURN(name() requires a callback argument) \
   }
 
 #define METHOD_SETUP_COMMON_ONEARG(name) \
   if (!args[0]->IsFunction()) { \
-    THROW_RETURN(name() requires a callback argument 1) \
+    THROW_RETURN(name() requires a callback argument) \
   } \
   METHOD_SETUP_COMMON(name, -1, 0)
 
diff --git a/test/common.js b/test/common.js
index 902ace2..a5db1c1 100644
--- a/test/common.js
+++ b/test/common.js
@@ -20,11 +20,11 @@ var dbidx = 0
           return (/^_leveldown_test_db_/).test(f)
         })
 
-        var ret = 0
-
         if (!list.length)
           return callback()
 
+        var ret = 0
+
         list.forEach(function (f) {
           rimraf(f, function () {
             if (++ret == list.length)
diff --git a/test/del-test.js b/test/del-test.js
new file mode 100644
index 0000000..0b33718
--- /dev/null
+++ b/test/del-test.js
@@ -0,0 +1,44 @@
+const test       = require('tap').test
+    , testCommon = require('./common')
+    , leveldown  = require('../')
+
+var db
+
+test('setUp', function (t) {
+  db = leveldown(testCommon.location())
+  db.open(testCommon.setUp.bind(null, t))
+})
+
+test('test argument-less del() throws', function (t) {
+  t.throws(db.del.bind(db), 'no-arg del() throws')
+  t.end()
+})
+
+test('test callback-less, 1-arg, del() throws', function (t) {
+  t.throws(db.del.bind(db, 'foo'), 'callback-less, 1-arg del() throws')
+  t.end()
+})
+
+test('test callback-less, 3-arg, del() throws', function (t) {
+  t.throws(db.del.bind(db, 'foo', {}), 'callback-less, 2-arg del() throws')
+  t.end()
+})
+
+test('test simple del()', function (t) {
+  db.put('foo', 'bar', function (err) {
+    t.notOk(err, 'no error')
+    db.del('foo', function (err) {
+      t.notOk(err, 'no error')
+      db.get('foo', function (err) {
+        t.ok(err, 'entry propertly deleted')
+        t.like(err.message, /NotFound/)
+        t.end()
+      })
+
+    })
+  })
+})
+
+test('tearDown', function (t) {
+  db.close(testCommon.tearDown.bind(null, t))
+})
\ No newline at end of file
diff --git a/test/get-test.js b/test/get-test.js
new file mode 100644
index 0000000..e872eb4
--- /dev/null
+++ b/test/get-test.js
@@ -0,0 +1,48 @@
+const test       = require('tap').test
+    , testCommon = require('./common')
+    , leveldown  = require('../')
+
+var db
+
+test('setUp', function (t) {
+  db = leveldown(testCommon.location())
+  db.open(testCommon.setUp.bind(null, t))
+})
+
+test('test argument-less get() throws', function (t) {
+  t.throws(db.get.bind(db), 'no-arg get() throws')
+  t.end()
+})
+
+test('test callback-less, 1-arg, get() throws', function (t) {
+  t.throws(db.get.bind(db, 'foo'), 'callback-less, 1-arg get() throws')
+  t.end()
+})
+
+test('test callback-less, 3-arg, get() throws', function (t) {
+  t.throws(db.get.bind(db, 'foo', {}), 'callback-less, 2-arg get() throws')
+  t.end()
+})
+
+test('test simple get()', function (t) {
+  db.put('foo', 'bar', function (err) {
+    t.notOk(err, 'no error')
+    db.get('foo', function (err, value) {
+      t.notOk(err, 'no error')
+      t.type(value, Buffer)
+      t.equal(value.toString(), 'bar')
+
+      db.get('foo', { asBuffer: false }, function (err, value) {
+        t.notOk(err, 'no error')
+        t.type(value, 'string')
+        t.equal(value, 'bar')
+        t.end()
+      })
+
+    })
+  })
+})
+
+test('tearDown', function (t) {
+  db.close(testCommon.tearDown.bind(null, t))
+})
\ No newline at end of file
diff --git a/test/open-test.js b/test/open-test.js
index 6842bff..16dba0a 100644
--- a/test/open-test.js
+++ b/test/open-test.js
@@ -10,6 +10,12 @@ test('test database open no-arg throws', function (t) {
   t.end()
 })
 
+test('test callback-less, 1-arg, open() throws', function (t) {
+  var db = leveldown(testCommon.location())
+  t.throws(db.open.bind(db, {}), 'callback-less, 1-arg open() throws')
+  t.end()
+})
+
 test('test database open', function (t) {
   var db = leveldown(testCommon.location())
 
diff --git a/test/put-get-del-test.js b/test/put-get-del-test.js
index 480da66..4dc3f66 100644
--- a/test/put-get-del-test.js
+++ b/test/put-get-del-test.js
@@ -11,7 +11,7 @@ const fs         = require('fs')
 var db
 
   , makeGetDelErrorTests = function (type, key, expectedError) {
-      test('get() with ' + type + ' causes error', function (t) {
+      test('test get() with ' + type + ' causes error', function (t) {
         db.get(key, function (err) {
           t.ok(err, 'has error')
           t.type(err, Error)
@@ -20,7 +20,7 @@ var db
         })
       })
 
-      test('del() with ' + type + ' causes error', function (t) {
+      test('test del() with ' + type + ' causes error', function (t) {
         db.del(key, function (err) {
           t.ok(err, 'has error')
           t.type(err, Error)
@@ -31,7 +31,7 @@ var db
     }
 
   , makePutErrorTest = function (type, key, value, expectedError) {
-      test('put() with ' + type + ' causes error', function (t) {
+      test('test put() with ' + type + ' causes error', function (t) {
         db.put(key, value, function (err) {
           t.ok(err, 'has error')
           t.type(err, Error)
@@ -42,7 +42,7 @@ var db
     }
 
   , makePutGetDelSuccessfulTest = function (type, key, value) {
-      test('put()/get()/del() with ' + type, function (t) {
+      test('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) {
@@ -69,9 +69,8 @@ var db
 /**** SETUP ENVIRONMENT ****/
 
 test('setUp', function (t) {
-  testCommon.setUp.apply(null, arguments)
   db = leveldown(testCommon.location())
-  db.open(t.end.bind(t))
+  db.open(testCommon.setUp.bind(null, t))
 })
 
 /**** TEST ERROR KEYS ****/
@@ -131,6 +130,5 @@ 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))
+  db.close(testCommon.tearDown.bind(null, t))
 })
\ No newline at end of file
diff --git a/test/put-test.js b/test/put-test.js
new file mode 100644
index 0000000..a420466
--- /dev/null
+++ b/test/put-test.js
@@ -0,0 +1,45 @@
+const test       = require('tap').test
+    , testCommon = require('./common')
+    , leveldown  = require('../')
+
+var db
+
+test('setUp', function (t) {
+  db = leveldown(testCommon.location())
+  db.open(testCommon.setUp.bind(null, t))
+})
+
+test('test argument-less put() throws', function (t) {
+  t.throws(db.put.bind(db), 'no-arg put() throws')
+  t.end()
+})
+
+test('test callback-less, 1-arg, put() throws', function (t) {
+  t.throws(db.put.bind(db, 'foo'), 'callback-less, 1-arg put() throws')
+  t.end()
+})
+
+test('test callback-less, 2-arg, put() throws', function (t) {
+  t.throws(db.put.bind(db, 'foo', 'bar'), 'callback-less, 2-arg put() throws')
+  t.end()
+})
+
+test('test callback-less, 3-arg, put() throws', function (t) {
+  t.throws(db.put.bind(db, 'foo', 'bar', {}), 'callback-less, 3-arg put() throws')
+  t.end()
+})
+
+test('test simple put()', function (t) {
+  db.put('foo', 'bar', function (err) {
+    t.notOk(err, 'no error')
+    db.get('foo', function (err, value) {
+      t.notOk(err, 'no error')
+      t.equal(value.toString(), 'bar')
+      t.end()
+    })
+  })
+})
+
+test('tearDown', function (t) {
+  db.close(testCommon.tearDown.bind(null, 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