/
couchRepositorySpec.js
138 lines (106 loc) · 4.01 KB
/
couchRepositorySpec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var http = require('http'),
jasmine = require('jasmine-node'),
CouchDb = require('../../lib/repository/couchRepository'),
EventEmitter = require('events').EventEmitter;
describe('CouchRepository', function() {
var couchdb;
beforeEach(function() {
couchdb = new CouchDb('cqrs');
})
describe('instance', function() {
it('should get instance of couchdb', function() {
var couchdb = CouchDb.getInstance()
expect(typeof couchdb.request).toEqual('function');
})
it('should return just one instance', function() {
var couch1 = CouchDb.getInstance(),
couch2 = CouchDb.getInstance();
couch1.database = 'foobar';
expect(couch2.database).toEqual('foobar')
})
})
describe('constructor', function() {
it('should store database name', function() {
expect(couchdb.database).toEqual('cqrs');
})
it('host should be default to localhost', function() {
expect(couchdb.options.host).toEqual('localhost');
})
it('port should be default to localhost', function() {
expect(couchdb.options.port).toEqual(5984);
})
})
describe('storeEvent', function() {
it('should call create document', function() {
spyOn(couchdb, 'createDocument');
spyOn(Date.prototype, 'getTime').andCallFake(function() { return 123456; })
couchdb.storeEvent(1, 'user:created', {foo: 'bar'});
expect(couchdb.createDocument).toHaveBeenCalledWith(JSON.stringify({
aggregateId: 1,
name: 'user:created',
type: 'event',
time: 123456,
attrs: {foo: 'bar'}
}));
})
})
describe('getEventsByAggregate', function() {
it('should call request', function() {
spyOn(couchdb, 'request');
couchdb.getEventsByAggregate(1, function() {});
expect(couchdb.request).toHaveBeenCalledWith({
method : 'GET',
path : '/cqrs/_design/cqrs/_view/aggregate?startkey=[1,0]&endkey=[1,9999999999999]'
}, jasmine.any(Function));
})
it('should call parseEvents', function() {
var f = function() {}
spyOn(couchdb, 'parseEvents');
spyOn(couchdb, 'request').andCallFake(function(data, callback) {
callback('data');
})
couchdb.getEventsByAggregate(1, f);
expect(couchdb.parseEvents).toHaveBeenCalledWith('data', f);
})
})
describe('getEventsByType', function() {
beforeEach(function() {
spyOn(couchdb, 'request').andCallFake(function(data, callback) {
callback('{"rows": [{"_id":1, "_ref":1, "value": {"foo": "bar"}}]}');
})
})
it('should call request', function() {
couchdb.getEventsByName('foo', 1000, function() {});
expect(couchdb.request).toHaveBeenCalledWith({
method: 'GET',
path: '/cqrs/_design/cqrs/_view/name?startkey=["foo",1000]&endkey=["foo",9999999999999]'
}, jasmine.any(Function));
})
describe('with event list', function() {
it('should call request for each event', function() {
couchdb.getEventsByName(['foo', 'bar'], 0, function() {});
expect(couchdb.request).toHaveBeenCalledWith({
method: 'GET',
path: '/cqrs/_design/cqrs/_view/name?startkey=["foo",0]&endkey=["foo",9999999999999]'
}, jasmine.any(Function));
expect(couchdb.request).toHaveBeenCalledWith({
method: 'GET',
path: '/cqrs/_design/cqrs/_view/name?startkey=["bar",0]&endkey=["bar",9999999999999]'
}, jasmine.any(Function));
})
it('should call callback just once', function() {
var foo = {f: function() {}}
spyOn(foo, 'f');
couchdb.getEventsByName(['foo', 'bar'], 0, foo.f);
expect(foo.f).toHaveBeenCalled();
expect(foo.f.callCount).toEqual(1);
})
})
it('should call parseEvents', function() {
var f = function() {}
spyOn(couchdb, 'parseEvents');
couchdb.getEventsByName('foo', 0, f);
expect(couchdb.parseEvents).toHaveBeenCalledWith('{"rows": [{"_id":1, "_ref":1, "value": {"foo": "bar"}}]}', f);
})
})
})