Merge pull request #866 from strongloop/fix_Mongo_comptaibility_replaceORCreate

Fix Mongo compatibility issue
This commit is contained in:
Amir-61 2016-03-30 11:25:22 -04:00
commit 049e888039
2 changed files with 48 additions and 22 deletions

View File

@ -853,7 +853,7 @@ describe('Optimized connector', function() {
}.bind(this)); }.bind(this));
}; };
require('./persistence-hooks.suite')(ds, should); require('./persistence-hooks.suite')(ds, should, {replaceOrCreateReportsNewInstance: true});
}); });
describe('Unoptimized connector', function() { describe('Unoptimized connector', function() {
@ -862,7 +862,7 @@ describe('Unoptimized connector', function() {
ds.connector.updateOrCreate = false; ds.connector.updateOrCreate = false;
ds.connector.findOrCreate = false; ds.connector.findOrCreate = false;
require('./persistence-hooks.suite')(ds, should); require('./persistence-hooks.suite')(ds, should, {replaceOrCreateReportsNewInstance: true});
}); });
describe('Memory connector with options', function() { describe('Memory connector with options', function() {

View File

@ -1,7 +1,11 @@
var ValidationError = require('../').ValidationError; var ValidationError = require('../').ValidationError;
var traverse = require('traverse'); var traverse = require('traverse');
module.exports = function(dataSource, should) { module.exports = function(dataSource, should, connectorCapabilities) {
if (!connectorCapabilities) connectorCapabilities = {};
if (connectorCapabilities.replaceOrCreateReportsNewInstance === undefined) {
console.warn('The connector does not support a recently added feature: replaceOrCreateReportsNewInstance');
}
describe('Persistence hooks', function() { describe('Persistence hooks', function() {
var observedContexts, expectedError, observersCalled; var observedContexts, expectedError, observersCalled;
var TestModel, existingInstance; var TestModel, existingInstance;
@ -2403,13 +2407,19 @@ module.exports = function(dataSource, should) {
{ id: 'new-id', name: 'a name' }, { id: 'new-id', name: 'a name' },
function(err, instance) { function(err, instance) {
if (err) return done(err); if (err) return done(err);
observedContexts.should.eql(aTestModelCtx({
var expected = {
data: { data: {
id: 'new-id', id: 'new-id',
name: 'a name' name: 'a name'
}, }
isNewInstance: true };
}));
expected.isNewInstance =
connectorCapabilities.replaceOrCreateReportsNewInstance ?
true : undefined;
observedContexts.should.eql(aTestModelCtx(expected));
done(); done();
}); });
}); });
@ -2422,14 +2432,19 @@ module.exports = function(dataSource, should) {
function(err, instance) { function(err, instance) {
if (err) return done(err); if (err) return done(err);
var expected = {
data: {
id: existingInstance.id,
name: 'replaced name'
}
};
expected.isNewInstance =
connectorCapabilities.replaceOrCreateReportsNewInstance ?
false : undefined;
if (dataSource.connector.replaceOrCreate) { if (dataSource.connector.replaceOrCreate) {
observedContexts.should.eql(aTestModelCtx({ observedContexts.should.eql(aTestModelCtx(expected));
data: {
id: existingInstance.id,
name: 'replaced name'
},
isNewInstance: false
}));
} else { } else {
// TODO: Please see loopback-datasource-juggler/issues#836 // TODO: Please see loopback-datasource-juggler/issues#836
// //
@ -2479,14 +2494,20 @@ module.exports = function(dataSource, should) {
{ id: existingInstance.id, name: 'replaced name' }, { id: existingInstance.id, name: 'replaced name' },
function(err, instance) { function(err, instance) {
if (err) return done(err); if (err) return done(err);
observedContexts.should.eql(aTestModelCtx({
var expected = {
instance: { instance: {
id: existingInstance.id, id: existingInstance.id,
name: 'replaced name', name: 'replaced name',
extra: undefined extra: undefined
}, }
isNewInstance: false };
}));
expected.isNewInstance =
connectorCapabilities.replaceOrCreateReportsNewInstance ?
false : undefined;
observedContexts.should.eql(aTestModelCtx(expected));
done(); done();
}); });
}); });
@ -2498,14 +2519,19 @@ module.exports = function(dataSource, should) {
{ id: 'new-id', name: 'a name' }, { id: 'new-id', name: 'a name' },
function(err, instance) { function(err, instance) {
if (err) return done(err); if (err) return done(err);
observedContexts.should.eql(aTestModelCtx({
var expected = {
instance: { instance: {
id: instance.id, id: instance.id,
name: 'a name', name: 'a name',
extra: undefined extra: undefined
}, }
isNewInstance: true };
})); expected.isNewInstance =
connectorCapabilities.replaceOrCreateReportsNewInstance ?
true : undefined;
observedContexts.should.eql(aTestModelCtx(expected));
done(); done();
}); });
}); });