[Pkg-javascript-commits] [node-leveldown] 06/492: binary data tests

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:38 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 67cfe1cf0950c569485fb6779b325cb5eaf89797
Author: Rod Vagg <rod at vagg.org>
Date:   Sun Jul 15 12:28:41 2012 +1000

    binary data tests
---
 lib/levelup.js      |  35 +++++++++----------
 src/database.cc     |   1 -
 test/binary-test.js |  95 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/testdata.bin   | Bin 0 -> 4688 bytes
 4 files changed, 113 insertions(+), 18 deletions(-)

diff --git a/lib/levelup.js b/lib/levelup.js
index 65b13b7..fd6ac0f 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -17,22 +17,19 @@ var bridge              = require('../build/Release/levelup')
       return eo
     }('hex utf8 utf-8 ascii binary base64 ucs2 ucs-2 utf16le utf-16le'.split(' ')))
 
-  , getOptions = function (options_, defaultEncoding) {
-      var options
-      if (typeof options_ == 'string')
-        options = encodingOpts[options_] || encodingOpts[defaultOptions.encoding]
-      else if (typeof options_ == 'object') {
-        options = options_
-        if (!options.encoding
-            || !(options.keyEncoding
-                  && options.valueEncoding
-                  && encodingOpts[options.keyEncoding]
-                  && encodingOpts[options.valueEncoding])
-            || !encodingOpts[options.encoding])
-          options.encoding = defaultOptions.encoding
-      } else
-        options = encodingOpts[defaultEncoding] || encodingOpts[defaultOptions.encoding]
-      return options
+  , getOptions = function (options, defaultEncoding) {
+      if (typeof options == 'string')
+        return encodingOpts[options] || encodingOpts[defaultOptions.encoding]
+      else if (typeof options == 'object') {
+        if ((options.encoding && encodingOpts[options.encoding]))
+          return options
+        if (options.keyEncoding
+              && options.valueEncoding
+              && encodingOpts[options.keyEncoding]
+              && encodingOpts[options.valueEncoding])
+          return options
+      }
+      return encodingOpts[defaultEncoding] || encodingOpts[defaultOptions.encoding]
     }
 
   , getCallback = function (options_, callback_) {
@@ -45,6 +42,10 @@ var bridge              = require('../build/Release/levelup')
       return Buffer.isBuffer(data) ? data : new Buffer('' + data, encoding)
     }
 
+  , toEncoding = function (buffer, encoding) {
+      return encoding == 'binary' ? buffer : buffer.toString(encoding)
+    }
+
   , Database = {
         open: function (callback) {
           var options = {}
@@ -97,7 +98,7 @@ var bridge              = require('../build/Release/levelup')
                   return callback(err)
                 throw err
               }
-              callback && callback(null, value.toString(options.valueEncoding || options.encoding), key)
+              callback && callback(null, toEncoding(value, options.valueEncoding || options.encoding), key)
             })
           } else
             callback(new errors.ReadError('Database has not been opened'))
diff --git a/src/database.cc b/src/database.cc
index 8ba9571..0f5452f 100644
--- a/src/database.cc
+++ b/src/database.cc
@@ -138,7 +138,6 @@ Handle<Value> Database::Put (const Arguments& args) {
   Slice key(Buffer::Data(keyBuffer), Buffer::Length(keyBuffer));
   Local<Object> valueBuffer = args[1]->ToObject();
   Slice value(Buffer::Data(valueBuffer), Buffer::Length(valueBuffer));
-
   Local<Object> optionsObj = Local<Object>::Cast(args[2]);
   bool sync = optionsObj->Has(option_sync) && optionsObj->Get(option_sync)->BooleanValue();
   Persistent<Function> callback = Persistent<Function>::New(Local<Function>::Cast(args[3]));
diff --git a/test/binary-test.js b/test/binary-test.js
new file mode 100644
index 0000000..d754879
--- /dev/null
+++ b/test/binary-test.js
@@ -0,0 +1,95 @@
+/*global cleanUp:true, openTestDatabase:true, loadBinaryTestData:true, binaryTestDataMD5Sum:true, checkBinaryTestData:true*/
+
+var buster  = require('buster')
+  , assert  = buster.assert
+  , levelup = require('../lib/levelup.js')
+  , errors  = require('../lib/errors.js')
+  , rimraf  = require('rimraf')
+  , async   = require('async')
+  , fs      = require('fs')
+
+buster.testCase('Binary API', {
+    'setUp': function (done) {
+      this.cleanupDirs = []
+      this.closeableDatabases = []
+      this.openTestDatabase = openTestDatabase.bind(this)
+      loadBinaryTestData(function (err, data) {
+        refute(err)
+        this.testData = data
+        done()
+      }.bind(this))
+    }
+
+  , 'tearDown': function (done) {
+      cleanUp(this.closeableDatabases, this.cleanupDirs, done)
+    }
+
+  , 'sanity check on test data': function (done) {
+      assert(Buffer.isBuffer(this.testData))
+      checkBinaryTestData(this.testData, done)
+    }
+
+  , 'test put() and get() with binary value {encoding:binary}': function (done) {
+      this.openTestDatabase(function (db) {
+        db.put('binarydata', this.testData, {encoding:'binary'}, function (err) {
+          refute(err)
+          db.get('binarydata', {encoding:'binary'}, function (err, value) {
+            refute(err)
+            assert(value)
+            checkBinaryTestData(value, done)
+          })
+        })
+      }.bind(this))
+    }
+
+  , 'test put() and get() with binary key {encoding:binary}': function (done) {
+      this.openTestDatabase(function (db) {
+        db.put(this.testData, 'binarydata', {encoding:'binary'}, function (err) {
+          refute(err)
+          db.get(this.testData, {encoding:'binary'}, function (err, value) {
+            refute(err)
+            assert.equals(value, 'binarydata')
+            done()
+          }.bind(this))
+        }.bind(this))
+      }.bind(this))
+    }
+
+  , 'test put() and get() with binary value {keyEncoding:utf8,valueEncoding:binary}': function (done) {
+      this.openTestDatabase(function (db) {
+        db.put('binarydata', this.testData, {encoding:'binary'}, function (err) {
+          refute(err)
+          db.get('binarydata', {encoding:'binary'}, function (err, value) {
+            refute(err)
+            assert(value)
+            checkBinaryTestData(value, done)
+          })
+        })
+      }.bind(this))
+    }
+
+  , 'test put() and get() with binary key {keyEncoding:binary,valueEncoding:urf8}': function (done) {
+      this.openTestDatabase(function (db) {
+        db.put(this.testData, 'binarydata', {encoding:'binary'}, function (err) {
+          refute(err)
+          db.get(this.testData, {encoding:'binary'}, function (err, value) {
+            refute(err)
+            assert.equals(value, 'binarydata')
+            done()
+          }.bind(this))
+        }.bind(this))
+      }.bind(this))
+    }
+
+  , 'test put() and get() with binary key & value {encoding:binary}': function (done) {
+      this.openTestDatabase(function (db) {
+        db.put(this.testData, this.testData, {encoding:'binary'}, function (err) {
+          refute(err)
+          db.get(this.testData, {encoding:'binary'}, function (err, value) {
+            refute(err)
+            checkBinaryTestData(value, done)
+          }.bind(this))
+        }.bind(this))
+      }.bind(this))
+    }
+})
\ No newline at end of file
diff --git a/test/testdata.bin b/test/testdata.bin
new file mode 100644
index 0000000..59229b9
Binary files /dev/null and b/test/testdata.bin differ

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