Skip to content

Instantly share code, notes, and snippets.

@avimar
Created July 21, 2012 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avimar/3157257 to your computer and use it in GitHub Desktop.
Save avimar/3157257 to your computer and use it in GitHub Desktop.
node-mysql unit test for affected rows
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