2018-01-03 04:05:53 +00:00
|
|
|
// Copyright IBM Corp. 2015,2018. All Rights Reserved.
|
2016-05-03 22:50:21 +00:00
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2016-11-22 14:30:04 +00:00
|
|
|
var expect = require('./helpers/expect');
|
2017-07-04 21:01:24 +00:00
|
|
|
var sinon = require('sinon');
|
2016-11-15 21:46:23 +00:00
|
|
|
var loopback = require('../');
|
|
|
|
|
2015-07-09 20:30:50 +00:00
|
|
|
describe('PersistedModel.createChangeStream()', function() {
|
|
|
|
describe('configured to source changes locally', function() {
|
|
|
|
before(function() {
|
|
|
|
var test = this;
|
2016-11-15 21:46:23 +00:00
|
|
|
var app = loopback({localRegistry: true});
|
|
|
|
var ds = app.dataSource('ds', {connector: 'memory'});
|
2016-06-03 20:51:48 +00:00
|
|
|
var Score = app.registry.createModel('Score');
|
|
|
|
this.Score = app.model(Score, {
|
2015-07-09 20:30:50 +00:00
|
|
|
dataSource: 'ds',
|
2016-04-01 09:14:26 +00:00
|
|
|
changeDataSource: false, // use only local observers
|
2015-07-09 20:30:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-07-04 21:01:24 +00:00
|
|
|
afterEach(verifyObserversRemoval);
|
|
|
|
|
2015-07-09 20:30:50 +00:00
|
|
|
it('should detect create', function(done) {
|
|
|
|
var Score = this.Score;
|
|
|
|
|
|
|
|
Score.createChangeStream(function(err, changes) {
|
|
|
|
changes.on('data', function(change) {
|
|
|
|
expect(change.type).to.equal('create');
|
|
|
|
changes.destroy();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
Score.create({team: 'foo'});
|
2015-07-09 20:30:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect update', function(done) {
|
|
|
|
var Score = this.Score;
|
2016-11-15 21:46:23 +00:00
|
|
|
Score.create({team: 'foo'}, function(err, newScore) {
|
2015-07-09 20:30:50 +00:00
|
|
|
Score.createChangeStream(function(err, changes) {
|
|
|
|
changes.on('data', function(change) {
|
|
|
|
expect(change.type).to.equal('update');
|
|
|
|
changes.destroy();
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-09 20:30:50 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
newScore.updateAttributes({
|
2016-04-01 09:14:26 +00:00
|
|
|
bat: 'baz',
|
2015-07-09 20:30:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect delete', function(done) {
|
|
|
|
var Score = this.Score;
|
2016-11-15 21:46:23 +00:00
|
|
|
Score.create({team: 'foo'}, function(err, newScore) {
|
2015-07-09 20:30:50 +00:00
|
|
|
Score.createChangeStream(function(err, changes) {
|
|
|
|
changes.on('data', function(change) {
|
|
|
|
expect(change.type).to.equal('remove');
|
|
|
|
changes.destroy();
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-09 20:30:50 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
newScore.remove();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-07-04 21:01:24 +00:00
|
|
|
|
2017-11-02 10:01:52 +00:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-07-04 21:01:24 +00:00
|
|
|
it('should not emit changes after destroy', function(done) {
|
|
|
|
var Score = this.Score;
|
|
|
|
|
|
|
|
var 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() {
|
|
|
|
var Score = this.Score;
|
|
|
|
expect(Score._observers['after save']).to.be.empty();
|
|
|
|
expect(Score._observers['after delete']).to.be.empty();
|
|
|
|
}
|
2015-07-09 20:30:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// TODO(ritch) implement multi-server support
|
|
|
|
describe.skip('configured to source changes using pubsub', function() {
|
|
|
|
before(function() {
|
|
|
|
var test = this;
|
2016-11-15 21:46:23 +00:00
|
|
|
var app = loopback({localRegistry: true});
|
|
|
|
var db = app.dataSource('ds', {connector: 'memory'});
|
2015-07-09 20:30:50 +00:00
|
|
|
var ps = app.dataSource('ps', {
|
|
|
|
host: 'localhost',
|
|
|
|
port: '12345',
|
|
|
|
connector: 'pubsub',
|
2016-04-01 09:14:26 +00:00
|
|
|
pubsubAdapter: 'mqtt',
|
2015-07-09 20:30:50 +00:00
|
|
|
});
|
|
|
|
this.Score = app.model('Score', {
|
|
|
|
dataSource: 'db',
|
2016-04-01 09:14:26 +00:00
|
|
|
changeDataSource: 'ps',
|
2015-07-09 20:30:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect a change', function(done) {
|
|
|
|
var Score = this.Score;
|
|
|
|
|
|
|
|
Score.createChangeStream(function(err, changes) {
|
|
|
|
changes.on('data', function(change) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|