[Pkg-javascript-commits] [node-leveldown] 221/492: approximateSize() 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 995ba860346632c7316ab61bf66d81e56645241c
Author: Rod Vagg <rod at vagg.org>
Date:   Sun Feb 17 22:01:53 2013 +1100

    approximateSize() tests
---
 src/database.cc               | 41 +++++++++++++++++----------
 src/leveldown.h               | 20 +++++++------
 test/approximate-size-test.js | 65 +++++++++++++++++++++++++++++++++++++++++++
 test/close-test.js            |  3 +-
 test/get-test.js              | 15 ++++++----
 5 files changed, 115 insertions(+), 29 deletions(-)

diff --git a/src/database.cc b/src/database.cc
index 9c6ac32..9f21856 100644
--- a/src/database.cc
+++ b/src/database.cc
@@ -290,12 +290,11 @@ v8::Handle<v8::Value> Database::Delete (const v8::Arguments& args) {
 v8::Handle<v8::Value> Database::Batch (const v8::Arguments& args) {
   v8::HandleScope scope;
 
-  Database* database = node::ObjectWrap::Unwrap<Database>(args.This());
-  v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(args[0]);
-  v8::Local<v8::Object> optionsObj = v8::Local<v8::Object>::Cast(args[1]);
+  METHOD_SETUP_COMMON(batch, 1, 2)
+
   BOOLEAN_OPTION_VALUE(optionsObj, sync)
-  v8::Persistent<v8::Function> callback =
-      v8::Persistent<v8::Function>::New(v8::Local<v8::Function>::Cast(args[2]));
+
+  v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(args[0]);
 
   std::vector<BatchOp*>* operations = new std::vector<BatchOp*>;
   for (unsigned int i = 0; i < array->Length(); i++) {
@@ -342,17 +341,31 @@ v8::Handle<v8::Value> Database::Batch (const v8::Arguments& args) {
 v8::Handle<v8::Value> Database::ApproximateSize (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]));
+  v8::Local<v8::Value> startBufferV = args[0];
+  v8::Local<v8::Value> endBufferV = args[1];
+
+  if (startBufferV->IsNull()
+      || startBufferV->IsUndefined()
+      || startBufferV->IsFunction() // callback in pos 0?
+      || endBufferV->IsNull()
+      || endBufferV->IsUndefined()
+      || endBufferV->IsFunction() // callback in pos 1?
+      ) {
+    THROW_RETURN("approximateSize() requires valid `start`, `end` and `callback` arguments")
+  }
+
+  METHOD_SETUP_COMMON(approximateSize, -1, 2)
+
+  CB_ERR_IF_NULL_OR_UNDEFINED(0, Start)
+  CB_ERR_IF_NULL_OR_UNDEFINED(1, End)
 
-  CB_ERR_IF_NULL_OR_UNDEFINED(0, "start")
-  CB_ERR_IF_NULL_OR_UNDEFINED(1, "end")
+  STRING_OR_BUFFER_TO_SLICE(start, startBufferV, Start)
+  STRING_OR_BUFFER_TO_SLICE(end, endBufferV, End)
 
-  v8::Persistent<v8::Value> startBuffer = v8::Persistent<v8::Value>::New(args[0]);
-  STRING_OR_BUFFER_TO_SLICE(start, startBuffer, Start)
-  v8::Persistent<v8::Value> endBuffer = v8::Persistent<v8::Value>::New(args[1]);
-  STRING_OR_BUFFER_TO_SLICE(end, endBuffer, End)
+  v8::Persistent<v8::Value> startBuffer =
+      v8::Persistent<v8::Value>::New(startBufferV);
+  v8::Persistent<v8::Value> endBuffer =
+      v8::Persistent<v8::Value>::New(endBufferV);
 
   ApproximateSizeWorker* worker  = new ApproximateSizeWorker(
       database
diff --git a/src/leveldown.h b/src/leveldown.h
index 7fb093e..9115871 100644
--- a/src/leveldown.h
+++ b/src/leveldown.h
@@ -64,7 +64,11 @@
       return v8::Undefined(); \
     } \
     to ## Ch_ = new char[to ## Sz_]; \
-    to ## Str->WriteUtf8(to ## Ch_, -1, NULL, v8::String::NO_NULL_TERMINATION); \
+    to ## Str->WriteUtf8( \
+        to ## Ch_ \
+      , -1 \
+      , NULL, v8::String::NO_NULL_TERMINATION \
+    ); \
   } \
   leveldb::Slice to(to ## Ch_, to ## Sz_);
 
@@ -109,15 +113,17 @@
   Database* database = ObjectWrap::Unwrap<Database>(args.This()); \
   v8::Local<v8::Object> optionsObj; \
   v8::Persistent<v8::Function> callback; \
-  if (optionPos == -1) { \
+  if (optionPos == -1 && args[callbackPos]->IsFunction()) { \
     callback = v8::Persistent<v8::Function>::New( \
       v8::Local<v8::Function>::Cast(args[callbackPos]) \
     ); \
-  } else if (args[callbackPos - 1]->IsFunction()) { \
+  } else if (optionPos != -1 && args[callbackPos - 1]->IsFunction()) { \
     callback = v8::Persistent<v8::Function>::New( \
       v8::Local<v8::Function>::Cast(args[callbackPos - 1]) \
     ); \
-  } else if (args[optionPos]->IsObject() && args[callbackPos]->IsFunction()) { \
+  } else if (optionPos != -1 \
+        && 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]) \
@@ -126,10 +132,6 @@
     THROW_RETURN(name() requires a callback argument) \
   }
 
-#define METHOD_SETUP_COMMON_ONEARG(name) \
-  if (!args[0]->IsFunction()) { \
-    THROW_RETURN(name() requires a callback argument) \
-  } \
-  METHOD_SETUP_COMMON(name, -1, 0)
+#define METHOD_SETUP_COMMON_ONEARG(name) METHOD_SETUP_COMMON(name, -1, 0)
 
 #endif
diff --git a/test/approximate-size-test.js b/test/approximate-size-test.js
new file mode 100644
index 0000000..abd8063
--- /dev/null
+++ b/test/approximate-size-test.js
@@ -0,0 +1,65 @@
+const test       = require('tap').test
+    , testCommon = require('./common')
+    , leveldown  = require('../')
+
+test('setUp', testCommon.setUp)
+
+test('test approximateSize()', function (t) {
+  var db   = leveldown(testCommon.location())
+    , data = Array.apply(null, Array(10000)).map(function () { return 'aaaaaaaaaa' }).join('')
+
+  db.open(function (err) {
+    t.notOk(err, 'no error')
+
+    db.batch(
+        Array.apply(null, Array(10)).map(function (x, i) {
+          return { type: 'put', key: 'foo' + i, value: data }
+        })
+      , function (err) {
+          t.notOk(err, 'no error')
+
+          // cycle open/close to ensure a pack to .sst
+
+          db.close(function (err) {
+            t.notOk(err, 'no error')
+
+            db.open(function (err) {
+              t.notOk(err, 'no error')
+
+              t.throws(db.approximateSize, 'arg-less approximateSize() throws')
+              t.throws(
+                  db.approximateSize.bind(db, 'foo')
+                , '1-arg approximateSize() throws'
+              )
+              t.throws(
+                  db.approximateSize.bind(db, 'foo', 'bar')
+                , '2-arg approximateSize() throws'
+              )
+              t.throws(
+                  db.approximateSize.bind(db, function () {})
+                , 'callback-only approximateSize() throws'
+              )
+              t.throws(
+                  db.approximateSize.bind(db, 'foo', function () {})
+                , '1-arg + callback approximateSize() throws'
+              )
+
+              db.approximateSize('foo', 'z', function (err, size) {
+                t.notOk(err, 'no error')
+
+                t.type(size, 'number')
+                t.ok(size > 100000, 'size reports a reasonable amount (' + size + ')')
+
+                db.close(function (err) {
+                  t.notOk(err, 'no error')
+                  t.end()
+                })
+              })
+            })
+          })
+        }
+    )
+  })
+})
+
+test('tearDown', testCommon.tearDown)
\ No newline at end of file
diff --git a/test/close-test.js b/test/close-test.js
index 1a64910..555fc55 100644
--- a/test/close-test.js
+++ b/test/close-test.js
@@ -4,12 +4,13 @@ const test       = require('tap').test
 
 test('setUp', testCommon.setUp)
 
-test('test database close', function (t) {
+test('test close()', function (t) {
   var db = leveldown(testCommon.location())
 
   db.open(function (err) {
     t.notOk(err, 'no error')
     t.throws(db.close.bind(db), 'no-arg close() throws')
+    t.throws(db.close.bind(db, 'foo'), 'non-callback close() throws')
 
     db.close(function (err) {
       t.notOk(err, 'no error')
diff --git a/test/get-test.js b/test/get-test.js
index e872eb4..a1033f0 100644
--- a/test/get-test.js
+++ b/test/get-test.js
@@ -32,13 +32,18 @@ test('test simple get()', function (t) {
       t.type(value, Buffer)
       t.equal(value.toString(), 'bar')
 
-      db.get('foo', { asBuffer: false }, function (err, value) {
+      db.get('foo', {}, function (err, value) { // same but with {}
         t.notOk(err, 'no error')
-        t.type(value, 'string')
-        t.equal(value, 'bar')
-        t.end()
+        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()
+        })
       })
-
     })
   })
 })

-- 
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