Missing the option argument (#1426)

* Fix missing option arguments in scope.js

Added option arguments in find.relatedmodel
Added case test

* Add order filter in verify function

Fix for cloudant test
This commit is contained in:
dmellonch 2017-08-01 20:15:21 +02:00 committed by Sakib Hasan
parent 25ead1ce97
commit 1b7c346bca
2 changed files with 57 additions and 9 deletions

View File

@ -85,9 +85,10 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres
} }
} }
cb = cb || utils.createPromiseCallback(); cb = cb || utils.createPromiseCallback();
const refreshIsNeeded = !self.__cachedRelations ||
if (!self.__cachedRelations || self.__cachedRelations[name] === undefined || self.__cachedRelations[name] === undefined ||
actualRefresh) { actualRefresh;
if (refreshIsNeeded) {
// It either doesn't hit the cache or refresh is required // It either doesn't hit the cache or refresh is required
var params = mergeQuery(actualCond, scopeParams, {nestedInclude: true}); var params = mergeQuery(actualCond, scopeParams, {nestedInclude: true});
var targetModel = this.targetModel(receiver); var targetModel = this.targetModel(receiver);
@ -144,7 +145,7 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres
queryRelated.where[IdKey] = collectTargetIds(data, IdKey); queryRelated.where[IdKey] = collectTargetIds(data, IdKey);
} }
relatedModel.find(queryRelated, cb); relatedModel.find(queryRelated, options, cb);
} else { } else {
cb(err, data); cb(err, data);
} }
@ -238,8 +239,10 @@ function defineScope(cls, targetClass, name, params, methods, options) {
return self.__cachedRelations[name]; return self.__cachedRelations[name];
} }
} else { } else {
if (typeof condOrRefresh === 'function' && const condOrRefreshIsCallBack = typeof condOrRefresh === 'function' &&
options === undefined && cb === undefined) { options === undefined &&
cb === undefined;
if (condOrRefreshIsCallBack) {
// customer.orders(cb) // customer.orders(cb)
cb = condOrRefresh; cb = condOrRefresh;
options = {}; options = {};

View File

@ -572,7 +572,7 @@ describe('relations', function() {
before(function(done) { before(function(done) {
Physician = db.define('Physician', {name: String}); Physician = db.define('Physician', {name: String});
Patient = db.define('Patient', {name: String, age: Number, Patient = db.define('Patient', {name: String, age: Number, realm: String,
sequence: {type: Number, index: true}}); sequence: {type: Number, index: true}});
Appointment = db.define('Appointment', {date: {type: Date, Appointment = db.define('Appointment', {date: {type: Date,
default: function() { default: function() {
@ -918,6 +918,51 @@ describe('relations', function() {
}; };
}); });
describe('find over related model with options', function() {
after(function() {
Physician.clearObservers('access');
Patient.clearObservers('access');
});
before(function() {
Physician.observe('access', beforeAccessFn);
Patient.observe('access', beforeAccessFn);
function beforeAccessFn(ctx, next) {
ctx.query.where.realm = ctx.options.realm;
next();
}
});
it('should find be filtered from option', function(done) {
var id;
Physician.create(function(err, physician) {
if (err) return done(err);
physician.patients.create({name: 'a', realm: 'test'}, function(err, ch) {
if (err) return done(err);
id = ch.id;
physician.patients.create({name: 'z', realm: 'test'}, function(err) {
if (err) return done(err);
physician.patients.create({name: 'c', realm: 'anotherRealm'}, function(err) {
if (err) return done(err);
verify(physician);
});
});
});
});
function verify(physician) {
physician.patients({order: 'name ASC'}, {realm: 'test'}, function(err, records) {
if (err) return done(err);
should.exist(records);
records.length.should.eql(2);
const expected = ['a:test', 'z:test'];
const actual = records.map(function(r) { return r.name + ':' + r.realm; });
actual.should.eql(expected);
done();
});
}
});
});
it('should find scoped record', function(done) { it('should find scoped record', function(done) {
var id; var id;
Physician.create(function(err, physician) { Physician.create(function(err, physician) {