forked from fullcube/loopback-component-migrate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
225 lines (205 loc) · 7.76 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
'use strict';
var debug = require('debug')('loopback-component-migrate');
var _ = require('lodash');
var loopback = require('loopback');
var lt = require('loopback-testing');
var chai = require('chai');
var expect = chai.expect;
var sinon = require('sinon');
chai.use(require('sinon-chai'));
var path = require('path');
var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-app');
var app = require(path.join(SIMPLE_APP, 'server/server.js'));
global.Promise = require('bluebird');
lt.beforeEach.withApp(app);
describe('loopback db migrate', function() {
describe('initialization', function() {
it('should attach a Migration model to the app', function() {
expect(app.models.Migration).to.exist;
expect(app.models.Migration).itself.to.respondTo('migrate');
});
it('should attach a MigrationMap model to the app', function() {
expect(app.models.Migration).to.exist;
});
it('should provide a Migration.migrate() method', function() {
expect(app.models.Migration).itself.to.respondTo('migrate');
});
});
describe('migration', function() {
// Set up a spy for each migration function.
before(function() {
var m1 = require(path.join(SIMPLE_APP, 'server/migrations/0001-initialize.js'));
var m2 = require(path.join(SIMPLE_APP, 'server/migrations/0002-somechanges.js'));
var m3 = require(path.join(SIMPLE_APP, 'server/migrations/0003-morechanges.js'));
this.spies = {
m1Up: sinon.spy(m1, 'up'),
m1Down: sinon.spy(m1, 'down'),
m2Up: sinon.spy(m2, 'up'),
m2Down: sinon.spy(m2, 'down'),
m3Up: sinon.spy(m3, 'up'),
m3Down: sinon.spy(m3, 'down')
};
this.resetSpies = function() {
_.forEach(this.spies, function(spy) {
spy.reset();
});
};
this.expectNoDown = function() {
expect(this.spies.m1Down).not.to.have.been.called;
expect(this.spies.m2Down).not.to.have.been.called;
expect(this.spies.m3Down).not.to.have.been.called;
};
this.expectNoUp = function() {
expect(this.spies.m1Up).not.to.have.been.called;
expect(this.spies.m2Up).not.to.have.been.called;
expect(this.spies.m3Up).not.to.have.been.called;
};
});
// Reset all the spies after each test.
afterEach(function() {
this.resetSpies();
});
// Delete all data after each test.
beforeEach(function() {
return Promise.all([
app.models.Migration.destroyAll(),
app.models.Migration.destroyAll()
])
.then(function() {
return app.models.Migration.create({
name: '0000-error.js',
runDtTm: new Date()
})
})
});
describe('migrateByName', function() {
it('should set a property on app to indicate that migration is running', function(done) {
var self = this;
expect(app.migrating).to.be.undefined;
var promise = app.models.Migration.migrateByName('0002-somechanges.js');
expect(app.migrating).to.be.true;
promise.then(function() {
expect(app.migrating).to.be.undefined;
done();
})
.catch(done);
});
it('should log errors', function() {
return app.models.Migration.migrateByName('0000-error.js')
.catch(function(err) {
expect(err).to.not.be.undefined;
})
});
});
describe('migrate', function() {
it('should set a property on app to indicate that migrations are running', function() {
var self = this;
expect(app.migrating).to.be.undefined;
var promise = app.models.Migration.migrate();
expect(app.migrating).to.be.true;
return promise.then(function() {
expect(app.migrating).to.be.undefined;
})
});
});
describe('up', function() {
it('should run all migration scripts', function() {
var self = this;
return app.models.Migration.migrate()
.then(function() {
expect(self.spies.m1Up).to.have.been.called;
expect(self.spies.m2Up).to.have.been.calledAfter(self.spies.m1Up);
expect(self.spies.m3Up).to.have.been.calledAfter(self.spies.m2Up);
self.expectNoDown();
})
});
it('should run migrations up to the specificed point only', function() {
var self = this;
return app.models.Migration.migrate('up', '0002-somechanges')
.then(function() {
expect(self.spies.m1Up).to.have.been.calledBefore(self.spies.m2Up);
expect(self.spies.m2Up).to.have.been.calledAfter(self.spies.m1Up);
expect(self.spies.m3Up).not.to.have.been.called;
self.expectNoDown();
})
});
it('should not rerun migrations that hae already been run', function() {
var self = this;
return app.models.Migration.migrate('up', '0002-somechanges')
.then(function() {
self.resetSpies();
return app.models.Migration.migrate('up');
})
.then(function() {
expect(self.spies.m1Up).not.to.have.been.called;
expect(self.spies.m2Up).not.to.have.been.called;
expect(self.spies.m3Up).to.have.been.called;
self.expectNoDown();
})
});
});
describe('down', function() {
it('should run all rollback scripts in reverse order', function() {
var self = this;
return app.models.Migration.migrate('up')
.then(function() {
self.expectNoDown();
self.resetSpies();
return app.models.Migration.migrate('down');
})
.then(function() {
expect(self.spies.m3Down).to.have.been.calledBefore(self.spies.m2Down);
expect(self.spies.m2Down).to.have.been.calledAfter(self.spies.m3Down);
expect(self.spies.m1Down).to.have.been.calledAfter(self.spies.m2Down);
self.expectNoUp();
})
});
it('should run rollbacks up to the specificed point only', function() {
var self = this;
return app.models.Migration.migrate('up')
.then(function() {
self.expectNoDown();
self.resetSpies();
return app.models.Migration.migrate('down', '0001-initialize');
})
.then(function() {
expect(self.spies.m3Down).to.have.been.called;
expect(self.spies.m2Down).to.have.been.calledAfter(self.spies.m3Down);
expect(self.spies.m1Down).not.to.have.been.called;
self.expectNoUp();
})
});
it('should not rerun rollbacks that hae already been run', function() {
var self = this;
return app.models.Migration.migrate('up')
.then(function() {
return app.models.Migration.migrate('down', '0001-initialize');
})
.then(function() {
self.resetSpies();
return app.models.Migration.migrate('down');
})
.then(function() {
expect(self.spies.m3Down).to.not.have.been.called;
expect(self.spies.m2Down).to.not.have.been.called;
expect(self.spies.m1Down).to.have.been.called;
self.expectNoUp();
})
});
it('should rollback a single migration that has not already run', function() {
var self = this;
return app.models.Migration.migrate('up', '0002-somechanges')
.then(function() {
self.resetSpies();
return app.models.Migration.migrate('down', '0003-morechanges');
})
.then(function() {
expect(self.spies.m3Down).to.have.been.called;
expect(self.spies.m2Down).to.not.have.been.called;
expect(self.spies.m1Down).to.not.have.been.called;
self.expectNoUp();
})
});
});
});
});