-
-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathcreate_test.js
More file actions
355 lines (288 loc) · 11.6 KB
/
create_test.js
File metadata and controls
355 lines (288 loc) · 11.6 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
'use strict';
const Code = require('@hapi/code');
const Lab = require('@hapi/lab');
const Promise = require('bluebird');
const lab = (exports.lab = Lab.script());
const fs = require('fs');
const path = require('path');
const cp = require('child_process');
const dbmUtil = require('db-migrate-shared').util;
const rmdir = Promise.promisify(require('rimraf'));
const writeFile = Promise.promisify(fs.writeFile);
function wipeMigrations () {
const dir = path.join(__dirname, 'migrations');
return rmdir(dir);
}
function dbMigrate () {
const args = dbmUtil.toArray(arguments);
const dbm = path.join(__dirname, '..', '..', 'bin', 'db-migrate');
args.unshift(dbm);
return cp.spawn('node', args, { cwd: __dirname });
}
lab.experiment('create', function () {
lab.experiment('without a migration directory', () => {
let exitCode;
lab.before(async () => {
await wipeMigrations();
const db = dbMigrate('create', 'first migration');
// db.stderr.on('data', data => console.log(data.toString()));
// db.stdout.on('data', data => console.log(data.toString()));
const exitCodePromise = new Promise((resolve) => db.on('exit', resolve));
exitCode = await exitCodePromise;
});
lab.test('does not cause an error', () => {
Code.expect(exitCode).to.equal(0);
});
lab.test('will create a new migration directory', () => {
const stats = fs.statSync(path.join(__dirname, 'migrations'));
Code.expect(stats.isDirectory()).to.be.true();
});
lab.test('will create a new migration', () => {
const files = fs.readdirSync(path.join(__dirname, 'migrations'));
const file = files.find(file => /first-migration\.js$/.test(file));
Code.expect(file).to.exist();
});
lab.test('will create migrations/package.json', () => {
const files = fs.readdirSync(path.join(__dirname, 'migrations'));
const packageJson = files.find(file => /package\.json$/.test(file));
Code.expect(packageJson).to.exist();
})
});
lab.experiment(
'with existing migrations/package.json file', () => {
let exitCode;
lab.before(async () => {
await wipeMigrations();
await fs.promises.mkdir(path.join(__dirname, 'migrations'), { recursive: true });
await writeFile(path.join(__dirname, 'migrations', 'package.json'), '{"name": "test", "type": "module"}');
const db = dbMigrate('create', 'first migration');
// db.stderr.on('data', data => console.log(data.toString()));
// db.stdout.on('data', data => console.log(data.toString()));
const exitCodePromise = new Promise((resolve) => db.on('exit', resolve));
exitCode = await exitCodePromise;
});
lab.test('does not cause an error', () => {
Code.expect(exitCode).to.equal(0);
});
lab.test('will modify an existing migrations/package.json\'s type field', () => {
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'migrations', 'package.json')))
Code.expect(packageJson.type).to.equal("commonjs");
});
lab.test('will preserve other properties in an existing package.json', () => {
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'migrations', 'package.json')))
Code.expect(packageJson.name).to.equal("test");
});
});
lab.experiment(
'with sql-file option set to true from config file', () => {
let exitCode;
lab.before(async () => {
const configOption = path.join(
'--config=',
__dirname,
'database_with_sql_file.json'
);
await wipeMigrations();
const exitCodePromise = new Promise((resolve) => {
dbMigrate('create', 'second migration', configOption)
.on('exit', resolve);
});
exitCode = await exitCodePromise;
});
lab.test('does not cause an error', () => {
Code.expect(exitCode).to.equal(0);
});
lab.test('will create a new migration', () => {
const files = fs.readdirSync(path.join(__dirname, 'migrations'));
for (let i = 0; i < files.length; i++) {
const file = files[i];
const stats = fs.statSync(path.join(__dirname, 'migrations', file));
if (stats.isFile() && !/package\.json$/.test(file)) {
Code.expect(file).to.match(/second-migration\.js$/);
}
}
});
lab.test('will create a new migration/sqls directory', () => {
const stats = fs.statSync(path.join(__dirname, 'migrations/sqls'));
Code.expect(stats.isDirectory()).to.be.true();
});
lab.test('will create a new migration sql up file', () => {
const files = fs.readdirSync(path.join(__dirname, 'migrations/sqls'));
const file = files.find(file => /second-migration-up\.sql$/.test(file));
Code.expect(file).to.exist();
});
}
);
lab.experiment(
'with sql-file option set to true as a command parameter', () => {
let exitCode;
lab.before(async () => {
const configOption = path.join('--sql-file');
await wipeMigrations();
const exitCodePromise = new Promise((resolve) => {
dbMigrate('create', 'third migration', configOption).on('exit', resolve);
});
exitCode = await exitCodePromise;
});
lab.test('does not cause an error', () => {
Code.expect(exitCode).to.equal(0);
});
lab.test('will create a new migration', () => {
const files = fs.readdirSync(path.join(__dirname, 'migrations'));
for (let i = 0; i < files.length; i++) {
const file = files[i];
const stats = fs.statSync(path.join(__dirname, 'migrations', file));
if (stats.isFile() && !/package\.json$/.test(file)) {
Code.expect(file).to.match(/third-migration\.js$/);
}
}
});
lab.test('will create a new migration/sqls directory', () => {
const stats = fs.statSync(path.join(__dirname, 'migrations/sqls'));
Code.expect(stats.isDirectory()).to.be.true();
});
lab.test('will create a new migration sql up file', () => {
const files = fs.readdirSync(path.join(__dirname, 'migrations/sqls'));
const file = files.find(file => /third-migration-up\.sql$/.test(file));
Code.expect(file).to.exist();
});
}
);
lab.experiment(
'with coffee-file option set to true from config file', () => {
let exitCode;
lab.before(async () => {
const configOption = path.join(
'--config=',
__dirname,
'database_with_coffee_file.json'
);
await wipeMigrations();
const exitCodePromise = new Promise((resolve) => {
dbMigrate('create', 'fourth migration', configOption).on('exit', resolve);
});
exitCode = await exitCodePromise;
});
lab.test('does not cause an error', () => {
Code.expect(exitCode).to.equal(0);
});
lab.test('will create a new coffeescript migration', () => {
const files = fs.readdirSync(path.join(__dirname, 'migrations'));
for (let i = 0; i < files.length; i++) {
const file = files[i];
const stats = fs.statSync(path.join(__dirname, 'migrations', file));
if (stats.isFile() && !/package\.json$/.test(file)) {
Code.expect(file).to.match(/fourth-migration\.coffee$/);
}
}
});
}
);
lab.experiment(
'with scoped migration', () => {
lab.experiment('without a migration directory', () => {
let exitCode;
lab.before(async () => {
await wipeMigrations();
const configOption = path.join('--sql-file');
const db = dbMigrate('create', 'test/first migration', configOption);
// db.stderr.on('data', data => console.log(data.toString()));
// db.stdout.on('data', data => console.log(data.toString()));
const exitCodePromise = new Promise((resolve) => db.on('exit', resolve));
exitCode = await exitCodePromise;
});
lab.test('does not cause an error', () => {
Code.expect(exitCode).to.equal(0);
});
lab.test('will create a new migration directory', () => {
const stats = fs.statSync(path.join(__dirname, 'migrations/test'));
Code.expect(stats.isDirectory()).to.be.true();
});
lab.test('will create a new migration', () => {
const files = fs.readdirSync(path.join(__dirname, 'migrations/test'));
const file = files.find(file => /first-migration\.js$/.test(file));
Code.expect(file).to.exist();
});
lab.test('will create a new migration/test/sqls directory', () => {
const stats = fs.statSync(path.join(__dirname, 'migrations/test/sqls'));
Code.expect(stats.isDirectory()).to.be.true();
});
lab.test('will create a new migration sql up file', () => {
const files = fs.readdirSync(path.join(__dirname, 'migrations/test/sqls'));
const file = files.find(file => /first-migration-up\.sql$/.test(file));
Code.expect(file).to.exist();
});
});
}
);
lab.experiment(
'with coffee-file option set to true as a command parameter', () => {
let exitCode;
lab.before(async () => {
const configOption = path.join('--coffee-file');
await wipeMigrations();
const exitCodePromise = new Promise((resolve) => {
dbMigrate('create', 'fifth migration', configOption).on('exit', resolve);
});
exitCode = await exitCodePromise;
});
lab.test('does not cause an error', () => {
Code.expect(exitCode).to.equal(0);
});
lab.test('will create a new coffeescript migration', () => {
const files = fs.readdirSync(path.join(__dirname, 'migrations'));
for (let i = 0; i < files.length; i++) {
const file = files[i];
const stats = fs.statSync(path.join(__dirname, 'migrations', file));
if (stats.isFile() && !/package\.json$/.test(file)) {
Code.expect(file).to.match(/fifth-migration\.coffee$/);
}
}
});
}
);
lab.experiment(
'with sql-file and a bad migration, causes an exit', () => {
let exitCode;
lab.before(async () => {
const configOption = path.join('--sql-file');
await wipeMigrations();
dbMigrate('create', 'sixth migration', configOption).on('exit', () => {
const files = fs.readdirSync(path.join(__dirname, 'migrations'));
for (let i = 0; i < files.length; i++) {
const file = files[i];
const stats = fs.statSync(
path.join(__dirname, 'migrations', file)
);
if (stats.isFile() && file.match(/sixth-migration\.js$/)) {
fs.writeFileSync(
path.join(__dirname, 'migrations', file),
'asdfghij;'
);
}
}
});
const codePromise = new Promise((resolve) => {
dbMigrate('up').on('exit', resolve);
});
exitCode = await codePromise;
});
lab.test('does cause an error', () => {
Code.expect(exitCode).to.equal(1);
});
lab.test('did create the new migration', () => {
const files = fs.readdirSync(path.join(__dirname, 'migrations'));
for (let i = 0; i < files.length; i++) {
const file = files[i];
const stats = fs.statSync(path.join(__dirname, 'migrations', file));
if (stats.isFile() && !/package\.json$/.test(file)) {
Code.expect(file).to.match(/sixth-migration\.js$/);
}
}
});
lab.after(() => {
cp.exec('rm -r ' + path.join(__dirname, 'migrations'));
});
}
);
});