diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..e702b2b --- /dev/null +++ b/.eslintrc @@ -0,0 +1,13 @@ +{ + "extends": "loopback", + "rules": { + "max-len": ["error", 120, 4, { + "ignoreComments": true, + "ignoreUrls": true, + "ignorePattern": "^\\s*var\\s.=\\s*(require\\s*\\()|(/)" + }], + "camelcase": 0, + "one-var": "off", + "no-unused-expressions": "off" + } + } diff --git a/example/app.js b/example/app.js index 18624cc..f4e68b2 100644 --- a/example/app.js +++ b/example/app.js @@ -3,6 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; var DataSource = require('loopback-datasource-juggler').DataSource; var config = require('rc')('loopback', {dev: {mysql: {}}}).dev.mysql; @@ -15,7 +16,7 @@ function show(err, models) { } else { console.log(models); if (models) { - models.forEach(function (m) { + models.forEach(function(m) { console.dir(m); }); } @@ -33,13 +34,8 @@ ds.discoverForeignKeys('inventory', show); ds.discoverExportedForeignKeys('location', show); -ds.discoverAndBuildModels('weapon', {owner: 'strongloop', visited: {}, associations: true}, function (err, models) { - +ds.discoverAndBuildModels('weapon', {owner: 'strongloop', visited: {}, associations: true}, function(err, models) { for (var m in models) { models[m].all(show); } - }); - - - diff --git a/index.js b/index.js index f9ff8d8..03f1307 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; var SG = require('strong-globalize'); SG.SetRootDir(__dirname); diff --git a/lib/discovery.js b/lib/discovery.js index 6a6a731..159384f 100644 --- a/lib/discovery.js +++ b/lib/discovery.js @@ -3,6 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; var g = require('strong-globalize')(); module.exports = mixinDiscovery; @@ -88,7 +89,6 @@ function mixinDiscovery(MySQL, mysql) { function queryViews(options) { var sqlViews = null; if (options.views) { - var schema = options.owner || options.schema; if (options.all && !schema) { @@ -183,7 +183,7 @@ function mixinDiscovery(MySQL, mysql) { schema: options.owner || options.schema, table: table, options: options, - cb: cb + cb: cb, }; } @@ -404,7 +404,7 @@ function mixinDiscovery(MySQL, mysql) { this.execute(sql, cb); }; - MySQL.prototype.buildPropertyType = function (columnDefinition) { + MySQL.prototype.buildPropertyType = function(columnDefinition) { var mysqlType = columnDefinition.dataType; var dataLength = columnDefinition.dataLength; @@ -452,7 +452,7 @@ function mixinDiscovery(MySQL, mysql) { default: return 'String'; } - } + }; MySQL.prototype.getDefaultSchema = function() { if (this.dataSource && this.dataSource.settings && diff --git a/lib/enumFactory.js b/lib/enumFactory.js index 12844f8..4e68174 100644 --- a/lib/enumFactory.js +++ b/lib/enumFactory.js @@ -3,6 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; var g = require('strong-globalize')(); var EnumFactory = function() { @@ -11,7 +12,7 @@ var EnumFactory = function() { if (typeof arg === 'number' && arg % 1 == 0) { return Enum._values[arg]; } else if (Enum[arg]) { - return Enum[arg] + return Enum[arg]; } else if (Enum._values.indexOf(arg) !== -1) { return arg; } else if (arg === null) { @@ -30,7 +31,7 @@ var EnumFactory = function() { configurable: false, enumerable: true, value: arg, - writable: false + writable: false, }); dxList.push(arg); } @@ -38,18 +39,18 @@ var EnumFactory = function() { configurable: false, enumerable: false, value: dxList, - writable: false + writable: false, }); Object.defineProperty(Enum, '_string', { configurable: false, enumerable: false, value: stringified(Enum), - writable: false + writable: false, }); Object.freeze(Enum); return Enum; } else { - throw g.f("No arguments - could not create {{Enum}}."); + throw g.f('No arguments - could not create {{Enum}}.'); } }; @@ -64,10 +65,3 @@ function stringified(anEnum) { } exports.EnumFactory = EnumFactory; - - - - - - - diff --git a/lib/migration.js b/lib/migration.js index 143c912..fb82cd7 100644 --- a/lib/migration.js +++ b/lib/migration.js @@ -3,6 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; var g = require('strong-globalize')(); var async = require('async'); module.exports = mixinMigration; @@ -49,7 +50,6 @@ function mixinMigration(MySQL, mysql) { }); }); }, cb); - }; /*! @@ -130,7 +130,7 @@ function mixinMigration(MySQL, mysql) { if (!ai[name]) { ai[name] = { info: i, - columns: [] + columns: [], }; } ai[name].columns[i.Seq_in_index - 1] = i.Column_name; @@ -216,7 +216,7 @@ function mixinMigration(MySQL, mysql) { ' (' + pName + ') ' + type); } else { if (typeof i === 'object' && i.unique && i.unique === true) { - kind = "UNIQUE"; + kind = 'UNIQUE'; } sql.push('ADD ' + kind + ' INDEX ' + pName + ' ' + type + ' (' + pName + ') '); @@ -350,7 +350,7 @@ function mixinMigration(MySQL, mysql) { return (kind + ' INDEX ' + columnName + ' (' + columnName + ') ' + type); } else { if (typeof i === 'object' && i.unique && i.unique === true) { - kind = "UNIQUE"; + kind = 'UNIQUE'; } return (kind + ' INDEX ' + columnName + ' ' + type + ' (' + columnName + ') '); } @@ -496,10 +496,10 @@ function mixinMigration(MySQL, mysql) { function stringOptions(p, columnType) { if (p.charset) { - columnType += " CHARACTER SET " + p.charset; + columnType += ' CHARACTER SET ' + p.charset; } if (p.collation) { - columnType += " COLLATE " + p.collation; + columnType += ' COLLATE ' + p.collation; } return columnType; } diff --git a/lib/mysql.js b/lib/mysql.js index 215e1dc..e9574c3 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -3,6 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; var g = require('strong-globalize')(); /*! @@ -33,12 +34,12 @@ exports.initialize = function initializeDataSource(dataSource, callback) { dataSource.EnumFactory = EnumFactory; // factory for Enums. Note that currently Enums can not be registered. - if(callback) { - if(dataSource.settings.lazyConnect) { + if (callback) { + if (dataSource.settings.lazyConnect) { process.nextTick(function() { callback(); }); - } else{ + } else { dataSource.connector.connect(callback); } } @@ -90,7 +91,7 @@ MySQL.prototype.connect = function(callback) { if (!err) { if (self.debug) { debug('MySQL connection is established: ' + self.settings || {}); - } + } connection.release(); } else { if (self.debug || !callback) { @@ -100,9 +101,9 @@ MySQL.prototype.connect = function(callback) { callback && callback(err, conn); }); } -} +}; -function generateOptions (settings) { +function generateOptions(settings) { var s = settings || {}; if (s.collation) { // Charset should be first 'chunk' of collation. @@ -133,7 +134,7 @@ function generateOptions (settings) { socketPath: s.socketPath, charset: s.collation.toUpperCase(), // Correct by docs despite seeming odd. supportBigNumbers: s.supportBigNumbers, - connectionLimit: s.connectionLimit + connectionLimit: s.connectionLimit, }; // Don't configure the DB if the pool can be used for multiple DBs @@ -240,44 +241,44 @@ MySQL.prototype.executeSQL = function(sql, params, options, callback) { }; MySQL.prototype._modifyOrCreate = function(model, data, options, fields, cb) { - var sql = new ParameterizedSQL('INSERT INTO ' + this.tableEscaped(model)); - var columnValues = fields.columnValues; - var fieldNames = fields.names; - if (fieldNames.length) { - sql.merge('(' + fieldNames.join(',') + ')', ''); - var values = ParameterizedSQL.join(columnValues, ','); - values.sql = 'VALUES(' + values.sql + ')'; - sql.merge(values); - } else { - sql.merge(this.buildInsertDefaultValues(model, data, options)); - } + var sql = new ParameterizedSQL('INSERT INTO ' + this.tableEscaped(model)); + var columnValues = fields.columnValues; + var fieldNames = fields.names; + if (fieldNames.length) { + sql.merge('(' + fieldNames.join(',') + ')', ''); + var values = ParameterizedSQL.join(columnValues, ','); + values.sql = 'VALUES(' + values.sql + ')'; + sql.merge(values); + } else { + sql.merge(this.buildInsertDefaultValues(model, data, options)); + } - sql.merge('ON DUPLICATE KEY UPDATE'); - var setValues = []; - for (var i = 0, n = fields.names.length; i < n; i++) { - if (!fields.properties[i].id) { - setValues.push(new ParameterizedSQL(fields.names[i] + '=' + + sql.merge('ON DUPLICATE KEY UPDATE'); + var setValues = []; + for (var i = 0, n = fields.names.length; i < n; i++) { + if (!fields.properties[i].id) { + setValues.push(new ParameterizedSQL(fields.names[i] + '=' + columnValues[i].sql, columnValues[i].params)); - } } + } - sql.merge(ParameterizedSQL.join(setValues, ',')); + sql.merge(ParameterizedSQL.join(setValues, ',')); - this.execute(sql.sql, sql.params, options, function(err, info) { - if (!err && info && info.insertId) { - data.id = info.insertId; - } - var meta = {}; - if (info) { + this.execute(sql.sql, sql.params, options, function(err, info) { + if (!err && info && info.insertId) { + data.id = info.insertId; + } + var meta = {}; + if (info) { // When using the INSERT ... ON DUPLICATE KEY UPDATE statement, // the returned value is as follows: // 1 for each successful INSERT. // 2 for each successful UPDATE. - meta.isNewInstance = (info.affectedRows === 1); - } - cb(err, data, meta); - }); - }; + meta.isNewInstance = (info.affectedRows === 1); + } + cb(err, data, meta); + }); +}; /** * Replace if the model instance exists with the same id or create a new instance @@ -363,7 +364,7 @@ MySQL.prototype.toColumnValue = function(prop, val) { if (prop.type.name === 'GeoPoint') { return new ParameterizedSQL({ sql: 'Point(?,?)', - params: [val.lat, val.lng] + params: [val.lat, val.lng], }); } if (prop.type === Object) { @@ -416,7 +417,7 @@ MySQL.prototype.fromColumnValue = function(prop, val) { case 'Point': val = { lat: val.x, - lng: val.y + lng: val.y, }; break; case 'List': @@ -465,7 +466,7 @@ MySQL.prototype._buildLimit = function(model, limit, offset) { return ''; } return 'LIMIT ' + (offset ? (offset + ',' + limit) : limit); -} +}; MySQL.prototype.applyPagination = function(model, stmt, filter) { var limitClause = this._buildLimit(model, filter.limit, diff --git a/lib/transaction.js b/lib/transaction.js index c895827..926aa07 100644 --- a/lib/transaction.js +++ b/lib/transaction.js @@ -3,6 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; var debug = require('debug')('loopback:connector:mysql:transaction'); module.exports = mixinTransaction; @@ -11,7 +12,6 @@ module.exports = mixinTransaction; * @param {Object} mysql mysql driver */ function mixinTransaction(MySQL, mysql) { - /** * Begin a new transaction * @param isolationLevel @@ -20,8 +20,8 @@ function mixinTransaction(MySQL, mysql) { MySQL.prototype.beginTransaction = function(isolationLevel, cb) { debug('Begin a transaction with isolation level: %s', isolationLevel); this.client.getConnection(function(err, connection) { - if(err) return cb(err); - if(isolationLevel) { + if (err) return cb(err); + if (isolationLevel) { connection.query( 'SET SESSION TRANSACTION ISOLATION LEVEL ' + isolationLevel, function(err) { @@ -65,4 +65,4 @@ function mixinTransaction(MySQL, mysql) { cb(err); }); }; -} \ No newline at end of file +} diff --git a/package.json b/package.json index 9df90d6..8dde754 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,10 @@ "main": "index.js", "scripts": { "pretest": "node pretest.js", - "test": "mocha" - }, + "lint": "eslint .", + "test": "mocha --timeout 10000 test/*.js", + "posttest": "npm run lint" +}, "dependencies": { "async": "^0.9.0", "debug": "^2.1.1", @@ -16,6 +18,8 @@ }, "devDependencies": { "bluebird": "~2.9.10", + "eslint": "^2.13.1", + "eslint-config-loopback": "^4.0.0", "loopback-datasource-juggler": "^2.28.0", "mocha": "^2.1.0", "rc": "^1.0.0", diff --git a/pretest.js b/pretest.js index 3c48401..5fb162b 100644 --- a/pretest.js +++ b/pretest.js @@ -1,3 +1,4 @@ +'use strict'; // TODO: used for testing support for parallel testing on ci.strongloop.com which // provides MYSQL_* env vars instead of TEST_MYSQL_* env vars. process.env.TEST_MYSQL_USER = process.env.TEST_MYSQL_USER || process.env.MYSQL_USER; diff --git a/test/connection.test.js b/test/connection.test.js index dc616ed..33d58ec 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -3,6 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; require('./init.js'); var assert = require('assert'); var should = require('should'); @@ -12,9 +13,8 @@ var url = require('url'); var db, DummyModel, odb, config; -describe('connections', function () { - - before(function () { +describe('connections', function() { + before(function() { require('./init.js'); config = global.getConfig(); @@ -23,12 +23,12 @@ describe('connections', function () { db = odb; }); - it('should pass with valid settings', function (done) { + it('should pass with valid settings', function(done) { var db = new DataSource(mysqlConnector, config); db.ping(done); }); - it('ignores all other settings when url is present', function (done) { + it('ignores all other settings when url is present', function(done) { var formatedUrl = generateURL(config); var dbConfig = { url: formatedUrl, @@ -43,36 +43,32 @@ describe('connections', function () { db.ping(done); }); - it('should use utf8 charset', function (done) { - + it('should use utf8 charset', function(done) { var test_set = /utf8/; var test_collo = /utf8_general_ci/; var test_set_str = 'utf8'; var test_set_collo = 'utf8_general_ci'; charsetTest(test_set, test_collo, test_set_str, test_set_collo, done); - }); - it('should disconnect first db', function (done) { - db.disconnect(function () { + it('should disconnect first db', function(done) { + db.disconnect(function() { odb = getDataSource(); done(); }); }); - it('should use latin1 charset', function (done) { - + it('should use latin1 charset', function(done) { var test_set = /latin1/; var test_collo = /latin1_general_ci/; var test_set_str = 'latin1'; var test_set_collo = 'latin1_general_ci'; charsetTest(test_set, test_collo, test_set_str, test_set_collo, done); - }); - it('should drop db and disconnect all', function (done) { - db.connector.execute('DROP DATABASE IF EXISTS ' + db.settings.database, function (err) { - db.disconnect(function () { + it('should drop db and disconnect all', function(done) { + db.connector.execute('DROP DATABASE IF EXISTS ' + db.settings.database, function(err) { + db.disconnect(function() { done(); }); }); @@ -113,21 +109,19 @@ describe('connections', function () { }); function charsetTest(test_set, test_collo, test_set_str, test_set_collo, done) { - - query('DROP DATABASE IF EXISTS ' + odb.settings.database, function (err) { + query('DROP DATABASE IF EXISTS ' + odb.settings.database, function(err) { assert.ok(!err); - odb.disconnect(function () { - + odb.disconnect(function() { db = getDataSource({collation: test_set_collo, createDatabase: true}); DummyModel = db.define('DummyModel', {string: String}); - db.automigrate(function () { + db.automigrate(function() { var q = 'SELECT DEFAULT_COLLATION_NAME' + ' FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ' + db.driver.escape(db.settings.database) + ' LIMIT 1'; - db.connector.execute(q, function (err, r) { + db.connector.execute(q, function(err, r) { assert.ok(!err); should(r[0].DEFAULT_COLLATION_NAME).match(test_collo); - db.connector.execute('SHOW VARIABLES LIKE "character_set%"', function (err, r) { + db.connector.execute('SHOW VARIABLES LIKE "character_set%"', function(err, r) { assert.ok(!err); var hit_all = 0; for (var result in r) { @@ -138,7 +132,7 @@ function charsetTest(test_set, test_collo, test_set_str, test_set_collo, done) { } assert.equal(hit_all, 4); }); - db.connector.execute('SHOW VARIABLES LIKE "collation%"', function (err, r) { + db.connector.execute('SHOW VARIABLES LIKE "collation%"', function(err, r) { assert.ok(!err); var hit_all = 0; for (var result in r) { @@ -152,7 +146,6 @@ function charsetTest(test_set, test_collo, test_set_str, test_set_collo, done) { }); }); }); - } function matchResult(result, variable_name, match) { @@ -163,7 +156,7 @@ function matchResult(result, variable_name, match) { return 0; } -var query = function (sql, cb) { +var query = function(sql, cb) { odb.connector.execute(sql, cb); }; @@ -173,7 +166,7 @@ function generateURL(config) { auth: config.username || '', hostname: config.host, pathname: config.database, - slashes: true + slashes: true, }; if (config.password) { urlObj.auth += ':' + config.password; @@ -181,8 +174,3 @@ function generateURL(config) { var formatedUrl = url.format(urlObj); return formatedUrl; } - - - - - diff --git a/test/datatypes.test.js b/test/datatypes.test.js index f2e9097..faa96cf 100644 --- a/test/datatypes.test.js +++ b/test/datatypes.test.js @@ -3,23 +3,23 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; require('./init.js'); var assert = require('assert'); var db, EnumModel, ANIMAL_ENUM; var mysqlVersion; -describe('MySQL specific datatypes', function () { - +describe('MySQL specific datatypes', function() { before(setup); - it('should run migration', function (done) { - db.automigrate(function () { + it('should run migration', function(done) { + db.automigrate(function() { done(); }); }); - it('An enum should parse itself', function (done) { + it('An enum should parse itself', function(done) { assert.equal(ANIMAL_ENUM.CAT, ANIMAL_ENUM('cat')); assert.equal(ANIMAL_ENUM.CAT, ANIMAL_ENUM('CAT')); assert.equal(ANIMAL_ENUM.CAT, ANIMAL_ENUM(2)); @@ -30,11 +30,11 @@ describe('MySQL specific datatypes', function () { done(); }); - it('should create a model instance with Enums', function (done) { - var em = EnumModel.create({animal: ANIMAL_ENUM.CAT, condition: 'sleepy', mood: 'happy'}, function (err, obj) { + it('should create a model instance with Enums', function(done) { + var em = EnumModel.create({animal: ANIMAL_ENUM.CAT, condition: 'sleepy', mood: 'happy'}, function(err, obj) { assert.ok(!err); assert.equal(obj.condition, 'sleepy'); - EnumModel.findOne({where: {animal: ANIMAL_ENUM.CAT}}, function (err, found) { + EnumModel.findOne({where: {animal: ANIMAL_ENUM.CAT}}, function(err, found) { assert.ok(!err); assert.equal(found.mood, 'happy'); assert.equal(found.animal, ANIMAL_ENUM.CAT); @@ -43,15 +43,15 @@ describe('MySQL specific datatypes', function () { }); }); - it('should fail spectacularly with invalid enum values', function (done) { + it('should fail spectacularly with invalid enum values', function(done) { // TODO: with a default install of MySQL 5.7, these queries actually do fail and raise errors... if (/^5\.7/.test(mysqlVersion)) { assert.ok(mysqlVersion, 'skipping decimal/number test on mysql 5.7'); return done(); } - var em = EnumModel.create({animal: 'horse', condition: 'sleepy', mood: 'happy'}, function (err, obj) { + var em = EnumModel.create({animal: 'horse', condition: 'sleepy', mood: 'happy'}, function(err, obj) { assert.ok(!err); - EnumModel.findById(obj.id, function (err, found) { + EnumModel.findById(obj.id, function(err, found) { assert.ok(!err); assert.equal(found.animal, ''); // MySQL fun. assert.equal(found.animal, 0); @@ -60,14 +60,14 @@ describe('MySQL specific datatypes', function () { }); }); - it('should create a model instance with object/json types', function (done) { + it('should create a model instance with object/json types', function(done) { var note = {a: 1, b: '2'}; var extras = {c: 3, d: '4'}; var em = EnumModel.create({animal: ANIMAL_ENUM.DOG, condition: 'sleepy', - mood: 'happy', note: note, extras: extras}, function (err, obj) { + mood: 'happy', note: note, extras: extras}, function(err, obj) { assert.ok(!err); assert.equal(obj.condition, 'sleepy'); - EnumModel.findOne({where: {animal: ANIMAL_ENUM.DOG}}, function (err, found) { + EnumModel.findOne({where: {animal: ANIMAL_ENUM.DOG}}, function(err, found) { assert.ok(!err); assert.equal(found.mood, 'happy'); assert.equal(found.animal, ANIMAL_ENUM.DOG); @@ -78,15 +78,13 @@ describe('MySQL specific datatypes', function () { }); }); - it('should disconnect when done', function (done) { + it('should disconnect when done', function(done) { db.disconnect(); - done() + done(); }); - }); function setup(done) { - require('./init.js'); db = getSchema(); @@ -94,11 +92,11 @@ function setup(done) { ANIMAL_ENUM = db.EnumFactory('dog', 'cat', 'mouse'); EnumModel = db.define('EnumModel', { - animal: { type: ANIMAL_ENUM, null: false }, - condition: { type: db.EnumFactory('hungry', 'sleepy', 'thirsty') }, - mood: { type: db.EnumFactory('angry', 'happy', 'sad') }, + animal: {type: ANIMAL_ENUM, null: false}, + condition: {type: db.EnumFactory('hungry', 'sleepy', 'thirsty')}, + mood: {type: db.EnumFactory('angry', 'happy', 'sad')}, note: Object, - extras: 'JSON' + extras: 'JSON', }); query('SELECT VERSION()', function(err, res) { @@ -107,15 +105,15 @@ function setup(done) { }); } -var query = function (sql, cb) { +var query = function(sql, cb) { db.adapter.execute(sql, cb); }; -var blankDatabase = function (db, cb) { +var blankDatabase = function(db, cb) { var dbn = db.settings.database; var cs = db.settings.charset; var co = db.settings.collation; - query('DROP DATABASE IF EXISTS ' + dbn, function (err) { + query('DROP DATABASE IF EXISTS ' + dbn, function(err) { var q = 'CREATE DATABASE ' + dbn; if (cs) { q += ' CHARACTER SET ' + cs; @@ -123,46 +121,40 @@ var blankDatabase = function (db, cb) { if (co) { q += ' COLLATE ' + co; } - query(q, function (err) { + query(q, function(err) { query('USE ' + dbn, cb); }); }); }; -getFields = function (model, cb) { - query('SHOW FIELDS FROM ' + model, function (err, res) { +var getFields = function(model, cb) { + query('SHOW FIELDS FROM ' + model, function(err, res) { if (err) { cb(err); } else { var fields = {}; - res.forEach(function (field) { + res.forEach(function(field) { fields[field.Field] = field; }); cb(err, fields); } }); -} +}; -getIndexes = function (model, cb) { - query('SHOW INDEXES FROM ' + model, function (err, res) { +var getIndexes = function(model, cb) { + query('SHOW INDEXES FROM ' + model, function(err, res) { if (err) { console.log(err); cb(err); } else { var indexes = {}; // Note: this will only show the first key of compound keys - res.forEach(function (index) { + res.forEach(function(index) { if (parseInt(index.Seq_in_index, 10) == 1) { - indexes[index.Key_name] = index + indexes[index.Key_name] = index; } }); cb(err, indexes); } }); }; - - - - - - diff --git a/test/imported.test.js b/test/imported.test.js index 112f877..3437d88 100644 --- a/test/imported.test.js +++ b/test/imported.test.js @@ -3,13 +3,12 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -describe('mysql imported features', function () { - - before(function () { +'use strict'; +describe('mysql imported features', function() { + before(function() { require('./init.js'); }); require('loopback-datasource-juggler/test/common.batch.js'); require('loopback-datasource-juggler/test/include.test.js'); - }); diff --git a/test/init.js b/test/init.js index 8d704f4..ce17ce0 100644 --- a/test/init.js +++ b/test/init.js @@ -3,6 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; // TODO: used for testing support for parallel testing on ci.strongloop.com which // provides MYSQL_* env vars instead of TEST_MYSQL_* env vars. process.env.TEST_MYSQL_USER = process.env.TEST_MYSQL_USER || process.env.MYSQL_USER; @@ -15,16 +16,15 @@ module.exports = require('should'); var DataSource = require('loopback-datasource-juggler').DataSource; var config = require('rc')('loopback', {test: {mysql: {}}}).test.mysql; -console.log(config) -global.getConfig = function (options) { - +console.log(config); +global.getConfig = function(options) { var dbConf = { host: process.env.TEST_MYSQL_HOST || config.host || 'localhost', port: process.env.TEST_MYSQL_PORT || config.port || 3306, database: 'myapp_test', username: process.env.TEST_MYSQL_USER || config.username, password: process.env.TEST_MYSQL_PASSWORD || config.password, - createDatabase: true + createDatabase: true, }; if (options) { @@ -35,7 +35,7 @@ global.getConfig = function (options) { return dbConf; }; -global.getDataSource = global.getSchema = function (options) { +global.getDataSource = global.getSchema = function(options) { var db = new DataSource(require('../'), getConfig(options)); return db; }; diff --git a/test/migration.test.js b/test/migration.test.js index 9118b7d..9b90cea 100644 --- a/test/migration.test.js +++ b/test/migration.test.js @@ -3,6 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; var should = require('./init.js'); var assert = require('assert'); var Schema = require('loopback-datasource-juggler').Schema; @@ -10,18 +11,17 @@ var Schema = require('loopback-datasource-juggler').Schema; var db, UserData, StringData, NumberData, DateData; var mysqlVersion; -describe('migrations', function () { - +describe('migrations', function() { before(setup); - it('should run migration', function (done) { - db.automigrate(function () { + it('should run migration', function(done) { + db.automigrate(function() { done(); }); }); - it('UserData should have correct columns', function (done) { - getFields('UserData', function (err, fields) { + it('UserData should have correct columns', function(done) { + getFields('UserData', function(err, fields) { fields.should.be.eql({ id: { Field: 'id', @@ -29,58 +29,58 @@ describe('migrations', function () { Null: 'NO', Key: 'PRI', Default: null, - Extra: 'auto_increment' }, + Extra: 'auto_increment'}, email: { Field: 'email', Type: 'varchar(512)', Null: 'NO', Key: 'MUL', Default: null, - Extra: '' }, + Extra: ''}, name: { Field: 'name', Type: 'varchar(512)', Null: 'YES', Key: '', Default: null, - Extra: '' }, + Extra: ''}, bio: { Field: 'bio', Type: 'text', Null: 'YES', Key: '', Default: null, - Extra: '' }, + Extra: ''}, birthDate: { Field: 'birthDate', Type: 'datetime', Null: 'YES', Key: '', Default: null, - Extra: '' }, + Extra: ''}, pendingPeriod: { Field: 'pendingPeriod', Type: 'int(11)', Null: 'YES', Key: '', Default: null, - Extra: '' }, + Extra: ''}, createdByAdmin: { Field: 'createdByAdmin', Type: 'tinyint(1)', Null: 'YES', Key: '', Default: null, - Extra: '' } + Extra: ''}, }); done(); }); }); - it('UserData should have correct indexes', function (done) { + it('UserData should have correct indexes', function(done) { // Note: getIndexes truncates multi-key indexes to the first member. // Hence index1 is correct. - getIndexes('UserData', function (err, fields) { + getIndexes('UserData', function(err, fields) { fields.should.match({ PRIMARY: { Table: /UserData/i, @@ -97,7 +97,7 @@ describe('migrations', function () { Packed: null, Null: '', Index_type: 'BTREE', - Comment: '' }, + Comment: ''}, email: { Table: /UserData/i, Non_unique: 1, @@ -113,7 +113,7 @@ describe('migrations', function () { Packed: null, Null: '', Index_type: 'BTREE', - Comment: '' }, + Comment: ''}, index0: { Table: /UserData/i, Non_unique: 1, @@ -129,141 +129,141 @@ describe('migrations', function () { Packed: null, Null: '', Index_type: 'BTREE', - Comment: '' } + Comment: ''}, }); done(); }); }); - it('StringData should have correct columns', function (done) { - getFields('StringData', function (err, fields) { + it('StringData should have correct columns', function(done) { + getFields('StringData', function(err, fields) { fields.should.be.eql({ - idString: { Field: "idString", + idString: {Field: 'idString', Type: 'varchar(255)', Null: 'NO', Key: 'PRI', Default: null, Extra: ''}, - smallString: { Field: 'smallString', + smallString: {Field: 'smallString', Type: 'char(127)', Null: 'NO', Key: 'MUL', Default: null, - Extra: '' }, - mediumString: { Field: 'mediumString', + Extra: ''}, + mediumString: {Field: 'mediumString', Type: 'varchar(255)', Null: 'NO', Key: '', Default: null, - Extra: '' }, - tinyText: { Field: 'tinyText', + Extra: ''}, + tinyText: {Field: 'tinyText', Type: 'tinytext', Null: 'YES', Key: '', Default: null, - Extra: '' }, - giantJSON: { Field: 'giantJSON', + Extra: ''}, + giantJSON: {Field: 'giantJSON', Type: 'longtext', Null: 'YES', Key: '', Default: null, - Extra: '' }, - text: { Field: 'text', + Extra: ''}, + text: {Field: 'text', Type: 'varchar(1024)', Null: 'YES', Key: '', Default: null, - Extra: '' } + Extra: ''}, }); done(); }); }); - it('NumberData should have correct columns', function (done) { - getFields('NumberData', function (err, fields) { + it('NumberData should have correct columns', function(done) { + getFields('NumberData', function(err, fields) { fields.should.be.eql({ - id: { Field: 'id', + id: {Field: 'id', Type: 'int(11)', Null: 'NO', Key: 'PRI', Default: null, - Extra: 'auto_increment' }, - number: { Field: 'number', + Extra: 'auto_increment'}, + number: {Field: 'number', Type: 'decimal(10,3) unsigned', Null: 'NO', Key: 'MUL', Default: null, - Extra: '' }, - tinyInt: { Field: 'tinyInt', + Extra: ''}, + tinyInt: {Field: 'tinyInt', Type: 'tinyint(2)', Null: 'YES', Key: '', Default: null, - Extra: '' }, - mediumInt: { Field: 'mediumInt', + Extra: ''}, + mediumInt: {Field: 'mediumInt', Type: 'mediumint(8) unsigned', Null: 'NO', Key: '', Default: null, - Extra: '' }, - floater: { Field: 'floater', + Extra: ''}, + floater: {Field: 'floater', Type: 'double(14,6)', Null: 'YES', Key: '', Default: null, - Extra: '' } + Extra: ''}, }); done(); }); }); - it('DateData should have correct columns', function (done) { - getFields('DateData', function (err, fields) { + it('DateData should have correct columns', function(done) { + getFields('DateData', function(err, fields) { fields.should.be.eql({ - id: { Field: 'id', + id: {Field: 'id', Type: 'int(11)', Null: 'NO', Key: 'PRI', Default: null, - Extra: 'auto_increment' }, - dateTime: { Field: 'dateTime', + Extra: 'auto_increment'}, + dateTime: {Field: 'dateTime', Type: 'datetime', Null: 'YES', Key: '', Default: null, - Extra: '' }, - timestamp: { Field: 'timestamp', + Extra: ''}, + timestamp: {Field: 'timestamp', Type: 'timestamp', Null: 'YES', Key: '', Default: null, - Extra: '' } + Extra: ''}, }); done(); }); }); - it('should autoupdate', function (done) { - var userExists = function (cb) { - query('SELECT * FROM UserData', function (err, res) { + it('should autoupdate', function(done) { + var userExists = function(cb) { + query('SELECT * FROM UserData', function(err, res) { cb(!err && res[0].email == 'test@example.com'); }); - } + }; - UserData.create({email: 'test@example.com'}, function (err, user) { + UserData.create({email: 'test@example.com'}, function(err, user) { assert.ok(!err, 'Could not create user: ' + err); - userExists(function (yep) { + userExists(function(yep) { assert.ok(yep, 'User does not exist'); }); - UserData.defineProperty('email', { type: String }); + UserData.defineProperty('email', {type: String}); UserData.defineProperty('name', {type: String, dataType: 'char', limit: 50}); UserData.defineProperty('newProperty', {type: Number, unsigned: true, dataType: 'bigInt'}); // UserData.defineProperty('pendingPeriod', false); // This will not work as expected. - db.autoupdate(function (err) { - getFields('UserData', function (err, fields) { + db.autoupdate(function(err) { + getFields('UserData', function(err, fields) { // change nullable for email assert.equal(fields.email.Null, 'YES', 'Email does not allow null'); // change type of name @@ -278,7 +278,7 @@ describe('migrations', function () { // assert.ok(!fields.pendingPeriod, // 'Did not drop column pendingPeriod'); // user still exists - userExists(function (yep) { + userExists(function(yep) { assert.ok(yep, 'User does not exist'); done(); }); @@ -287,20 +287,20 @@ describe('migrations', function () { }); }); - it('should check actuality of dataSource', function (done) { + it('should check actuality of dataSource', function(done) { // 'drop column' - UserData.dataSource.isActual(function (err, ok) { + UserData.dataSource.isActual(function(err, ok) { assert.ok(ok, 'dataSource is not actual (should be)'); UserData.defineProperty('essay', {type: Schema.Text}); // UserData.defineProperty('email', false); Can't undefine currently. - UserData.dataSource.isActual(function (err, ok) { + UserData.dataSource.isActual(function(err, ok) { assert.ok(!ok, 'dataSource is actual (shouldn\t be)'); - done() + done(); }); }); }); - it('should allow numbers with decimals', function (done) { + it('should allow numbers with decimals', function(done) { // TODO: Default install of MySQL 5.7 returns an error here, which we assert should not happen. if (/^5\.7/.test(mysqlVersion)) { assert.ok(mysqlVersion, 'skipping decimal/number test on mysql 5.7'); @@ -308,10 +308,10 @@ describe('migrations', function () { } NumberData.create({number: 1.1234567, tinyInt: 123456, mediumInt: -1234567, - floater: 123456789.1234567 }, function (err, obj) { + floater: 123456789.1234567}, function(err, obj) { assert.ok(!err); assert.ok(obj); - NumberData.findById(obj.id, function (err, found) { + NumberData.findById(obj.id, function(err, found) { assert.equal(found.number, 1.123); assert.equal(found.tinyInt, 127); assert.equal(found.mediumInt, 0); @@ -321,14 +321,14 @@ describe('migrations', function () { }); }); - it('should allow both kinds of date columns', function (done) { + it('should allow both kinds of date columns', function(done) { DateData.create({ dateTime: new Date('Aug 9 1996 07:47:33 GMT'), - timestamp: new Date('Sep 22 2007 17:12:22 GMT') - }, function (err, obj) { + timestamp: new Date('Sep 22 2007 17:12:22 GMT'), + }, function(err, obj) { assert.ok(!err); assert.ok(obj); - DateData.findById(obj.id, function (err, found) { + DateData.findById(obj.id, function(err, found) { assert.equal(found.dateTime.toGMTString(), 'Fri, 09 Aug 1996 07:47:33 GMT'); assert.equal(found.timestamp.toGMTString(), @@ -350,31 +350,29 @@ describe('migrations', function () { }); }); - it('should disconnect when done', function (done) { + it('should disconnect when done', function(done) { db.disconnect(); done(); }); - }); function setup(done) { - require('./init.js'); db = getSchema(); UserData = db.define('UserData', { - email: { type: String, null: false, index: true }, + email: {type: String, null: false, index: true}, name: String, bio: Schema.Text, birthDate: Date, pendingPeriod: Number, createdByAdmin: Boolean, - }, { indexes: { + }, {indexes: { index0: { - columns: 'email, createdByAdmin' - } - } + columns: 'email, createdByAdmin', + }, + }, }); StringData = db.define('StringData', { @@ -384,7 +382,7 @@ function setup(done) { mediumString: {type: String, null: false, dataType: 'varchar', limit: 255}, tinyText: {type: String, dataType: 'tinyText'}, giantJSON: {type: Schema.JSON, dataType: 'longText'}, - text: {type: Schema.Text, dataType: 'varchar', limit: 1024} + text: {type: Schema.Text, dataType: 'varchar', limit: 1024}, }); NumberData = db.define('NumberData', { @@ -393,12 +391,12 @@ function setup(done) { tinyInt: {type: Number, dataType: 'tinyInt', display: 2}, mediumInt: {type: Number, dataType: 'mediumInt', unsigned: true, required: true}, - floater: {type: Number, dataType: 'double', precision: 14, scale: 6} + floater: {type: Number, dataType: 'double', precision: 14, scale: 6}, }); DateData = db.define('DateData', { dateTime: {type: Date, dataType: 'datetime'}, - timestamp: {type: Date, dataType: 'timestamp'} + timestamp: {type: Date, dataType: 'timestamp'}, }); query('SELECT VERSION()', function(err, res) { @@ -407,15 +405,15 @@ function setup(done) { }); } -var query = function (sql, cb) { +var query = function(sql, cb) { db.adapter.execute(sql, cb); }; -var blankDatabase = function (db, cb) { +var blankDatabase = function(db, cb) { var dbn = db.settings.database; var cs = db.settings.charset; var co = db.settings.collation; - query('DROP DATABASE IF EXISTS ' + dbn, function (err) { + query('DROP DATABASE IF EXISTS ' + dbn, function(err) { var q = 'CREATE DATABASE ' + dbn; if (cs) { q += ' CHARACTER SET ' + cs; @@ -423,46 +421,40 @@ var blankDatabase = function (db, cb) { if (co) { q += ' COLLATE ' + co; } - query(q, function (err) { + query(q, function(err) { query('USE ' + dbn, cb); }); }); }; -getFields = function (model, cb) { - query('SHOW FIELDS FROM ' + model, function (err, res) { +var getFields = function(model, cb) { + query('SHOW FIELDS FROM ' + model, function(err, res) { if (err) { cb(err); } else { var fields = {}; - res.forEach(function (field) { + res.forEach(function(field) { fields[field.Field] = field; }); cb(err, fields); } }); -} +}; -getIndexes = function (model, cb) { - query('SHOW INDEXES FROM ' + model, function (err, res) { +var getIndexes = function(model, cb) { + query('SHOW INDEXES FROM ' + model, function(err, res) { if (err) { console.log(err); cb(err); } else { var indexes = {}; // Note: this will only show the first key of compound keys - res.forEach(function (index) { + res.forEach(function(index) { if (parseInt(index.Seq_in_index, 10) == 1) { - indexes[index.Key_name] = index + indexes[index.Key_name] = index; } }); cb(err, indexes); } }); }; - - - - - - diff --git a/test/mysql.autoupdate.test.js b/test/mysql.autoupdate.test.js index 8ad1394..aa933a5 100644 --- a/test/mysql.autoupdate.test.js +++ b/test/mysql.autoupdate.test.js @@ -3,97 +3,96 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; var assert = require('assert'); require('./init'); var ds; -before(function () { +before(function() { ds = getDataSource(); }); -describe('MySQL connector', function () { - it('should auto migrate/update tables', function (done) { - +describe('MySQL connector', function() { + it('should auto migrate/update tables', function(done) { var schema_v1 = - { - "name": "CustomerTest", - "options": { - "idInjection": false, - "mysql": { - "schema": "myapp_test", - "table": "customer_test" - } - }, - "properties": { - "id": { - "type": "String", - "length": 20, - "id": 1 + { + 'name': 'CustomerTest', + 'options': { + 'idInjection': false, + 'mysql': { + 'schema': 'myapp_test', + 'table': 'customer_test', + }, }, - "name": { - "type": "String", - "required": false, - "length": 40 + 'properties': { + 'id': { + 'type': 'String', + 'length': 20, + 'id': 1, + }, + 'name': { + 'type': 'String', + 'required': false, + 'length': 40, + }, + 'email': { + 'type': 'String', + 'required': true, + 'length': 40, + }, + 'age': { + 'type': 'Number', + 'required': false, + }, }, - "email": { - "type": "String", - "required": true, - "length": 40 - }, - "age": { - "type": "Number", - "required": false - } - } - } + }; var schema_v2 = - { - "name": "CustomerTest", - "options": { - "idInjection": false, - "mysql": { - "schema": "myapp_test", - "table": "customer_test" - } - }, - "properties": { - "id": { - "type": "String", - "length": 20, - "id": 1 + { + 'name': 'CustomerTest', + 'options': { + 'idInjection': false, + 'mysql': { + 'schema': 'myapp_test', + 'table': 'customer_test', + }, }, - "email": { - "type": "String", - "required": false, - "length": 60, - "mysql": { - "columnName": "email", - "dataType": "varchar", - "dataLength": 60, - "nullable": "YES" - } + 'properties': { + 'id': { + 'type': 'String', + 'length': 20, + 'id': 1, + }, + 'email': { + 'type': 'String', + 'required': false, + 'length': 60, + 'mysql': { + 'columnName': 'email', + 'dataType': 'varchar', + 'dataLength': 60, + 'nullable': 'YES', + }, + }, + 'firstName': { + 'type': 'String', + 'required': false, + 'length': 40, + }, + 'lastName': { + 'type': 'String', + 'required': false, + 'length': 40, + }, }, - "firstName": { - "type": "String", - "required": false, - "length": 40 - }, - "lastName": { - "type": "String", - "required": false, - "length": 40 - } - } - } + }; ds.createModel(schema_v1.name, schema_v1.properties, schema_v1.options); - ds.automigrate(function () { - - ds.discoverModelProperties('customer_test', function (err, props) { + ds.automigrate(function() { + ds.discoverModelProperties('customer_test', function(err, props) { assert.equal(props.length, 4); - var names = props.map(function (p) { + var names = props.map(function(p) { return p.columnName; }); assert.equal(props[0].nullable, 'N'); @@ -107,10 +106,10 @@ describe('MySQL connector', function () { ds.createModel(schema_v2.name, schema_v2.properties, schema_v2.options); - ds.autoupdate(function (err, result) { - ds.discoverModelProperties('customer_test', function (err, props) { + ds.autoupdate(function(err, result) { + ds.discoverModelProperties('customer_test', function(err, props) { assert.equal(props.length, 4); - var names = props.map(function (p) { + var names = props.map(function(p) { return p.columnName; }); assert.equal(names[0], 'id'); @@ -137,6 +136,4 @@ describe('MySQL connector', function () { done(); }); }); - }); - diff --git a/test/mysql.discover.test.js b/test/mysql.discover.test.js index ea80743..9e83723 100644 --- a/test/mysql.discover.test.js +++ b/test/mysql.discover.test.js @@ -3,6 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; process.env.NODE_ENV = 'test'; require('should'); @@ -10,14 +11,14 @@ var assert = require('assert'); var DataSource = require('loopback-datasource-juggler').DataSource; var db, config; -before(function () { +before(function() { require('./init'); config = getConfig(); config.database = 'STRONGLOOP'; db = new DataSource(require('../'), config); }); -describe('discoverModels', function () { +describe('discoverModels', function() { describe('Discover database schemas', function() { it('should return an array of db schemas', function(done) { db.connector.discoverDatabaseSchemas(function(err, schemas) { @@ -29,19 +30,18 @@ describe('discoverModels', function () { }); }); - describe('Discover models including views', function () { - it('should return an array of tables and views', function (done) { - + describe('Discover models including views', function() { + it('should return an array of tables and views', function(done) { db.discoverModelDefinitions({ views: true, - limit: 3 - }, function (err, models) { + limit: 3, + }, function(err, models) { if (err) { console.error(err); done(err); } else { var views = false; - models.forEach(function (m) { + models.forEach(function(m) { // console.dir(m); if (m.type === 'view') { views = true; @@ -54,18 +54,17 @@ describe('discoverModels', function () { }); }); - describe('Discover current user\'s tables', function () { - it('should return an array of tables for the current user', function (done) { - + describe('Discover current user\'s tables', function() { + it('should return an array of tables for the current user', function(done) { db.discoverModelDefinitions({ - limit: 3 - }, function (err, models) { + limit: 3, + }, function(err, models) { if (err) { console.error(err); done(err); } else { var views = false; - models.forEach(function (m) { + models.forEach(function(m) { assert.equal(m.owner, config.username); }); done(null, models); @@ -74,20 +73,19 @@ describe('discoverModels', function () { }); }); - describe('Discover models excluding views', function () { + describe('Discover models excluding views', function() { // TODO: this test assumes the current user owns the tables - it.skip('should return an array of only tables', function (done) { - + it.skip('should return an array of only tables', function(done) { db.discoverModelDefinitions({ views: false, - limit: 3 - }, function (err, models) { + limit: 3, + }, function(err, models) { if (err) { console.error(err); done(err); } else { var views = false; - models.forEach(function (m) { + models.forEach(function(m) { // console.dir(m); if (m.type === 'view') { views = true; @@ -102,19 +100,18 @@ describe('discoverModels', function () { }); }); -describe('Discover models including other users', function () { - it('should return an array of all tables and views', function (done) { - +describe('Discover models including other users', function() { + it('should return an array of all tables and views', function(done) { db.discoverModelDefinitions({ all: true, - limit: 3 - }, function (err, models) { + limit: 3, + }, function(err, models) { if (err) { console.error(err); done(err); } else { var others = false; - models.forEach(function (m) { + models.forEach(function(m) { // console.dir(m); if (m.owner !== 'STRONGLOOP') { others = true; @@ -127,15 +124,15 @@ describe('Discover models including other users', function () { }); }); -describe('Discover model properties', function () { - describe('Discover a named model', function () { - it('should return an array of columns for product', function (done) { - db.discoverModelProperties('product', function (err, models) { +describe('Discover model properties', function() { + describe('Discover a named model', function() { + it('should return an array of columns for product', function(done) { + db.discoverModelProperties('product', function(err, models) { if (err) { console.error(err); done(err); } else { - models.forEach(function (m) { + models.forEach(function(m) { // console.dir(m); assert(m.tableName === 'product'); }); @@ -144,17 +141,16 @@ describe('Discover model properties', function () { }); }); }); - }); -describe('Discover model primary keys', function () { - it('should return an array of primary keys for product', function (done) { - db.discoverPrimaryKeys('product', function (err, models) { +describe('Discover model primary keys', function() { + it('should return an array of primary keys for product', function(done) { + db.discoverPrimaryKeys('product', function(err, models) { if (err) { console.error(err); done(err); } else { - models.forEach(function (m) { + models.forEach(function(m) { // console.dir(m); assert(m.tableName === 'product'); }); @@ -163,13 +159,13 @@ describe('Discover model primary keys', function () { }); }); - it('should return an array of primary keys for STRONGLOOP.PRODUCT', function (done) { - db.discoverPrimaryKeys('product', {owner: 'STRONGLOOP'}, function (err, models) { + it('should return an array of primary keys for STRONGLOOP.PRODUCT', function(done) { + db.discoverPrimaryKeys('product', {owner: 'STRONGLOOP'}, function(err, models) { if (err) { console.error(err); done(err); } else { - models.forEach(function (m) { + models.forEach(function(m) { // console.dir(m); assert(m.tableName === 'product'); }); @@ -179,14 +175,14 @@ describe('Discover model primary keys', function () { }); }); -describe('Discover model foreign keys', function () { - it('should return an array of foreign keys for INVENTORY', function (done) { - db.discoverForeignKeys('INVENTORY', function (err, models) { +describe('Discover model foreign keys', function() { + it('should return an array of foreign keys for INVENTORY', function(done) { + db.discoverForeignKeys('INVENTORY', function(err, models) { if (err) { console.error(err); done(err); } else { - models.forEach(function (m) { + models.forEach(function(m) { // console.dir(m); assert(m.fkTableName === 'INVENTORY'); }); @@ -194,13 +190,13 @@ describe('Discover model foreign keys', function () { } }); }); - it('should return an array of foreign keys for STRONGLOOP.INVENTORY', function (done) { - db.discoverForeignKeys('INVENTORY', {owner: 'STRONGLOOP'}, function (err, models) { + it('should return an array of foreign keys for STRONGLOOP.INVENTORY', function(done) { + db.discoverForeignKeys('INVENTORY', {owner: 'STRONGLOOP'}, function(err, models) { if (err) { console.error(err); done(err); } else { - models.forEach(function (m) { + models.forEach(function(m) { // console.dir(m); assert(m.fkTableName === 'INVENTORY'); }); @@ -210,15 +206,15 @@ describe('Discover model foreign keys', function () { }); }); -describe('Discover LDL schema from a table', function () { +describe('Discover LDL schema from a table', function() { var schema; - before(function (done) { - db.discoverSchema('INVENTORY', {owner: 'STRONGLOOP'}, function (err, schema_) { + before(function(done) { + db.discoverSchema('INVENTORY', {owner: 'STRONGLOOP'}, function(err, schema_) { schema = schema_; done(err); }); }); - it('should return an LDL schema for INVENTORY', function () { + it('should return an LDL schema for INVENTORY', function() { var productId = 'productId' in schema.properties ? 'productId' : 'productid'; var locationId = 'locationId' in schema.properties ? 'locationId' : 'locationid'; console.error('schema:', schema); @@ -241,15 +237,16 @@ describe('Discover LDL schema from a table', function () { }); }); -describe('Discover and build models', function () { +describe('Discover and build models', function() { var models; - before(function (done) { - db.discoverAndBuildModels('INVENTORY', {owner: 'STRONGLOOP', visited: {}, associations: true}, function (err, models_) { - models = models_; - done(err); - }); + before(function(done) { + db.discoverAndBuildModels('INVENTORY', {owner: 'STRONGLOOP', visited: {}, associations: true}, + function(err, models_) { + models = models_; + done(err); + }); }); - it('should discover and build models', function () { + it('should discover and build models', function() { assert(models.Inventory, 'Inventory model should be discovered and built'); var schema = models.Inventory.definition; var productId = 'productId' in schema.properties ? 'productId' : 'productid'; @@ -267,9 +264,9 @@ describe('Discover and build models', function () { assert(schema.properties.total); assert.strictEqual(schema.properties.total.type, Number); }); - it('should be able to find an instance', function (done) { + it('should be able to find an instance', function(done) { assert(models.Inventory, 'Inventory model must exist'); - models.Inventory.findOne(function (err, inv) { + models.Inventory.findOne(function(err, inv) { assert(!err, 'error should not be reported'); done(); }); diff --git a/test/mysql.test.js b/test/mysql.test.js index 10a8bc7..beefe33 100644 --- a/test/mysql.test.js +++ b/test/mysql.test.js @@ -3,6 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; var should = require('./init.js'); var Post, PostWithStringId, PostWithUniqueTitle, db; @@ -20,51 +21,49 @@ ObjectID.prototype.toJSON = function() { return this.id1 + this.id2; }; -describe('mysql', function () { - - before(function (done) { +describe('mysql', function() { + before(function(done) { db = getDataSource(); Post = db.define('PostWithDefaultId', { - title: { type: String, length: 255, index: true }, - content: { type: String }, + title: {type: String, length: 255, index: true}, + content: {type: String}, comments: [String], history: Object, stars: Number, - userId: ObjectID + userId: ObjectID, }); PostWithStringId = db.define('PostWithStringId', { id: {type: String, id: true}, - title: { type: String, length: 255, index: true }, - content: { type: String } + title: {type: String, length: 255, index: true}, + content: {type: String}, }); PostWithUniqueTitle = db.define('PostWithUniqueTitle', { - title: { type: String, length: 255, index: {unique: true} }, - content: { type: String } + title: {type: String, length: 255, index: {unique: true}}, + content: {type: String}, }); - db.automigrate(['PostWithDefaultId', 'PostWithStringId', 'PostWithUniqueTitle'], function (err) { + db.automigrate(['PostWithDefaultId', 'PostWithStringId', 'PostWithUniqueTitle'], function(err) { should.not.exist(err); done(err); }); }); - beforeEach(function (done) { - Post.destroyAll(function () { - PostWithStringId.destroyAll(function () { - PostWithUniqueTitle.destroyAll(function () { + beforeEach(function(done) { + Post.destroyAll(function() { + PostWithStringId.destroyAll(function() { + PostWithUniqueTitle.destroyAll(function() { done(); }); }); }); }); - it('should allow array or object', function (done) { + it('should allow array or object', function(done) { Post.create({title: 'a', content: 'AAA', comments: ['1', '2'], history: {a: 1, b: 'b'}}, function(err, post) { - should.not.exist(err); Post.findById(post.id, function(err, p) { @@ -84,7 +83,6 @@ describe('mysql', function () { var uid = new ObjectID('123'); Post.create({title: 'a', content: 'AAA', userId: uid}, function(err, post) { - should.not.exist(err); Post.findById(post.id, function(err, p) { @@ -98,15 +96,15 @@ describe('mysql', function () { }); }); - it('updateOrCreate should update the instance', function (done) { - Post.create({title: 'a', content: 'AAA'}, function (err, post) { + it('updateOrCreate should update the instance', function(done) { + Post.create({title: 'a', content: 'AAA'}, function(err, post) { post.title = 'b'; - Post.updateOrCreate(post, function (err, p) { + Post.updateOrCreate(post, function(err, p) { should.not.exist(err); p.id.should.be.equal(post.id); p.content.should.be.equal(post.content); - Post.findById(post.id, function (err, p) { + Post.findById(post.id, function(err, p) { p.id.should.be.equal(post.id); p.content.should.be.equal(post.content); @@ -115,19 +113,18 @@ describe('mysql', function () { done(); }); }); - }); }); - it('updateOrCreate should update the instance without removing existing properties', function (done) { - Post.create({title: 'a', content: 'AAA'}, function (err, post) { + it('updateOrCreate should update the instance without removing existing properties', function(done) { + Post.create({title: 'a', content: 'AAA'}, function(err, post) { post = post.toObject(); delete post.title; - Post.updateOrCreate(post, function (err, p) { + Post.updateOrCreate(post, function(err, p) { should.not.exist(err); p.id.should.be.equal(post.id); p.content.should.be.equal(post.content); - Post.findById(post.id, function (err, p) { + Post.findById(post.id, function(err, p) { p.id.should.be.equal(post.id); p.content.should.be.equal(post.content); @@ -136,19 +133,18 @@ describe('mysql', function () { done(); }); }); - }); }); - it('updateOrCreate should create a new instance if it does not exist', function (done) { + it('updateOrCreate should create a new instance if it does not exist', function(done) { var post = {id: 123, title: 'a', content: 'AAA'}; - Post.updateOrCreate(post, function (err, p) { + Post.updateOrCreate(post, function(err, p) { should.not.exist(err); p.title.should.be.equal(post.title); p.content.should.be.equal(post.content); p.id.should.be.equal(post.id); - Post.findById(p.id, function (err, p) { + Post.findById(p.id, function(err, p) { p.id.should.be.equal(post.id); p.content.should.be.equal(post.content); @@ -158,7 +154,6 @@ describe('mysql', function () { done(); }); }); - }); context('replaceOrCreate', function() { @@ -170,13 +165,13 @@ describe('mysql', function () { Post.replaceOrCreate(post, function(err, p) { if (err) return done(err); p.id.should.equal(post.id); - p.title.should.equal('a'); + p.title.should.equal('a'); should.not.exist(p.content); should.not.exist(p._id); Post.findById(post.id, function(err, p) { if (err) return done(err); p.id.should.equal(post.id); - p.title.should.equal('a'); + p.title.should.equal('a'); should.not.exist(post.content); should.not.exist(p._id); done(); @@ -197,7 +192,7 @@ describe('mysql', function () { if (err) return done(err); p.id.should.equal(post.id); should.not.exist(p._id); - p.title.should.equal('b'); + p.title.should.equal('b'); should.not.exist(p.content); should.not.exist(p.comments); Post.findById(post.id, function(err, p) { @@ -233,15 +228,15 @@ describe('mysql', function () { }); }); - it('save should update the instance with the same id', function (done) { - Post.create({title: 'a', content: 'AAA'}, function (err, post) { + it('save should update the instance with the same id', function(done) { + Post.create({title: 'a', content: 'AAA'}, function(err, post) { post.title = 'b'; - post.save(function (err, p) { + post.save(function(err, p) { should.not.exist(err); p.id.should.be.equal(post.id); p.content.should.be.equal(post.content); - Post.findById(post.id, function (err, p) { + Post.findById(post.id, function(err, p) { p.id.should.be.equal(post.id); p.content.should.be.equal(post.content); @@ -250,19 +245,18 @@ describe('mysql', function () { done(); }); }); - }); }); - it('save should update the instance without removing existing properties', function (done) { - Post.create({title: 'a', content: 'AAA'}, function (err, post) { + it('save should update the instance without removing existing properties', function(done) { + Post.create({title: 'a', content: 'AAA'}, function(err, post) { delete post.title; - post.save(function (err, p) { + post.save(function(err, p) { should.not.exist(err); p.id.should.be.equal(post.id); p.content.should.be.equal(post.content); - Post.findById(post.id, function (err, p) { + Post.findById(post.id, function(err, p) { p.id.should.be.equal(post.id); p.content.should.be.equal(post.content); @@ -271,19 +265,18 @@ describe('mysql', function () { done(); }); }); - }); }); - it('save should create a new instance if it does not exist', function (done) { + it('save should create a new instance if it does not exist', function(done) { var post = new Post({id: 123, title: 'a', content: 'AAA'}); - post.save(post, function (err, p) { + post.save(post, function(err, p) { should.not.exist(err); p.title.should.be.equal(post.title); p.content.should.be.equal(post.content); p.id.should.be.equal(post.id); - Post.findById(p.id, function (err, p) { + Post.findById(p.id, function(err, p) { should.not.exist(err); p.id.should.be.equal(post.id); @@ -294,13 +287,12 @@ describe('mysql', function () { done(); }); }); - }); - it('all return should honor filter.fields', function (done) { - var post = new Post({title: 'b', content: 'BBB'}) - post.save(function (err, post) { - Post.all({fields: ['title'], where: {title: 'b'}}, function (err, posts) { + it('all return should honor filter.fields', function(done) { + var post = new Post({title: 'b', content: 'BBB'}); + post.save(function(err, post) { + Post.all({fields: ['title'], where: {title: 'b'}}, function(err, posts) { should.not.exist(err); posts.should.have.lengthOf(1); post = posts[0]; @@ -310,25 +302,24 @@ describe('mysql', function () { done(); }); - }); }); it('find should order by id if the order is not set for the query filter', - function (done) { - PostWithStringId.create({id: '2', title: 'c', content: 'CCC'}, function (err, post) { - PostWithStringId.create({id: '1', title: 'd', content: 'DDD'}, function (err, post) { - PostWithStringId.find(function (err, posts) { + function(done) { + PostWithStringId.create({id: '2', title: 'c', content: 'CCC'}, function(err, post) { + PostWithStringId.create({id: '1', title: 'd', content: 'DDD'}, function(err, post) { + PostWithStringId.find(function(err, posts) { should.not.exist(err); posts.length.should.be.equal(2); posts[0].id.should.be.equal('1'); - PostWithStringId.find({limit: 1, offset: 0}, function (err, posts) { + PostWithStringId.find({limit: 1, offset: 0}, function(err, posts) { should.not.exist(err); posts.length.should.be.equal(1); posts[0].id.should.be.equal('1'); - PostWithStringId.find({limit: 1, offset: 1}, function (err, posts) { + PostWithStringId.find({limit: 1, offset: 1}, function(err, posts) { should.not.exist(err); posts.length.should.be.equal(1); posts[0].id.should.be.equal('2'); @@ -340,9 +331,9 @@ describe('mysql', function () { }); }); - it('should allow to find using like', function (done) { - Post.create({title: 'My Post', content: 'Hello'}, function (err, post) { - Post.find({where: {title: {like: 'M%st'}}}, function (err, posts) { + it('should allow to find using like', function(done) { + Post.create({title: 'My Post', content: 'Hello'}, function(err, post) { + Post.find({where: {title: {like: 'M%st'}}}, function(err, posts) { should.not.exist(err); posts.should.have.property('length', 1); done(); @@ -350,9 +341,9 @@ describe('mysql', function () { }); }); - it('should support like for no match', function (done) { - Post.create({title: 'My Post', content: 'Hello'}, function (err, post) { - Post.find({where: {title: {like: 'M%XY'}}}, function (err, posts) { + it('should support like for no match', function(done) { + Post.create({title: 'My Post', content: 'Hello'}, function(err, post) { + Post.find({where: {title: {like: 'M%XY'}}}, function(err, posts) { should.not.exist(err); posts.should.have.property('length', 0); done(); @@ -360,9 +351,9 @@ describe('mysql', function () { }); }); - it('should allow to find using nlike', function (done) { - Post.create({title: 'My Post', content: 'Hello'}, function (err, post) { - Post.find({where: {title: {nlike: 'M%st'}}}, function (err, posts) { + it('should allow to find using nlike', function(done) { + Post.create({title: 'My Post', content: 'Hello'}, function(err, post) { + Post.find({where: {title: {nlike: 'M%st'}}}, function(err, posts) { should.not.exist(err); posts.should.have.property('length', 0); done(); @@ -370,9 +361,9 @@ describe('mysql', function () { }); }); - it('should support nlike for no match', function (done) { - Post.create({title: 'My Post', content: 'Hello'}, function (err, post) { - Post.find({where: {title: {nlike: 'M%XY'}}}, function (err, posts) { + it('should support nlike for no match', function(done) { + Post.create({title: 'My Post', content: 'Hello'}, function(err, post) { + Post.find({where: {title: {nlike: 'M%XY'}}}, function(err, posts) { should.not.exist(err); posts.should.have.property('length', 1); done(); @@ -380,12 +371,12 @@ describe('mysql', function () { }); }); - it('should support "and" operator that is satisfied', function (done) { - Post.create({title: 'My Post', content: 'Hello'}, function (err, post) { + it('should support "and" operator that is satisfied', function(done) { + Post.create({title: 'My Post', content: 'Hello'}, function(err, post) { Post.find({where: {and: [ {title: 'My Post'}, - {content: 'Hello'} - ]}}, function (err, posts) { + {content: 'Hello'}, + ]}}, function(err, posts) { should.not.exist(err); posts.should.have.property('length', 1); done(); @@ -393,12 +384,12 @@ describe('mysql', function () { }); }); - it('should support "and" operator that is not satisfied', function (done) { - Post.create({title: 'My Post', content: 'Hello'}, function (err, post) { + it('should support "and" operator that is not satisfied', function(done) { + Post.create({title: 'My Post', content: 'Hello'}, function(err, post) { Post.find({where: {and: [ {title: 'My Post'}, - {content: 'Hello1'} - ]}}, function (err, posts) { + {content: 'Hello1'}, + ]}}, function(err, posts) { should.not.exist(err); posts.should.have.property('length', 0); done(); @@ -406,12 +397,12 @@ describe('mysql', function () { }); }); - it('should support "or" that is satisfied', function (done) { - Post.create({title: 'My Post', content: 'Hello'}, function (err, post) { + it('should support "or" that is satisfied', function(done) { + Post.create({title: 'My Post', content: 'Hello'}, function(err, post) { Post.find({where: {or: [ {title: 'My Post'}, - {content: 'Hello1'} - ]}}, function (err, posts) { + {content: 'Hello1'}, + ]}}, function(err, posts) { should.not.exist(err); posts.should.have.property('length', 1); done(); @@ -419,12 +410,12 @@ describe('mysql', function () { }); }); - it('should support "or" operator that is not satisfied', function (done) { - Post.create({title: 'My Post', content: 'Hello'}, function (err, post) { + it('should support "or" operator that is not satisfied', function(done) { + Post.create({title: 'My Post', content: 'Hello'}, function(err, post) { Post.find({where: {or: [ {title: 'My Post1'}, - {content: 'Hello1'} - ]}}, function (err, posts) { + {content: 'Hello1'}, + ]}}, function(err, posts) { should.not.exist(err); posts.should.have.property('length', 0); done(); @@ -433,18 +424,18 @@ describe('mysql', function () { }); // The where object should be parsed by the connector - it('should support where for count', function (done) { - Post.create({title: 'My Post', content: 'Hello'}, function (err, post) { + it('should support where for count', function(done) { + Post.create({title: 'My Post', content: 'Hello'}, function(err, post) { Post.count({and: [ {title: 'My Post'}, - {content: 'Hello'} - ]}, function (err, count) { + {content: 'Hello'}, + ]}, function(err, count) { should.not.exist(err); count.should.be.equal(1); Post.count({and: [ {title: 'My Post1'}, - {content: 'Hello'} - ]}, function (err, count) { + {content: 'Hello'}, + ]}, function(err, count) { should.not.exist(err); count.should.be.equal(0); done(); @@ -454,15 +445,15 @@ describe('mysql', function () { }); // The where object should be parsed by the connector - it('should support where for destroyAll', function (done) { - Post.create({title: 'My Post1', content: 'Hello'}, function (err, post) { - Post.create({title: 'My Post2', content: 'Hello'}, function (err, post) { + it('should support where for destroyAll', function(done) { + Post.create({title: 'My Post1', content: 'Hello'}, function(err, post) { + Post.create({title: 'My Post2', content: 'Hello'}, function(err, post) { Post.destroyAll({and: [ {title: 'My Post1'}, - {content: 'Hello'} - ]}, function (err) { + {content: 'Hello'}, + ]}, function(err) { should.not.exist(err); - Post.count(function (err, count) { + Post.count(function(err, count) { should.not.exist(err); count.should.be.equal(1); done(); @@ -472,13 +463,13 @@ describe('mysql', function () { }); }); - it('should not allow SQL injection for inq operator', function (done) { + it('should not allow SQL injection for inq operator', function(done) { Post.create({title: 'My Post1', content: 'Hello', stars: 5}, - function (err, post) { + function(err, post) { Post.create({title: 'My Post2', content: 'Hello', stars: 20}, - function (err, post) { + function(err, post) { Post.find({where: {title: {inq: ['SELECT title from PostWithDefaultId']}}}, - function (err, posts) { + function(err, posts) { should.not.exist(err); posts.should.have.property('length', 0); done(); @@ -487,13 +478,13 @@ describe('mysql', function () { }); }); - it('should not allow SQL injection for lt operator', function (done) { + it('should not allow SQL injection for lt operator', function(done) { Post.create({title: 'My Post1', content: 'Hello', stars: 5}, - function (err, post) { + function(err, post) { Post.create({title: 'My Post2', content: 'Hello', stars: 20}, - function (err, post) { + function(err, post) { Post.find({where: {stars: {lt: 'SELECT title from PostWithDefaultId'}}}, - function (err, posts) { + function(err, posts) { should.not.exist(err); posts.should.have.property('length', 0); done(); @@ -502,13 +493,13 @@ describe('mysql', function () { }); }); - it('should not allow SQL injection for nin operator', function (done) { + it('should not allow SQL injection for nin operator', function(done) { Post.create({title: 'My Post1', content: 'Hello', stars: 5}, - function (err, post) { + function(err, post) { Post.create({title: 'My Post2', content: 'Hello', stars: 20}, - function (err, post) { + function(err, post) { Post.find({where: {title: {nin: ['SELECT title from PostWithDefaultId']}}}, - function (err, posts) { + function(err, posts) { should.not.exist(err); posts.should.have.property('length', 2); done(); @@ -517,14 +508,13 @@ describe('mysql', function () { }); }); - - it('should not allow SQL injection for inq operator with number column', function (done) { + it('should not allow SQL injection for inq operator with number column', function(done) { Post.create({title: 'My Post1', content: 'Hello', stars: 5}, - function (err, post) { + function(err, post) { Post.create({title: 'My Post2', content: 'Hello', stars: 20}, - function (err, post) { + function(err, post) { Post.find({where: {stars: {inq: ['SELECT title from PostWithDefaultId']}}}, - function (err, posts) { + function(err, posts) { should.not.exist(err); posts.should.have.property('length', 0); done(); @@ -533,13 +523,13 @@ describe('mysql', function () { }); }); - it('should not allow SQL injection for inq operator with array value', function (done) { + it('should not allow SQL injection for inq operator with array value', function(done) { Post.create({title: 'My Post1', content: 'Hello', stars: 5}, - function (err, post) { + function(err, post) { Post.create({title: 'My Post2', content: 'Hello', stars: 20}, - function (err, post) { + function(err, post) { Post.find({where: {stars: {inq: [5, 'SELECT title from PostWithDefaultId']}}}, - function (err, posts) { + function(err, posts) { should.not.exist(err); posts.should.have.property('length', 1); done(); @@ -548,13 +538,13 @@ describe('mysql', function () { }); }); - it('should not allow SQL injection for between operator', function (done) { + it('should not allow SQL injection for between operator', function(done) { Post.create({title: 'My Post1', content: 'Hello', stars: 5}, - function (err, post) { + function(err, post) { Post.create({title: 'My Post2', content: 'Hello', stars: 20}, - function (err, post) { + function(err, post) { Post.find({where: {stars: {between: [5, 'SELECT title from PostWithDefaultId']}}}, - function (err, posts) { + function(err, posts) { should.not.exist(err); posts.should.have.property('length', 0); done(); @@ -563,11 +553,11 @@ describe('mysql', function () { }); }); - it('should not allow duplicate titles', function (done) { + it('should not allow duplicate titles', function(done) { var data = {title: 'a', content: 'AAA'}; - PostWithUniqueTitle.create(data, function (err, post) { + PostWithUniqueTitle.create(data, function(err, post) { should.not.exist(err); - PostWithUniqueTitle.create(data, function (err, post) { + PostWithUniqueTitle.create(data, function(err, post) { should.exist(err); done(); }); @@ -581,7 +571,7 @@ describe('mysql', function () { beforeEach(function createTestFixtures(done) { Post.create([ {title: 'a', content: 'AAA'}, - {title: 'b', content: 'BBB'} + {title: 'b', content: 'BBB'}, ], done); }); after(function deleteTestFixtures(done) { @@ -619,27 +609,27 @@ describe('mysql', function () { it('should print a warning when the ignore flag is set', function(done) { - Post.find({where: {content: {regexp: '^a/i'}}}, function(err, posts) { - console.warn.calledOnce.should.be.ok; - done(); - }); - }); + Post.find({where: {content: {regexp: '^a/i'}}}, function(err, posts) { + console.warn.calledOnce.should.be.ok; + done(); + }); + }); it('should print a warning when the global flag is set', function(done) { - Post.find({where: {content: {regexp: '^a/g'}}}, function(err, posts) { - console.warn.calledOnce.should.be.ok; - done(); - }); - }); + Post.find({where: {content: {regexp: '^a/g'}}}, function(err, posts) { + console.warn.calledOnce.should.be.ok; + done(); + }); + }); it('should print a warning when the multiline flag is set', function(done) { - Post.find({where: {content: {regexp: '^a/m'}}}, function(err, posts) { - console.warn.calledOnce.should.be.ok; - done(); - }); - }); + Post.find({where: {content: {regexp: '^a/m'}}}, function(err, posts) { + console.warn.calledOnce.should.be.ok; + done(); + }); + }); }); }); @@ -674,27 +664,27 @@ describe('mysql', function () { it('should print a warning when the ignore flag is set', function(done) { - Post.find({where: {content: {regexp: /^a/i}}}, function(err, posts) { - console.warn.calledOnce.should.be.ok; - done(); - }); - }); + Post.find({where: {content: {regexp: /^a/i}}}, function(err, posts) { + console.warn.calledOnce.should.be.ok; + done(); + }); + }); it('should print a warning when the global flag is set', function(done) { - Post.find({where: {content: {regexp: /^a/g}}}, function(err, posts) { - console.warn.calledOnce.should.be.ok; - done(); - }); - }); + Post.find({where: {content: {regexp: /^a/g}}}, function(err, posts) { + console.warn.calledOnce.should.be.ok; + done(); + }); + }); it('should print a warning when the multiline flag is set', function(done) { - Post.find({where: {content: {regexp: /^a/m}}}, function(err, posts) { - console.warn.calledOnce.should.be.ok; - done(); - }); - }); + Post.find({where: {content: {regexp: /^a/m}}}, function(err, posts) { + console.warn.calledOnce.should.be.ok; + done(); + }); + }); }); }); @@ -710,11 +700,11 @@ describe('mysql', function () { it('should work', function(done) { Post.find({where: {content: {regexp: new RegExp(/^A/)}}}, function(err, posts) { - should.not.exist(err); - posts.length.should.equal(1); - posts[0].content.should.equal('AAA'); - done(); - }); + should.not.exist(err); + posts.length.should.equal(1); + posts[0].content.should.equal('AAA'); + done(); + }); }); }); @@ -722,45 +712,45 @@ describe('mysql', function () { it('should work', function(done) { Post.find({where: {content: {regexp: new RegExp(/^a/i)}}}, function(err, posts) { - should.not.exist(err); - posts.length.should.equal(1); - posts[0].content.should.equal('AAA'); - done(); - }); + should.not.exist(err); + posts.length.should.equal(1); + posts[0].content.should.equal('AAA'); + done(); + }); }); it('should print a warning when the ignore flag is set', function(done) { - Post.find({where: {content: {regexp: new RegExp(/^a/i)}}}, + Post.find({where: {content: {regexp: new RegExp(/^a/i)}}}, function(err, posts) { - console.warn.calledOnce.should.be.ok; - done(); - }); - }); + console.warn.calledOnce.should.be.ok; + done(); + }); + }); it('should print a warning when the global flag is set', function(done) { - Post.find({where: {content: {regexp: new RegExp(/^a/g)}}}, + Post.find({where: {content: {regexp: new RegExp(/^a/g)}}}, function(err, posts) { - console.warn.calledOnce.should.be.ok; - done(); - }); - }); + console.warn.calledOnce.should.be.ok; + done(); + }); + }); it('should print a warning when the multiline flag is set', function(done) { - Post.find({where: {content: {regexp: new RegExp(/^a/m)}}}, + Post.find({where: {content: {regexp: new RegExp(/^a/m)}}}, function(err, posts) { - console.warn.calledOnce.should.be.ok; - done(); - }); - }); + console.warn.calledOnce.should.be.ok; + done(); + }); + }); }); }); }); - after(function (done) { - Post.destroyAll(function () { - PostWithStringId.destroyAll(function () { + after(function(done) { + Post.destroyAll(function() { + PostWithStringId.destroyAll(function() { PostWithUniqueTitle.destroyAll(done); }); }); diff --git a/test/persistence-hooks.test.js b/test/persistence-hooks.test.js index de97f6f..ddcb28b 100644 --- a/test/persistence-hooks.test.js +++ b/test/persistence-hooks.test.js @@ -3,9 +3,10 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; var should = require('./init'); var suite = require('loopback-datasource-juggler/test/persistence-hooks.suite.js'); suite(global.getDataSource(), should, { - replaceOrCreateReportsNewInstance: true + replaceOrCreateReportsNewInstance: true, }); diff --git a/test/transaction.promise.test.js b/test/transaction.promise.test.js index ffecb68..127cbe4 100644 --- a/test/transaction.promise.test.js +++ b/test/transaction.promise.test.js @@ -3,6 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; if (typeof Promise === 'undefined') { global.Promise = require('bluebird'); } @@ -13,17 +14,16 @@ require('should'); var db, Post, Review; describe('transactions with promise', function() { - before(function(done) { db = getDataSource({collation: 'utf8_general_ci', createDatabase: true}); db.once('connected', function() { Post = db.define('PostTX', { title: {type: String, length: 255, index: true}, - content: {type: String} + content: {type: String}, }, {mysql: {engine: 'INNODB'}}); Review = db.define('ReviewTX', { author: String, - content: {type: String} + content: {type: String}, }, {mysql: {engine: 'INNODB'}}); Post.hasMany(Review, {as: 'reviews', foreignKey: 'postId'}); db.automigrate(['PostTX', 'ReviewTX'], done); @@ -38,7 +38,7 @@ describe('transactions with promise', function() { // Transaction.begin(db.connector, Transaction.READ_COMMITTED, var promise = Post.beginTransaction({ isolationLevel: Transaction.READ_COMMITTED, - timeout: timeout + timeout: timeout, }); promise.then(function(tx) { (typeof tx.id).should.be.eql('string'); @@ -65,7 +65,7 @@ describe('transactions with promise', function() { function(p) { p.reviews.create({ author: 'John', - content: 'Review for ' + p.title + content: 'Review for ' + p.title, }, {transaction: currentTx}).then( function(c) { done(null, c); @@ -102,7 +102,6 @@ describe('transactions with promise', function() { } describe('commit', function() { - var post = {title: 't1', content: 'c1'}; before(createPostInTx(post)); @@ -129,7 +128,6 @@ describe('transactions with promise', function() { }); describe('rollback', function() { - var post = {title: 't2', content: 'c2'}; before(createPostInTx(post)); @@ -156,7 +154,6 @@ describe('transactions with promise', function() { }); describe('timeout', function() { - var post = {title: 't3', content: 'c3'}; before(createPostInTx(post, 500)); @@ -176,7 +173,5 @@ describe('transactions with promise', function() { done(); }); }); - }); }); - diff --git a/test/transaction.test.js b/test/transaction.test.js index c0487ee..b4f9d9b 100644 --- a/test/transaction.test.js +++ b/test/transaction.test.js @@ -3,6 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; var Transaction = require('loopback-datasource-juggler').Transaction; require('./init.js'); require('should'); @@ -10,17 +11,16 @@ require('should'); var db, Post, Review; describe('transactions', function() { - before(function(done) { db = getDataSource({collation: 'utf8_general_ci', createDatabase: true}); db.once('connected', function() { Post = db.define('PostTX', { title: {type: String, length: 255, index: true}, - content: {type: String} + content: {type: String}, }, {mysql: {engine: 'INNODB'}}); Review = db.define('ReviewTX', { author: String, - content: {type: String} + content: {type: String}, }, {mysql: {engine: 'INNODB'}}); Post.hasMany(Review, {as: 'reviews', foreignKey: 'postId'}); db.automigrate(['PostTX', 'ReviewTX'], done); @@ -34,9 +34,9 @@ describe('transactions', function() { return function(done) { // Transaction.begin(db.connector, Transaction.READ_COMMITTED, Post.beginTransaction({ - isolationLevel: Transaction.READ_COMMITTED, - timeout: timeout - }, + isolationLevel: Transaction.READ_COMMITTED, + timeout: timeout, + }, function(err, tx) { if (err) return done(err); (typeof tx.id).should.be.eql('string'); @@ -64,9 +64,9 @@ describe('transactions', function() { done(err); } else { p.reviews.create({ - author: 'John', - content: 'Review for ' + p.title - }, {transaction: tx}, + author: 'John', + content: 'Review for ' + p.title, + }, {transaction: tx}, function(err, c) { done(err); }); @@ -105,7 +105,6 @@ describe('transactions', function() { } describe('commit', function() { - var post = {title: 't1', content: 'c1'}; before(createPostInTx(post)); @@ -132,7 +131,6 @@ describe('transactions', function() { }); describe('rollback', function() { - var post = {title: 't2', content: 'c2'}; before(createPostInTx(post)); @@ -159,7 +157,6 @@ describe('transactions', function() { }); describe('timeout', function() { - var post = {title: 't3', content: 'c3'}; before(createPostInTx(post, 500)); @@ -179,7 +176,5 @@ describe('transactions', function() { done(); }); }); - }); }); -