Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make test with mocha

  • Loading branch information...
commit 9550bff0ce213f3cb46cbb742a4b897bfe6742aa 1 parent be0210d
@adrai authored
View
1  lib/eventStore.js
@@ -83,7 +83,6 @@ Store.prototype = {
// if fork enabled, start event dispatcher as child process...
if (this.storage.filename && this.storage.options) {
-
if (this.logger) {
this.logger.info('Start event dispatcher as child process!');
View
5 package.json
@@ -23,8 +23,9 @@
"async": ">= 0.0.1"
}
, "devDependencies": {
- "vows": ">= 0.0.1"
+ "mocha": ">= 0.0.1"
+ , "expect.js": ">= 0.0.1"
}
, "scripts" : {
- "test" : "vows test/* --spec" }
+ "test" : "mocha" }
}
View
7 storage/mongoDb/package.json
@@ -18,7 +18,10 @@
, "dependencies": {
"mongodb": ">= 0.0.1"
}
- , "devDependencies": { }
+ , "devDependencies": {
+ "mocha": ">= 0.0.1"
+ , "expect.js": ">= 0.0.1"
+ }
, "scripts" : {
- "test" : "vows test/* --spec" }
+ "test" : "mocha" }
}
View
424 storage/mongoDb/test/mongoDbStorageSpec.js
@@ -1,176 +1,266 @@
-var vows = require('vows')
- , assert = require('assert');
-
-var options = {
- dbName: 'testeventstore'
-};
-
-var storageName = "mongoDb";
-
-vows.describe('The ' + storageName + ' Storage')
-.addBatch({
- 'An empty Storage': {
- topic: function () {
- require('../storage').createStorage(options, this.callback);
- },
-
- 'has a function getId': function(storage) {
- assert.isFunction(storage.getId);
- },
-
- 'getId returns a string': function(storage) {
- storage.getId(function(err, id) {
- assert.isString(id);
+var expect = require('expect.js')
+ , storageModule = require('../storage');
+
+ describe('Storage', function() {
+
+ var storage;
+
+ describe('beeing not connected', function() {
+
+ describe('calling createStorage', function() {
+
+ describe('without a callback', function() {
+
+ before(function() {
+ storage = storageModule.createStorage({ dbName: 'testeventstore' });
+ });
+
+ describe('calling connect', function() {
+
+ it('it should connect successfully', function(done) {
+
+ storage.connect(function(err) {
+ expect(err).to.eql(null);
+ done();
+ });
+
+ });
+
+ });
+
+ });
+
+ describe('with a callback', function() {
+
+ it('it should connect successfully', function(done) {
+
+ storageModule.createStorage({ dbName: 'testeventstore' }, function(err, str) {
+ storage = str;
+ expect(err).to.eql(null);
+ expect(str).to.not.eql(null);
+ done();
+ });
+
+ });
+
+ });
+
+ });
+
+ });
+
+ describe('beeing connected', function() {
+
+ before(function(done) {
+ storage.events.remove({}, function() {
+ storage.snapshots.remove({}, done);
});
- },
-
- 'a second id returned by getId won\'t equal the first': function(storage) {
- storage.getId(function(err, id1) {
- storage.getId(function(err, id2) {
- assert.notEqual(id1, id2);
+ });
+
+ describe('calling getId', function() {
+
+ it('it should callback with a new id', function(done) {
+
+ storage.getId(function(err, id) {
+ expect(err).to.eql(null);
+ expect(id).to.be.an('string');
+ done();
});
+
});
- },
-
- 'can be filled with events': function(storage) {
- var id = "1234-abcd";
- storage.addEvents([{'streamId': id, 'streamRevision': 0, 'payload': {event:'bla'}}], function(err) {
- storage.getEvents(id, 0, -1, function(err, events) {
- assert.equal(events.length, 1);
+
+ });
+
+ describe('calling addEvents', function() {
+
+ it('it should save the events', function(done) {
+
+ var events = [
+ {
+ streamId: 'id1',
+ streamRevision: 0,
+ commitId: '10',
+ payload: {
+ event:'bla'
+ }
+ }
+ ];
+
+ storage.addEvents(events, function(err) {
+ expect(err).to.eql(null);
+
+ storage.getEvents('id1', -1, function(err, evts) {
+ expect(err).to.eql(null);
+ expect(evts).to.be.an('array');
+ expect(evts).to.have.length(1);
+
+ done();
+ });
});
+
+ });
+
+ });
+
+ describe('calling addSnapshot', function() {
+
+ it('it should save the snapshot', function(done) {
+
+ var snapshot = {
+ snapshotId: '1',
+ streamId: '3',
+ revision: 1,
+ data: 'data'
+ };
+
+ storage.addSnapshot(snapshot, function(err) {
+ expect(err).to.eql(null);
+
+ storage.getSnapshot('3', function(err, snap) {
+ expect(err).to.eql(null);
+ expect(snap.data).to.eql(snapshot.data);
+ expect(snap.snapshotId).to.eql(snapshot.snapshotId);
+ expect(snap.revision).to.eql(snapshot.revision);
+ expect(snap.streamId).to.eql(snapshot.streamId);
+
+ done();
+ });
+ });
+
});
- }
- }
-})
-.addBatch({
- 'An filled Storage': {
- topic: function() {
- require('../storage').createStorage(options, function(err, storage) {
- fillStore(storage, this.callback);
- }.bind(this));
- },
-
- 'after a successful `fill` we get events for id 2': {
- topic: function (storage) {
- storage.getEvents('2', 0, -1, this.callback);
- },
-
- 'we can assert if length is right': function (events) {
- assert.equal(events.length, 4);
- },
-
- 'we can assert if sorting is right': function (events) {
- assert.equal(events[0].commitId, '0');
- assert.equal(events[1].commitId, '1');
- assert.equal(events[3].commitId, '3');
- }
- },
-
- 'after a successful `fill` we get events for id 3': {
- topic: function (storage) {
- storage.getEvents('3', 0, -1, this.callback);
- },
-
- 'we can assert if length is right': function (events) {
- assert.equal(events.length, 2);
- }
- },
-
- 'after a successful `fill` we get events for id 2 from 1 to 3': {
- topic: function (storage) {
- storage.getEvents('2', 1, 3, this.callback);
- },
-
- 'we can assert if length is right': function (events) {
- assert.equal(events.length, 2);
- }
- },
-
- 'after a successful `fill` we get all undispatched events': {
- topic: function (storage) {
- storage.getUndispatchedEvents(this.callback);
- },
-
- 'we can assert if length is right': function (events) {
- assert.equal(events.length, 6);
- },
-
- 'we can assert if sorting is right': function (events) {
- assert.equal(events[0].commitId, '0');
- assert.equal(events[2].commitId, '2');
- assert.equal(events[5].commitId, '5');
- }
- },
-
- 'after a successful `fill` we get a range of events searching by event id': {
- topic: function (storage) {
- storage.getEventRange({id: '2'}, 2, this.callback);
- },
-
- 'we can assert if length is right': function (events) {
- assert.equal(events.length, 2);
- },
-
- 'we can assert if sorting is right': function (events) {
- assert.equal(events[0].commitId, '2');
- assert.equal(events[1].commitId, '3');
- }
- },
-
- 'after a successful `fill with a snapshot` we get the snapshot': {
- topic: function (storage) {
- storage.getSnapshot('3', -1, this.callback);
- },
-
- 'we can assert if snapshot is right': function (snapshot) {
- assert.equal(snapshot.data, 'dataPlus');
- assert.equal(snapshot.snapshotId, '2');
- assert.equal(snapshot.streamId, '3');
- assert.equal(snapshot.revision, '2');
- }
- },
-
- 'after a successful `fill with a snapshot` we get the snapshot with maxRev': {
- topic: function (storage) {
- storage.getSnapshot('3', 1, this.callback);
- },
-
- 'we can assert if snapshot is right': function (snapshot) {
- assert.equal(snapshot.data, 'data');
- assert.equal(snapshot.snapshotId, '1');
- assert.equal(snapshot.streamId, '3');
- assert.equal(snapshot.revision, '1');
- }
- },
-
- teardown: function(storage) {
- storage.client.dropDatabase();
- }
- }
-}).export(module);
-
-
-function fillStore(storage, callback) {
- storage.events.remove({}, function(err) {
- storage.addEvents([
- {streamId: '2', streamRevision: 0, commitId: 0, commitStamp: new Date(2012, 3, 14, 8, 0, 0), payload: {id: '1', event:'blaaaaaaaaaaa'}, dispatched: false},
- {streamId: '2', streamRevision: 1, commitId: 1, commitStamp: new Date(2012, 3, 14, 9, 0, 0), payload: {id: '2', event:'blaaaaaaaaaaa'}, dispatched: false},
- {streamId: '2', streamRevision: 2, commitId: 2, commitStamp: new Date(2012, 3, 14, 10, 0, 0), payload: {id: '3', event:'blaaaaaaaaaaa'}, dispatched: false},
- {streamId: '2', streamRevision: 3, commitId: 3, commitStamp: new Date(2012, 3, 15, 8, 0, 0), payload: {id: '4', event:'blaaaaaaaaaaa'}, dispatched: false}
- ],
- function (err) {
- storage.addEvents([
- {streamId: '3', streamRevision: 0, commitId: 4, commitStamp: new Date(2012, 3, 16, 8, 0, 0), payload: {id: '5', event:'blaaaaaaaaaaa'}, dispatched: false},
- {streamId: '3', streamRevision: 1, commitId: 5, commitStamp: new Date(2012, 3, 17, 8, 0, 0), payload: {id: '6', event:'blaaaaaaaaaaa'}, dispatched: false}
- ],
+
+ });
+
+ describe('having a filled store with example data', function() {
+
+ before(function(done) {
+ storage.addEvents([
+ {streamId: '2', streamRevision: 0, commitId: 0, commitStamp: new Date(2012, 3, 14, 8, 0, 0), payload: {id: '1', event:'blaaaaaaaaaaa'}, dispatched: false},
+ {streamId: '2', streamRevision: 1, commitId: 1, commitStamp: new Date(2012, 3, 14, 9, 0, 0), payload: {id: '2', event:'blaaaaaaaaaaa'}, dispatched: false},
+ {streamId: '2', streamRevision: 2, commitId: 2, commitStamp: new Date(2012, 3, 14, 10, 0, 0), payload: {id: '3', event:'blaaaaaaaaaaa'}, dispatched: false},
+ {streamId: '2', streamRevision: 3, commitId: 3, commitStamp: new Date(2012, 3, 15, 8, 0, 0), payload: {id: '4', event:'blaaaaaaaaaaa'}, dispatched: false}
+ ],
function (err) {
- storage.addSnapshot({snapshotId: '1', streamId: '3', revision: 1, data: 'data'}, function() {
- storage.addSnapshot({snapshotId: '2', streamId: '3', revision: 2, data: 'dataPlus'}, function() {
- callback(null, storage);
- });
+ storage.addEvents([
+ {streamId: '3', streamRevision: 0, commitId: 4, commitStamp: new Date(2012, 3, 16, 8, 0, 0), payload: {id: '5', event:'blaaaaaaaaaaa'}, dispatched: false},
+ {streamId: '3', streamRevision: 1, commitId: 5, commitStamp: new Date(2012, 3, 17, 8, 0, 0), payload: {id: '6', event:'blaaaaaaaaaaa'}, dispatched: false}
+ ],
+ function (err) {
+ storage.addSnapshot({snapshotId: '1', streamId: '3', revision: 1, data: 'data'}, function() {
+ storage.addSnapshot({snapshotId: '2', streamId: '3', revision: 2, data: 'dataPlus'}, done);
+ });
+ }
+ );
+ });
+ });
+
+ describe('calling getEvents for id 2', function() {
+
+ it('it should callback with the correct values', function(done) {
+ storage.getEvents('2', 0, -1, function(err, events) {
+ expect(err).to.eql(null);
+ expect(events).to.have.length(4);
+ expect(events[0].commitId).to.eql('0');
+ expect(events[1].commitId).to.eql('1');
+ expect(events[3].commitId).to.eql('3');
+
+ done();
});
- }
- );
+ });
+
+ });
+
+ describe('calling getEvents for id 3', function() {
+
+ it('it should callback with the correct values', function(done) {
+ storage.getEvents('3', 0, -1, function(err, events) {
+ expect(err).to.eql(null);
+ expect(events).to.have.length(2);
+
+ done();
+ });
+ });
+
+ });
+
+ describe('calling getEvents for id 2 from 1 to 3', function() {
+
+ it('it should callback with the correct values', function(done) {
+ storage.getEvents('2', 1, 3, function(err, events) {
+ expect(err).to.eql(null);
+ expect(events).to.have.length(2);
+
+ done();
+ });
+ });
+
+ });
+
+ describe('calling getUndispatchedEvents', function() {
+
+ it('it should callback with the correct values', function(done) {
+ storage.getUndispatchedEvents(function(err, events) {
+ expect(err).to.eql(null);
+ expect(events).to.have.length(6);
+ expect(events[0].commitId).to.eql('0');
+ expect(events[2].commitId).to.eql('2');
+ expect(events[5].commitId).to.eql('5');
+
+ done();
+ });
+ });
+
+ });
+
+ describe('calling getEventRange searching by event id', function() {
+
+ it('it should callback with the correct values', function(done) {
+ storage.getEventRange({id: '2'}, 2, function(err, events) {
+ expect(err).to.eql(null);
+ expect(events).to.have.length(2);
+ expect(events[0].commitId).to.eql('2');
+ expect(events[1].commitId).to.eql('3');
+
+ done();
+ });
+ });
+
+ });
+
+ describe('calling getSnapshot for id 3', function() {
+
+ it('it should callback with the correct values', function(done) {
+ storage.getSnapshot('3', -1, function(err, snap) {
+ expect(err).to.eql(null);
+ expect(snap.data).to.eql('dataPlus');
+ expect(snap.snapshotId).to.eql('2');
+ expect(snap.streamId).to.eql('3');
+ expect(snap.revision).to.eql('2');
+
+ done();
+ });
+ });
+
+ });
+
+ describe('calling getSnapshot for id 3 with maxRev 1', function() {
+
+ it('it should callback with the correct values', function(done) {
+ storage.getSnapshot('3', 1, function(err, snap) {
+ expect(err).to.eql(null);
+ expect(snap.data).to.eql('data');
+ expect(snap.snapshotId).to.eql('1');
+ expect(snap.streamId).to.eql('3');
+ expect(snap.revision).to.eql('1');
+
+ done();
+ });
+ });
+
+ });
+
});
+
});
-}
+
+ });
View
378 test/eventStoreSpec.js
@@ -1,142 +1,252 @@
-var vows = require('vows')
- , assert = require('assert')
- , eventstore = require('../lib/eventStore').createStore();
+var expect = require('expect.js')
+ , eventstoreModule = require('../lib/eventStore')
+ , storageModule = require('../lib/storage/inMemory/storage');
-eventstore.start();
-
-vows.describe('The EventStore')
-.addBatch({
- 'An unconfigured eventstore does not throw error when': {
- topic: eventstore,
-
- 'requesting an eventstream': function(es) {
- assert.doesNotThrow(function() {es.getEventStream('1', 0, -1, function (error, es) {});});
- },
-
- 'committing': function(es) {
- assert.doesNotThrow(function() {es.commit({currentRevision: function() {return 0;}, events: [], uncommittedEvents: []});}, this.callback);
- }
- }
-})
-.addBatch({
- 'A configured eventstore': {
- topic: function() {
- require('../lib/storage/inMemory/storage').createStorage(function(err, storage) {
- eventstore.configure(function() {
- // configure eventstore
- eventstore.use(storage);
- this.callback(null, eventstore);
- }.bind(this));
- }.bind(this));
- },
-
- 'can commit a single event': {
- topic: function() {
- eventstore.commit({currentRevision: function() {return 0;}, events: [],uncommittedEvents:[{streamId: 'e1', payload: {event:'bla'}}]}, this.callback);
- },
-
- 'and can commit an additional array of events': {
- topic: function() {
- eventstore.commit({currentRevision: function() {return 0;},events: [],uncommittedEvents:[{streamId: 'e1', payload: {event:'bla'}}, {streamId: 'e1', payload: {event:'bla'}}]}, this.callback);
- },
-
- 'and can request it`s full eventstream': {
- topic: function() {
- eventstore.getEventStream('e1', 0, -1, this.callback);
- },
-
- 'correctly': function(err, stream) {
- assert.equal(stream.events.length, 3);
- },
-
- 'the eventstream has no uncommitted events': function(err, stream) {
- assert.equal(stream.uncommittedEvents.length, 0);
- }
- }
- }
- },
-
- 'can work with eventstream': {
- topic: function() {
- eventstore.getEventStream('e2', 0, -1, this.callback);
- },
-
- 'so to add events to the stream': function(err, stream) {
- stream.addEvent({streamId: 'e2', payload: null});
- },
-
- 'and commit it': function(err, stream) {
- stream.commit();
- }
- },
-
- 'can work with snapshots': {
- topic: function() {
- eventstore.getEventStream('e3', 0, -1, function(err, stream) {
- stream.addEvent({streamId: 'e3', payload: null});
- stream.commit(function(err) {
- eventstore.getEventStream('e3', 0, -1, this.callback);
- }.bind(this));
- }.bind(this));
- },
-
- 'so create a snapshot from eventstream': {
- topic: function(stream) {
- assert.equal(stream.currentRevision(), 0);
- assert.equal(stream.currentRevision(), stream.lastRevision);
- eventstore.createSnapshot(stream.streamId, stream.currentRevision(), 'data', this.callback);
- },
-
- 'and request it': {
- topic: function(err) {
- eventstore.getFromSnapshot('e3', this.callback);
+ describe('EventStore', function() {
+
+ var eventstore;
+
+ describe('not beeing configured', function() {
+
+ before(function() {
+ eventstore = eventstoreModule.createStore();
+ });
+
+ describe('requesting an eventstream', function() {
+
+ it('it should callback with an error', function(done) {
+
+ eventstore.getEventStream('1', 0, -1, function(err) {
+ expect(err).to.not.eql(null);
+ done();
+ });
+
+ });
+
+ });
+
+ describe('committing', function() {
+
+ it('it should callback with an error', function(done) {
+
+ var fakeEventStream = {
+ currentRevision: function() {
+ return 0;
},
-
- 'correctly': function(err, snapshot, stream) {
- assert.equal(snapshot.data, 'data');
- assert.equal(snapshot.streamId, 'e3');
- assert.equal(stream.currentRevision(), 0);
- assert.equal(snapshot.revision, stream.lastRevision);
- assert.equal(snapshot.revision, stream.currentRevision());
- }
- }
- }
- },
-
- 'can request': {
- topic: function() {
+ events: [],
+ uncommittedEvents: []
+ };
+
+ eventstore.commit(fakeEventStream, function(err) {
+ expect(err).to.not.eql(null);
+ done();
+ });
+
+ });
+
+ });
+
+ });
+
+ describe('beeing configured', function() {
+
+ before(function(done) {
+ eventstore = eventstoreModule.createStore();
+ storageModule.createStorage(function(err, storage) {
+ eventstore.configure(function() {
+ // to use the in process dispatcher
+ delete storage.filename;
+ this.use(storage);
+ });
+ eventstore.start();
+ done();
+ });
+ });
+
+ describe('requesting an eventstream', function() {
+
+ it('it should callback without an error', function(done) {
+
+ eventstore.getEventStream('1', 0, -1, function(err, es) {
+ expect(err).to.eql(null);
+ done();
+ });
+
+ });
+
+ });
+
+ describe('requesting all events of a stream', function() {
+
+ before(function(done) {
eventstore.getEventStream('e4', 0, -1, function(err, stream) {
stream.addEvent({ id: '1'});
stream.addEvent({ id: '2'});
stream.addEvent({ id: '3'});
stream.addEvent({ id: '4'});
stream.addEvent({ id: '5'});
- stream.commit(this.callback);
- }.bind(this));
- },
-
- 'all events of a stream': {
- topic: function(err) {
- eventstore.getEvents('e4', this.callback);
- },
-
- 'correctly': function(err, events) {
- assert.equal(events[0].payload.id, '1');
- assert.equal(events.length, 5);
- }
- },
-
- 'events by eventId': {
- topic: function(err) {
- eventstore.getEventRange({id: '2'}, 2, function(err, evts) {
- evts.next(this.callback);
- }.bind(this));
- },
-
- 'correctly': function(err, events) {
- assert.equal(events[0].payload.id, '5');
- }
- }
- }
- }
-}).export(module);
+ stream.commit(done);
+ });
+ });
+
+ it('it should callback with the correct values', function(done) {
+
+ eventstore.getEvents('e4', function(err, events) {
+ expect(err).to.eql(null);
+ expect(events[0].payload.id).to.eql('1');
+ expect(events).to.have.length(5);
+ done();
+ });
+
+ });
+
+ });
+
+ describe('requesting an event range', function() {
+
+ before(function(done) {
+ eventstore.getEventStream('e5', 0, -1, function(err, stream) {
+ stream.addEvent({ id: '11'});
+ stream.addEvent({ id: '22'});
+ stream.addEvent({ id: '33'});
+ stream.addEvent({ id: '44'});
+ stream.addEvent({ id: '55'});
+ stream.commit(done);
+ });
+ });
+
+ var events;
+
+ it('it should callback with the correct values', function(done) {
+
+ eventstore.getEventRange({id: '22'}, 2, function(err, evts) {
+ events = evts;
+ expect(err).to.eql(null);
+ expect(events[0].payload.id).to.eql('33');
+ expect(events).to.have.length(2);
+ done();
+ });
+
+ });
+
+ describe('requesting the next range', function() {
+
+ it('it should callback with the correct values', function(done) {
+
+ events.next(function(err, evts) {
+ expect(err).to.eql(null);
+ expect(evts[0].payload.id).to.eql('55');
+ expect(evts).to.have.length(1);
+ done();
+ });
+
+ });
+
+ });
+
+ });
+
+ describe('committing', function() {
+
+ it('it should callback without an error', function(done) {
+
+ var fakeEventStream = {
+ currentRevision: function() {
+ return 0;
+ },
+ events: [],
+ uncommittedEvents: [
+ {streamId: 'e1', payload: { event:'bla' } },
+ {streamId: 'e1', payload: { event:'blabli' } }
+ ]
+ };
+
+ eventstore.commit(fakeEventStream, function(err) {
+ expect(err).to.eql(null);
+ done();
+ });
+
+ });
+
+ describe('requesting the full eventstream', function() {
+
+ it('it should callback with the correct values', function(done) {
+
+ eventstore.getEventStream('e1', 0, -1, function(err, es) {
+ expect(es.events).to.have.length(2);
+ expect(es.uncommittedEvents).to.have.length(0);
+ done();
+ });
+
+ });
+
+ });
+
+ });
+
+ describe('adding an event to an eventstream', function() {
+
+ before(function(done) {
+ eventstore.getEventStream('e2', 0, -1, function(err, es) {
+ es.addEvent({streamId: 'e2', payload: 'test'});
+ es.commit(done);
+ });
+ });
+
+ describe('requesting the full eventstream', function() {
+
+ var stream;
+
+ it('it should callback with the correct values', function(done) {
+
+ eventstore.getEventStream('e2', 0, -1, function(err, es) {
+ stream = es;
+ expect(es.currentRevision()).to.be(0);
+ expect(es.currentRevision()).to.be(es.lastRevision);
+ expect(es.events).to.have.length(1);
+ expect(es.uncommittedEvents).to.have.length(0);
+ expect(es.events[0].streamId).to.eql('e2');
+ expect(es.events[0].payload.payload).to.eql('test');
+ done();
+ });
+
+ });
+
+ describe('creating a snapshot', function() {
+
+ it('it should callback without an error', function(done) {
+
+ eventstore.createSnapshot(stream.streamId, stream.currentRevision(), 'data', function(err) {
+ expect(err).to.eql(null);
+ done();
+ });
+
+ });
+
+ describe('calling getFromSnapshot', function() {
+
+ it('it should callback with the correct values', function(done) {
+
+ eventstore.getFromSnapshot('e2', function(err, snapshot, es) {
+ expect(err).to.eql(null);
+ expect(snapshot.data).to.eql('data');
+ expect(snapshot.streamId).to.eql('e2');
+ expect(es.currentRevision()).to.be(0);
+ expect(snapshot.revision).to.be(es.lastRevision);
+ expect(snapshot.revision).to.be(es.currentRevision());
+ done();
+ });
+
+ });
+
+ });
+
+ });
+
+ });
+
+ });
+
+ });
+
+ });
View
414 test/inMemoryStorageSpec.js
@@ -1,171 +1,259 @@
-var vows = require('vows')
- , assert = require('assert');
-
-var storageName = "inMemory";
-
-var options = {};
-
-vows.describe('The ' + storageName + ' Storage')
-.addBatch({
- 'An empty Storage': {
- topic: function () {
- require('../lib/storage/' + storageName + '/storage').createStorage(options, function(err, storage) {
- this.callback(null, storage);
- }.bind(this));
- },
-
- 'has a function getId': function(storage) {
- assert.isFunction(storage.getId);
- },
-
- 'getId returns a string': function(storage) {
- storage.getId(function(err, id) {
- assert.isString(id);
+var expect = require('expect.js')
+ , storageModule = require('../lib/storage/inMemory/storage');
+
+ describe('Storage', function() {
+
+ var storage;
+
+ describe('beeing not connected', function() {
+
+ describe('calling createStorage', function() {
+
+ describe('without a callback', function() {
+
+ before(function() {
+ storage = storageModule.createStorage();
+ });
+
+ describe('calling connect', function() {
+
+ it('it should connect successfully', function(done) {
+
+ storage.connect(function(err) {
+ expect(err).to.eql(null);
+ done();
+ });
+
+ });
+
+ });
+
});
- },
-
- 'a second id returned by getId won\'t equal the first': function(storage) {
- storage.getId(function(err, id1) {
- storage.getId(function(err, id2) {
- assert.notEqual(id1, id2);
+
+ describe('with a callback', function() {
+
+ it('it should connect successfully', function(done) {
+
+ storageModule.createStorage(function(err, str) {
+ storage = str;
+ expect(err).to.eql(null);
+ expect(str).to.not.eql(null);
+ done();
+ });
+
});
+
});
- },
-
- 'can be filled with events': function(storage) {
- var id = '1';
- storage.addEvents([{'streamId': id, commitId: '10', 'payload': {event:'bla'}}], function() {
- storage.getEvents(id, 0, -1, function(err, events) {
- assert.isArray(events);
- assert.equal(events.length, 1);
+
+ });
+
+ });
+
+ describe('beeing connected', function() {
+
+ describe('calling getId', function() {
+
+ it('it should callback with a new id', function(done) {
+
+ storage.getId(function(err, id) {
+ expect(err).to.eql(null);
+ expect(id).to.be.an('string');
+ done();
});
+
});
- }
- }
-})
-.addBatch({
- 'An filled Storage': {
- topic: function() {
- require('../lib/storage/' + storageName + '/storage').createStorage(options, function(err, storage) {
- fillStore(storage, this.callback);
- }.bind(this));
- },
-
- 'after a successful `fill` we get events for id 2': {
- topic: function (storage) {
- storage.getEvents('2', 0, -1, this.callback);
- },
-
- 'we can assert if length is right': function (events) {
- assert.equal(events.length, 4);
- },
-
- 'we can assert if sorting is right': function (events) {
- assert.equal(events[0].commitId, '0');
- assert.equal(events[1].commitId, '1');
- assert.equal(events[3].commitId, '3');
- }
- },
-
- 'after a successful `fill` we get events for id 3': {
- topic: function (storage) {
- storage.getEvents('3', 0, -1, this.callback);
- },
-
- 'we can assert if length is right': function (events) {
- assert.equal(events.length, 2);
- }
- },
-
- 'after a successful `fill` we get events for id 2 from 1 to 3': {
- topic: function (storage) {
- storage.getEvents('2', 1, 3, this.callback);
- },
-
- 'we can assert if length is right': function (events) {
- assert.equal(events.length, 2);
- }
- },
-
- 'after a successful `fill` we get all undispatched events': {
- topic: function (storage) {
- storage.getUndispatchedEvents(this.callback);
- },
-
- 'we can assert if length is right': function (events) {
- assert.equal(events.length, 6);
- },
-
- 'we can assert if sorting is right': function (events) {
- assert.equal(events[0].commitId, '0');
- assert.equal(events[2].commitId, '2');
- assert.equal(events[5].commitId, '5');
- }
- },
-
- 'after a successful `fill` we get a range of events searching by event id': {
- topic: function (storage) {
- storage.getEventRange({id: '2'}, 2, this.callback);
- },
-
- 'we can assert if length is right': function (events) {
- assert.equal(events.length, 2);
- },
-
- 'we can assert if sorting is right': function (events) {
- assert.equal(events[0].commitId, '2');
- assert.equal(events[1].commitId, '3');
- }
- },
-
- 'after a successful `fill with a snapshot` we get the snapshot': {
- topic: function (storage) {
- storage.getSnapshot('3', -1, this.callback);
- },
-
- 'we can assert if snapshot is right': function (snapshot) {
- assert.equal(snapshot.data, 'dataPlus');
- assert.equal(snapshot.snapshotId, '2');
- assert.equal(snapshot.streamId, '3');
- assert.equal(snapshot.revision, '2');
- }
- },
-
- 'after a successful `fill with a snapshot` we get the snapshot with maxRev': {
- topic: function (storage) {
- storage.getSnapshot('3', 1, this.callback);
- },
-
- 'we can assert if snapshot is right': function (snapshot) {
- assert.equal(snapshot.data, 'data');
- assert.equal(snapshot.snapshotId, '1');
- assert.equal(snapshot.streamId, '3');
- assert.equal(snapshot.revision, '1');
- }
- }
- }
-}).export(module);
-
-
-function fillStore(storage, callback) {
- storage.addEvents([
- {streamId: '2', streamRevision: 0, commitId: 0, commitStamp: new Date(2012, 3, 14, 8, 0, 0), payload: {id: '1', event:'blaaaaaaaaaaa'}, dispatched: false},
- {streamId: '2', streamRevision: 1, commitId: 1, commitStamp: new Date(2012, 3, 14, 9, 0, 0), payload: {id: '2', event:'blaaaaaaaaaaa'}, dispatched: false},
- {streamId: '2', streamRevision: 2, commitId: 2, commitStamp: new Date(2012, 3, 14, 10, 0, 0), payload: {id: '3', event:'blaaaaaaaaaaa'}, dispatched: false},
- {streamId: '2', streamRevision: 3, commitId: 3, commitStamp: new Date(2012, 3, 15, 8, 0, 0), payload: {id: '4', event:'blaaaaaaaaaaa'}, dispatched: false}
- ],
- function (err) {
- storage.addEvents([
- {streamId: '3', streamRevision: 0, commitId: 4, commitStamp: new Date(2012, 3, 16, 8, 0, 0), payload: {id: '5', event:'blaaaaaaaaaaa'}, dispatched: false},
- {streamId: '3', streamRevision: 1, commitId: 5, commitStamp: new Date(2012, 3, 17, 8, 0, 0), payload: {id: '6', event:'blaaaaaaaaaaa'}, dispatched: false}
- ],
- function (err) {
- storage.addSnapshot({snapshotId: '1', streamId: '3', revision: 1, data: 'data'}, function() {
- storage.addSnapshot({snapshotId: '2', streamId: '3', revision: 2, data: 'dataPlus'}, function() {
- callback(null, storage);
+
+ });
+
+ describe('calling addEvents', function() {
+
+ it('it should save the events', function(done) {
+
+ var events = [
+ {
+ streamId: 'id1',
+ commitId: '10',
+ payload: {
+ event:'bla'
+ }
+ }
+ ];
+
+ storage.addEvents(events, function(err) {
+ expect(err).to.eql(null);
+
+ storage.getEvents('id1', -1, function(err, evts) {
+ expect(err).to.eql(null);
+ expect(evts).to.be.an('array');
+ expect(evts).to.have.length(1);
+
+ done();
+ });
+ });
+
+ });
+
+ });
+
+ describe('calling addSnapshot', function() {
+
+ it('it should save the snapshot', function(done) {
+
+ var snapshot = {
+ snapshotId: '1',
+ streamId: '3',
+ revision: 1,
+ data: 'data'
+ };
+
+ storage.addSnapshot(snapshot, function(err) {
+ expect(err).to.eql(null);
+
+ storage.getSnapshot('3', function(err, snap) {
+ expect(err).to.eql(null);
+ expect(snap.data).to.eql(snapshot.data);
+ expect(snap.snapshotId).to.eql(snapshot.snapshotId);
+ expect(snap.revision).to.eql(snapshot.revision);
+ expect(snap.streamId).to.eql(snapshot.streamId);
+
+ done();
});
});
- }
- );
+
+ });
+
+ });
+
+ describe('having a filled store with example data', function() {
+
+ before(function(done) {
+ storage.addEvents([
+ {streamId: '2', streamRevision: 0, commitId: 0, commitStamp: new Date(2012, 3, 14, 8, 0, 0), payload: {id: '1', event:'blaaaaaaaaaaa'}, dispatched: false},
+ {streamId: '2', streamRevision: 1, commitId: 1, commitStamp: new Date(2012, 3, 14, 9, 0, 0), payload: {id: '2', event:'blaaaaaaaaaaa'}, dispatched: false},
+ {streamId: '2', streamRevision: 2, commitId: 2, commitStamp: new Date(2012, 3, 14, 10, 0, 0), payload: {id: '3', event:'blaaaaaaaaaaa'}, dispatched: false},
+ {streamId: '2', streamRevision: 3, commitId: 3, commitStamp: new Date(2012, 3, 15, 8, 0, 0), payload: {id: '4', event:'blaaaaaaaaaaa'}, dispatched: false}
+ ],
+ function (err) {
+ storage.addEvents([
+ {streamId: '3', streamRevision: 0, commitId: 4, commitStamp: new Date(2012, 3, 16, 8, 0, 0), payload: {id: '5', event:'blaaaaaaaaaaa'}, dispatched: false},
+ {streamId: '3', streamRevision: 1, commitId: 5, commitStamp: new Date(2012, 3, 17, 8, 0, 0), payload: {id: '6', event:'blaaaaaaaaaaa'}, dispatched: false}
+ ],
+ function (err) {
+ storage.addSnapshot({snapshotId: '1', streamId: '3', revision: 1, data: 'data'}, function() {
+ storage.addSnapshot({snapshotId: '2', streamId: '3', revision: 2, data: 'dataPlus'}, done);
+ });
+ }
+ );
+ });
+ });
+
+ describe('calling getEvents for id 2', function() {
+
+ it('it should callback with the correct values', function(done) {
+ storage.getEvents('2', 0, -1, function(err, events) {
+ expect(err).to.eql(null);
+ expect(events).to.have.length(4);
+ expect(events[0].commitId).to.eql('0');
+ expect(events[1].commitId).to.eql('1');
+ expect(events[3].commitId).to.eql('3');
+
+ done();
+ });
+ });
+
+ });
+
+ describe('calling getEvents for id 3', function() {
+
+ it('it should callback with the correct values', function(done) {
+ storage.getEvents('3', 0, -1, function(err, events) {
+ expect(err).to.eql(null);
+ expect(events).to.have.length(2);
+
+ done();
+ });
+ });
+
+ });
+
+ describe('calling getEvents for id 2 from 1 to 3', function() {
+
+ it('it should callback with the correct values', function(done) {
+ storage.getEvents('2', 1, 3, function(err, events) {
+ expect(err).to.eql(null);
+ expect(events).to.have.length(2);
+
+ done();
+ });
+ });
+
+ });
+
+ describe('calling getUndispatchedEvents', function() {
+
+ it('it should callback with the correct values', function(done) {
+ storage.getUndispatchedEvents(function(err, events) {
+ expect(err).to.eql(null);
+ expect(events).to.have.length(6);
+ expect(events[0].commitId).to.eql('0');
+ expect(events[2].commitId).to.eql('2');
+ expect(events[5].commitId).to.eql('5');
+
+ done();
+ });
+ });
+
+ });
+
+ describe('calling getEventRange searching by event id', function() {
+
+ it('it should callback with the correct values', function(done) {
+ storage.getEventRange({id: '2'}, 2, function(err, events) {
+ expect(err).to.eql(null);
+ expect(events).to.have.length(2);
+ expect(events[0].commitId).to.eql('2');
+ expect(events[1].commitId).to.eql('3');
+
+ done();
+ });
+ });
+
+ });
+
+ describe('calling getSnapshot for id 3', function() {
+
+ it('it should callback with the correct values', function(done) {
+ storage.getSnapshot('3', -1, function(err, snap) {
+ expect(err).to.eql(null);
+ expect(snap.data).to.eql('dataPlus');
+ expect(snap.snapshotId).to.eql('2');
+ expect(snap.streamId).to.eql('3');
+ expect(snap.revision).to.eql('2');
+
+ done();
+ });
+ });
+
+ });
+
+ describe('calling getSnapshot for id 3 with maxRev 1', function() {
+
+ it('it should callback with the correct values', function(done) {
+ storage.getSnapshot('3', 1, function(err, snap) {
+ expect(err).to.eql(null);
+ expect(snap.data).to.eql('data');
+ expect(snap.snapshotId).to.eql('1');
+ expect(snap.streamId).to.eql('3');
+ expect(snap.revision).to.eql('1');
+
+ done();
+ });
+ });
+
+ });
+
+ });
+
});
-}
+
+ });

0 comments on commit 9550bff

Please sign in to comment.
Something went wrong with that request. Please try again.