From bcee504d777a239da38c984d50f08533ba89ff43 Mon Sep 17 00:00:00 2001 From: dgsan Date: Thu, 13 Jun 2013 09:34:31 -0700 Subject: [PATCH 1/3] Proper tests and support for connection collation/charset. Added semicov as a dev dependency because of how tests are run. --- lib/mysql.js | 12 +++-- package.json | 3 +- test/connection.test.js | 114 ++++++++++++++++++++++++++++++++++++++++ test/imported.test.js | 2 +- test/init.js | 20 +++++-- test/migration.test.js | 14 +++-- 6 files changed, 152 insertions(+), 13 deletions(-) create mode 100644 test/connection.test.js diff --git a/lib/mysql.js b/lib/mysql.js index ad4f2a4..8a2a012 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -9,8 +9,13 @@ exports.initialize = function initializeSchema(schema, callback) { var s = schema.settings; - s.collation = (s.collation || 'utf8mb4_general_ci'); - s.charset = (s.charset || 'utf8mb4'); + if (s.collation) { + s.charset = s.collation.substr(0,s.collation.indexOf('_')); // Charset should be first 'chunk' of collation. + } else { + s.collation = 'utf8mb4_general_ci'; + s.charset = 'utf8mb4'; + } + s.supportBigNumbers = (s.supportBigNumbers || false); s.timezone = (s.timezone || 'local'); @@ -22,7 +27,7 @@ exports.initialize = function initializeSchema(schema, callback) { timezone: s.timezone, debug: s.debug, socketPath: s.socketPath, - charset: s.collation, // Correct by docs despite seeming odd. + charset: s.collation.toUpperCase(), // Correct by docs despite seeming odd. supportBigNumbers: s.supportBigNumbers }); @@ -42,7 +47,6 @@ exports.initialize = function initializeSchema(schema, callback) { var q = 'CREATE DATABASE ' + dbName + ' CHARACTER SET ' + charset + ' COLLATE ' + collation; schema.client.query(q, function (error) { if (!error) { - console.log('go'); schema.client.query('USE ' + s.database, callback); } else { throw error; diff --git a/package.json b/package.json index 3ba4d17..36a1936 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "devDependencies": { "coffee-script": "latest", "should": "~1.2.2", - "mocha": "~1.8.2" + "mocha": "~1.8.2", + "semicov": "*" }, "repository": { "type": "git", diff --git a/test/connection.test.js b/test/connection.test.js new file mode 100644 index 0000000..cacced1 --- /dev/null +++ b/test/connection.test.js @@ -0,0 +1,114 @@ +var should = require('./init.js'); +var assert = require('assert'); +var Schema = require('jugglingdb').Schema; + +var db, settings, adapter, DummyModel, odb; + +describe('migrations', function() { + + + + before(function() { + require('./init.js'); + + odb = getSchema({collation: 'utf8mb4_general_ci'}); + db = odb; + }); + + + it('should use utf8mb4 charset', function(done) { + + var test_set = /utf8mb4/; + var test_collo = /utf8mb4_general_ci/; + var test_set_str = 'utf8mb4'; + var test_set_collo = 'utf8mb4_general_ci'; + charsetTest(test_set, test_collo, test_set_str, test_set_collo, done); + + }); + + it('should disconnect first db', function(done) { + db.client.end(function(){ + odb = getSchema(); + 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.adapter.query('DROP DATABASE IF EXISTS ' + db.settings.database, function(err) { + db.client.end(function(){ + done(); + }); + }); + }); +}); + + + +function charsetTest(test_set, test_collo, test_set_str, test_set_collo, done){ + + query('DROP DATABASE IF EXISTS ' + odb.settings.database, function(err) { + assert.ok(!err); + odb.client.end(function(){ + + db = getSchema({collation: test_set_collo}); + DummyModel = db.define('DummyModel', {string: String}); + db.automigrate(function(){ + var q = 'SELECT DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ' + db.client.escape(db.settings.database) + ' LIMIT 1'; + db.client.query(q, function(err, r) { + assert.ok(!err); + assert.ok(r[0].DEFAULT_COLLATION_NAME.match(test_collo)); + db.client.query('SHOW VARIABLES LIKE "character_set%"', function(err, r){ + assert.ok(!err); + var hit_all = 0; + for (var result in r) { + hit_all += matchResult(r[result], 'character_set_connection', test_set); + hit_all += matchResult(r[result], 'character_set_database', test_set); + hit_all += matchResult(r[result], 'character_set_results', test_set); + hit_all += matchResult(r[result], 'character_set_client', test_set); + } + assert.equal(hit_all, 4); + }); + db.client.query('SHOW VARIABLES LIKE "collation%"', function(err, r){ + assert.ok(!err); + var hit_all = 0; + for (var result in r) { + hit_all += matchResult(r[result], 'collation_connection', test_set); + hit_all += matchResult(r[result], 'collation_database', test_set); + } + assert.equal(hit_all, 2); + done(); + }); + }); + }); + }); + }); + +} + +function matchResult(result, variable_name, match) { + if(result.Variable_name == variable_name){ + assert.ok(result.Value.match(match)); + return 1; + } + return 0; +} + +var query = function (sql, cb) { + odb.adapter.query(sql, cb); +}; + + + + + + diff --git a/test/imported.test.js b/test/imported.test.js index 5a010a9..37f32f3 100644 --- a/test/imported.test.js +++ b/test/imported.test.js @@ -6,6 +6,6 @@ describe('mysql imported features', function() { require('jugglingdb/test/common.batch.js'); require('jugglingdb/test/include.test.js'); - require('./migration.test.js'); + }); diff --git a/test/init.js b/test/init.js index 41de0dc..ecf3c18 100644 --- a/test/init.js +++ b/test/init.js @@ -2,11 +2,23 @@ module.exports = require('should'); var Schema = require('jugglingdb').Schema; -global.getSchema = function() { - var db = new Schema(require('../'), { +global.getConfig = function(options) { + + var dbConf = { database: 'myapp_test', username: 'root' - }); - // db.log = function (a) { console.log(a); }; + }; + + if (options) { + for (var el in options) { + dbConf[el] = options[el] + } + } + + return dbConf; +} + +global.getSchema = function(options) { + var db = new Schema(require('../'), getConfig(options)); return db; }; diff --git a/test/migration.test.js b/test/migration.test.js index f348680..5eeaa62 100644 --- a/test/migration.test.js +++ b/test/migration.test.js @@ -2,7 +2,7 @@ var should = require('./init.js'); var assert = require('assert'); var Schema = require('jugglingdb').Schema; -var db, UserData, StringData, NumberData, DateData; +var db, UserData, StringData, NumberData, DateData; describe('migrations', function() { @@ -331,6 +331,9 @@ describe('migrations', function() { }); function setup(done) { + + require('./init.js'); + db = getSchema(); UserData = db.define('UserData', { @@ -380,8 +383,13 @@ var blankDatabase = function (db, cb) { var cs = db.settings.charset; var co = db.settings.collation; query('DROP DATABASE IF EXISTS ' + dbn, function(err) { - var q = 'CREATE DATABASE ' + dbn + ' CHARACTER SET ' + cs + ' COLLATE ' + co; - // console.log(q); + var q = 'CREATE DATABASE ' + dbn; + if(cs){ + q += ' CHARACTER SET ' + cs; + } + if(co){ + q += ' COLLATE ' + co; + } query(q, function(err) { query('USE '+ dbn, cb); }); From f14679b16fb3ad499ac4a98ee623705f739de6c7 Mon Sep 17 00:00:00 2001 From: dgsan Date: Thu, 13 Jun 2013 09:52:14 -0700 Subject: [PATCH 2/3] Added 2nd global util test function to test makefile. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 478ecc3..302c026 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ ## TESTS TESTER = ./node_modules/.bin/mocha -OPTS = --growl --globals getSchema --timeout 15000 +OPTS = --growl --globals getSchema getConfig --timeout 15000 TESTS = test/*.test.js test: From b75e74fd1f65bbb4c7ee65f77c3dad89acfe1929 Mon Sep 17 00:00:00 2001 From: dgsan Date: Thu, 13 Jun 2013 11:00:06 -0600 Subject: [PATCH 3/3] Update Makefile Didn't do what I thought. Not sure why it had no adverse effect on running tests locally. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 302c026..478ecc3 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ ## TESTS TESTER = ./node_modules/.bin/mocha -OPTS = --growl --globals getSchema getConfig --timeout 15000 +OPTS = --growl --globals getSchema --timeout 15000 TESTS = test/*.test.js test: