forked from strongloop/loopback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange-stream.test.js
160 lines (137 loc) · 4.43 KB
/
change-stream.test.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright IBM Corp. 2015,2018. All Rights Reserved.
// Node module: loopback
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
const expect = require('./helpers/expect');
const sinon = require('sinon');
const loopback = require('../');
describe('PersistedModel.createChangeStream()', function() {
describe('configured to source changes locally', function() {
before(function() {
const test = this;
const app = loopback({localRegistry: true});
const ds = app.dataSource('ds', {connector: 'memory'});
const Score = app.registry.createModel('Score');
this.Score = app.model(Score, {
dataSource: 'ds',
changeDataSource: false, // use only local observers
});
});
afterEach(verifyObserversRemoval);
it('should detect create', function(done) {
const Score = this.Score;
Score.createChangeStream(function(err, changes) {
changes.on('data', function(change) {
expect(change.type).to.equal('create');
changes.destroy();
done();
});
Score.create({team: 'foo'});
});
});
it('should detect update', function(done) {
const Score = this.Score;
Score.create({team: 'foo'}, function(err, newScore) {
Score.createChangeStream(function(err, changes) {
changes.on('data', function(change) {
expect(change.type).to.equal('update');
changes.destroy();
done();
});
newScore.updateAttributes({
bat: 'baz',
});
});
});
});
it('should detect delete', function(done) {
const Score = this.Score;
Score.create({team: 'foo'}, function(err, newScore) {
Score.createChangeStream(function(err, changes) {
changes.on('data', function(change) {
expect(change.type).to.equal('remove');
changes.destroy();
done();
});
newScore.remove();
});
});
});
it('should apply "where" and "fields" to create events', function() {
const Score = this.Score;
const data = [
{team: 'baz', player: 'baz', value: 1},
{team: 'bar', player: 'baz', value: 2},
{team: 'foo', player: 'bar', value: 3},
];
const options = {where: {player: 'bar'}, fields: ['team', 'value']};
const changes = [];
let changeStream;
return Score.createChangeStream(options)
.then(stream => {
changeStream = stream;
changeStream.on('data', function(change) {
changes.push(change);
});
return Score.create(data);
})
.then(scores => {
changeStream.destroy();
expect(changes).to.have.length(1);
expect(changes[0]).to.have.property('type', 'create');
expect(changes[0].data).to.eql({
'team': 'foo',
value: 3,
});
});
});
it('should not emit changes after destroy', function(done) {
const Score = this.Score;
const spy = sinon.spy();
Score.createChangeStream(function(err, changes) {
changes.on('data', function() {
spy();
changes.destroy();
});
Score.create({team: 'foo'})
.then(() => Score.deleteAll())
.then(() => {
expect(spy.calledOnce);
done();
});
});
});
function verifyObserversRemoval() {
const Score = this.Score;
expect(Score._observers['after save']).to.be.empty();
expect(Score._observers['after delete']).to.be.empty();
}
});
// TODO(ritch) implement multi-server support
describe.skip('configured to source changes using pubsub', function() {
before(function() {
const test = this;
const app = loopback({localRegistry: true});
const db = app.dataSource('ds', {connector: 'memory'});
const ps = app.dataSource('ps', {
host: 'localhost',
port: '12345',
connector: 'pubsub',
pubsubAdapter: 'mqtt',
});
this.Score = app.model('Score', {
dataSource: 'db',
changeDataSource: 'ps',
});
});
it('should detect a change', function(done) {
const Score = this.Score;
Score.createChangeStream(function(err, changes) {
changes.on('data', function(change) {
done();
});
});
});
});
});