diff --git a/.eslintignore b/.eslintignore index 4ebc8aea5..336bfec18 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1,2 @@ coverage +**/node_modules/ diff --git a/deps/juggler-v3/package.json b/deps/juggler-v3/package.json new file mode 100644 index 000000000..b8b11477a --- /dev/null +++ b/deps/juggler-v3/package.json @@ -0,0 +1,8 @@ +{ + "name": "juggler-v3", + "version": "3.0.0", + "dependencies": { + "loopback-datasource-juggler":"3.x", + "should": "^8.4.0" + } +} diff --git a/deps/juggler-v3/test.js b/deps/juggler-v3/test.js new file mode 100644 index 000000000..f810b4f46 --- /dev/null +++ b/deps/juggler-v3/test.js @@ -0,0 +1,49 @@ +// Copyright IBM Corp. 2019. All Rights Reserved. +// Node module: loopback-connector-postgresql +// This file is licensed under the Artistic License 2.0. +// License text available at https://opensource.org/licenses/Artistic-2.0 + +'use strict'; + +const semver = require('semver'); +const should = require('should'); +const juggler = require('loopback-datasource-juggler'); +const name = require('./package.json').name; + +require('../../test/init'); + +describe(name, function() { + before(function() { + return global.resetDataSourceClass(juggler.DataSource); + }); + + after(function() { + return global.resetDataSourceClass(); + }); + + require('loopback-datasource-juggler/test/common.batch.js'); + require('loopback-datasource-juggler/test/default-scope.test.js'); + require('loopback-datasource-juggler/test/include.test.js'); + + // === Operation hooks ==== // + + const suite = require('loopback-datasource-juggler/test/persistence-hooks.suite.js'); + + const DB_VERSION = process.env.MONGODB_VERSION; + + if (!DB_VERSION) { + console.log('The ENV variable MONGODB_VERSION is not set.' + + ' Assuming MongoDB version 2.6 or newer.'); + } + + const DB_HAS_2_6_FEATURES = (!DB_VERSION || + semver.satisfies(DB_VERSION, '>=2.6.0')); + + const customConfig = Object.assign({}, global.config, { + enableOptimisedFindOrCreate: DB_HAS_2_6_FEATURES, + }); + + suite(global.getDataSource(customConfig, juggler.DataSource), should, { + replaceOrCreateReportsNewInstance: DB_HAS_2_6_FEATURES, + }); +}); diff --git a/deps/juggler-v4/package.json b/deps/juggler-v4/package.json new file mode 100644 index 000000000..e73724e5c --- /dev/null +++ b/deps/juggler-v4/package.json @@ -0,0 +1,8 @@ +{ + "name": "juggler-v4", + "version": "4.0.0", + "dependencies": { + "loopback-datasource-juggler":"4.x", + "should": "^13.2.3" + } +} diff --git a/deps/juggler-v4/test.js b/deps/juggler-v4/test.js new file mode 100644 index 000000000..f810b4f46 --- /dev/null +++ b/deps/juggler-v4/test.js @@ -0,0 +1,49 @@ +// Copyright IBM Corp. 2019. All Rights Reserved. +// Node module: loopback-connector-postgresql +// This file is licensed under the Artistic License 2.0. +// License text available at https://opensource.org/licenses/Artistic-2.0 + +'use strict'; + +const semver = require('semver'); +const should = require('should'); +const juggler = require('loopback-datasource-juggler'); +const name = require('./package.json').name; + +require('../../test/init'); + +describe(name, function() { + before(function() { + return global.resetDataSourceClass(juggler.DataSource); + }); + + after(function() { + return global.resetDataSourceClass(); + }); + + require('loopback-datasource-juggler/test/common.batch.js'); + require('loopback-datasource-juggler/test/default-scope.test.js'); + require('loopback-datasource-juggler/test/include.test.js'); + + // === Operation hooks ==== // + + const suite = require('loopback-datasource-juggler/test/persistence-hooks.suite.js'); + + const DB_VERSION = process.env.MONGODB_VERSION; + + if (!DB_VERSION) { + console.log('The ENV variable MONGODB_VERSION is not set.' + + ' Assuming MongoDB version 2.6 or newer.'); + } + + const DB_HAS_2_6_FEATURES = (!DB_VERSION || + semver.satisfies(DB_VERSION, '>=2.6.0')); + + const customConfig = Object.assign({}, global.config, { + enableOptimisedFindOrCreate: DB_HAS_2_6_FEATURES, + }); + + suite(global.getDataSource(customConfig, juggler.DataSource), should, { + replaceOrCreateReportsNewInstance: DB_HAS_2_6_FEATURES, + }); +}); diff --git a/package.json b/package.json index 46771b251..20db5abc2 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "scripts": { "benchmarks": "make benchmarks", "leak-detection": "make leak-detection", - "test": "mocha", + "test": "mocha test/*.test.js node_modules/juggler-v3/test.js node_modules/juggler-v4/test.js", "lint": "eslint .", "posttest": "npm run lint" }, @@ -39,7 +39,9 @@ "bluebird": "^3.5.4", "eslint": "^5.1.0", "eslint-config-loopback": "^10.0.0", - "loopback-datasource-juggler": "^3.23.0", + "juggler-v3": "file:./deps/juggler-v3", + "juggler-v4": "file:./deps/juggler-v4", + "loopback-datasource-juggler": "^3.0.0 || ^4.0.0", "mocha": "^5.2.0", "rc": "^1.2.8", "semver": "^5.5.1", diff --git a/test/imported.test.js b/test/imported.test.js deleted file mode 100644 index b4b59013c..000000000 --- a/test/imported.test.js +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright IBM Corp. 2013,2019. All Rights Reserved. -// Node module: loopback-connector-mongodb -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -'use strict'; - -describe('mongodb imported features', function() { - before(function() { - require('./init.js'); - }); - - require('loopback-datasource-juggler/test/common.batch.js'); - require('loopback-datasource-juggler/test/default-scope.test.js'); - require('loopback-datasource-juggler/test/include.test.js'); -}); diff --git a/test/init.js b/test/init.js index b8b3592f5..d114969e9 100644 --- a/test/init.js +++ b/test/init.js @@ -7,7 +7,8 @@ module.exports = require('should'); -var DataSource = require('loopback-datasource-juggler').DataSource; +var juggler = require('loopback-datasource-juggler'); +var DataSource = juggler.DataSource; var TEST_ENV = process.env.TEST_ENV || 'test'; var config = require('rc')('loopback', {test: {mongodb: {}}})[TEST_ENV] @@ -24,8 +25,10 @@ config = { global.config = config; -global.getDataSource = global.getSchema = function(customConfig) { - var db = new DataSource(require('../'), customConfig || config); +var db; +global.getDataSource = global.getSchema = function(customConfig, customClass) { + const ctor = customClass || DataSource; + db = new ctor(require('../'), customConfig || config); db.log = function(a) { console.log(a); }; @@ -33,6 +36,13 @@ global.getDataSource = global.getSchema = function(customConfig) { return db; }; +global.resetDataSourceClass = function(ctor) { + DataSource = ctor || juggler.DataSource; + var promise = db ? db.disconnect() : Promise.resolve(); + db = undefined; + return promise; +}; + global.connectorCapabilities = { ilike: false, nilike: false, diff --git a/test/mongodb.test.js b/test/mongodb.test.js index aa80b5e7f..dec88b771 100644 --- a/test/mongodb.test.js +++ b/test/mongodb.test.js @@ -371,8 +371,9 @@ describe('mongodb connector', function() { }); it('ignores invalid option', function(done) { - var configWithInvalidOption = global.config; - configWithInvalidOption.invalidOption = 'invalid'; + var configWithInvalidOption = Object.assign({}, global.config, { + invalidOption: 'invalid', + }); var ds = global.getDataSource(configWithInvalidOption); ds.ping(function(err) { if (err) return done(err); @@ -393,7 +394,7 @@ describe('mongodb connector', function() { it('should prioritize to the database given in the url property', function(done) { var cfg = JSON.parse(JSON.stringify(global.config)); var testDb = 'lb-ds-overriden-test-1'; - cfg.url = 'mongodb://' + cfg.hostname + ':' + cfg.port + '/' + testDb; + cfg.url = 'mongodb://' + cfg.host + ':' + cfg.port + '/' + testDb; var ds = global.getDataSource(cfg); ds.once('connected', function() { var db = ds.connector.db; diff --git a/test/persistence-hooks.test.js b/test/persistence-hooks.test.js deleted file mode 100644 index 4e71ed5b7..000000000 --- a/test/persistence-hooks.test.js +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright IBM Corp. 2015,2019. All Rights Reserved. -// Node module: loopback-connector-mongodb -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -'use strict'; - -var semver = require('semver'); -var should = require('./init'); -var suite = require('loopback-datasource-juggler/test/persistence-hooks.suite.js'); - -var customConfig = { - enableOptimisedfindOrCreate: false, -}; - -for (var i in global.config) { - customConfig[i] = global.config[i]; -} -var DB_VERSION = process.env.MONGODB_VERSION; - -if (!DB_VERSION) { - console.log( - 'The ENV variable MONGODB_VERSION is not set.' + - ' Assuming MongoDB version 2.6 or newer.' - ); -} - -var DB_HAS_2_6_FEATURES = - !DB_VERSION || semver.satisfies(DB_VERSION, '>=2.6.0'); - -if (DB_HAS_2_6_FEATURES) { - customConfig.enableOptimisedfindOrCreate = true; -} - -suite(global.getDataSource(customConfig), should, { - replaceOrCreateReportsNewInstance: DB_HAS_2_6_FEATURES, -});