|
| 1 | +var async = require('async'); |
| 2 | +var helper = require('../test-helper'); |
| 3 | + |
| 4 | +var db = helper.client(); |
| 5 | + |
| 6 | +function createTableFoo(callback){ |
| 7 | + db.query("create temp table foo(column1 int, column2 int)", callback); |
| 8 | +} |
| 9 | + |
| 10 | +function createTableBar(callback){ |
| 11 | + db.query("create temp table bar(column1 text, column2 text)", callback); |
| 12 | +} |
| 13 | + |
| 14 | +function insertDataFoo(callback){ |
| 15 | + db.query({ |
| 16 | + name: 'insertFoo', |
| 17 | + text: 'insert into foo values($1,$2)', |
| 18 | + values:['one','two'] |
| 19 | + }, callback ); |
| 20 | +} |
| 21 | + |
| 22 | +function insertDataBar(callback){ |
| 23 | + db.query({ |
| 24 | + name: 'insertBar', |
| 25 | + text: 'insert into bar values($1,$2)', |
| 26 | + values:['one','two'] |
| 27 | + }, callback ); |
| 28 | +} |
| 29 | + |
| 30 | +function startTransaction(callback) { |
| 31 | + db.query('BEGIN', callback); |
| 32 | +} |
| 33 | +function endTransaction(callback) { |
| 34 | + db.query('COMMIT', callback); |
| 35 | +} |
| 36 | + |
| 37 | +function doTransaction(callback) { |
| 38 | + // The transaction runs startTransaction, then all queries, then endTransaction, |
| 39 | + // no matter if there has been an error in a query in the middle. |
| 40 | + startTransaction(function() { |
| 41 | + insertDataFoo(function() { |
| 42 | + insertDataBar(function() { |
| 43 | + endTransaction( callback ); |
| 44 | + }); |
| 45 | + }); |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +var steps = [ |
| 50 | + createTableFoo, |
| 51 | + createTableBar, |
| 52 | + doTransaction, |
| 53 | + insertDataBar |
| 54 | +] |
| 55 | + |
| 56 | +test('test if query fails', function() { |
| 57 | + async.series(steps, assert.success(function() { |
| 58 | + db.end() |
| 59 | + })) |
| 60 | +}) |
| 61 | + |
| 62 | +test('test if prepare works but bind fails', function() { |
| 63 | + var client = helper.client(); |
| 64 | + var q = { |
| 65 | + text: 'SELECT $1::int as name', |
| 66 | + values: ['brian'], |
| 67 | + name: 'test' |
| 68 | + }; |
| 69 | + client.query(q, assert.calls(function(err, res) { |
| 70 | + q.values = [1]; |
| 71 | + client.query(q, assert.calls(function(err, res) { |
| 72 | + assert.ifError(err); |
| 73 | + client.end(); |
| 74 | + })); |
| 75 | + })); |
| 76 | +}); |
| 77 | + |
0 commit comments