Skip to content

Commit

Permalink
feat: run shared tests from v3 n v4 of juggler
Browse files Browse the repository at this point in the history
  • Loading branch information
Agnes Lin committed Jun 20, 2019
1 parent a5b45f0 commit b364cf9
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 77 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/node_modules/
8 changes: 8 additions & 0 deletions deps/juggler-v3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "juggler-v3",
"version": "3.0.0",
"dependencies": {
"loopback-datasource-juggler":"3.x",
"should": "^8.4.0"
}
}
31 changes: 31 additions & 0 deletions deps/juggler-v3/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: loopback-connector-cloudant
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0

'use strict';

const juggler = require('loopback-datasource-juggler');
const name = require('./package.json').name;

require('../../test/init.js');

describe(name, function() {
before(function() {
IMPORTED_TEST = true;
return global.resetDataSourceClass(juggler.DataSource);
});

after(function() {
IMPORTED_TEST = false;
return global.resetDataSourceClass();
});
require('loopback-datasource-juggler/test/include.test.js');
require('loopback-datasource-juggler/test/common.batch.js');
});

/* TODO: run persistence-hooks, default scope test suites too
var testHooks = require('loopback-datasource-juggler/test/persistence-hooks.suite.js');
var testDefaultScope = require('loopback-datasource-juggler/test/default-scope.test.js');
*/

9 changes: 9 additions & 0 deletions deps/juggler-v4/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "juggler-v4",
"version": "4.0.0",
"dependencies": {
"loopback-datasource-juggler":"4.x",
"should": "^13.2.3"
}
}

30 changes: 30 additions & 0 deletions deps/juggler-v4/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: loopback-connector-cloudant
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0

'use strict';

const juggler = require('loopback-datasource-juggler');
const name = require('./package.json').name;

require('../../test/init.js');

describe(name, function() {
before(function() {
IMPORTED_TEST = true;
return global.resetDataSourceClass(juggler.DataSource);
});

after(function() {
IMPORTED_TEST = false;
return global.resetDataSourceClass();
});
require('loopback-datasource-juggler/test/include.test.js');
require('loopback-datasource-juggler/test/common.batch.js');
});

/* TODO: run persistence-hooks, default scope test suites too
var testHooks = require('loopback-datasource-juggler/test/persistence-hooks.suite.js');
var testDefaultScope = require('loopback-datasource-juggler/test/default-scope.test.js');
*/
4 changes: 2 additions & 2 deletions lib/cloudant.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Cloudant.prototype.getTypes = function() {
Cloudant.prototype.connect = function(cb) {
debug('Cloudant.prototype.connect');
var self = this;
// strip db name if defined in path of url before
// strip db name if defined in path of url before
// sending it to our driver
if (self.options.url) {
var parsedUrl = url.parse(self.options.url);
Expand Down Expand Up @@ -106,7 +106,7 @@ Cloudant.prototype.getDriverInst = function() {
return this.cloudant;
};

/**
/**
* Called by function CouchDB.prototype.selectModel, overriden by Cloudant
*/
Cloudant.prototype.getModelObjectSettings = function(mo) {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
"dockerode": "^2.4.3",
"eslint": "^2.13.1",
"eslint-config-loopback": "^4.0.0",
"loopback-datasource-juggler": "^3.0.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",
"ms": "^2.0.0",
"rc": "^1.1.5",
Expand Down
10 changes: 6 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ async.waterfall([
setCloudantEnv,
waitFor('/_all_dbs'),
createDB('test-db'),
run([mochaBin, '--timeout', '40000', '--require', 'strong-mocha-interfaces',
'--require', 'test/init.js', '--ui', 'strong-bdd']),
run([mochaBin, 'test/*.test.js', 'node_modules/juggler-v3/test.js',
'node_modules/juggler-v4/test.js', '--timeout', '40000',
'--require', 'strong-mocha-interfaces', '--require', 'test/init.js',
'--ui', 'strong-bdd']),
], function(testErr) {
dockerCleanup(function(cleanupErr) {
if (cleanupErr) {
Expand Down Expand Up @@ -172,8 +174,8 @@ function createDB(db) {
setImmediate(next, null, container);
});
})
.on('error', next)
.end();
.on('error', next)
.end();
};
}

Expand Down
106 changes: 53 additions & 53 deletions test/cloudant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,33 +143,33 @@ describe('cloudant connector', function() {
});

it('updates all matching instances with array props',
function(done) {
var data = {
price: 200,
releases: [7],
type: ['everything'],
foo: [{id: 1, name: 'bar'}],
};

Product.updateAll({price: 100}, data, function(err, res) {
if (err) done(err);
Product.find(function(err, res) {
if (err) done(err);
res.length.should.equal(2);
res[0].name.should.oneOf(prod1.name, prod2.name);
res[0].price.should.equal(data.price);
res[0].releases.should.deepEqual(data.releases);
res[0].type.should.deepEqual(data.type);
res[0].foo.should.deepEqual(data.foo);
res[1].name.should.oneOf(prod1.name, prod2.name);
res[1].price.should.equal(data.price);
res[1].releases.should.deepEqual(data.releases);
res[1].type.should.deepEqual(data.type);
res[1].foo.should.deepEqual(data.foo);
done();
});
});
});
function(done) {
var data = {
price: 200,
releases: [7],
type: ['everything'],
foo: [{id: 1, name: 'bar'}],
};

Product.updateAll({price: 100}, data, function(err, res) {
if (err) done(err);
Product.find(function(err, res) {
if (err) done(err);
res.length.should.equal(2);
res[0].name.should.oneOf(prod1.name, prod2.name);
res[0].price.should.equal(data.price);
res[0].releases.should.deepEqual(data.releases);
res[0].type.should.deepEqual(data.type);
res[0].foo.should.deepEqual(data.foo);
res[1].name.should.oneOf(prod1.name, prod2.name);
res[1].price.should.equal(data.price);
res[1].releases.should.deepEqual(data.releases);
res[1].type.should.deepEqual(data.type);
res[1].foo.should.deepEqual(data.foo);
done();
});
});
});
});

// the test suite is to make sure when
Expand Down Expand Up @@ -200,11 +200,11 @@ describe('cloudant connector', function() {
});
it('returns null when first level property is array', function(done) {
CustomerSimple.find({where: {'friends.name': {regexp: /^Ringo/}}},
function(err, customers) {
if (err) return done(err);
customers.should.be.empty();
done();
});
function(err, customers) {
if (err) return done(err);
customers.should.be.empty();
done();
});
});
it('returns result when first level property is array type' +
' and $elemMatch provided', function(done) {
Expand All @@ -223,11 +223,11 @@ describe('cloudant connector', function() {
it('returns null when multi-level nested property' +
' contains array type', function(done) {
CustomerSimple.find({where: {'address.tags.tag': 'business'}},
function(err, customers) {
if (err) return done(err);
customers.should.be.empty();
done();
});
function(err, customers) {
if (err) return done(err);
customers.should.be.empty();
done();
});
});
it('returns result when multi-level nested property contains array type' +
' and $elemMatch provided', function(done) {
Expand All @@ -243,12 +243,12 @@ describe('cloudant connector', function() {
});
it('returns error missing data type when sorting', function(done) {
CustomerSimple.find({where: {'address.state': 'CA'},
order: 'address.state DESC'},
function(err, customers) {
should.exist(err);
err.message.should.match(/no_usable_index,missing_sort_index/);
done();
});
order: 'address.state DESC'},
function(err, customers) {
should.exist(err);
err.message.should.match(/no_usable_index,missing_sort_index/);
done();
});
});
it('returns result when sorting type provided - missing first level ' +
'property', function(done) {
Expand All @@ -271,17 +271,17 @@ describe('cloudant connector', function() {
// - Each object in the sort array has a single key.
// http://docs.couchdb.org/en/2.0.0/api/database/find.html#sort-syntax
it('returns result when sorting type provided - nested property',
function(done) {
CustomerSimple.find({where: {'address.city': {gt: null}},
order: 'address.city DESC'},
function(err, customers) {
if (err) return done(err);
customers.length.should.be.equal(2);
customers[0].address.city.should.be.eql('San Mateo');
customers[1].address.city.should.be.eql('San Jose');
done();
function(done) {
CustomerSimple.find({where: {'address.city': {gt: null}},
order: 'address.city DESC'},
function(err, customers) {
if (err) return done(err);
customers.length.should.be.equal(2);
customers[0].address.city.should.be.eql('San Mateo');
customers[1].address.city.should.be.eql('San Jose');
done();
});
});
});
});
describe('defined in modelDef', function() {
it('returns result when complete query of' +
Expand Down
14 changes: 0 additions & 14 deletions test/imported.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,6 @@

'use strict';

describe('cloudant imported features', function() {
before(function() {
IMPORTED_TEST = true;
});

after(function() {
IMPORTED_TEST = false;
});

require('loopback-datasource-juggler/test/include.test.js');
require('loopback-datasource-juggler/test/common.batch.js');
});

// Run the COUCHDB2 Test Suite.
require('./init.js');

process.env.COUCHDB2_TEST_SKIP_INIT = true;
Expand Down
16 changes: 13 additions & 3 deletions test/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 _ = require('lodash');

var config = {
Expand Down Expand Up @@ -40,8 +41,10 @@ if (process.env.LOOPBACK_MOCHA_SKIPS) {
process.env.LOOPBACK_MOCHA_SKIPS = JSON.stringify(skips);
}

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);
};
Expand Down Expand Up @@ -115,6 +118,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,
Expand Down

0 comments on commit b364cf9

Please sign in to comment.