Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

make test with mocha

  • Loading branch information...
commit be0210d18081ac773aed0634d68f09c70d334332 1 parent 9eabd84
Adriano Raiano adrai authored
7 storage/redis/package.json
@@ -19,7 +19,10 @@
19 19 "redis": ">= 0.0.1"
20 20 , "hiredis": ">= 0.0.1"
21 21 }
22   - , "devDependencies": { }
  22 + , "devDependencies": {
  23 + "mocha": ">= 0.0.1"
  24 + , "expect.js": ">= 0.0.1"
  25 + }
23 26 , "scripts" : {
24   - "test" : "vows test/* --spec" }
  27 + "test" : "mocha" }
25 28 }
1  storage/redis/test/mocha.opts
... ... @@ -0,0 +1 @@
  1 +-R spec
427 storage/redis/test/redisStorageSpec.js
... ... @@ -1,179 +1,266 @@
1   -var vows = require('vows')
2   - , assert = require('assert');
3   -
4   -var storageName = "redis";
5   -
6   -var options = {
7   - database: 1
8   -};
9   -
10   -vows.describe('The ' + storageName + ' Storage')
11   -.addBatch({
12   - 'An empty Storage': {
13   - topic: function () {
14   - require('../storage').createStorage(options, function(err, storage) {
15   - storage.client.flushdb();
16   - this.callback(null, storage);
17   - }.bind(this));
18   - },
19   -
20   - 'has a function getId': function(storage) {
21   - assert.isFunction(storage.getId);
22   - },
23   -
24   - 'getId returns a string': function(storage) {
25   - storage.getId(function(err, id) {
26   - assert.isString(id);
  1 +var expect = require('expect.js')
  2 + , storageModule = require('../storage');
  3 +
  4 + describe('Storage', function() {
  5 +
  6 + var storage;
  7 +
  8 + describe('beeing not connected', function() {
  9 +
  10 + describe('calling createStorage', function() {
  11 +
  12 + describe('without a callback', function() {
  13 +
  14 + before(function() {
  15 + storage = storageModule.createStorage({ dbName: 'testeventstore' });
  16 + });
  17 +
  18 + describe('calling connect', function() {
  19 +
  20 + it('it should connect successfully', function(done) {
  21 +
  22 + storage.connect(function(err) {
  23 + expect(err).to.eql(null);
  24 + done();
  25 + });
  26 +
  27 + });
  28 +
  29 + });
  30 +
27 31 });
28   - },
29   -
30   - 'a second id returned by getId won\'t equal the first': function(storage) {
31   - storage.getId(function(err, id1) {
32   - storage.getId(function(err, id2) {
33   - assert.notEqual(id1, id2);
  32 +
  33 + describe('with a callback', function() {
  34 +
  35 + it('it should connect successfully', function(done) {
  36 +
  37 + storageModule.createStorage({ dbName: 'testeventstore' }, function(err, str) {
  38 + storage = str;
  39 + expect(err).to.eql(null);
  40 + expect(str).to.not.eql(null);
  41 + done();
  42 + });
  43 +
34 44 });
  45 +
35 46 });
36   - },
37   -
38   - 'can be filled with events': function(storage) {
39   - var id = '1';
40   - storage.addEvents([{'streamId': id, commitId: '10', 'payload': {event:'bla'}}], function() {
41   - storage.getEvents(id, 0, -1, function(err, events) {
42   - assert.isArray(events);
43   - assert.equal(events.length, 1);
  47 +
  48 + });
  49 +
  50 + });
  51 +
  52 + describe('beeing connected', function() {
  53 +
  54 + before(function(done) {
  55 + storage.client.flushdb();
  56 + done();
  57 + });
  58 +
  59 + describe('calling getId', function() {
  60 +
  61 + it('it should callback with a new id', function(done) {
  62 +
  63 + storage.getId(function(err, id) {
  64 + expect(err).to.eql(null);
  65 + expect(id).to.be.an('string');
  66 + done();
  67 + });
  68 +
  69 + });
  70 +
  71 + });
  72 +
  73 + describe('calling addEvents', function() {
  74 +
  75 + it('it should save the events', function(done) {
  76 +
  77 + var events = [
  78 + {
  79 + streamId: 'id1',
  80 + streamRevision: 0,
  81 + commitId: '10',
  82 + payload: {
  83 + event:'bla'
  84 + }
  85 + }
  86 + ];
  87 +
  88 + storage.addEvents(events, function(err) {
  89 + expect(err).to.eql(null);
  90 +
  91 + storage.getEvents('id1', -1, function(err, evts) {
  92 + expect(err).to.eql(null);
  93 + expect(evts).to.be.an('array');
  94 + expect(evts).to.have.length(1);
  95 +
  96 + done();
  97 + });
44 98 });
  99 +
45 100 });
46   - }
47   - }
48   -})
49   -.addBatch({
50   - 'An filled Storage': {
51   - topic: function() {
52   - require('../storage').createStorage(options, function(err, storage) {
  101 +
  102 + });
  103 +
  104 + describe('calling addSnapshot', function() {
  105 +
  106 + it('it should save the snapshot', function(done) {
  107 +
  108 + var snapshot = {
  109 + snapshotId: '1',
  110 + streamId: '3',
  111 + revision: 1,
  112 + data: 'data'
  113 + };
  114 +
  115 + storage.addSnapshot(snapshot, function(err) {
  116 + expect(err).to.eql(null);
  117 +
  118 + storage.getSnapshot('3', function(err, snap) {
  119 + expect(err).to.eql(null);
  120 + expect(snap.data).to.eql(snapshot.data);
  121 + expect(snap.snapshotId).to.eql(snapshot.snapshotId);
  122 + expect(snap.revision).to.eql(snapshot.revision);
  123 + expect(snap.streamId).to.eql(snapshot.streamId);
  124 +
  125 + done();
  126 + });
  127 + });
  128 +
  129 + });
  130 +
  131 + });
  132 +
  133 + describe('having a filled store with example data', function() {
  134 +
  135 + before(function(done) {
53 136 storage.client.flushdb();
54   - fillStore(storage, this.callback);
55   - }.bind(this));
56   - },
57   -
58   - 'after a successful `fill` we get events for id 2': {
59   - topic: function (storage) {
60   - storage.getEvents('2', 0, -1, this.callback);
61   - },
62   -
63   - 'we can assert if length is right': function (events) {
64   - assert.equal(events.length, 4);
65   - },
66   -
67   - 'we can assert if sorting is right': function (events) {
68   - assert.equal(events[0].commitId, '0');
69   - assert.equal(events[1].commitId, '1');
70   - assert.equal(events[3].commitId, '3');
71   - }
72   - },
73   -
74   - 'after a successful `fill` we get events for id 3': {
75   - topic: function (storage) {
76   - storage.getEvents('3', 0, -1, this.callback);
77   - },
78   -
79   - 'we can assert if length is right': function (events) {
80   - assert.equal(events.length, 2);
81   - }
82   - },
83   -
84   - 'after a successful `fill` we get events for id 2 from 1 to 3': {
85   - topic: function (storage) {
86   - storage.getEvents('2', 1, 3, this.callback);
87   - },
88   -
89   - 'we can assert if length is right': function (events) {
90   - assert.equal(events.length, 2);
91   - }
92   - },
93   -
94   - 'after a successful `fill` we get all undispatched events': {
95   - topic: function (storage) {
96   - storage.getUndispatchedEvents(this.callback);
97   - },
98   -
99   - 'we can assert if length is right': function (events) {
100   - assert.equal(events.length, 6);
101   - },
102   -
103   - 'we can assert if sorting is right': function (events) {
104   - assert.equal(events[0].commitId, '0');
105   - assert.equal(events[2].commitId, '2');
106   - assert.equal(events[5].commitId, '5');
107   - }
108   - },
109   -
110   - 'after a successful `fill` we get a range of events searching by event id': {
111   - topic: function (storage) {
112   - storage.getEventRange({id: '2'}, 2, this.callback);
113   - },
114   -
115   - 'we can assert if length is right': function (events) {
116   - assert.equal(events.length, 2);
117   - },
118   -
119   - 'we can assert if sorting is right': function (events) {
120   - assert.equal(events[0].commitId, '2');
121   - assert.equal(events[1].commitId, '3');
122   - }
123   - },
124   -
125   - 'after a successful `fill with a snapshot` we get the snapshot': {
126   - topic: function (storage) {
127   - storage.getSnapshot('3', -1, this.callback);
128   - },
129   -
130   - 'we can assert if snapshot is right': function (snapshot) {
131   - assert.equal(snapshot.data, 'dataPlus');
132   - assert.equal(snapshot.snapshotId, '2');
133   - assert.equal(snapshot.streamId, '3');
134   - assert.equal(snapshot.revision, '2');
135   - }
136   - },
137   -
138   - 'after a successful `fill with a snapshot` we get the snapshot with maxRev': {
139   - topic: function (storage) {
140   - storage.getSnapshot('3', 1, this.callback);
141   - },
142   -
143   - 'we can assert if snapshot is right': function (snapshot) {
144   - assert.equal(snapshot.data, 'data');
145   - assert.equal(snapshot.snapshotId, '1');
146   - assert.equal(snapshot.streamId, '3');
147   - assert.equal(snapshot.revision, '1');
148   - }
149   - },
150   -
151   - teardown: function(storage) {
152   - storage.client.flushdb();
153   - }
154   - }
155   -}).export(module);
156   -
157   -
158   -function fillStore(storage, callback) {
159   - storage.addEvents([
160   - {streamId: '2', streamRevision: 0, commitId: 0, commitStamp: new Date(2012, 3, 14, 8, 0, 0), payload: {id: '1', event:'blaaaaaaaaaaa'}, dispatched: false},
161   - {streamId: '2', streamRevision: 1, commitId: 1, commitStamp: new Date(2012, 3, 14, 9, 0, 0), payload: {id: '2', event:'blaaaaaaaaaaa'}, dispatched: false},
162   - {streamId: '2', streamRevision: 2, commitId: 2, commitStamp: new Date(2012, 3, 14, 10, 0, 0), payload: {id: '3', event:'blaaaaaaaaaaa'}, dispatched: false},
163   - {streamId: '2', streamRevision: 3, commitId: 3, commitStamp: new Date(2012, 3, 15, 8, 0, 0), payload: {id: '4', event:'blaaaaaaaaaaa'}, dispatched: false}
164   - ],
165   - function (err) {
166   - storage.addEvents([
167   - {streamId: '3', streamRevision: 0, commitId: 4, commitStamp: new Date(2012, 3, 16, 8, 0, 0), payload: {id: '5', event:'blaaaaaaaaaaa'}, dispatched: false},
168   - {streamId: '3', streamRevision: 1, commitId: 5, commitStamp: new Date(2012, 3, 17, 8, 0, 0), payload: {id: '6', event:'blaaaaaaaaaaa'}, dispatched: false}
169   - ],
170   - function (err) {
171   - storage.addSnapshot({snapshotId: '1', streamId: '3', revision: 1, data: 'data'}, function() {
172   - storage.addSnapshot({snapshotId: '2', streamId: '3', revision: 2, data: 'dataPlus'}, function() {
173   - callback(null, storage);
  137 + storage.addEvents([
  138 + {streamId: '2', streamRevision: 0, commitId: 0, commitStamp: new Date(2012, 3, 14, 8, 0, 0), payload: {id: '1', event:'blaaaaaaaaaaa'}, dispatched: false},
  139 + {streamId: '2', streamRevision: 1, commitId: 1, commitStamp: new Date(2012, 3, 14, 9, 0, 0), payload: {id: '2', event:'blaaaaaaaaaaa'}, dispatched: false},
  140 + {streamId: '2', streamRevision: 2, commitId: 2, commitStamp: new Date(2012, 3, 14, 10, 0, 0), payload: {id: '3', event:'blaaaaaaaaaaa'}, dispatched: false},
  141 + {streamId: '2', streamRevision: 3, commitId: 3, commitStamp: new Date(2012, 3, 15, 8, 0, 0), payload: {id: '4', event:'blaaaaaaaaaaa'}, dispatched: false}
  142 + ],
  143 + function (err) {
  144 + storage.addEvents([
  145 + {streamId: '3', streamRevision: 0, commitId: 4, commitStamp: new Date(2012, 3, 16, 8, 0, 0), payload: {id: '5', event:'blaaaaaaaaaaa'}, dispatched: false},
  146 + {streamId: '3', streamRevision: 1, commitId: 5, commitStamp: new Date(2012, 3, 17, 8, 0, 0), payload: {id: '6', event:'blaaaaaaaaaaa'}, dispatched: false}
  147 + ],
  148 + function (err) {
  149 + storage.addSnapshot({snapshotId: '1', streamId: '3', revision: 1, data: 'data'}, function() {
  150 + storage.addSnapshot({snapshotId: '2', streamId: '3', revision: 2, data: 'dataPlus'}, done);
  151 + });
  152 + }
  153 + );
  154 + });
  155 + });
  156 +
  157 + describe('calling getEvents for id 2', function() {
  158 +
  159 + it('it should callback with the correct values', function(done) {
  160 + storage.getEvents('2', 0, -1, function(err, events) {
  161 + expect(err).to.eql(null);
  162 + expect(events).to.have.length(4);
  163 + expect(events[0].commitId).to.eql('0');
  164 + expect(events[1].commitId).to.eql('1');
  165 + expect(events[3].commitId).to.eql('3');
  166 +
  167 + done();
  168 + });
  169 + });
  170 +
  171 + });
  172 +
  173 + describe('calling getEvents for id 3', function() {
  174 +
  175 + it('it should callback with the correct values', function(done) {
  176 + storage.getEvents('3', 0, -1, function(err, events) {
  177 + expect(err).to.eql(null);
  178 + expect(events).to.have.length(2);
  179 +
  180 + done();
  181 + });
  182 + });
  183 +
  184 + });
  185 +
  186 + describe('calling getEvents for id 2 from 1 to 3', function() {
  187 +
  188 + it('it should callback with the correct values', function(done) {
  189 + storage.getEvents('2', 1, 3, function(err, events) {
  190 + expect(err).to.eql(null);
  191 + expect(events).to.have.length(2);
  192 +
  193 + done();
  194 + });
  195 + });
  196 +
  197 + });
  198 +
  199 + describe('calling getUndispatchedEvents', function() {
  200 +
  201 + it('it should callback with the correct values', function(done) {
  202 + storage.getUndispatchedEvents(function(err, events) {
  203 + expect(err).to.eql(null);
  204 + expect(events).to.have.length(6);
  205 + expect(events[0].commitId).to.eql('0');
  206 + expect(events[2].commitId).to.eql('2');
  207 + expect(events[5].commitId).to.eql('5');
  208 +
  209 + done();
  210 + });
  211 + });
  212 +
  213 + });
  214 +
  215 + describe('calling getEventRange searching by event id', function() {
  216 +
  217 + it('it should callback with the correct values', function(done) {
  218 + storage.getEventRange({id: '2'}, 2, function(err, events) {
  219 + expect(err).to.eql(null);
  220 + expect(events).to.have.length(2);
  221 + expect(events[0].commitId).to.eql('2');
  222 + expect(events[1].commitId).to.eql('3');
  223 +
  224 + done();
174 225 });
175 226 });
176   - }
177   - );
  227 +
  228 + });
  229 +
  230 + describe('calling getSnapshot for id 3', function() {
  231 +
  232 + it('it should callback with the correct values', function(done) {
  233 + storage.getSnapshot('3', -1, function(err, snap) {
  234 + expect(err).to.eql(null);
  235 + expect(snap.data).to.eql('dataPlus');
  236 + expect(snap.snapshotId).to.eql('2');
  237 + expect(snap.streamId).to.eql('3');
  238 + expect(snap.revision).to.eql('2');
  239 +
  240 + done();
  241 + });
  242 + });
  243 +
  244 + });
  245 +
  246 + describe('calling getSnapshot for id 3 with maxRev 1', function() {
  247 +
  248 + it('it should callback with the correct values', function(done) {
  249 + storage.getSnapshot('3', 1, function(err, snap) {
  250 + expect(err).to.eql(null);
  251 + expect(snap.data).to.eql('data');
  252 + expect(snap.snapshotId).to.eql('1');
  253 + expect(snap.streamId).to.eql('3');
  254 + expect(snap.revision).to.eql('1');
  255 +
  256 + done();
  257 + });
  258 + });
  259 +
  260 + });
  261 +
  262 + });
  263 +
178 264 });
179   - }
  265 +
  266 + });

0 comments on commit be0210d

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