Bring up the test cases

This commit is contained in:
Raymond Feng 2013-07-21 17:27:54 -07:00
parent fb375fdcc8
commit 9674a0308e
8 changed files with 60 additions and 45 deletions

View File

@ -1,10 +1,10 @@
## JugglingDB-MySQL [![Build Status](https://travis-ci.org/jugglingdb/mysql-adapter.png)](https://travis-ci.org/jugglingdb/mysql-adapter) ## Loopback MySQL Connector
MySQL adapter for JugglingDB. MySQL connector for Loopback Data.
## Usage ## Usage
To use it you need `jugglingdb@0.2.x`. To use it you need `loopback-data`.
1. Setup dependencies in `package.json`: 1. Setup dependencies in `package.json`:
@ -12,8 +12,8 @@ To use it you need `jugglingdb@0.2.x`.
{ {
... ...
"dependencies": { "dependencies": {
"jugglingdb": "0.2.x", "loopback-data": "latest",
"jugglingdb-mysql": "latest" "loopback-connector-mysql": "latest"
}, },
... ...
} }
@ -22,13 +22,13 @@ To use it you need `jugglingdb@0.2.x`.
2. Use: 2. Use:
```javascript ```javascript
var Schema = require('jugglingdb').Schema; var Schema = require('loopback-data').Schema;
var schema = new Schema('mysql', { var schema = new Schema('mysql', {
database: 'myapp_test', database: 'myapp_test',
username: 'root' username: 'root'
}); });
``` ```
You can optionally pass a few additional parameters supported by `node-mysql`, most particularly `password` and `collation`. `Collation` currently defaults to `utf8mb4_general_ci`. The `collation` value will also be used to derive the connection charset. You can optionally pass a few additional parameters supported by `node-mysql`, most particularly `password` and `collation`. `Collation` currently defaults to `utf8_general_ci`. The `collation` value will also be used to derive the connection charset.
## Running tests ## Running tests
@ -36,7 +36,7 @@ To use it you need `jugglingdb@0.2.x`.
## Using the `dataType` field/column option with MySQL ## Using the `dataType` field/column option with MySQL
The jugglingdb MySQL adapter now supports using the `dataType` column/property attribute to specify what MySQL column type is used for many jugglingdb types. The loopback-data MySQL adapter now supports using the `dataType` column/property attribute to specify what MySQL column type is used for many loopback-data types.
The following type-dataType combinations are supported: The following type-dataType combinations are supported:
* <h4> Number </h4> * <h4> Number </h4>

View File

@ -14,8 +14,8 @@ exports.initialize = function initializeSchema(schema, callback) {
if (s.collation) { if (s.collation) {
s.charset = s.collation.substr(0,s.collation.indexOf('_')); // Charset should be first 'chunk' of collation. s.charset = s.collation.substr(0,s.collation.indexOf('_')); // Charset should be first 'chunk' of collation.
} else { } else {
s.collation = 'utf8mb4_general_ci'; s.collation = 'utf8_general_ci';
s.charset = 'utf8mb4'; s.charset = 'utf8';
} }
s.supportBigNumbers = (s.supportBigNumbers || false); s.supportBigNumbers = (s.supportBigNumbers || false);
@ -246,6 +246,9 @@ MySQL.prototype.escapeName = function (name) {
MySQL.prototype.getColumns = function(model, props){ MySQL.prototype.getColumns = function(model, props){
var cols = this._models[model].properties; var cols = this._models[model].properties;
if(!cols) {
return '*';
}
var self = this; var self = this;
var keys = Object.keys(cols); var keys = Object.keys(cols);
if (Array.isArray(props) && props.length > 0) { if (Array.isArray(props) && props.length > 0) {
@ -469,7 +472,7 @@ MySQL.prototype.alterTable = function (model, actualFields, actualIndexes, done,
// change/add new fields // change/add new fields
propNames.forEach(function (propName) { propNames.forEach(function (propName) {
if (self.id(model, key)) return; if (m.properties[propName] && self.id(model, propName)) return;
var found; var found;
if (actualFields) { if (actualFields) {
actualFields.forEach(function (f) { actualFields.forEach(function (f) {
@ -490,7 +493,7 @@ MySQL.prototype.alterTable = function (model, actualFields, actualIndexes, done,
if (actualFields) { if (actualFields) {
actualFields.forEach(function (f) { actualFields.forEach(function (f) {
var notFound = !~propNames.indexOf(f.Field); var notFound = !~propNames.indexOf(f.Field);
if (mysql.id(model, f.Field)) return; if (m.properties[f.Field] && self.id(model, f.Field)) return;
if (notFound || !m.properties[f.Field]) { if (notFound || !m.properties[f.Field]) {
sql.push('DROP COLUMN `' + f.Field + '`'); sql.push('DROP COLUMN `' + f.Field + '`');
} }
@ -499,7 +502,7 @@ MySQL.prototype.alterTable = function (model, actualFields, actualIndexes, done,
// remove indexes // remove indexes
aiNames.forEach(function (indexName) { aiNames.forEach(function (indexName) {
if (self.id(model, indexName) || indexName === 'PRIMARY') return; if (indexName === 'PRIMARY'|| (m.properties[indexName] && self.id(model, indexName))) return;
if (indexNames.indexOf(indexName) === -1 && !m.properties[indexName] || m.properties[indexName] && !m.properties[indexName].index) { if (indexNames.indexOf(indexName) === -1 && !m.properties[indexName] || m.properties[indexName] && !m.properties[indexName].index) {
sql.push('DROP INDEX `' + indexName + '`'); sql.push('DROP INDEX `' + indexName + '`');
} else { } else {

View File

@ -19,7 +19,7 @@
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/raymondfeng/jugglingdb-mysql.git" "url": "https://github.com/strongloop/loopback-connector-mysql.git"
}, },
"author": "Anatoliy Chakkaev <mail@anatoliy.in>", "author": "Anatoliy Chakkaev <mail@anatoliy.in>",
"maintainers": [ "maintainers": [

View File

@ -1,6 +1,6 @@
var should = require('./init.js'); var should = require('./init.js');
var assert = require('assert'); var assert = require('assert');
var Schema = require('jugglingdb').Schema; var Schema = require('loopback-data').Schema;
var db, settings, adapter, DummyModel, odb; var db, settings, adapter, DummyModel, odb;
@ -11,17 +11,17 @@ describe('migrations', function() {
before(function() { before(function() {
require('./init.js'); require('./init.js');
odb = getSchema({collation: 'utf8mb4_general_ci'}); odb = getSchema({collation: 'utf8_general_ci'});
db = odb; db = odb;
}); });
it('should use utf8mb4 charset', function(done) { it('should use utf8 charset', function(done) {
var test_set = /utf8mb4/; var test_set = /utf8/;
var test_collo = /utf8mb4_general_ci/; var test_collo = /utf8_general_ci/;
var test_set_str = 'utf8mb4'; var test_set_str = 'utf8';
var test_set_collo = 'utf8mb4_general_ci'; var test_set_collo = 'utf8_general_ci';
charsetTest(test_set, test_collo, test_set_str, test_set_collo, done); charsetTest(test_set, test_collo, test_set_str, test_set_collo, done);
}); });

View File

@ -1,6 +1,6 @@
var should = require('./init.js'); var should = require('./init.js');
var assert = require('assert'); var assert = require('assert');
var Schema = require('jugglingdb').Schema; var Schema = require('loopback-data').Schema;
var db, settings, adapter, EnumModel, ANIMAL_ENUM; var db, settings, adapter, EnumModel, ANIMAL_ENUM;
@ -41,7 +41,7 @@ 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) {
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); assert.ok(!err);
EnumModel.find(obj.id, function(err, found){ EnumModel.findById(obj.id, function(err, found){
assert.ok(!err); assert.ok(!err);
assert.equal(found.animal, ''); // MySQL fun. assert.equal(found.animal, ''); // MySQL fun.
assert.equal(found.animal, 0); assert.equal(found.animal, 0);

View File

@ -2,14 +2,28 @@ module.exports = require('should');
var Schema = require('loopback-data').Schema; var Schema = require('loopback-data').Schema;
global.getSchema = function() { global.getConfig = function(options) {
var db = new Schema(require('../'), {
var dbConf = {
host: '127.0.0.1', host: '127.0.0.1',
port: 3306, port: 3306,
database: 'test', database: 'myapp_test',
username: 'strongloop', username: 'strongloop',
password: 'password' password: 'password'
}); };
// 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; return db;
}; };

View File

@ -1,10 +1,10 @@
juggling = require('jugglingdb') juggling = require('loopback-data')
Schema = juggling.Schema Schema = juggling.Schema
Text = Schema.Text Text = Schema.Text
DBNAME = 'myapp_test' DBNAME = 'myapp_test'
DBUSER = 'root' DBUSER = 'strongloop'
DBPASS = '' DBPASS = 'password'
DBENGINE = 'mysql' DBENGINE = 'mysql'
schema = new Schema __dirname + '/..', database: '', username: DBUSER, password: DBPASS schema = new Schema __dirname + '/..', database: '', username: DBUSER, password: DBPASS

View File

@ -1,6 +1,6 @@
var should = require('./init.js'); var should = require('./init.js');
var assert = require('assert'); var assert = require('assert');
var Schema = require('jugglingdb').Schema; var Schema = require('loopback-data').Schema;
var db, UserData, StringData, NumberData, DateData; var db, UserData, StringData, NumberData, DateData;
@ -72,8 +72,9 @@ describe('migrations', function() {
}); });
it('UserData should have correct indexes', function(done) { it('UserData should have correct indexes', function(done) {
// Note: getIdexes truncates multi-key indexes to the first member. Hence index1 is correct. // Note: getIndexes truncates multi-key indexes to the first member. Hence index1 is correct.
getIndexes('UserData', function(err, fields) { getIndexes('UserData', function(err, fields) {
// console.log('....', fields);
assert.deepEqual(fields, { PRIMARY: assert.deepEqual(fields, { PRIMARY:
{ Table: 'UserData', { Table: 'UserData',
Non_unique: 0, Non_unique: 0,
@ -86,8 +87,7 @@ describe('migrations', function() {
Packed: null, Packed: null,
Null: '', Null: '',
Index_type: 'BTREE', Index_type: 'BTREE',
Comment: '', Comment: '' },
Index_comment: '' },
email: email:
{ Table: 'UserData', { Table: 'UserData',
Non_unique: 1, Non_unique: 1,
@ -95,13 +95,12 @@ describe('migrations', function() {
Seq_in_index: 1, Seq_in_index: 1,
Column_name: 'email', Column_name: 'email',
Collation: 'A', Collation: 'A',
Cardinality: 0, Cardinality: null,
Sub_part: 191, Sub_part: null,
Packed: null, Packed: null,
Null: '', Null: '',
Index_type: 'BTREE', Index_type: 'BTREE',
Comment: '', Comment: '' },
Index_comment: '' },
index0: index0:
{ Table: 'UserData', { Table: 'UserData',
Non_unique: 1, Non_unique: 1,
@ -109,13 +108,12 @@ describe('migrations', function() {
Seq_in_index: 1, Seq_in_index: 1,
Column_name: 'email', Column_name: 'email',
Collation: 'A', Collation: 'A',
Cardinality: 0, Cardinality: null,
Sub_part: 191, Sub_part: null,
Packed: null, Packed: null,
Null: '', Null: '',
Index_type: 'BTREE', Index_type: 'BTREE',
Comment: '', Comment: '' }
Index_comment: '' }
}); });
done(); done();
}); });
@ -298,7 +296,7 @@ describe('migrations', function() {
NumberData.create({number: 1.1234567, tinyInt: 123456, mediumInt: -1234567, floater: 123456789.1234567 }, function(err, obj) { NumberData.create({number: 1.1234567, tinyInt: 123456, mediumInt: -1234567, floater: 123456789.1234567 }, function(err, obj) {
assert.ok(!err); assert.ok(!err);
assert.ok(obj); assert.ok(obj);
NumberData.find(obj.id, function(err, found) { NumberData.findById(obj.id, function(err, found) {
assert.equal(found.number, 1.123); assert.equal(found.number, 1.123);
assert.equal(found.tinyInt, 127); assert.equal(found.tinyInt, 127);
assert.equal(found.mediumInt, 0); assert.equal(found.mediumInt, 0);
@ -315,7 +313,7 @@ describe('migrations', function() {
}, function(err, obj){ }, function(err, obj){
assert.ok(!err); assert.ok(!err);
assert.ok(obj); assert.ok(obj);
DateData.find(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.dateTime.toGMTString(), 'Fri, 09 Aug 1996 07:47:33 GMT');
assert.equal(found.timestamp.toGMTString(), 'Sat, 22 Sep 2007 17:12:22 GMT'); assert.equal(found.timestamp.toGMTString(), 'Sat, 22 Sep 2007 17:12:22 GMT');
done(); done();