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() {
|
2014-05-03 01:12:24 +00:00
|
|
|
var remoteApp;
|
|
|
|
var 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'),
|
|
|
|
connector: loopback.Remote
|
|
|
|
});
|
|
|
|
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({
|
|
|
|
connector: loopback.Memory
|
|
|
|
}));
|
2014-05-03 01:12:24 +00:00
|
|
|
remoteApp.model(RemoteModel);
|
|
|
|
}
|
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'),
|
|
|
|
connector: loopback.Remote
|
|
|
|
});
|
|
|
|
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();
|
|
|
|
|
|
|
|
var m = new RemoteModel({foo: 'bar'});
|
|
|
|
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
|
|
|
});
|