-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
197 lines (185 loc) · 6.25 KB
/
index.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
const async = require('async');
const { Suite, Test, Hook } = require('zunit');
const { ok, strictEqual: eq } = require('assert');
const setupDatabase = new Hook('Setup database', (h, done) => {
const driver1 = h.test.locals.get('driver1');
const driver2 = h.test.locals.get('driver2');
async.series([driver1.connect, driver2.connect, driver1.dropMigrations], done);
});
const teardownDatabase = new Hook('Teardown database', (h, done) => {
const driver1 = h.test.locals.get('driver1');
const driver2 = h.test.locals.get('driver2');
async.series([driver1.disconnect, driver2.disconnect], done);
});
const shouldCreateMigrationsTableIfNotExists = new Test('should create migration table if not exists', (t, done) => {
const driver1 = t.locals.get('driver1');
async.series(
{
meh1: driver1.ensureMigrations,
migrations: driver1.getMigrations,
},
(err, results) => {
if (err) return done(err);
ok(results.migrations, 'Migrations table was not created');
eq(results.migrations.length, 0);
done();
}
);
});
const shouldCreateMigrationsTableInParallel = new Test('should create migration table in parallel', (t, done) => {
const driver1 = t.locals.get('driver1');
const driver2 = t.locals.get('driver2');
async.series(
{
meh1: async.parallel.bind(async, [driver1.ensureMigrations, driver2.ensureMigrations]),
migrations: driver1.getMigrations,
},
(err, results) => {
if (err) return done(err);
ok(results.migrations, 'Migrations table was not created');
eq(results.migrations.length, 0);
done();
}
);
});
const shouldNotFailIfMigrationsTableAlreadyExists = new Test(
'should not fail if migration table already exists',
(t, done) => {
const driver1 = t.locals.get('driver1');
async.series([driver1.ensureMigrations, driver1.ensureMigrations], done);
}
);
const shouldLockMigrationsTable = new Test('should lock migrations table', (t, done) => {
const driver1 = t.locals.get('driver1');
const driver2 = t.locals.get('driver2');
let delay;
async.series(
[
driver1.ensureMigrations,
driver1.lockMigrations,
(cb) => {
delay = Date.now();
cb();
},
(cb) => {
setTimeout(() => {
driver1.unlockMigrations(cb);
delay = Date.now() - delay;
}, 200);
},
driver2.lockMigrations,
driver2.unlockMigrations,
],
(err) => {
if (err) return done(err);
ok(delay >= 200);
ok(delay <= 400);
done();
}
);
});
const shouldRunMigration = new Test('should run migration', (t, done) => {
const driver1 = t.locals.get('driver1');
const migration = t.locals.get('migrations').simple;
async.waterfall(
[driver1.ensureMigrations, driver1.runMigration.bind(driver1, migration), driver1.getMigrations],
(err, migrations) => {
if (err) return done(err);
ok(migrations, 'Migrations created');
eq(migrations.length, 1);
eq(migrations[0].level, migration.level);
eq(migrations[0].comment, migration.comment);
eq(migrations[0].timestamp.toISOString(), migration.timestamp.toISOString());
eq(migrations[0].checksum, migration.checksum);
done();
}
);
});
const shouldRerunRepeatableMigration = new Test('should rerun repeatable migration', (t, done) => {
const driver1 = t.locals.get('driver1');
const migration = t.locals.get('migrations').simple;
async.waterfall(
[
driver1.ensureMigrations,
driver1.runMigration.bind(driver1, {
level: migration.level,
script: migration.script,
directives: { audit: false },
}),
driver1.runMigration.bind(driver1, {
level: migration.level,
script: migration.script,
directives: { audit: false },
}),
driver1.getMigrations,
],
(err, migrations) => {
if (err) return done(err);
ok(migrations, 'Migrations created');
eq(migrations.length, 0);
done();
}
);
});
const shouldRecordNamespace = new Test('should record namespace', (t, done) => {
const driver1 = t.locals.get('driver1');
const migration = t.locals.get('migrations').namespace;
async.waterfall(
[driver1.ensureMigrations, driver1.runMigration.bind(driver1, migration), driver1.getMigrations],
(err, migrations) => {
if (err) return done(err);
ok(migrations, 'Migrations created');
eq(migrations.length, 1);
eq(migrations[0].level, migration.level);
eq(migrations[0].comment, migration.comment);
eq(migrations[0].timestamp.toISOString(), migration.timestamp.toISOString());
eq(migrations[0].checksum, migration.checksum);
eq(migrations[0].namespace, migration.namespace);
done();
}
);
});
const shouldIsolateByNamespace = new Test('should isolate by namespace', (t, done) => {
const driver1 = t.locals.get('driver1');
const { simple } = t.locals.get('migrations');
const namespaced = t.locals.get('migrations').namespace;
async.waterfall(
[
driver1.ensureMigrations,
driver1.runMigration.bind(driver1, simple),
driver1.runMigration.bind(driver1, namespaced),
driver1.getMigrations,
],
(err, migrations) => {
if (err) return done(err);
ok(migrations, 'Migrations created');
eq(migrations.length, 2);
eq(migrations[0].namespace, 'default');
eq(migrations[1].namespace, namespaced.namespace);
done();
}
);
});
const shouldReportMigrationErrors = new Test('should report migration errors', (t, done) => {
const driver1 = t.locals.get('driver1');
const migration = t.locals.get('migrations').fail;
async.waterfall([driver1.ensureMigrations, driver1.runMigration.bind(driver1, migration)], (err) => {
ok(err);
ok(err.migration);
eq(err.migration.level, 5);
eq(err.migration.script, 'INVALID');
done();
});
});
module.exports = new Suite('Compliance Tests')
.beforeEach(setupDatabase)
.add(shouldCreateMigrationsTableIfNotExists)
.add(shouldCreateMigrationsTableInParallel)
.add(shouldNotFailIfMigrationsTableAlreadyExists)
.add(shouldLockMigrationsTable)
.add(shouldRunMigration)
.add(shouldRerunRepeatableMigration)
.add(shouldRecordNamespace)
.add(shouldIsolateByNamespace)
.add(shouldReportMigrationErrors)
.afterEach(teardownDatabase);