forked from bradzacher/mysqldump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
268 lines (238 loc) · 7.08 KB
/
test.js
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
var expect = require('chai').expect;
var async = require('async');
var mysql = require('mq-node');
var fs = require('fs');
var mysqlDump = require('./');
describe('mysql test', function() {
var dbTest = 'dump_test';
var connection = {
host: 'localhost',
user: 'root',
password: ''
};
mysql = mysql(connection);
it('should create data for using in test', function(done) {
this.timeout(8000);
var run = [];
run.push(function(callback){
mysql.query('CREATE DATABASE IF NOT EXISTS '+dbTest,callback)
});
run.push(function(callback){
mysql.connection.changeUser({database:dbTest},callback);
});
run.push(function(callback){
mysql.query("SHOW TABLES FROM "+dbTest,function(err,data){
for(var i=0;i<data.length;i++) {
if(data[i]['Tables_in_'+dbTest]=='players'){
mysql.query('DROP TABLE `players`',callback)
return;
}
}
callback();
});
});
run.push(function(callback){
mysql.query("SHOW TABLES FROM "+dbTest,function(err,data){
for(var i=0;i<data.length;i++) {
if(data[i]['Tables_in_'+dbTest]=='teams'){
mysql.query('DROP TABLE `teams`',callback)
return;
}
}
callback();
});
});
run.push(function(callback){
mysql.query("CREATE TABLE IF NOT EXISTS `players` ("+
" `id` int(10) unsigned NOT NULL,"+
" `name` varchar(32) NOT NULL,"+
" `gender` tinyint(3) unsigned NOT NULL,"+
" `team` varchar(32) DEFAULT NULL,"+
" `country` varchar(2) NOT NULL,"+
" `lvl` tinyint(3) unsigned NOT NULL DEFAULT '1',"+
" `cadtime` int(10) unsigned NOT NULL,"+
" `logtime` int(10) unsigned NOT NULL,"+
" PRIMARY KEY (`id`),"+
" UNIQUE KEY `name` (`name`)"+
") ENGINE=InnoDB DEFAULT CHARSET=latin1;",callback);
});
run.push(function(callback){
mysql.query("CREATE TABLE IF NOT EXISTS `teams` ("+
" `id` int(10) unsigned NOT NULL AUTO_INCREMENT,"+
" `name` varchar(32) NOT NULL,"+
" `country` varchar(2) NOT NULL,"+
" `avatar` varchar(16) NOT NULL,"+
" `adm` int(32) unsigned NOT NULL,"+
" PRIMARY KEY (`id`)"+
") ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;",callback);
});
run.push(function(callback){
mysql.query("INSERT INTO `teams` (`id`, `name`, `country`, `avatar`, `adm`) VALUES"+
"(2, 'Flamengo 203', 'br', 'C????CC??', 7),"+ // binary test
"(3, 'Heroes Team', 'br', '??\0???CC', 7);",callback);
});
run.push(function(callback){
mysql.query("INSERT INTO `players` (`id`, `name`, `gender`, `team`, `country`, `lvl`, `cadtime`, `logtime`) VALUES"+
"(3, 'Distillers 530', 0, NULL, 'br', 1, 1442441886, 1442441886),"+
"(4, 'Distillers 42', 1, NULL, 'br', 1, 1442441956, 1442441956),"+
"(5, 'Distillers 904', 0, NULL, 'br', 1, 1442442042, 1442442042),"+
"(6, 'Distillers 302', 0, NULL, 'br', 1, 1442442295, 1442442295),"+
"(7, 'Distillers 345', 1, '3', 'br', 1, 1442443725, 1442680916),"+
"(8, 'Distillers', 0, NULL, 'br', 1, 1442550731, 1442550731),"+
"(9, 'Distillers 886', 0, NULL, 'br', 1, 1442550731, 1442550731),"+
"(11, 'Distillers 17', 0, NULL, 'br', 1, 1442550761, 1442550761),"+
"(12, 'Distillers 444', 0, NULL, 'br', 1, 1442551137, 1442551137),"+
"(13, 'Distillers 10', 0, NULL, 'br', 1, 1442551209, 1442551209),"+
"(14, 'Distillers 236', 1, NULL, 'br', 1, 1442551209, 1442551209),"+
"(15, 'Distillers 340', 1, NULL, 'br', 1, 1442551267, 1442551267),"+
"(16, 'Distillers 280', 1, NULL, 'br', 1, 1442551268, 1442551268);",callback);
});
async.series(run,function(err,data){
expect(err).to.be.null;
done();
});
});
it('should create a dump file with only schema', function(done) {
this.timeout(8000);
var dest = './data.sql';
mysqlDump({
host: 'localhost',
user: 'root',
password: '',
database: dbTest,
data:false,
dest:dest
},function(err){
var file = String(fs.readFileSync(dest));
expect(err).to.be.null;
expect(file).not.to.be.null;
expect(file).to.not.contain("INSERT INTO ");
expect(file).to.contain("CREATE TABLE ");
fs.unlinkSync(dest);
done();
})
});
it('should create a dump file with only data', function(done) {
this.timeout(8000);
var dest = './data.sql';
mysqlDump({
host: 'localhost',
user: 'root',
password: '',
database: dbTest,
schema:false,
dest:dest
},function(err){
var file = String(fs.readFileSync(dest));
expect(err).to.be.null;
expect(file).not.to.be.null;
expect(file).to.contain("INSERT INTO ");
expect(file).to.not.contain("CREATE TABLE ");
fs.unlinkSync(dest);
done();
})
});
it('should create a dump file with data using where', function(done) {
this.timeout(8000);
var dest = './data.sql';
mysqlDump({
host: 'localhost',
user: 'root',
password: '',
database: dbTest,
where:{
players:'id<10 AND gender=1'
},
schema:false,
dest:dest
},function(err){
var file = String(fs.readFileSync(dest));
expect(err).to.be.null;
expect(file).not.to.be.null;
expect(file.match(/INSERT INTO `players`/g)).to.have.length.below(3);
expect(file).to.contain("INSERT INTO ");
expect(file).to.not.contain("CREATE TABLE ");
fs.unlinkSync(dest);
done();
})
});
it('should return a dump as raw data on callback', function(done) {
this.timeout(8000);
var dest = './data.sql';
mysqlDump({
host: 'localhost',
user: 'root',
password: '',
database: dbTest,
schema:false,
getDump:true,
dest:dest
},function(err,file){
expect(err).to.be.null;
expect(file).not.to.be.null;
expect(file).to.contain("INSERT INTO ");
expect(file).to.not.contain("CREATE TABLE ");
done();
})
});
it('should create a dump file with schema and data', function(done) {
this.timeout(8000);
var dest = './data.sql';
mysqlDump({
host: 'localhost',
user: 'root',
password: '',
database: dbTest,
dest:dest
},function(err){
var file = String(fs.readFileSync(dest));
expect(err).to.be.null;
expect(file).not.to.be.null;
expect(file).to.contain("INSERT INTO ");
expect(file).to.contain("CREATE TABLE ");
fs.unlinkSync(dest);
done();
})
});
it('should create a dump file with schema and data with tables and create if exist', function(done) {
this.timeout(8000);
var dest = './data.sql';
mysqlDump({
host: 'localhost',
user: 'root',
password: '',
database: dbTest,
tables:['players'],
ifNotExist:true,
dest:dest
},function(err){
var file = String(fs.readFileSync(dest));
expect(err).to.be.null;
expect(file).not.to.be.null;
expect(file).to.not.contain("CREATE TABLE IF NOT EXISTS `teams`");
expect(file).to.contain("CREATE TABLE IF NOT EXISTS `players`");
fs.unlinkSync(dest);
done();
})
});
it('should remove auto increment', function(done) {
this.timeout(8000);
var dest = './data.sql';
mysqlDump({
host: 'localhost',
user: 'root',
password: '',
database: dbTest,
autoIncrement:false,
ifNotExist:true,
dest:dest
},function(err){
var file = String(fs.readFileSync(dest));
expect(err).to.be.null;
expect(file).not.to.be.null;
expect(file).to.not.contain(" AUTO_INCREMENT=");
fs.unlinkSync(dest);
done();
})
});
});