Add tests and fixes for checkAllPatchesAvailable()
This commit is contained in:
Родитель
9e5b7a10ea
Коммит
64fe490a93
16
index.js
16
index.js
|
@ -123,7 +123,6 @@ function readDbPatchLevel(callback) {
|
|||
)
|
||||
}
|
||||
|
||||
|
||||
function readPatchFiles(callback) {
|
||||
var ctx = this
|
||||
|
||||
|
@ -183,7 +182,7 @@ function checkAllPatchesAvailable(callback) {
|
|||
nextPatchLevel = currentPatchLevel + direction
|
||||
|
||||
// check that this patch exists
|
||||
if ( !patches[currentPatchLevel][nextPatchLevel] ) {
|
||||
if ( !ctx.patches[currentPatchLevel] || !ctx.patches[currentPatchLevel][nextPatchLevel] ) {
|
||||
process.nextTick(function() {
|
||||
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
|
||||
ctx.patchesToApply.push({
|
||||
sql : patches[currentPatchLevel][nextPatchLevel],
|
||||
sql : ctx.patches[currentPatchLevel][nextPatchLevel],
|
||||
from : currentPatchLevel,
|
||||
to : nextPatchLevel,
|
||||
})
|
||||
|
@ -219,6 +218,13 @@ function closeConnection(callback) {
|
|||
this.connection.end(callback)
|
||||
}
|
||||
|
||||
// exports
|
||||
// main export
|
||||
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.patch = patch
|
||||
module.exports.checkAllPatchesAvailable = checkAllPatchesAvailable
|
||||
module.exports.applyPatches = applyPatches
|
||||
|
|
110
test/patches.js
110
test/patches.js
|
@ -8,11 +8,13 @@ var patcher = require('../')
|
|||
|
||||
test('read patch set (ok)', function (t) {
|
||||
var ctx = {
|
||||
dir : path.join(__dirname, 'patches')
|
||||
dir : path.join(__dirname, 'patches'),
|
||||
}
|
||||
|
||||
// call readPatchFiles() with the above context
|
||||
patcher.readPatchFiles.call(ctx, function(err) {
|
||||
t.ok(!err, 'No error occurred')
|
||||
|
||||
var patches = ctx.patches
|
||||
|
||||
// check there are 3 patch levels
|
||||
|
@ -33,3 +35,109 @@ test('read patch set (ok)', function (t) {
|
|||
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()
|
||||
})
|
||||
|
||||
})
|
||||
|
|
Загрузка…
Ссылка в новой задаче