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
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
describe('Registry', function() {
|
2016-05-05 22:20:36 +00:00
|
|
|
describe('createModel', function() {
|
|
|
|
it('should throw error upon extending non-exist base model', function() {
|
|
|
|
var app = loopback();
|
|
|
|
var props = {};
|
|
|
|
var opts = { base: 'nonexistent' };
|
|
|
|
expect(function() { app.registry.createModel('aModel', props, opts); })
|
|
|
|
.to.throw(/model\s`aModel`(.*)unknown\smodel\s`nonexistent`/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
describe('one per app', function() {
|
|
|
|
it('should allow two apps to reuse the same model name', function(done) {
|
|
|
|
var appFoo = loopback();
|
|
|
|
var appBar = loopback();
|
|
|
|
var modelName = 'MyModel';
|
|
|
|
var subModelName = 'Sub' + modelName;
|
2016-04-01 09:14:26 +00:00
|
|
|
var settings = { base: 'PersistedModel' };
|
2015-04-01 21:50:36 +00:00
|
|
|
appFoo.set('perAppRegistries', true);
|
|
|
|
appBar.set('perAppRegistries', true);
|
2016-04-01 09:14:26 +00:00
|
|
|
var dsFoo = appFoo.dataSource('dsFoo', { connector: 'memory' });
|
|
|
|
var dsBar = appFoo.dataSource('dsBar', { connector: 'memory' });
|
2015-04-01 21:50:36 +00:00
|
|
|
|
|
|
|
var FooModel = appFoo.model(modelName, settings);
|
|
|
|
var FooSubModel = appFoo.model(subModelName, settings);
|
|
|
|
var BarModel = appBar.model(modelName, settings);
|
|
|
|
var BarSubModel = appBar.model(subModelName, settings);
|
|
|
|
|
|
|
|
FooModel.attachTo(dsFoo);
|
|
|
|
FooSubModel.attachTo(dsFoo);
|
|
|
|
BarModel.attachTo(dsBar);
|
|
|
|
BarSubModel.attachTo(dsBar);
|
|
|
|
|
|
|
|
FooModel.hasMany(FooSubModel);
|
|
|
|
BarModel.hasMany(BarSubModel);
|
|
|
|
|
|
|
|
expect(appFoo.models[modelName]).to.not.equal(appBar.models[modelName]);
|
|
|
|
|
2016-04-01 09:14:26 +00:00
|
|
|
BarModel.create({ name: 'bar' }, function(err, bar) {
|
2015-04-01 21:50:36 +00:00
|
|
|
assert(!err);
|
2016-04-01 09:14:26 +00:00
|
|
|
bar.subMyModels.create({ parent: 'bar' }, function(err) {
|
2015-04-01 21:50:36 +00:00
|
|
|
assert(!err);
|
|
|
|
FooSubModel.find(function(err, foos) {
|
|
|
|
assert(!err);
|
|
|
|
expect(foos).to.eql([]);
|
|
|
|
BarSubModel.find(function(err, bars) {
|
|
|
|
assert(!err);
|
|
|
|
expect(bars.map(function(f) {
|
|
|
|
return f.parent;
|
|
|
|
})).to.eql(['bar']);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-04-01 21:50:36 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|