Created
July 21, 2012 21:31
-
-
Save avimar/3157257 to your computer and use it in GitHub Desktop.
node-mysql unit test for affected rows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var common = require('../common'); | |
var connection = common.createConnection(); | |
var assert = require('assert'); | |
common.useTestDb(connection); | |
var table = 'insert_test'; | |
connection.query([ | |
'CREATE TEMPORARY TABLE `' + table + '` (', | |
'`title` varchar(255),', | |
'`text` varchar(255)', | |
') ENGINE=InnoDB DEFAULT CHARSET=utf8' | |
].join('\n')); | |
connection.query( | |
'INSERT INTO ' + table + ' SET title = ?, text = ?', ['my title', 'this is a nice long text'] | |
,function(err, _result) { | |
if (err) throw err; | |
result = _result; | |
}); | |
var unchanged; | |
connection.query( | |
'UPDATE ' + table + ' SET text = ? where text = ?', | |
['this is a nice long text','this is a nice long text'], function(err, results, fields){ | |
if (err) throw err; | |
unchanged=results; | |
} | |
); | |
var changed; | |
connection.query( | |
'UPDATE ' + table + ' SET title = ? where title = ?', | |
['new title','my title'], function(err, results, fields){ | |
if (err) throw err; | |
changed=results; | |
} | |
); | |
connection.end(); | |
process.on('exit', function() { | |
assert.equal(unchanged.affectedRows, 0); | |
assert.equal(unchanged.message, "(Rows matched: 1 Changed: 0 Warnings: 0"); | |
assert.equal(changed.affectedRows, 1); | |
assert.equal(changed.message, "(Rows matched: 1 Changed: 1 Warnings: 0"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment