Add tests and fixes for checkAllPatchesAvailable()

This commit is contained in:
Andrew Chilton 2014-08-20 17:14:01 +12:00
Родитель 9e5b7a10ea
Коммит 64fe490a93
2 изменённых файлов: 120 добавлений и 6 удалений

Просмотреть файл

@ -123,7 +123,6 @@ function readDbPatchLevel(callback) {
) )
} }
function readPatchFiles(callback) { function readPatchFiles(callback) {
var ctx = this var ctx = this
@ -183,7 +182,7 @@ function checkAllPatchesAvailable(callback) {
nextPatchLevel = currentPatchLevel + direction nextPatchLevel = currentPatchLevel + direction
// check that this patch exists // check that this patch exists
if ( !patches[currentPatchLevel][nextPatchLevel] ) { if ( !ctx.patches[currentPatchLevel] || !ctx.patches[currentPatchLevel][nextPatchLevel] ) {
process.nextTick(function() { process.nextTick(function() {
callback(new Error('Patch from level ' + currentPatchLevel + ' to ' + (currentPatchLevel+1) + ' does not exist')) callback(new Error('Patch from level ' + currentPatchLevel + ' to ' + (currentPatchLevel+1) + ' does not exist'))
}) })
@ -192,7 +191,7 @@ function checkAllPatchesAvailable(callback) {
// add this patch onto the patchesToApply // add this patch onto the patchesToApply
ctx.patchesToApply.push({ ctx.patchesToApply.push({
sql : patches[currentPatchLevel][nextPatchLevel], sql : ctx.patches[currentPatchLevel][nextPatchLevel],
from : currentPatchLevel, from : currentPatchLevel,
to : nextPatchLevel, to : nextPatchLevel,
}) })
@ -219,6 +218,13 @@ function closeConnection(callback) {
this.connection.end(callback) this.connection.end(callback)
} }
// exports // main export
module.exports.readPatchFiles = readPatchFiles
module.exports.patch = patch module.exports.patch = patch
// and these for testing purposes
module.exports.createDatabase = createDatabase
module.exports.changeUser = changeUser
module.exports.checkDbMetadataExists = checkDbMetadataExists
module.exports.readDbPatchLevel = readDbPatchLevel
module.exports.readPatchFiles = readPatchFiles
module.exports.checkAllPatchesAvailable = checkAllPatchesAvailable
module.exports.applyPatches = applyPatches

Просмотреть файл

@ -8,11 +8,13 @@ var patcher = require('../')
test('read patch set (ok)', function (t) { test('read patch set (ok)', function (t) {
var ctx = { var ctx = {
dir : path.join(__dirname, 'patches') dir : path.join(__dirname, 'patches'),
} }
// call readPatchFiles() with the above context // call readPatchFiles() with the above context
patcher.readPatchFiles.call(ctx, function(err) { patcher.readPatchFiles.call(ctx, function(err) {
t.ok(!err, 'No error occurred')
var patches = ctx.patches var patches = ctx.patches
// check there are 3 patch levels // check there are 3 patch levels
@ -33,3 +35,109 @@ test('read patch set (ok)', function (t) {
t.end() t.end()
}) })
}) })
test('check all patches are available (forwards)', function(t) {
var ctx = {
options : {
patchLevel : 2,
},
currentPatchLevel : 0,
patches : {
'0' : {
'1' : '-- 0->1\n',
},
'1' : {
'2' : '-- 1->2\n',
},
},
}
patcher.checkAllPatchesAvailable.call(ctx, function(err) {
t.ok(!err, 'No error occurred')
var patches = [
{ sql : '-- 0->1\n', from : 0, to : 1, },
{ sql : '-- 1->2\n', from : 1, to : 2, },
]
t.deepEqual(ctx.patchesToApply, patches, 'The patches to be applied')
t.end()
})
})
test('check all patches are available (backwards)', function(t) {
var ctx = {
options : {
patchLevel : 0,
},
currentPatchLevel : 2,
patches : {
'2' : {
'1' : '-- 2->1\n',
},
'1' : {
'0' : '-- 1->0\n',
},
},
}
patcher.checkAllPatchesAvailable.call(ctx, function(err) {
t.ok(!err, 'No error occurred')
var patches = [
{ sql : '-- 2->1\n', from : 2, to : 1, },
{ sql : '-- 1->0\n', from : 1, to : 0, },
]
t.deepEqual(ctx.patchesToApply, patches, 'The patches to be applied')
t.end()
})
})
test('check all patches are available (fails, no patch #2)', function(t) {
var ctx = {
options : {
patchLevel : 2,
},
currentPatchLevel : 0,
patches : {
'0' : {
'1' : '-- 0->1\n',
},
},
}
patcher.checkAllPatchesAvailable.call(ctx, function(err) {
t.ok(err, 'An error occurred since patch 2 is missing')
t.equal(err.message, 'Patch from level 1 to 2 does not exist', 'The error message is correct')
t.end()
})
})
test('check all patches are available (fails, no patch #1)', function(t) {
var ctx = {
options : {
patchLevel : 2,
},
currentPatchLevel : 0,
patches : {
'1' : {
'2' : '-- 1->2\n',
},
},
}
patcher.checkAllPatchesAvailable.call(ctx, function(err) {
t.ok(err, 'An error occurred since patch 1 is missing')
t.equal(err.message, 'Patch from level 0 to 1 does not exist', 'The error message is correct')
t.end()
})
})