[Pkg-javascript-commits] [node-leveldown] 87/492: added more functional tests!

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:47 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 625ba80ed9731e5a90c266707950387519efed08
Author: Rod Vagg <rod at vagg.org>
Date:   Wed Nov 21 22:41:29 2012 +1100

    added more functional tests!
---
 .gitignore                          |   3 +-
 .npmignore                          |   4 +-
 package.json                        |   3 +-
 test/functional/binary-data-test.js |  37 +++++++++++++++
 test/functional/compat-test.js      |  40 ++++++++++++++++
 test/functional/fstream-test.js     |   6 +++
 test/functional/tarcommon.js        |  92 ++++++++++++++++++++++++++++++++++++
 test/functional/test-data.db.tar    | Bin 0 -> 3153920 bytes
 test/functional/test-data.tar       | Bin 0 -> 2273280 bytes
 9 files changed, 182 insertions(+), 3 deletions(-)

diff --git a/.gitignore b/.gitignore
index e690b24..b96b959 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,4 +2,5 @@ node_modules/
 build/
 libleveldb.so
 libleveldb.a
-leveldb-1.7.0*
+leveldb-1.7*
+test-data/
\ No newline at end of file
diff --git a/.npmignore b/.npmignore
index a4f4d64..a5fbe5e 100644
--- a/.npmignore
+++ b/.npmignore
@@ -1,2 +1,4 @@
 *.tar.gz
-build/
\ No newline at end of file
+build/
+test-data.tar
+test-data.db.tar
\ No newline at end of file
diff --git a/package.json b/package.json
index 3824faf..f5ce74e 100644
--- a/package.json
+++ b/package.json
@@ -34,6 +34,7 @@
       , "slow-stream"   : "*"
       , "delayed"       : "*"
       , "boganipsum"    : "*"
+      , "tar"           : "*"
     }
   , "repository": {
         "type": "git"
@@ -42,7 +43,7 @@
   , "scripts"         : {
         "test"            : "./node_modules/.bin/buster-test"
       , "install"         : "node-gyp rebuild"
-      , "functionaltest"  : "node ./test/functional/fstream-test.js"
+      , "functionaltests" : "node ./test/functional/fstream-test.js && node ./test/functional/binary-data-test.js && node ./test/functional/compat-test.js"
     }
   , "license"         : "MIT"
   , "gypfile"         : true
diff --git a/test/functional/binary-data-test.js b/test/functional/binary-data-test.js
new file mode 100644
index 0000000..5246f17
--- /dev/null
+++ b/test/functional/binary-data-test.js
@@ -0,0 +1,37 @@
+/*
+ * This test unpacks a tar file, pushes that data into a
+ * database then compares the database data with the files
+ * on the filesystem.
+ * The different types of data are useful for testing, particularly
+ * the binary files.
+ */
+
+var async     = require('async')
+  , rimraf    = require('rimraf')
+  , tarcommon = require('./tarcommon')
+
+console.log('***************************************************')
+console.log('RUNNING BINARY-DATA-TEST...')
+
+async.series([
+    // pre-clean
+    rimraf.bind(null, tarcommon.dblocation)
+  , rimraf.bind(null, tarcommon.datadir)
+    // extract data for comparison
+  , tarcommon.extract.bind(null, tarcommon.datatar, tarcommon.datadir)
+    // open database
+  , tarcommon.opendb.bind(null, tarcommon.dblocation)
+    // push the data into a database
+  , tarcommon.fstreamWrite
+    // run a sync put & del to force an fs sync
+  , tarcommon.sync
+    // verify database entries are the same as the files
+  , tarcommon.verify
+    // clean up
+  , rimraf.bind(null, tarcommon.dblocation)
+  , rimraf.bind(null, tarcommon.datadir)
+], function (err) {
+  if (err) console.error('Error', err)
+  else console.log('No errors? All good then!')
+  console.log('***************************************************')
+})
\ No newline at end of file
diff --git a/test/functional/compat-test.js b/test/functional/compat-test.js
new file mode 100644
index 0000000..a3f5d19
--- /dev/null
+++ b/test/functional/compat-test.js
@@ -0,0 +1,40 @@
+/*
+ * This test verifies that an existing database contains the
+ * correct data, by comparing it to the original data contained
+ * in a tar file.
+ * Useful for comparing across LevelDB versions.
+ */
+
+var async      = require('async')
+  , rimraf     = require('rimraf')
+  , path       = require('path')
+  , tarcommon  = require('./tarcommon')
+
+  , dbtar      = path.join(__dirname, 'test-data.db.tar')
+  , dblocation = path.join(__dirname, 'levelup_test_compat.db')
+
+console.log('***************************************************')
+console.log('RUNNING COMPAT-DATA-TEST...')
+
+async.series([
+    // pre-clean
+    rimraf.bind(null, tarcommon.dblocation)
+  , rimraf.bind(null, dblocation)
+  , rimraf.bind(null, tarcommon.datadir)
+    // extract existing database
+  , tarcommon.extract.bind(null, dbtar, __dirname)
+    // extract data for comparison
+  , tarcommon.extract.bind(null, tarcommon.datatar, tarcommon.datadir)
+    // open database
+  , tarcommon.opendb.bind(null, dblocation)
+    // verify database entries are the same as the files
+  , tarcommon.verify
+    // clean up
+  , rimraf.bind(null, tarcommon.dblocation)
+  , rimraf.bind(null, dblocation)
+  , rimraf.bind(null, tarcommon.datadir)
+], function (err) {
+  if (err) console.error('Error', err)
+  else console.log('No errors? All good then!')
+  console.log('***************************************************')
+})
\ No newline at end of file
diff --git a/test/functional/fstream-test.js b/test/functional/fstream-test.js
index 9ce55d1..0a267be 100644
--- a/test/functional/fstream-test.js
+++ b/test/functional/fstream-test.js
@@ -73,6 +73,9 @@ process.on('uncaughtException', function (err) {
   refute(err)
 })
 
+console.log('***************************************************')
+console.log('RUNNING FSTREAM-TEST...')
+
 async.waterfall([
     rimraf.bind(null, dblocation)
   , mkfiletree.makeTemp.bind(null, 'levelup_test_fstream', fixtureFiles)
@@ -88,4 +91,7 @@ async.waterfall([
     }
   , verify
   , cleanUp
+  , function () {
+      console.log('***************************************************')
+    }
 ])
\ No newline at end of file
diff --git a/test/functional/tarcommon.js b/test/functional/tarcommon.js
new file mode 100644
index 0000000..d36e9fa
--- /dev/null
+++ b/test/functional/tarcommon.js
@@ -0,0 +1,92 @@
+var assert       = require('buster').assert
+  , fs           = require('fs')
+  , path         = require('path')
+  , fstream      = require('fstream')
+  , tar          = require('tar')
+  , crypto       = require('crypto')
+  , levelup      = require('../../lib/levelup')
+
+  , dblocation   = path.join(__dirname, 'levelup_test_binary.db')
+  , datatar      = path.join(__dirname, 'test-data.tar')
+  , datadir      = path.join(__dirname, 'test-data')
+  , db
+  , expectedEntries
+
+module.exports.dblocation = dblocation
+module.exports.datatar    = datatar
+module.exports.datadir    = datadir
+
+module.exports.opendb = function (dblocation, callback) {
+  levelup(
+      dblocation
+    , { createIfMissing: true , errorIfExists: false, keyEncoding: 'utf8', valueEncoding: 'binary' }
+    , function (err, _db) {
+        db = _db
+        console.log('Opened database...')
+        callback(err)
+      }
+  )
+}
+
+module.exports.extract = function (tarfile, dir, callback) {
+  expectedEntries = 0
+  fs.createReadStream(tarfile)
+    .pipe(tar.Extract({ path: dir }))
+    .on('entry', function (entry) {
+      if (entry.props.File || entry.File || entry.type == 'File')
+        expectedEntries++
+    })
+    .on('end', function () {
+      console.log('Extracted tar file...')
+      callback()
+    })
+}
+
+module.exports.fstreamWrite = function (callback) {
+  fstream.Reader(datadir)
+    .pipe(db.writeStream({ fstreamRoot: path.resolve(__dirname) })
+      .on('close', function () {
+        console.log('Piped data to database...')
+        callback()
+      }))
+      .on('error', callback)
+}
+
+// using sync:true will force a flush to the fs, otherwise the readStream() is too
+// quick and won't get the full data
+module.exports.sync = function (callback) {
+  db.put('__', '__', { sync: true }, function (err) {
+    if (err) return callback(err)
+    db.del('__', { sync: true }, callback)
+  })
+}
+
+module.exports.verify = function (callback) {
+  var entries = 0
+  db.readStream()
+    .on('data', function (data) {
+      var md5sum = crypto.createHash('md5')
+        , dbmd5sum
+
+      md5sum.update(data.value)
+      dbmd5sum = md5sum.digest('hex')
+      md5sum = crypto.createHash('md5')
+      entries++
+      fs.createReadStream(path.join(__dirname, data.key))
+        .on('data', function (d) { md5sum.update(d) })
+        .on('end', function () {
+          var fsmd5sum = md5sum.digest('hex')
+          assert.equals(
+              dbmd5sum
+            , fsmd5sum
+            , 'MD5 sum compare of ' + data.key + ' failed (' + dbmd5sum + ' != ' + fsmd5sum + ')'
+          )
+        })
+    })
+    .on('end', function () {
+      assert.equals(entries, expectedEntries, 'correct number of entries in the database')
+      console.log('Finished comparing database entries...')
+      console.log('Cleaning up...')
+      callback()
+    })
+}
\ No newline at end of file
diff --git a/test/functional/test-data.db.tar b/test/functional/test-data.db.tar
new file mode 100644
index 0000000..062f46d
Binary files /dev/null and b/test/functional/test-data.db.tar differ
diff --git a/test/functional/test-data.tar b/test/functional/test-data.tar
new file mode 100644
index 0000000..2a69b32
Binary files /dev/null and b/test/functional/test-data.tar 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