Fixed remote models resolve duplication

This commit is contained in:
maxim.sharai 2017-12-16 15:06:31 +03:00
parent 906979a960
commit c642bbb24c
3 changed files with 149 additions and 14 deletions

View File

@ -75,8 +75,7 @@ function RelationMixin() {
* @property {Object} model Model object
*/
RelationMixin.hasMany = function hasMany(modelTo, params) {
var def = RelationDefinition.hasMany(this, modelTo, params);
this.dataSource.adapter.resolve(this);
let def = RelationDefinition.hasMany(this, modelTo, params);
defineRelationProperty(this, def);
};
@ -139,8 +138,7 @@ RelationMixin.hasMany = function hasMany(modelTo, params) {
*
*/
RelationMixin.belongsTo = function(modelTo, params) {
var def = RelationDefinition.belongsTo(this, modelTo, params);
this.dataSource.adapter.resolve(this);
let def = RelationDefinition.belongsTo(this, modelTo, params);
defineRelationProperty(this, def);
};
@ -179,32 +177,27 @@ RelationMixin.belongsTo = function(modelTo, params) {
*/
RelationMixin.hasAndBelongsToMany =
function hasAndBelongsToMany(modelTo, params) {
var def = RelationDefinition.hasAndBelongsToMany(this, modelTo, params);
this.dataSource.adapter.resolve(this);
let def = RelationDefinition.hasAndBelongsToMany(this, modelTo, params);
defineRelationProperty(this, def);
};
RelationMixin.hasOne = function hasOne(modelTo, params) {
var def = RelationDefinition.hasOne(this, modelTo, params);
this.dataSource.adapter.resolve(this);
let def = RelationDefinition.hasOne(this, modelTo, params);
defineRelationProperty(this, def);
};
RelationMixin.referencesMany = function referencesMany(modelTo, params) {
var def = RelationDefinition.referencesMany(this, modelTo, params);
this.dataSource.adapter.resolve(this);
let def = RelationDefinition.referencesMany(this, modelTo, params);
defineRelationProperty(this, def);
};
RelationMixin.embedsOne = function embedsOne(modelTo, params) {
var def = RelationDefinition.embedsOne(this, modelTo, params);
this.dataSource.adapter.resolve(this);
let def = RelationDefinition.embedsOne(this, modelTo, params);
defineRelationProperty(this, def);
};
RelationMixin.embedsMany = function embedsMany(modelTo, params) {
var def = RelationDefinition.embedsMany(this, modelTo, params);
this.dataSource.adapter.resolve(this);
let def = RelationDefinition.embedsMany(this, modelTo, params);
defineRelationProperty(this, def);
};

View File

@ -44,6 +44,7 @@
"grunt-mocha-test": "^0.12.7",
"loopback": "^3.0.0",
"mocha": "^3.0.2",
"sinon": "^4.1.3",
"strong-task-emitter": "^0.0.7"
},
"optionalDependencies": {}

141
test/models-resolve.test.js Normal file
View File

@ -0,0 +1,141 @@
// Copyright IBM Corp. 2016. All Rights Reserved.
// Node module: loopback-connector-remote
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
const assert = require('assert');
const helper = require('./helper');
const loopback = require('loopback');
const sinon = require('sinon');
const RemoteConnector = require('../lib/remote-connector');
describe('Model Resolve Tests', function() {
let serverApp, clientApp, remoteDs, resolveSpy, ChildModel;
before('spy remote connector resolve() function', () => {
resolveSpy = sinon.spy(RemoteConnector.prototype, 'resolve');
});
beforeEach('reset the resolve()', () => {
resolveSpy.reset();
});
after('restore remote connector resolve() function', () => {
resolveSpy.restore();
});
before('create remote datasource', () => {
serverApp = helper.createRestAppAndListen();
clientApp = loopback({localRegistry: true});
remoteDs = helper.createRemoteDataSource(clientApp, serverApp);
});
before('create a child model', () => {
const db = helper.createMemoryDataSource(clientApp);
ChildModel = clientApp.registry.createModel({
name: 'ChildModel',
});
clientApp.model(ChildModel, {dataSource: db});
});
it('should resolve a remote model only once (hasMany)', () => {
const RemoteModel = clientApp.registry.createModel({
name: 'RemoteModelHasMany',
relations: {
children: {
type: 'hasMany',
model: 'ChildModel',
},
},
});
clientApp.model(RemoteModel, {dataSource: remoteDs});
assert(resolveSpy.withArgs(RemoteModel).calledOnce);
});
it('should resolve a remote model only once (belongsTo)', () => {
const RemoteModel = clientApp.registry.createModel({
name: 'RemoteModelBelongsTo',
relations: {
children: {
type: 'belongsTo',
model: 'ChildModel',
},
},
});
clientApp.model(RemoteModel, {dataSource: remoteDs});
assert(resolveSpy.withArgs(RemoteModel).calledOnce);
});
it('should resolve a remote model only once (hasAndBelongsToMany)', () => {
const RemoteModel = clientApp.registry.createModel({
name: 'RemoteModelHasAndBelongsToMany',
relations: {
children: {
type: 'hasAndBelongsToMany',
model: 'ChildModel',
},
},
});
clientApp.model(RemoteModel, {dataSource: remoteDs});
assert(resolveSpy.withArgs(RemoteModel).calledOnce);
});
it('should resolve a remote model only once (hasOne)', () => {
const RemoteModel = clientApp.registry.createModel({
name: 'RemoteModelHasOne',
relations: {
children: {
type: 'hasOne',
model: 'ChildModel',
},
},
});
clientApp.model(RemoteModel, {dataSource: remoteDs});
assert(resolveSpy.withArgs(RemoteModel).calledOnce);
});
it('should resolve a remote model only once (referencesMany)', () => {
const RemoteModel = clientApp.registry.createModel({
name: 'RemoteModelReferencesMany',
relations: {
children: {
type: 'referencesMany',
model: 'ChildModel',
},
},
});
clientApp.model(RemoteModel, {dataSource: remoteDs});
assert(resolveSpy.withArgs(RemoteModel).calledOnce);
});
it('should resolve a remote model only once (embedsOne)', () => {
const RemoteModel = clientApp.registry.createModel({
name: 'RemoteModelEmbedsOne',
relations: {
children: {
type: 'embedsOne',
model: 'ChildModel',
},
},
});
clientApp.model(RemoteModel, {dataSource: remoteDs});
assert(resolveSpy.withArgs(RemoteModel).calledOnce);
});
it('should resolve a remote model only once (embedsMany)', () => {
const RemoteModel = clientApp.registry.createModel({
name: 'RemoteModelEmbedsMany',
relations: {
children: {
type: 'embedsMany',
model: 'ChildModel',
},
},
});
clientApp.model(RemoteModel, {dataSource: remoteDs});
assert(resolveSpy.withArgs(RemoteModel).calledOnce);
});
});