|
1 | 1 | var helper = require(__dirname + '/test-helper');
|
2 | 2 | var util = require('util');
|
3 | 3 |
|
4 |
| -test('error during query execution', function() { |
5 |
| - var client = new Client(helper.args); |
6 |
| - process.removeAllListeners('uncaughtException'); |
7 |
| - assert.emits(process, 'uncaughtException', function() { |
8 |
| - assert.equal(client.activeQuery, null, 'should remove active query even if error happens in callback'); |
9 |
| - client.query('SELECT * FROM blah', assert.success(function(result) { |
10 |
| - assert.equal(result.rows.length, 1); |
11 |
| - client.end(); |
12 |
| - })); |
13 |
| - }); |
14 |
| - client.connect(assert.success(function() { |
15 |
| - client.query('CREATE TEMP TABLE "blah"(data text)', assert.success(function() { |
16 |
| - var q = client.query('INSERT INTO blah(data) VALUES($1)', ['yo'], assert.success(function() { |
17 |
| - assert.emits(client, 'drain'); |
18 |
| - throw new Error('WHOOOAAAHH!!'); |
| 4 | +var withQuery = function(text, resultLength, cb) { |
| 5 | + test('error during query execution', function() { |
| 6 | + var client = new Client(helper.args); |
| 7 | + process.removeAllListeners('uncaughtException'); |
| 8 | + assert.emits(process, 'uncaughtException', function() { |
| 9 | + console.log('got uncaught exception') |
| 10 | + assert.equal(client.activeQuery, null, 'should remove active query even if error happens in callback'); |
| 11 | + client.query('SELECT * FROM blah', assert.success(function(result) { |
| 12 | + assert.equal(result.rows.length, resultLength); |
| 13 | + client.end(); |
| 14 | + cb(); |
| 15 | + })); |
| 16 | + }); |
| 17 | + client.connect(assert.success(function() { |
| 18 | + client.query('CREATE TEMP TABLE "blah"(data text)', assert.success(function() { |
| 19 | + var q = client.query(text, ['yo'], assert.calls(function() { |
| 20 | + assert.emits(client, 'drain'); |
| 21 | + throw new Error('WHOOOAAAHH!!'); |
| 22 | + })); |
19 | 23 | }));
|
20 | 24 | }));
|
21 |
| - })); |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +//test with good query so our callback is called |
| 29 | +//as a successful callback |
| 30 | +withQuery('INSERT INTO blah(data) VALUES($1)', 1, function() { |
| 31 | + //test with an error query so our callback is called with an error |
| 32 | + withQuery('INSERT INTO asldkfjlaskfj eoooeoriiri', 0, function() { |
| 33 | + }); |
22 | 34 | });
|
0 commit comments