From 2b20ccdbd7016c616490c1543f82e1d4194ef2fb Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Wed, 21 Aug 2013 21:53:48 -0700 Subject: [PATCH 1/2] Use .loopback rc to load the config --- example/app.js | 16 +-- package.json | 3 +- test/init.js | 15 ++- test/migration.coffee | 256 ------------------------------------ test/mysql.discover.test.js | 13 +- 5 files changed, 22 insertions(+), 281 deletions(-) delete mode 100644 test/migration.coffee diff --git a/example/app.js b/example/app.js index d38ede6..4212828 100644 --- a/example/app.js +++ b/example/app.js @@ -1,13 +1,12 @@ +var config = require('rc')('loopback'); +config = (config.test && config.test.mysql) || {}; + var DataSource = require('loopback-datasource-juggler').DataSource; -var ds = new DataSource(require('../'), { - host: '127.0.0.1', - port: 3306, - database: 'test', - username: 'strongloop', - password: 'password', - debug: false -}); +var config = require('rc')('loopback'); +config = (config.dev && config.dev.mysql) || {}; + +var ds = new DataSource(require('../'), config); function show(err, models) { if (err) { @@ -40,7 +39,6 @@ ds.discoverAndBuildModels('User', {owner: 'test', visited: {}, associations: tru for (var m in models) { models[m].all(show); } - ; }); diff --git a/package.json b/package.json index 7473357..c57cc0d 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "coffee-script": "latest", "should": "~1.2.2", "mocha": "~1.8.2", - "blanket": "latest" + "blanket": "latest", + "rc": "~0.3.0" }, "repository": { "type": "git", diff --git a/test/init.js b/test/init.js index 0320e62..97e3dde 100644 --- a/test/init.js +++ b/test/init.js @@ -1,15 +1,18 @@ module.exports = require('should'); -var Schema = require('loopback-datasource-juggler').Schema; +var DataSource = require('loopback-datasource-juggler').DataSource; + +var config = require('rc')('loopback'); +config = (config.test && config.test.mysql) || {}; global.getConfig = function(options) { var dbConf = { - host: '127.0.0.1', - port: 3306, + host: config.host || 'localhost', + port: config.port || 3306, database: 'myapp_test', - username: 'strongloop', - password: 'password' + username: config.username, + password: config.password }; if (options) { @@ -22,7 +25,7 @@ global.getConfig = function(options) { } global.getSchema = function(options) { - var db = new Schema(require('../'), getConfig(options)); + var db = new DataSource(require('../'), getConfig(options)); return db; }; diff --git a/test/migration.coffee b/test/migration.coffee deleted file mode 100644 index 1e5e25f..0000000 --- a/test/migration.coffee +++ /dev/null @@ -1,256 +0,0 @@ -juggling = require('loopback-datasource-juggler') -Schema = juggling.Schema -Text = Schema.Text - -DBNAME = 'myapp_test' -DBUSER = 'strongloop' -DBPASS = 'password' -DBENGINE = 'mysql' - -dataSource = new Schema __dirname + '/..', database: '', username: DBUSER, password: DBPASS -dataSource.log = (q) -> console.log q - -query = (sql, cb) -> - dataSource.adapter.query sql, cb - -User = dataSource.define 'User', - email: { type: String, null: false, index: true } - name: String - bio: Text - password: String - birthDate: Date - pendingPeriod: Number - createdByAdmin: Boolean -, indexes: - index1: - columns: 'email, createdByAdmin' - -withBlankDatabase = (cb) -> - db = dataSource.settings.database = DBNAME - query 'DROP DATABASE IF EXISTS ' + db, (err) -> - query 'CREATE DATABASE ' + db, (err) -> - query 'USE '+ db, cb - -getFields = (model, cb) -> - query 'SHOW FIELDS FROM ' + model, (err, res) -> - if err - cb err - else - fields = {} - res.forEach (field) -> fields[field.Field] = field - cb err, fields - -getIndexes = (model, cb) -> - query 'SHOW INDEXES FROM ' + model, (err, res) -> - if err - console.log err - cb err - else - indexes = {} - # Note: this will only show the first key of compound keys - res.forEach (index) -> - indexes[index.Key_name] = index if parseInt(index.Seq_in_index, 10) == 1 - cb err, indexes - -it = (name, testCases) -> - module.exports[name] = testCases - -it 'should run migration', (test) -> - withBlankDatabase (err) -> - dataSource.automigrate -> - getFields 'User', (err, fields) -> - test.deepEqual fields, - id: - Field: 'id' - Type: 'int(11)' - Null: 'NO' - Key: 'PRI' - Default: null - Extra: 'auto_increment' - email: - Field: 'email' - Type: 'varchar(255)' - Null: 'NO' - Key: 'MUL' - Default: null - Extra: '' - name: - Field: 'name' - Type: 'varchar(255)' - Null: 'YES' - Key: '' - Default: null - Extra: '' - bio: - Field: 'bio' - Type: 'text' - Null: 'YES' - Key: '' - Default: null - Extra: '' - password: - Field: 'password' - Type: 'varchar(255)' - Null: 'YES' - Key: '' - Default: null - Extra: '' - birthDate: - Field: 'birthDate' - Type: 'datetime' - Null: 'YES' - Key: '' - Default: null - Extra: '' - pendingPeriod: - Field: 'pendingPeriod' - Type: 'int(11)' - Null: 'YES' - Key: '' - Default: null - Extra: '' - createdByAdmin: - Field: 'createdByAdmin' - Type: 'tinyint(1)' - Null: 'YES' - Key: '' - Default: null - Extra: '' - # Once gain, getIdexes truncates multi-key indexes to the first member. Hence index1 is correct. - getIndexes 'User', (err, fields) -> - test.deepEqual fields, - PRIMARY: - Table: 'User' - Non_unique: 0 - Key_name: 'PRIMARY' - Seq_in_index: 1 - Column_name: 'id' - Collation: 'A' - Cardinality: 0 - Sub_part: null - Packed: null - Null: '' - Index_type: 'BTREE' - Comment: '' - Index_comment: '' - email: - Table: 'User' - Non_unique: 1 - Key_name: 'email' - Seq_in_index: 1 - Column_name: 'email' - Collation: 'A' - Cardinality: 0 - Sub_part: null - Packed: null - Null: '' - Index_type: 'BTREE' - Comment: '' - Index_comment: '' - index1: - Table: 'User' - Non_unique: 1 - Key_name: 'index1' - Seq_in_index: 1 - Column_name: 'email' - Collation: 'A' - Cardinality: 0 - Sub_part: null - Packed: null - Null: '' - Index_type: 'BTREE' - Comment: '' - Index_comment: '' - test.done() - -it 'should autoupgrade', (test) -> - userExists = (cb) -> - query 'SELECT * FROM User', (err, res) -> - cb(not err and res[0].email == 'test@example.com') - - User.create email: 'test@example.com', (err, user) -> - test.ok not err - userExists (yep) -> - test.ok yep - User.defineProperty 'email', type: String - User.defineProperty 'name', type: String, limit: 50 - User.defineProperty 'newProperty', type: Number - User.defineProperty 'pendingPeriod', false - dataSource.autoupdate (err) -> - getFields 'User', (err, fields) -> - # change nullable for email - test.equal fields.email.Null, 'YES', 'Email is not null' - # change type of name - test.equal fields.name.Type, 'varchar(50)', 'Name is not varchar(50)' - # add new column - test.ok fields.newProperty, 'New column was not added' - if fields.newProperty - test.equal fields.newProperty.Type, 'int(11)', 'New column type is not int(11)' - # drop column - test.ok not fields.pendingPeriod, 'drop column' - - # user still exists - userExists (yep) -> - test.ok yep - test.done() - -it 'should check actuality of dataSource', (test) -> - # drop column - User.dataSource.isActual (err, ok) -> - test.ok ok, 'dataSource is actual' - User.defineProperty 'email', false - User.dataSource.isActual (err, ok) -> - test.ok not ok, 'dataSource is not actual' - test.done() - -it 'should add single-column index', (test) -> - User.defineProperty 'email', type: String, index: { kind: 'FULLTEXT', type: 'HASH'} - User.dataSource.autoupdate (err) -> - return console.log(err) if err - getIndexes 'User', (err, ixs) -> - test.ok ixs.email && ixs.email.Column_name == 'email' - console.log(ixs) - test.equal ixs.email.Index_type, 'BTREE', 'default index type' - test.done() - -it 'should change type of single-column index', (test) -> - User.defineProperty 'email', type: String, index: { type: 'BTREE' } - User.dataSource.isActual (err, ok) -> - test.ok ok, 'dataSource is actual' - User.dataSource.autoupdate (err) -> - return console.log(err) if err - getIndexes 'User', (err, ixs) -> - test.ok ixs.email && ixs.email.Column_name == 'email' - test.equal ixs.email.Index_type, 'BTREE' - test.done() - -it 'should remove single-column index', (test) -> - User.defineProperty 'email', type: String, index: false - User.dataSource.autoupdate (err) -> - return console.log(err) if err - getIndexes 'User', (err, ixs) -> - test.ok !ixs.email - test.done() - -it 'should update multi-column index when order of columns changed', (test) -> - User.dataSource.adapter._models.User.settings.indexes.index1.columns = 'createdByAdmin, email' - User.dataSource.isActual (err, ok) -> - test.ok not ok, 'dataSource is not actual' - User.dataSource.autoupdate (err) -> - return console.log(err) if err - getIndexes 'User', (err, ixs) -> - test.equals ixs.index1.Column_name, 'createdByAdmin' - test.done() - - -it 'test', (test) -> - User.defineProperty 'email', type: String, index: true - User.dataSource.autoupdate (err) -> - User.dataSource.autoupdate (err) -> - User.dataSource.autoupdate (err) -> - test.done() - -it 'should disconnect when done', (test) -> - dataSource.disconnect() - test.done() - diff --git a/test/mysql.discover.test.js b/test/mysql.discover.test.js index 515ae1a..f8c4a6d 100644 --- a/test/mysql.discover.test.js +++ b/test/mysql.discover.test.js @@ -2,19 +2,14 @@ process.env.NODE_ENV = 'test'; require('should'); var assert = require('assert'); - -var Schema = require('loopback-datasource-juggler').Schema; +var DataSource = require('loopback-datasource-juggler').DataSource; var db; before(function() { + var config = require('rc')('loopback'); + config = (config.dev && config.dev.mysql) || {}; - db = new Schema(require('../'), { - host: '127.0.0.1', - port: 3306, - database: 'STRONGLOOP', - username: 'strongloop', - password: 'password' - }); + db = new DataSource(require('../'), config); }); From 9276025bc63c7b2ea585b5b113220022aae18915 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 23 Aug 2013 14:31:43 -0700 Subject: [PATCH 2/2] Refine the usage of rc --- example/app.js | 3 +-- test/connection.test.js | 11 ++++------- test/datatypes.test.js | 5 ++--- test/init.js | 9 ++++----- test/mysql.discover.test.js | 5 +---- 5 files changed, 12 insertions(+), 21 deletions(-) diff --git a/example/app.js b/example/app.js index 4212828..fc08c3e 100644 --- a/example/app.js +++ b/example/app.js @@ -3,8 +3,7 @@ config = (config.test && config.test.mysql) || {}; var DataSource = require('loopback-datasource-juggler').DataSource; -var config = require('rc')('loopback'); -config = (config.dev && config.dev.mysql) || {}; +var config = require('rc')('loopback', {dev: {mysql: {}}}).dev.mysql; var ds = new DataSource(require('../'), config); diff --git a/test/connection.test.js b/test/connection.test.js index 3c88656..359aaa4 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -1,17 +1,14 @@ -var should = require('./init.js'); +require('./init.js'); var assert = require('assert'); -var Schema = require('loopback-datasource-juggler').Schema; -var db, settings, adapter, DummyModel, odb; +var db, DummyModel, odb; describe('migrations', function() { - - - + before(function() { require('./init.js'); - odb = getSchema({collation: 'utf8_general_ci'}); + odb = getDataSource({collation: 'utf8_general_ci'}); db = odb; }); diff --git a/test/datatypes.test.js b/test/datatypes.test.js index 8250f15..2a6433d 100644 --- a/test/datatypes.test.js +++ b/test/datatypes.test.js @@ -1,8 +1,7 @@ -var should = require('./init.js'); +require('./init.js'); var assert = require('assert'); -var Schema = require('loopback-datasource-juggler').Schema; -var db, settings, adapter, EnumModel, ANIMAL_ENUM; +var db, EnumModel, ANIMAL_ENUM; describe('MySQL specific datatypes', function() { diff --git a/test/init.js b/test/init.js index 97e3dde..b455117 100644 --- a/test/init.js +++ b/test/init.js @@ -2,8 +2,7 @@ module.exports = require('should'); var DataSource = require('loopback-datasource-juggler').DataSource; -var config = require('rc')('loopback'); -config = (config.test && config.test.mysql) || {}; +var config = require('rc')('loopback', {test: {mysql: {}}}).test.mysql; global.getConfig = function(options) { @@ -17,14 +16,14 @@ global.getConfig = function(options) { if (options) { for (var el in options) { - dbConf[el] = options[el] + dbConf[el] = options[el]; } } return dbConf; -} +}; -global.getSchema = function(options) { +global.getDataSource = global.getSchema = function(options) { var db = new DataSource(require('../'), getConfig(options)); return db; }; diff --git a/test/mysql.discover.test.js b/test/mysql.discover.test.js index f8c4a6d..1766781 100644 --- a/test/mysql.discover.test.js +++ b/test/mysql.discover.test.js @@ -6,11 +6,8 @@ var DataSource = require('loopback-datasource-juggler').DataSource; var db; before(function() { - var config = require('rc')('loopback'); - config = (config.dev && config.dev.mysql) || {}; - + var config = require('rc')('loopback', {dev: {mysql: {}}}).dev.mysql; db = new DataSource(require('../'), config); - }); describe('discoverModels', function() {