From c642bbb24cd44b2dd0df91f2fd054f50dd1deca6 Mon Sep 17 00:00:00 2001 From: "maxim.sharai" Date: Sat, 16 Dec 2017 15:06:31 +0300 Subject: [PATCH] Fixed remote models resolve duplication --- lib/relations.js | 21 ++---- package.json | 1 + test/models-resolve.test.js | 141 ++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 14 deletions(-) create mode 100644 test/models-resolve.test.js diff --git a/lib/relations.js b/lib/relations.js index 5827a43..6a07736 100644 --- a/lib/relations.js +++ b/lib/relations.js @@ -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); }; diff --git a/package.json b/package.json index d11a83c..44c7dc5 100644 --- a/package.json +++ b/package.json @@ -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": {} diff --git a/test/models-resolve.test.js b/test/models-resolve.test.js new file mode 100644 index 0000000..c1a76d6 --- /dev/null +++ b/test/models-resolve.test.js @@ -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); + }); +});