You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.9 KiB
69 lines
1.9 KiB
8 years ago
|
const assert = require('assert')
|
||
|
const clone = require('clone')
|
||
7 years ago
|
const Migrator = require('../../../app/scripts/lib/migrator/')
|
||
|
const liveMigrations = require('../../../app/scripts/migrations/')
|
||
7 years ago
|
const stubMigrations = [
|
||
8 years ago
|
{
|
||
|
version: 1,
|
||
|
migrate: (data) => {
|
||
|
// clone the data just like we do in migrations
|
||
|
const clonedData = clone(data)
|
||
|
clonedData.meta.version = 1
|
||
|
return Promise.resolve(clonedData)
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
version: 2,
|
||
|
migrate: (data) => {
|
||
|
const clonedData = clone(data)
|
||
|
clonedData.meta.version = 2
|
||
|
return Promise.resolve(clonedData)
|
||
|
},
|
||
|
},
|
||
|
{
|
||
|
version: 3,
|
||
|
migrate: (data) => {
|
||
|
const clonedData = clone(data)
|
||
|
clonedData.meta.version = 3
|
||
|
return Promise.resolve(clonedData)
|
||
|
},
|
||
|
},
|
||
|
]
|
||
8 years ago
|
const versionedData = {meta: {version: 0}, data: {hello: 'world'}}
|
||
7 years ago
|
|
||
|
const firstTimeState = {
|
||
|
meta: { version: 0 },
|
||
7 years ago
|
data: require('../../../app/scripts/first-time-state'),
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
describe('Migrator', () => {
|
||
7 years ago
|
const migrator = new Migrator({ migrations: stubMigrations })
|
||
8 years ago
|
it('migratedData version should be version 3', (done) => {
|
||
|
migrator.migrateData(versionedData)
|
||
|
.then((migratedData) => {
|
||
7 years ago
|
assert.equal(migratedData.meta.version, stubMigrations[2].version)
|
||
8 years ago
|
done()
|
||
|
}).catch(done)
|
||
|
})
|
||
7 years ago
|
|
||
|
it('should match the last version in live migrations', (done) => {
|
||
|
const migrator = new Migrator({ migrations: liveMigrations })
|
||
|
migrator.migrateData(firstTimeState)
|
||
|
.then((migratedData) => {
|
||
|
const last = liveMigrations.length - 1
|
||
|
assert.equal(migratedData.meta.version, liveMigrations[last].version)
|
||
|
done()
|
||
|
}).catch(done)
|
||
|
})
|
||
|
|
||
7 years ago
|
it('should emit an error', function (done) {
|
||
|
this.timeout(15000)
|
||
|
const migrator = new Migrator({ migrations: [{ version: 1, migrate: async () => { throw new Error('test') } } ] })
|
||
|
migrator.on('error', () => done())
|
||
|
migrator.migrateData({ meta: {version: 0} })
|
||
|
.then((migratedData) => {
|
||
|
}).catch(done)
|
||
|
})
|
||
|
|
||
8 years ago
|
})
|