Skip to content

Commit

Permalink
Merge pull request #390 from strongloop/refactor/juggler-test
Browse files Browse the repository at this point in the history
feat: run shared tests
  • Loading branch information
jannyHou authored Jul 9, 2019
2 parents 869061c + 01c94b6 commit f556b7c
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 41 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/node_modules/
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: node_js
node_js:
- "6"
- "8"
- "10"
# Xenial build environment comes with MySQL 5.7
Expand Down
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-mysql
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0

'use strict';

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/include.test.js');

// === Operation hooks ==== //

const suite = require('loopback-datasource-juggler/test/persistence-hooks.suite.js');
const customConfig = Object.assign({}, global.config);
suite(global.getDataSource(customConfig, juggler.DataSource), should, {replaceOrCreateReportsNewInstance: false});
});
8 changes: 8 additions & 0 deletions deps/juggler-v4/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "juggler-v4",
"version": "4.0.0",
"dependencies": {
"loopback-datasource-juggler":"4.x",
"should": "^13.2.3"
}
}
32 changes: 32 additions & 0 deletions deps/juggler-v4/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: loopback-connector-mysql
// 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/include.test.js');

// === Operation hooks ==== //

const suite = require('loopback-datasource-juggler/test/persistence-hooks.suite.js');
const customConfig = Object.assign({}, global.config);
suite(global.getDataSource(customConfig, juggler.DataSource), should, {replaceOrCreateReportsNewInstance: false});
});
10 changes: 6 additions & 4 deletions lib/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,12 @@ MySQL.prototype.fromColumnValue = function(prop, val) {
lat: val.y,
};
break;
case 'ObjectID':
val = new prop.type(val);
break;
case 'Buffer':
val = prop.type(val);
break;
case 'List':
case 'Array':
case 'Object':
Expand All @@ -453,10 +459,6 @@ MySQL.prototype.fromColumnValue = function(prop, val) {
}
break;
default:
if (!Array.isArray(prop.type) && !prop.type.modelName) {
// Do not convert array and model types
val = prop.type(val);
}
break;
}
}
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"version": "5.4.1",
"description": "MySQL connector for loopback-datasource-juggler",
"engines": {
"node": ">=6"
"node": ">=8"
},
"main": "index.js",
"scripts": {
"pretest": "node pretest.js",
"lint": "eslint .",
"test": "mocha --timeout 10000 --require test/init.js test/*.js",
"test": "mocha --timeout 10000 test/*.test.js node_modules/juggler-v3/test.js node_modules/juggler-v4/test.js",
"posttest": "npm run lint"
},
"dependencies": {
Expand All @@ -24,7 +24,9 @@
"bluebird": "~2.9.10",
"eslint": "^4.3.0",
"eslint-config-loopback": "^8.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": "^2.1.0",
"rc": "^1.0.0",
"should": "^8.0.2",
Expand Down
4 changes: 3 additions & 1 deletion test/datatypes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require('./init.js');
var assert = require('assert');
var _ = require('lodash');
const GeoPoint = require('loopback-datasource-juggler').GeoPoint;

var db, BlobModel, EnumModel, ANIMAL_ENUM, City, Account;

Expand Down Expand Up @@ -206,7 +207,8 @@ describe('MySQL specific datatypes', function() {
var xcor, ycor;
City.create(city1, function(err, res) {
if (err) return done(err);
res.loc.should.deepEqual(city1.loc);
const loc_in_geo_type = new GeoPoint(city1.loc);
res.loc.should.deepEqual(loc_in_geo_type);
res.name.should.equal(city1.name);
var sqlStmt = 'select ST_X(loc),ST_Y(loc) from City where id=1';
db.connector.execute(sqlStmt, function(err, res) {
Expand Down
14 changes: 0 additions & 14 deletions test/imported.test.js

This file was deleted.

20 changes: 16 additions & 4 deletions test/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
'use strict';

module.exports = require('should');

var DataSource = require('loopback-datasource-juggler').DataSource;
const juggler = require('loopback-datasource-juggler');
let DataSource = juggler.DataSource;

var config = require('rc')('loopback', {test: {mysql: {}}}).test.mysql;
global.getConfig = function(options) {
Expand All @@ -28,11 +28,23 @@ global.getConfig = function(options) {
return dbConf;
};

global.getDataSource = global.getSchema = function(options) {
var db = new DataSource(require('../'), global.getConfig(options));
let db;
global.getDataSource = global.getSchema = function(options, customClass) {
const ctor = customClass || DataSource;
db = new ctor(require('../'), global.getConfig(options));
db.log = function(a) {
console.log(a);
};
return db;
};

global.resetDataSourceClass = function(ctor) {
DataSource = ctor || juggler.DataSource;
const promise = db ? db.disconnect() : Promise.resolve();
db = undefined;
return promise;
};

global.connectorCapabilities = {
ilike: false,
nilike: false,
Expand Down
4 changes: 3 additions & 1 deletion test/migration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,9 @@ var getFields = function(model, cb) {
res.forEach(function(field) {
fields[field.Field] = field;
});
cb(err, fields);
// The returned data are in arrays of type `RowDataPacket`,
// which are not objects.
cb(err, JSON.parse(JSON.stringify(fields)));
}
});
};
Expand Down
3 changes: 2 additions & 1 deletion test/mysql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
var async = require('async');
var should = require('./init.js');
var sinon = require('sinon');
const List = require('loopback-datasource-juggler/lib/list');

var Post, PostWithStringId, PostWithUniqueTitle, PostWithNumId, Student, db;

Expand Down Expand Up @@ -88,7 +89,7 @@ describe('mysql', function() {

p.content.should.be.equal(post.content);
p.title.should.be.equal('a');
p.comments.should.eql(['1', '2']);
p.comments.should.eql(new List(['1', '2']));
p.history.should.eql({a: 1, b: 'b'});

done();
Expand Down
12 changes: 0 additions & 12 deletions test/persistence-hooks.test.js

This file was deleted.

0 comments on commit f556b7c

Please sign in to comment.