Skip to content

Commit

Permalink
test:add all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jtyjty99999 committed Aug 29, 2016
1 parent 09d618f commit 3524eae
Show file tree
Hide file tree
Showing 54 changed files with 850 additions and 39 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# egg-rds
# egg-mysql

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
Expand All @@ -7,25 +7,25 @@
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/egg-rds.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-rds
[travis-image]: https://img.shields.io/travis/eggjs/egg-rds.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-rds
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-rds.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-rds?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-rds.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-rds
[snyk-image]: https://snyk.io/test/npm/egg-rds/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-rds
[download-image]: https://img.shields.io/npm/dm/egg-rds.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-rds

Aliyun rds client for egg framework
[npm-image]: https://img.shields.io/npm/v/egg-mysql.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-mysql
[travis-image]: https://img.shields.io/travis/eggjs/egg-mysql.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-mysql
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-mysql.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-mysql?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-mysql.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-mysql
[snyk-image]: https://snyk.io/test/npm/egg-mysql/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-mysql
[download-image]: https://img.shields.io/npm/dm/egg-mysql.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-mysql

Aliyun rds client(support mysql portocal) for egg framework

## Install

```bash
$ npm i egg-rds
$ npm i egg-mysql
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
const rds = require('./lib/rds');

module.exports = agent => {
if (agent.config.rds.agent) rds(agent);
if (agent.config.mysql.agent) rds(agent);
};
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
const rds = require('./lib/rds');

module.exports = app => {
if (app.config.rds.app) rds(app);
if (app.config.mysql.app) rds(app);
};
2 changes: 1 addition & 1 deletion lib/client-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ClientManager {

const client = rds(config);
client._superQuery = client.query;
client.query = function*() {
client.query = function* () {
// agent 没有 instrument
if (!app.instrument) {
return yield this._superQuery.apply(this, arguments);
Expand Down
84 changes: 73 additions & 11 deletions lib/rds.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,77 @@
'use strict';

const assert = require('assert');
const RDS = require('ali-rds');

module.exports = app => {
app.addSingoneTon('rds', (config, app) => {
config = Object.assign({}, config);
/* todo
if (config.cluster) {
return rds(config);
}*/
return RDS(config);
const ClientManager = require('./client-manager');

module.exports = function(app) {
// 兼容老的配置
const mysqlConfig = app.config.mysql;
app.config.mysql = compact(mysqlConfig);

if (app.config.mysql !== mysqlConfig) {
app.deprecate('[egg-mysql] 配置格式已修改,请尽快查阅 egg-mysql 文档并更新到新版配置格式');
}
// 创建 mysql
const clientManager = new ClientManager(app);
let count = 0;
app.addSingleton('mysql', (config, app) => {
const done = app.readyCallback(`createMysql-${count++}`);
return clientManager.create(config, done);
});

// 兼容老的写法
if (mysqlConfig.clients) {
// 增加 end 方法
app.mysql.end = function(config, callback) {
callback = callback || function() {};
const clientId = typeof config === 'string' ? config : getClientId(config);
const client = app.mysql.get(clientId);
if (!client) {
return callback();
}
app.mysql.clients.delete(clientId);
client.end(callback);
};
app.mysqls = app.mysql;
}
};

/**
* 兼容老的配置写法
* @param {Object} originalConfig 原始配置
* @return {Object} 新版配置
*/
function compact(originalConfig) {
const config = {
app: originalConfig.app,
agent: originalConfig.agent,
};

if (Array.isArray(originalConfig.clients)) {
config.clients = {};
for (const client of originalConfig.clients) {
config.clients[getClientId(client)] = client;
}
// config.default = originalConfig.default; // 原来配置多实例不支持 default
return config;
}

if (originalConfig.host) {
config.client = originalConfig;
config.default = originalConfig.default; // 原来配置单实例有默认值
delete config.client.default;
delete config.client.agent;
delete config.client.app;
return config;
}

return originalConfig;
}

/**
* 获取 clientId
* @param {Object} config 配置对象
* @return {String} 配置 id
*/
function getClientId(config) {
return config.clientId || `${config.host}#${config.port}#${config.database}#${config.user}`;
}
25 changes: 16 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
{
"name": "egg-rds",
"name": "egg-mysql",
"version": "0.0.1",
"description": "rds plugin for egg",
"description": "mysql plugin for egg",
"eggPlugin": {
"name": "rds"
"name": "mysql"
},
"keywords": [
"egg",
"plugin"
],
"dependencies": {},
"dependencies": {
"ali-rds": "~2.6.0",
"co": "~4.6.0"
},
"devDependencies": {
"egg-ci": "1",
"egg-bin": "1",
"autod": "2",
"egg": "~0.1.0",
"egg-bin": "1",
"egg-ci": "1",
"egg-mock": "0.0.4",
"eslint": "3",
"eslint-config-egg": "3",
"pedding": "^1.0.0",
"should": "8",
"supertest": "1"
"supertest": "1",
"utility": "^1.8.0"
},
"engines": {
"node": ">=4.0.0"
Expand All @@ -35,12 +42,12 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/eggjs/egg-rds.git"
"url": "git+https://github.com/eggjs/egg-mysql.git"
},
"bugs": {
"url": "https://github.com/eggjs/egg/issues"
},
"homepage": "https://github.com/eggjs/egg-rds#readme",
"homepage": "https://github.com/eggjs/egg-mysql#readme",
"author": "jtyjty99999",
"license": "MIT"
}
69 changes: 69 additions & 0 deletions test/client_manager.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

/**
* Module dependencies.
*/

const pedding = require('pedding');
const request = require('supertest');
const mm = require('egg-mock');

describe('test/multiclient.test.js', () => {
describe('old config', () => {
let app;
before(done => {
app = mm.app({
baseDir: 'apps/mysqlapp-multi-client',
plugin: 'mysql',
});
app.ready(done);
});

it('should multi client work', done => {
request(app.callback())
.get('/')
.expect({
hasRows: true,
})
.expect(200, done);
});

it('should close all mysql client work', done => {
done = pedding(Object.keys(app.config.mysql.clients).length, done);
for (const key in app.config.mysql.clients) {
app.mysqls.end(key, done);
}
app.mysqls.end({ host: 'not exists host' });
app.mysqls.end('db1');
});
});

describe('new config', () => {
let app;
before(done => {
app = mm.app({
baseDir: 'apps/mysqlapp-multi-client-new',
plugin: 'mysql',
});
app.ready(done);
});

it('should multi client work', done => {
request(app.callback())
.get('/')
.expect({
hasRows: true,
})
.expect(200, done);
});

it('should close all mysql client work', done => {
done = pedding(Object.keys(app.config.mysql.clients).length, done);
for (const key in app.config.mysql.clients) {
app.mysqls.end(key, done);
}
app.mysqls.end({ host: 'not exists host' });
app.mysqls.end('db1');
});
});
});
8 changes: 8 additions & 0 deletions test/fixtures/apps/hpmweb/app/controllers/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = function* () {
const rows = yield this.app.mysql.query('show tables');
this.body = {
hasRows: rows.length > 0,
};
};
5 changes: 5 additions & 0 deletions test/fixtures/apps/hpmweb/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = function(app) {
app.get('/', app.controller.home);
};
14 changes: 14 additions & 0 deletions test/fixtures/apps/hpmweb/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

exports.mysql = {
host: '127.0.0.1',
port: 3307,
user: 'root',
password: '',
database: 'test',
// host: env.ALI_SDK_RDS_HOST || 'localhost',
// port: env.ALI_SDK_RDS_PORT || 3306,
// user: env.ALI_SDK_RDS_USER || 'root',
// password: env.ALI_SDK_RDS_PASSWORD || '',
// database: env.ALI_SDK_RDS_DATABASE || 'test',
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/hpmweb/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "npmweb"
}
15 changes: 15 additions & 0 deletions test/fixtures/apps/mysqlapp-disable/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

exports.mysql = {
host: '127.0.0.1',
port: 3307,
user: 'root',
password: '',
database: 'test',
app:false
// host: env.ALI_SDK_RDS_HOST || 'localhost',
// port: env.ALI_SDK_RDS_PORT || 3306,
// user: env.ALI_SDK_RDS_USER || 'root',
// password: env.ALI_SDK_RDS_PASSWORD || '',
// database: env.ALI_SDK_RDS_DATABASE || 'test',
};
3 changes: 3 additions & 0 deletions test/fixtures/apps/mysqlapp-disable/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "mysqlapp"
}
17 changes: 17 additions & 0 deletions test/fixtures/apps/mysqlapp-dynamic/agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const co = require('co');
const fs = require('fs');
const path = require('path');

module.exports = function(agent) {
agent.mysql1 = agent.mysql.createInstance(agent.config.mysql1);
const done = agent.readyCallback('agent-mysql');
const p = path.join(__dirname, 'run/agent_result.json');
fs.existsSync(p) && fs.unlinkSync(p);

co(function* () {
const result = yield agent.mysql1.query('select now() as currentTime;');
fs.writeFileSync(p, JSON.stringify(result));
}).then(done, done);
};
5 changes: 5 additions & 0 deletions test/fixtures/apps/mysqlapp-dynamic/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = function(app) {
app.mysql1 = app.mysql.createInstance(app.config.mysql1);
};
10 changes: 10 additions & 0 deletions test/fixtures/apps/mysqlapp-dynamic/app/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

module.exports = function* () {
const users = yield this.service.user.list(this);

this.body = {
status: 'success',
users: users,
};
};
5 changes: 5 additions & 0 deletions test/fixtures/apps/mysqlapp-dynamic/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = function(app) {
app.get('/', app.controller.home);
};
5 changes: 5 additions & 0 deletions test/fixtures/apps/mysqlapp-dynamic/app/service/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.list = function* (ctx) {
return yield ctx.app.mysql1.query('select * from npm_auth');
};
Loading

0 comments on commit 3524eae

Please sign in to comment.