2016-05-03 22:50:21 +00:00
|
|
|
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
|
|
|
|
// 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';
|
|
|
|
var expect = require('chai').expect;
|
|
|
|
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
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-09 20:30:50 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|