From 5b6bffb5b360600ee5f831969a5fa7d311b7f540 Mon Sep 17 00:00:00 2001 From: Amir Jafarian Date: Fri, 4 Mar 2016 15:45:40 -0500 Subject: [PATCH] Fix Mongo compatibility issue *Fix Mongo compatibility issue for replaceOrCreate --- test/memory.test.js | 4 +- test/persistence-hooks.suite.js | 66 +++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/test/memory.test.js b/test/memory.test.js index 8cd8fa3a..4bc53e56 100644 --- a/test/memory.test.js +++ b/test/memory.test.js @@ -853,7 +853,7 @@ describe('Optimized connector', function() { }.bind(this)); }; - require('./persistence-hooks.suite')(ds, should); + require('./persistence-hooks.suite')(ds, should, {replaceOrCreateReportsNewInstance: true}); }); describe('Unoptimized connector', function() { @@ -862,7 +862,7 @@ describe('Unoptimized connector', function() { ds.connector.updateOrCreate = 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() { diff --git a/test/persistence-hooks.suite.js b/test/persistence-hooks.suite.js index 8987bec0..4a7744e8 100644 --- a/test/persistence-hooks.suite.js +++ b/test/persistence-hooks.suite.js @@ -1,7 +1,11 @@ var ValidationError = require('../').ValidationError; 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() { var observedContexts, expectedError, observersCalled; var TestModel, existingInstance; @@ -2403,13 +2407,19 @@ module.exports = function(dataSource, should) { { id: 'new-id', name: 'a name' }, function(err, instance) { if (err) return done(err); - observedContexts.should.eql(aTestModelCtx({ + + var expected = { data: { id: 'new-id', name: 'a name' - }, - isNewInstance: true - })); + } + }; + + expected.isNewInstance = + connectorCapabilities.replaceOrCreateReportsNewInstance ? + true : undefined; + + observedContexts.should.eql(aTestModelCtx(expected)); done(); }); }); @@ -2422,14 +2432,19 @@ module.exports = function(dataSource, should) { function(err, instance) { if (err) return done(err); + var expected = { + data: { + id: existingInstance.id, + name: 'replaced name' + } + }; + + expected.isNewInstance = + connectorCapabilities.replaceOrCreateReportsNewInstance ? + false : undefined; + if (dataSource.connector.replaceOrCreate) { - observedContexts.should.eql(aTestModelCtx({ - data: { - id: existingInstance.id, - name: 'replaced name' - }, - isNewInstance: false - })); + observedContexts.should.eql(aTestModelCtx(expected)); } else { // TODO: Please see loopback-datasource-juggler/issues#836 // @@ -2479,14 +2494,20 @@ module.exports = function(dataSource, should) { { id: existingInstance.id, name: 'replaced name' }, function(err, instance) { if (err) return done(err); - observedContexts.should.eql(aTestModelCtx({ + + var expected = { instance: { id: existingInstance.id, name: 'replaced name', extra: undefined - }, - isNewInstance: false - })); + } + }; + + expected.isNewInstance = + connectorCapabilities.replaceOrCreateReportsNewInstance ? + false : undefined; + + observedContexts.should.eql(aTestModelCtx(expected)); done(); }); }); @@ -2498,14 +2519,19 @@ module.exports = function(dataSource, should) { { id: 'new-id', name: 'a name' }, function(err, instance) { if (err) return done(err); - observedContexts.should.eql(aTestModelCtx({ + + var expected = { instance: { id: instance.id, name: 'a name', extra: undefined - }, - isNewInstance: true - })); + } + }; + expected.isNewInstance = + connectorCapabilities.replaceOrCreateReportsNewInstance ? + true : undefined; + + observedContexts.should.eql(aTestModelCtx(expected)); done(); }); });