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';
|
|
|
|
var assert = require('assert');
|
2014-02-21 23:03:45 +00:00
|
|
|
var loopback = require('../');
|
2014-05-03 01:12:24 +00:00
|
|
|
var defineModelTestsWithDataSource = require('./util/model-tests');
|
2014-02-21 23:03:45 +00:00
|
|
|
|
|
|
|
describe('RemoteConnector', function() {
|
2016-08-08 13:12:56 +00:00
|
|
|
this.timeout(10000);
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
var remoteApp, remote;
|
2014-02-21 23:03:45 +00:00
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
defineModelTestsWithDataSource({
|
|
|
|
beforeEach: function(done) {
|
|
|
|
var test = this;
|
|
|
|
remoteApp = loopback();
|
2016-08-31 13:25:08 +00:00
|
|
|
remoteApp.set('remoting', {
|
2016-11-15 21:46:23 +00:00
|
|
|
errorHandler: {debug: true, log: false},
|
|
|
|
types: {warnWhenOverridingType: false},
|
2016-08-31 13:25:08 +00:00
|
|
|
});
|
2014-05-03 01:12:24 +00:00
|
|
|
remoteApp.use(loopback.rest());
|
|
|
|
remoteApp.listen(0, function() {
|
|
|
|
test.dataSource = loopback.createDataSource({
|
2014-10-17 07:58:02 +00:00
|
|
|
host: 'localhost',
|
2014-05-03 01:12:24 +00:00
|
|
|
port: remoteApp.get('port'),
|
2016-04-01 09:14:26 +00:00
|
|
|
connector: loopback.Remote,
|
2014-05-03 01:12:24 +00:00
|
|
|
});
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
},
|
2016-07-29 14:49:29 +00:00
|
|
|
|
|
|
|
// We are defining the model attached to the remote connector datasource,
|
|
|
|
// therefore change tracking must be disabled, only the remote API for
|
|
|
|
// replication should be present
|
|
|
|
trackChanges: false,
|
|
|
|
enableRemoteReplication: true,
|
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
onDefine: function(Model) {
|
2016-07-29 14:49:29 +00:00
|
|
|
var ServerModel = Model.extend('Server' + Model.modelName, {}, {
|
|
|
|
plural: Model.pluralModelName,
|
|
|
|
// This is the model running on the server & attached to a real
|
|
|
|
// datasource, that's the place where to keep track of changes
|
|
|
|
trackChanges: true,
|
|
|
|
});
|
|
|
|
ServerModel.attachTo(loopback.createDataSource({
|
2016-04-01 09:14:26 +00:00
|
|
|
connector: loopback.Memory,
|
2014-05-03 03:04:06 +00:00
|
|
|
}));
|
2016-07-29 14:49:29 +00:00
|
|
|
remoteApp.model(ServerModel);
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2014-04-14 21:17:56 +00:00
|
|
|
});
|
2014-05-06 20:31:23 +00:00
|
|
|
|
|
|
|
beforeEach(function(done) {
|
|
|
|
var test = this;
|
|
|
|
remoteApp = this.remoteApp = loopback();
|
2016-08-31 13:25:08 +00:00
|
|
|
remoteApp.set('remoting', {
|
2016-11-15 21:46:23 +00:00
|
|
|
types: {warnWhenOverridingType: false},
|
2016-08-31 13:25:08 +00:00
|
|
|
});
|
2014-05-06 20:31:23 +00:00
|
|
|
remoteApp.use(loopback.rest());
|
2014-06-05 07:45:09 +00:00
|
|
|
var ServerModel = this.ServerModel = loopback.PersistedModel.extend('TestModel');
|
2014-05-06 20:31:23 +00:00
|
|
|
|
|
|
|
remoteApp.model(ServerModel);
|
|
|
|
|
|
|
|
remoteApp.listen(0, function() {
|
|
|
|
test.remote = loopback.createDataSource({
|
2014-10-17 07:58:02 +00:00
|
|
|
host: 'localhost',
|
2014-05-06 20:31:23 +00:00
|
|
|
port: remoteApp.get('port'),
|
2016-04-01 09:14:26 +00:00
|
|
|
connector: loopback.Remote,
|
2014-05-06 20:31:23 +00:00
|
|
|
});
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-06 20:31:23 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should support the save method', function(done) {
|
2014-05-06 20:31:23 +00:00
|
|
|
var calledServerCreate = false;
|
2014-06-05 07:45:09 +00:00
|
|
|
var RemoteModel = loopback.PersistedModel.extend('TestModel');
|
2014-05-06 20:31:23 +00:00
|
|
|
RemoteModel.attachTo(this.remote);
|
|
|
|
|
|
|
|
var ServerModel = this.ServerModel;
|
|
|
|
|
2016-09-20 08:54:41 +00:00
|
|
|
ServerModel.create = function(data, options, cb) {
|
2014-05-06 20:31:23 +00:00
|
|
|
calledServerCreate = true;
|
|
|
|
data.id = 1;
|
|
|
|
cb(null, data);
|
2014-11-21 01:46:21 +00:00
|
|
|
};
|
2014-05-06 20:31:23 +00:00
|
|
|
|
|
|
|
ServerModel.setupRemoting();
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
var m = new RemoteModel({foo: 'bar'});
|
2014-05-06 20:31:23 +00:00
|
|
|
m.save(function(err, inst) {
|
2014-10-17 07:58:02 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-06 20:31:23 +00:00
|
|
|
assert(inst instanceof RemoteModel);
|
|
|
|
assert(calledServerCreate);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-05-06 20:31:23 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-02-21 23:03:45 +00:00
|
|
|
});
|