2018-01-03 04:05:53 +00:00
|
|
|
// Copyright IBM Corp. 2014,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';
|
2019-10-07 09:45:34 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const async = require('async');
|
|
|
|
const expect = require('./helpers/expect');
|
|
|
|
const loopback = require('../');
|
2015-03-05 12:13:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Change', function() {
|
2016-11-21 08:13:16 +00:00
|
|
|
let Change, TestModel;
|
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
beforeEach(function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const memory = loopback.createDataSource({
|
2016-04-01 09:14:26 +00:00
|
|
|
connector: loopback.Memory,
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
2015-02-26 15:36:44 +00:00
|
|
|
TestModel = loopback.PersistedModel.extend('ChangeTestModel',
|
|
|
|
{
|
2016-11-15 21:46:23 +00:00
|
|
|
id: {id: true, type: 'string', defaultFn: 'guid'},
|
2015-02-26 15:36:44 +00:00
|
|
|
},
|
|
|
|
{
|
2016-04-01 09:14:26 +00:00
|
|
|
trackChanges: true,
|
2015-02-26 15:36:44 +00:00
|
|
|
});
|
2014-01-26 21:20:19 +00:00
|
|
|
this.modelName = TestModel.modelName;
|
|
|
|
TestModel.attachTo(memory);
|
2014-05-16 00:27:02 +00:00
|
|
|
Change = TestModel.getChangeModel();
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
2014-01-26 21:20:19 +00:00
|
|
|
test.data = {
|
2016-04-01 09:14:26 +00:00
|
|
|
foo: 'bar',
|
2014-01-26 21:20:19 +00:00
|
|
|
};
|
|
|
|
TestModel.create(test.data, function(err, model) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
test.model = model;
|
|
|
|
test.modelId = model.id;
|
|
|
|
test.revisionForModel = Change.revisionForInst(model);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-12-24 15:40:48 +00:00
|
|
|
describe('Change.getCheckpointModel()', function() {
|
|
|
|
it('Shouldnt create two models if called twice', function() {
|
|
|
|
assert.equal(Change.getCheckpointModel(), Change.getCheckpointModel());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('change.id', function() {
|
|
|
|
it('should be a hash of the modelName and modelId', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const change = new Change({
|
2014-01-26 21:20:19 +00:00
|
|
|
rev: 'abc',
|
|
|
|
modelName: 'foo',
|
2016-04-01 09:14:26 +00:00
|
|
|
modelId: 'bar',
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const hash = Change.hash([change.modelName, change.modelId].join('-'));
|
2014-01-26 21:20:19 +00:00
|
|
|
|
|
|
|
assert.equal(change.id, hash);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Change.rectifyModelChanges(modelName, modelIds, callback)', function() {
|
|
|
|
describe('using an existing untracked model', function() {
|
2014-01-26 21:20:19 +00:00
|
|
|
beforeEach(function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
2014-05-16 00:27:02 +00:00
|
|
|
Change.rectifyModelChanges(this.modelName, [this.modelId], function(err, trackedChanges) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should create an entry', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
2014-05-19 22:56:26 +00:00
|
|
|
Change.find(function(err, trackedChanges) {
|
|
|
|
assert.equal(trackedChanges[0].modelId, test.modelId.toString());
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-19 22:56:26 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should only create one change', function(done) {
|
2014-01-26 21:20:19 +00:00
|
|
|
Change.count(function(err, count) {
|
|
|
|
assert.equal(count, 1);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-27 19:42:47 +00:00
|
|
|
describe('Change.rectifyModelChanges - promise variant', function() {
|
|
|
|
describe('using an existing untracked model', function() {
|
|
|
|
beforeEach(function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
2016-01-27 19:42:47 +00:00
|
|
|
Change.rectifyModelChanges(this.modelName, [this.modelId])
|
|
|
|
.then(function(trackedChanges) {
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create an entry', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
2016-01-27 19:42:47 +00:00
|
|
|
Change.find()
|
|
|
|
.then(function(trackedChanges) {
|
|
|
|
assert.equal(trackedChanges[0].modelId, test.modelId.toString());
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-01-27 19:42:47 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should only create one change', function(done) {
|
|
|
|
Change.count()
|
|
|
|
.then(function(count) {
|
|
|
|
assert.equal(count, 1);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-01-27 19:42:47 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Change.findOrCreateChange(modelName, modelId, callback)', function() {
|
|
|
|
describe('when a change doesnt exist', function() {
|
2014-01-26 21:20:19 +00:00
|
|
|
beforeEach(function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
2014-05-19 22:56:26 +00:00
|
|
|
Change.findOrCreateChange(this.modelName, this.modelId, function(err, result) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
test.result = result;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should create an entry', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
2014-01-26 21:20:19 +00:00
|
|
|
Change.findById(this.result.id, function(err, change) {
|
2014-10-10 17:10:42 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
assert.equal(change.id, test.result.id);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-27 19:42:47 +00:00
|
|
|
describe('when a change doesnt exist - promise variant', function() {
|
|
|
|
beforeEach(function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
2016-01-27 19:42:47 +00:00
|
|
|
Change.findOrCreateChange(this.modelName, this.modelId)
|
2017-12-12 08:33:15 +00:00
|
|
|
.then(function(result) {
|
|
|
|
test.result = result;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
2016-01-27 19:42:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should create an entry', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
2016-01-27 19:42:47 +00:00
|
|
|
Change.findById(this.result.id, function(err, change) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-01-27 19:42:47 +00:00
|
|
|
assert.equal(change.id, test.result.id);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-01-27 19:42:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('when a change does exist', function() {
|
2014-01-26 21:20:19 +00:00
|
|
|
beforeEach(function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
2014-01-26 21:20:19 +00:00
|
|
|
Change.create({
|
|
|
|
modelName: test.modelName,
|
2016-04-01 09:14:26 +00:00
|
|
|
modelId: test.modelId,
|
2014-01-26 21:20:19 +00:00
|
|
|
}, function(err, change) {
|
|
|
|
test.existingChange = change;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
2014-05-19 22:56:26 +00:00
|
|
|
Change.findOrCreateChange(this.modelName, this.modelId, function(err, result) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
test.result = result;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should find the entry', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
2014-01-26 21:20:19 +00:00
|
|
|
assert.equal(test.existingChange.id, test.result.id);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('change.rectify(callback)', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
let change;
|
2015-03-16 12:38:37 +00:00
|
|
|
beforeEach(function(done) {
|
|
|
|
Change.findOrCreate(
|
|
|
|
{
|
|
|
|
modelName: this.modelName,
|
2016-04-01 09:14:26 +00:00
|
|
|
modelId: this.modelId,
|
2015-03-16 12:38:37 +00:00
|
|
|
},
|
|
|
|
function(err, ch) {
|
|
|
|
change = ch;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-16 12:38:37 +00:00
|
|
|
done(err);
|
2018-08-08 15:22:20 +00:00
|
|
|
}
|
|
|
|
);
|
2015-03-05 12:13:21 +00:00
|
|
|
});
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
it('should create a new change with the correct revision', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
2014-01-26 21:20:19 +00:00
|
|
|
change.rectify(function(err, ch) {
|
|
|
|
assert.equal(ch.rev, test.revisionForModel);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-03-05 12:13:21 +00:00
|
|
|
|
|
|
|
// This test is a low-level equivalent of the test in replication.test.js
|
|
|
|
// called "replicates multiple updates within the same CP"
|
|
|
|
it('should merge updates within the same checkpoint', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
|
|
|
const originalRev = this.revisionForModel;
|
|
|
|
let cp;
|
2015-03-05 12:13:21 +00:00
|
|
|
|
|
|
|
async.series([
|
|
|
|
rectify,
|
|
|
|
checkpoint,
|
|
|
|
update,
|
|
|
|
rectify,
|
|
|
|
update,
|
|
|
|
rectify,
|
|
|
|
function(next) {
|
|
|
|
expect(change.checkpoint, 'checkpoint').to.equal(cp);
|
|
|
|
expect(change.type(), 'type').to.equal('update');
|
|
|
|
expect(change.prev, 'prev').to.equal(originalRev);
|
|
|
|
expect(change.rev, 'rev').to.equal(test.revisionForModel);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
next();
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2015-03-05 12:13:21 +00:00
|
|
|
], done);
|
|
|
|
|
|
|
|
function rectify(next) {
|
|
|
|
change.rectify(next);
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkpoint(next) {
|
|
|
|
TestModel.checkpoint(function(err, inst) {
|
|
|
|
if (err) return next(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
cp = inst.seq;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function update(next) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const model = test.model;
|
2015-03-05 12:13:21 +00:00
|
|
|
|
|
|
|
model.name += 'updated';
|
|
|
|
model.save(function(err) {
|
|
|
|
test.revisionForModel = Change.revisionForInst(model);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-12-07 12:55:14 +00:00
|
|
|
|
|
|
|
it('should not change checkpoint when rev is the same', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
|
|
|
const originalCheckpoint = change.checkpoint;
|
|
|
|
const originalRev = change.rev;
|
2015-12-07 12:55:14 +00:00
|
|
|
|
|
|
|
TestModel.checkpoint(function(err, inst) {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
change.rectify(function(err, c) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-12-07 12:55:14 +00:00
|
|
|
expect(c.rev, 'rev').to.equal(originalRev); // sanity check
|
|
|
|
expect(c.checkpoint, 'checkpoint').to.equal(originalCheckpoint);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-12-07 12:55:14 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
|
2016-01-27 19:42:47 +00:00
|
|
|
describe('change.rectify - promise variant', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
let change;
|
2016-01-27 19:42:47 +00:00
|
|
|
beforeEach(function(done) {
|
|
|
|
Change.findOrCreateChange(this.modelName, this.modelId)
|
|
|
|
.then(function(ch) {
|
|
|
|
change = ch;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-01-27 19:42:47 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create a new change with the correct revision', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
2016-01-27 19:42:47 +00:00
|
|
|
change.rectify()
|
|
|
|
.then(function(ch) {
|
|
|
|
assert.equal(ch.rev, test.revisionForModel);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('change.currentRevision(callback)', function() {
|
|
|
|
it('should get the correct revision', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
|
|
|
const change = new Change({
|
2014-01-26 21:20:19 +00:00
|
|
|
modelName: this.modelName,
|
2016-04-01 09:14:26 +00:00
|
|
|
modelId: this.modelId,
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
change.currentRevision(function(err, rev) {
|
|
|
|
assert.equal(rev, test.revisionForModel);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-27 19:42:47 +00:00
|
|
|
describe('change.currentRevision - promise variant', function() {
|
|
|
|
it('should get the correct revision', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const test = this;
|
|
|
|
const change = new Change({
|
2016-01-27 19:42:47 +00:00
|
|
|
modelName: this.modelName,
|
2016-04-01 09:14:26 +00:00
|
|
|
modelId: this.modelId,
|
2016-01-27 19:42:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
change.currentRevision()
|
2017-12-12 08:33:15 +00:00
|
|
|
.then(function(rev) {
|
|
|
|
assert.equal(rev, test.revisionForModel);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
2016-01-27 19:42:47 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Change.hash(str)', function() {
|
2014-01-26 21:20:19 +00:00
|
|
|
// todo(ritch) test other hashing algorithms
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should hash the given string', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const str = 'foo';
|
|
|
|
const hash = Change.hash(str);
|
2014-01-26 21:20:19 +00:00
|
|
|
assert(hash !== str);
|
|
|
|
assert(typeof hash === 'string');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Change.revisionForInst(inst)', function() {
|
|
|
|
it('should return the same revision for the same data', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const a = {
|
2014-01-26 21:20:19 +00:00
|
|
|
b: {
|
|
|
|
b: ['c', 'd'],
|
2016-04-01 09:14:26 +00:00
|
|
|
c: ['d', 'e'],
|
|
|
|
},
|
2014-01-26 21:20:19 +00:00
|
|
|
};
|
2019-10-07 09:45:34 +00:00
|
|
|
const b = {
|
2014-01-26 21:20:19 +00:00
|
|
|
b: {
|
|
|
|
c: ['d', 'e'],
|
2016-04-01 09:14:26 +00:00
|
|
|
b: ['c', 'd'],
|
|
|
|
},
|
2014-01-26 21:20:19 +00:00
|
|
|
};
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const aRev = Change.revisionForInst(a);
|
|
|
|
const bRev = Change.revisionForInst(b);
|
2014-01-26 21:20:19 +00:00
|
|
|
assert.equal(aRev, bRev);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('change.type()', function() {
|
|
|
|
it('CREATE', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const change = new Change({
|
2016-04-01 09:14:26 +00:00
|
|
|
rev: this.revisionForModel,
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
assert.equal(Change.CREATE, change.type());
|
|
|
|
});
|
2014-11-21 02:35:36 +00:00
|
|
|
it('UPDATE', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const change = new Change({
|
2014-01-26 21:20:19 +00:00
|
|
|
rev: this.revisionForModel,
|
2016-04-01 09:14:26 +00:00
|
|
|
prev: this.revisionForModel,
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
assert.equal(Change.UPDATE, change.type());
|
|
|
|
});
|
2014-11-21 02:35:36 +00:00
|
|
|
it('DELETE', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const change = new Change({
|
2016-04-01 09:14:26 +00:00
|
|
|
prev: this.revisionForModel,
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
assert.equal(Change.DELETE, change.type());
|
|
|
|
});
|
2014-11-21 02:35:36 +00:00
|
|
|
it('UNKNOWN', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const change = new Change();
|
2014-01-26 21:20:19 +00:00
|
|
|
assert.equal(Change.UNKNOWN, change.type());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('change.getModelCtor()', function() {
|
|
|
|
it('should get the correct model class', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const change = new Change({
|
2016-04-01 09:14:26 +00:00
|
|
|
modelName: this.modelName,
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(change.getModelCtor(), TestModel);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('change.equals(otherChange)', function() {
|
|
|
|
it('should return true when the change is equal', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const change = new Change({
|
2016-04-01 09:14:26 +00:00
|
|
|
rev: this.revisionForModel,
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const otherChange = new Change({
|
2016-04-01 09:14:26 +00:00
|
|
|
rev: this.revisionForModel,
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(change.equals(otherChange), true);
|
|
|
|
});
|
2014-05-16 16:58:23 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should return true when both changes are deletes', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const REV = 'foo';
|
|
|
|
const change = new Change({
|
2014-05-16 16:58:23 +00:00
|
|
|
rev: null,
|
|
|
|
prev: REV,
|
|
|
|
});
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const otherChange = new Change({
|
2014-05-16 16:58:23 +00:00
|
|
|
rev: undefined,
|
2016-04-01 09:14:26 +00:00
|
|
|
prev: REV,
|
2014-05-16 16:58:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(change.type(), Change.DELETE);
|
|
|
|
assert.equal(otherChange.type(), Change.DELETE);
|
|
|
|
|
|
|
|
assert.equal(change.equals(otherChange), true);
|
|
|
|
});
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('change.isBasedOn(otherChange)', function() {
|
|
|
|
it('should return true when the change is based on the other', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const change = new Change({
|
2016-04-01 09:14:26 +00:00
|
|
|
prev: this.revisionForModel,
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const otherChange = new Change({
|
2016-04-01 09:14:26 +00:00
|
|
|
rev: this.revisionForModel,
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(change.isBasedOn(otherChange), true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Change.diff(modelName, since, remoteChanges, callback)', function() {
|
2014-01-26 21:20:19 +00:00
|
|
|
beforeEach(function(done) {
|
|
|
|
Change.create([
|
2016-11-15 21:46:23 +00:00
|
|
|
{rev: 'foo', modelName: this.modelName, modelId: 9, checkpoint: 1},
|
|
|
|
{rev: 'bar', modelName: this.modelName, modelId: 10, checkpoint: 1},
|
|
|
|
{rev: 'bat', modelName: this.modelName, modelId: 11, checkpoint: 1},
|
2014-01-26 21:20:19 +00:00
|
|
|
], done);
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should return delta and conflict lists', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const remoteChanges = [
|
2014-01-28 22:32:13 +00:00
|
|
|
// an update => should result in a delta
|
2016-11-15 21:46:23 +00:00
|
|
|
{rev: 'foo2', prev: 'foo', modelName: this.modelName, modelId: 9, checkpoint: 1},
|
2014-01-28 22:32:13 +00:00
|
|
|
// no change => should not result in a delta / conflict
|
2016-11-15 21:46:23 +00:00
|
|
|
{rev: 'bar', prev: 'bar', modelName: this.modelName, modelId: 10, checkpoint: 1},
|
2014-01-28 22:32:13 +00:00
|
|
|
// a conflict => should result in a conflict
|
2016-11-15 21:46:23 +00:00
|
|
|
{rev: 'bat2', prev: 'bat0', modelName: this.modelName, modelId: 11, checkpoint: 1},
|
2014-01-26 21:20:19 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
Change.diff(this.modelName, 0, remoteChanges, function(err, diff) {
|
2015-03-16 12:38:37 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
assert.equal(diff.deltas.length, 1);
|
|
|
|
assert.equal(diff.conflicts.length, 1);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-01-26 21:20:19 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-03-16 12:38:37 +00:00
|
|
|
|
2016-01-27 19:42:47 +00:00
|
|
|
it('should return delta and conflict lists - promise variant', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const remoteChanges = [
|
2016-01-27 19:42:47 +00:00
|
|
|
// an update => should result in a delta
|
2016-11-15 21:46:23 +00:00
|
|
|
{rev: 'foo2', prev: 'foo', modelName: this.modelName, modelId: 9, checkpoint: 1},
|
2016-01-27 19:42:47 +00:00
|
|
|
// no change => should not result in a delta / conflict
|
2016-11-15 21:46:23 +00:00
|
|
|
{rev: 'bar', prev: 'bar', modelName: this.modelName, modelId: 10, checkpoint: 1},
|
2016-01-27 19:42:47 +00:00
|
|
|
// a conflict => should result in a conflict
|
2016-11-15 21:46:23 +00:00
|
|
|
{rev: 'bat2', prev: 'bat0', modelName: this.modelName, modelId: 11, checkpoint: 1},
|
2016-01-27 19:42:47 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
Change.diff(this.modelName, 0, remoteChanges)
|
|
|
|
.then(function(diff) {
|
|
|
|
assert.equal(diff.deltas.length, 1);
|
|
|
|
assert.equal(diff.conflicts.length, 1);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-01-27 19:42:47 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done);
|
|
|
|
});
|
|
|
|
|
2015-03-16 12:38:37 +00:00
|
|
|
it('should set "prev" to local revision in non-conflicting delta', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const updateRecord = {
|
2015-03-16 12:38:37 +00:00
|
|
|
rev: 'foo-new',
|
|
|
|
prev: 'foo',
|
|
|
|
modelName: this.modelName,
|
|
|
|
modelId: '9',
|
2016-04-01 09:14:26 +00:00
|
|
|
checkpoint: 2,
|
2015-03-16 12:38:37 +00:00
|
|
|
};
|
|
|
|
Change.diff(this.modelName, 0, [updateRecord], function(err, diff) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-16 12:38:37 +00:00
|
|
|
expect(diff.conflicts, 'conflicts').to.have.length(0);
|
|
|
|
expect(diff.deltas, 'deltas').to.have.length(1);
|
2019-10-07 09:45:34 +00:00
|
|
|
const actual = diff.deltas[0].toObject();
|
2015-03-16 12:38:37 +00:00
|
|
|
delete actual.id;
|
|
|
|
expect(actual).to.eql({
|
|
|
|
checkpoint: 2,
|
|
|
|
modelId: '9',
|
|
|
|
modelName: updateRecord.modelName,
|
|
|
|
prev: 'foo', // this is the current local revision
|
|
|
|
rev: 'foo-new',
|
|
|
|
});
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-16 12:38:37 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set "prev" to local revision in remote-only delta', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const updateRecord = {
|
2015-03-16 12:38:37 +00:00
|
|
|
rev: 'foo-new',
|
|
|
|
prev: 'foo-prev',
|
|
|
|
modelName: this.modelName,
|
|
|
|
modelId: '9',
|
2016-04-01 09:14:26 +00:00
|
|
|
checkpoint: 2,
|
2015-03-16 12:38:37 +00:00
|
|
|
};
|
|
|
|
// IMPORTANT: the diff call excludes the local change
|
|
|
|
// with rev=foo CP=1
|
|
|
|
Change.diff(this.modelName, 2, [updateRecord], function(err, diff) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-16 12:38:37 +00:00
|
|
|
expect(diff.conflicts, 'conflicts').to.have.length(0);
|
|
|
|
expect(diff.deltas, 'deltas').to.have.length(1);
|
2019-10-07 09:45:34 +00:00
|
|
|
const actual = diff.deltas[0].toObject();
|
2015-03-16 12:38:37 +00:00
|
|
|
delete actual.id;
|
|
|
|
expect(actual).to.eql({
|
|
|
|
checkpoint: 2,
|
|
|
|
modelId: '9',
|
|
|
|
modelName: updateRecord.modelName,
|
|
|
|
prev: 'foo', // this is the current local revision
|
|
|
|
rev: 'foo-new',
|
|
|
|
});
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-16 12:38:37 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should set "prev" to null for a new instance', function(done) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const updateRecord = {
|
2015-03-16 12:38:37 +00:00
|
|
|
rev: 'new-rev',
|
|
|
|
prev: 'new-prev',
|
|
|
|
modelName: this.modelName,
|
|
|
|
modelId: 'new-id',
|
2016-04-01 09:14:26 +00:00
|
|
|
checkpoint: 2,
|
2015-03-16 12:38:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Change.diff(this.modelName, 0, [updateRecord], function(err, diff) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-16 12:38:37 +00:00
|
|
|
expect(diff.conflicts).to.have.length(0);
|
|
|
|
expect(diff.deltas).to.have.length(1);
|
2019-10-07 09:45:34 +00:00
|
|
|
const actual = diff.deltas[0].toObject();
|
2015-03-16 12:38:37 +00:00
|
|
|
delete actual.id;
|
|
|
|
expect(actual).to.eql({
|
|
|
|
checkpoint: 2,
|
|
|
|
modelId: 'new-id',
|
|
|
|
modelName: updateRecord.modelName,
|
|
|
|
prev: null, // this is the current local revision
|
|
|
|
rev: 'new-rev',
|
|
|
|
});
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-16 12:38:37 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
});
|
2016-11-21 08:13:16 +00:00
|
|
|
|
|
|
|
describe('Change with with custom properties', function() {
|
|
|
|
let Change, TestModel;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const memory = loopback.createDataSource({
|
2016-11-21 08:13:16 +00:00
|
|
|
connector: loopback.Memory,
|
|
|
|
});
|
|
|
|
|
|
|
|
TestModel = loopback.PersistedModel.extend('ChangeTestModelWithTenant',
|
|
|
|
{
|
|
|
|
id: {id: true, type: 'string', defaultFn: 'guid'},
|
|
|
|
tenantId: 'string',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
trackChanges: true,
|
|
|
|
additionalChangeModelProperties: {tenantId: 'string'},
|
|
|
|
});
|
|
|
|
this.modelName = TestModel.modelName;
|
|
|
|
|
|
|
|
TestModel.prototype.fillCustomChangeProperties = function(change, cb) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const inst = this;
|
2016-11-21 08:13:16 +00:00
|
|
|
|
|
|
|
if (inst && inst.tenantId) {
|
|
|
|
change.tenantId = inst.tenantId;
|
|
|
|
} else {
|
|
|
|
change.tenantId = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb();
|
|
|
|
};
|
|
|
|
|
|
|
|
TestModel.attachTo(memory);
|
|
|
|
TestModel._defineChangeModel();
|
|
|
|
Change = TestModel.getChangeModel();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('change.rectify', function() {
|
|
|
|
const TENANT_ID = '123';
|
|
|
|
let change;
|
|
|
|
|
|
|
|
beforeEach(givenChangeInstance);
|
|
|
|
|
|
|
|
it('stores the custom property in the Change instance', function() {
|
|
|
|
return change.rectify().then(function(ch) {
|
|
|
|
expect(ch.toObject()).to.have.property('tenantId', TENANT_ID);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function givenChangeInstance() {
|
|
|
|
const data = {
|
|
|
|
foo: 'bar',
|
|
|
|
tenantId: TENANT_ID,
|
|
|
|
};
|
|
|
|
|
|
|
|
return TestModel.create(data)
|
|
|
|
.then(function(model) {
|
|
|
|
const modelName = TestModel.modelName;
|
|
|
|
return Change.findOrCreateChange(modelName, model.id);
|
|
|
|
}).then(function(ch) {
|
|
|
|
change = ch;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|