Permalink
Browse files

first commit

  • Loading branch information...
pgte committed Sep 15, 2011
0 parents commit 273d787dfdb6166fa8f148debf47d7dbdf15d3ef
Showing with 113 additions and 0 deletions.
  1. +3 −0 README.md
  2. +29 −0 index.js
  3. +15 −0 package.json
  4. +66 −0 test.js
@@ -0,0 +1,3 @@
+# banzai-statestore-mem
+
+Banzai memory state store. Used mainly for testing.
@@ -0,0 +1,29 @@
+function clone(doc) {
+ return JSON.parse(JSON.stringify(doc));
+};
+
+module.exports = function() {
+ var nextId = 1;
+ var store = {};
+
+ function load(docId, done) {
+ done(null, store[docId]);
+ }
+
+ function save(doc, done) {
+ doc = clone(doc);
+ var id = doc.id || doc._id;
+ if (! id) {
+ id = nextId;
+ nextId += 1;
+ doc.id = id;
+ }
+ store[id] = doc;
+ done(null, clone(doc));
+ }
+
+ return {
+ load: load
+ , save: save
+ }
+};
@@ -0,0 +1,15 @@
+{ "name" : "banzai-statestore-mem"
+, "description" : "Plugin module for Banzai state store. Mainly used for testing."
+, "tags" : ["ETL", "pipeline", "document", "state machine"]
+, "version" : "1.0.0"
+, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com>"
+, "repository" :
+ { "type" : "git"
+ , "url" : "http://github.com/pgte/banzai-statestore-mem.git"
+ }
+, "bugs" :
+ { "web" : "http://github.com/pgte/banzai-statestore-mem/issues" }
+, "engines" : ["node >= 0.4.10"]
+, "main" : "./index"
+, "scripts": { "test": "expresso test.js" }
+}
66 test.js
@@ -0,0 +1,66 @@
+var store = require('./')()
+ , assert = require('assert');
+
+module.exports.test_1 = function(beforeExit) {
+ var cb1 = false
+ , cb2 = false
+ , doc_id;
+
+ store.save({a:1, b:2}, function(err, doc) {
+ assert.ok(! cb1);
+ cb1 = true;
+ assert.isNull(err);
+ assert.isNotNull(doc.id);
+ doc_id = doc.id;
+ delete doc.id;
+ assert.eql(doc, {a:1, b:2});
+
+ store.load(doc_id, function(err, doc) {
+ assert.ok(! cb2);
+ cb2 = true;
+ assert.isNull(err);
+ assert.isNotNull(doc.id);
+ assert.eql(doc.id, doc_id);
+ delete doc.id;
+ assert.eql(doc, {a:1, b:2});
+ });
+
+ });
+
+ beforeExit(function() {
+ assert.ok(cb1);
+ assert.ok(cb2);
+ });
+};
+
+module.exports.test_2 = function(beforeExit) {
+ var cb1 = false
+ , cb2 = false
+ , doc_id;
+
+ store.save({c:1, d:2}, function(err, doc) {
+ assert.ok(! cb1);
+ cb1 = true;
+ assert.isNull(err);
+ assert.isNotNull(doc.id);
+ doc_id = doc.id;
+ delete doc.id;
+ assert.eql(doc, {c:1, d:2});
+
+ store.load(doc_id, function(err, doc) {
+ assert.ok(! cb2);
+ cb2 = true;
+ assert.isNull(err);
+ assert.isNotNull(doc.id);
+ assert.eql(doc.id, doc_id);
+ delete doc.id;
+ assert.eql(doc, {c:1, d:2});
+ });
+
+ });
+
+ beforeExit(function() {
+ assert.ok(cb1);
+ assert.ok(cb2);
+ });
+};

0 comments on commit 273d787

Please sign in to comment.