333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'use strict';
 
const assert = require('assert');
const request = require('supertest');
const mm = require('egg-mock');
const utility = require('utility');
const path = require('path');
const fs = require('fs');
 
describe('test/mysql.test.js', () => {
  let app;
  const uid = utility.randomString();
 
  before(() => {
    app = mm.app({
      baseDir: 'apps/mysqlapp',
    });
    return app.ready();
  });
 
  beforeEach(function* () {
    // init test datas
    try {
      yield app.mysql.query(`insert into npm_auth set user_id = 'egg-${uid}-1', password = '1'`);
      yield app.mysql.query(`insert into npm_auth set user_id = 'egg-${uid}-2', password = '2'`);
      yield app.mysql.query(`insert into npm_auth set user_id = 'egg-${uid}-3', password = '3'`);
      yield app.mysql.queryOne(`select * from npm_auth where user_id = 'egg-${uid}-3'`);
    } catch (err) {
      console.log('init test datas error: %s', err);
    }
  });
 
  afterEach(function* () {
    // 清空测试数据
    yield app.mysql.query(`delete from npm_auth where user_id like 'egg-${uid}%'`);
  });
 
  after(done => {
    app.mysql.end(err => {
      app.close();
      done(err);
    });
  });
 
  afterEach(mm.restore);
 
  it('should query mysql user table success', () => {
    return request(app.callback())
      .get('/')
      .expect(200);
  });
 
  it('should query limit 2', function* () {
    const users = yield app.mysql.query('select * from npm_auth order by id desc limit 2');
    assert(users.length === 2);
 
    const rows = yield app.mysql.select('npm_auth', {
      orders: [[ 'id', 'desc' ]],
      limit: 2,
    });
    assert(rows.length === 2);
    assert.deepEqual(rows[0], users[0]);
    assert.deepEqual(rows[1], users[1]);
  });
 
  it('should update successfully', function* () {
    const user = yield app.mysql.queryOne('select * from npm_auth order by id desc limit 10');
    const result = yield app.mysql.update('npm_auth', { id: user.id, user_id: `79744-${uid}-update` });
    assert(result.affectedRows === 1);
  });
 
  it('should delete successfully', function* () {
    const user = yield app.mysql.queryOne('select * from npm_auth order by id desc limit 10');
    const result = yield app.mysql.delete('npm_auth', { id: user.id });
    assert(result.affectedRows === 1);
  });
 
  it('should query one success', function* () {
    const user = yield app.mysql.queryOne('select * from npm_auth order by id desc limit 10');
    assert(user);
    assert(typeof user.user_id === 'string' && user.user_id);
 
    const row = yield app.mysql.get('npm_auth', { user_id: user.user_id });
    assert(row.id === user.id);
  });
 
  it('should query one not exists return null', function* () {
    let user = yield app.mysql.queryOne('select * from npm_auth where id = -1');
    assert(!user);
 
    user = yield app.mysql.get('npm_auth', { id: -1 });
    assert(!user);
  });
 
  it('should escape value', () => {
    const val = app.mysql.escape('\'"?><=!@#');
    assert(val === '\'\\\'\\"?><=!@#\'');
  });
 
  it('should agent error when password wrong on multi clients', done => {
    const app = mm.app({
      baseDir: 'apps/mysqlapp-multi-client-wrong',
    });
    app.ready(err => {
      assert(err.message.includes('ER_ACCESS_DENIED_ERROR'));
      done();
    });
  });
 
  it('should queryOne work on transaction', function* () {
    const result = yield app.mysql.beginTransactionScope(function* (conn) {
      const row = yield conn.queryOne('select * from npm_auth order by id desc limit 10');
      return { row };
    }, {});
    assert(result.row);
    assert(result.row.user_id && typeof result.row.user_id === 'string');
    assert(result.row.password === '3');
  });
 
  describe('config.mysql.agent = true', () => {
    let app;
    before(() => {
      app = mm.cluster({
        baseDir: 'apps/mysqlapp',
        plugin: 'mysql',
      });
      return app.ready();
    });
    after(() => app.close());
 
    it('should agent.mysql work', () => {
      const result = fs.readFileSync(path.join(__dirname,
        './fixtures/apps/mysqlapp/run/agent_result.json'), 'utf8');
      assert(/\[\{"currentTime":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z"\}\]/.test(result));
    });
  });
 
  describe('config.mysql.app = false', () => {
    let app;
    before(() => {
      app = mm.app({
        baseDir: 'apps/mysqlapp-disable',
        plugin: 'mysql',
      });
      return app.ready();
    });
    after(() => app.close());
 
    it('should disable app work', () => {
      assert(!app.mysql);
    });
  });
 
  describe('newConfig', () => {
    let app;
    before(() => {
      app = mm.cluster({
        baseDir: 'apps/mysqlapp-new',
        plugin: 'mysql',
      });
      return app.ready();
    });
 
    after(() => app.close());
 
    it('should new config agent.mysql work', () => {
      const result = fs.readFileSync(path.join(__dirname,
        './fixtures/apps/mysqlapp-new/run/agent_result.json'), 'utf8');
      assert(/\[\{"currentTime":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z"\}\]/.test(result));
    });
 
    it('should query mysql user table success', () => {
      return request(app.callback())
        .get('/')
        .expect(200);
    });
  });
 
  describe('createInstance', () => {
    let app;
    before(() => {
      app = mm.cluster({
        baseDir: 'apps/mysqlapp-dynamic',
        plugin: 'mysql',
      });
      return app.ready();
    });
 
    after(() => app.close());
 
    it('should new config agent.mysql work', () => {
      const result = fs.readFileSync(path.join(__dirname,
        './fixtures/apps/mysqlapp-dynamic/run/agent_result.json'), 'utf8');
      assert(/\[\{"currentTime":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z"\}\]/.test(result));
    });
 
    it('should query mysql user table success', () => {
      return request(app.callback())
        .get('/')
        .expect(200);
    });
  });
});