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-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();
|
|
|
|
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
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onDefine: function(Model) {
|
2015-02-26 15:36:44 +00:00
|
|
|
var RemoteModel = Model.extend('Remote' + Model.modelName, {},
|
|
|
|
{ plural: Model.pluralModelName });
|
2014-05-03 03:04:06 +00:00
|
|
|
RemoteModel.attachTo(loopback.createDataSource({
|
2016-04-01 09:14:26 +00:00
|
|
|
connector: loopback.Memory,
|
2014-05-03 03:04:06 +00:00
|
|
|
}));
|
2014-05-03 01:12:24 +00:00
|
|
|
remoteApp.model(RemoteModel);
|
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();
|
|
|
|
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
|
|
|
});
|
|
|
|
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;
|
|
|
|
|
|
|
|
ServerModel.create = function(data, cb) {
|
|
|
|
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-04-01 09:14:26 +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);
|
2014-05-06 20:31:23 +00:00
|
|
|
assert(inst instanceof RemoteModel);
|
|
|
|
assert(calledServerCreate);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-02-21 23:03:45 +00:00
|
|
|
});
|