|
| 1 | +var Libpq = require('../'); |
| 2 | +var assert = require('assert'); |
| 3 | + |
| 4 | +describe('transactionStatus', function() { |
| 5 | + |
| 6 | + it('returns PQTRANS_UNKNOWN when not connected', function() { |
| 7 | + var pq = new Libpq(); |
| 8 | + assert.strictEqual(pq.transactionStatus(), 4); |
| 9 | + }); |
| 10 | + |
| 11 | + it('returns PQTRANS_IDLE after connecting', function() { |
| 12 | + var pq = new Libpq(); |
| 13 | + pq.connectSync(); |
| 14 | + assert.strictEqual(pq.transactionStatus(), 0); |
| 15 | + pq.finish(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('returns PQTRANS_IDLE after a simple query', function() { |
| 19 | + var pq = new Libpq(); |
| 20 | + pq.connectSync(); |
| 21 | + pq.exec('SELECT 1'); |
| 22 | + assert.strictEqual(pq.transactionStatus(), 0); |
| 23 | + pq.finish(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns PQTRANS_INTRANS inside a transaction', function() { |
| 27 | + var pq = new Libpq(); |
| 28 | + pq.connectSync(); |
| 29 | + pq.exec('BEGIN'); |
| 30 | + assert.strictEqual(pq.transactionStatus(), 2); |
| 31 | + pq.exec('ROLLBACK'); |
| 32 | + pq.finish(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('returns PQTRANS_IDLE after COMMIT', function() { |
| 36 | + var pq = new Libpq(); |
| 37 | + pq.connectSync(); |
| 38 | + pq.exec('BEGIN'); |
| 39 | + assert.strictEqual(pq.transactionStatus(), 2); |
| 40 | + pq.exec('COMMIT'); |
| 41 | + assert.strictEqual(pq.transactionStatus(), 0); |
| 42 | + pq.finish(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns PQTRANS_INERROR after a failed query in a transaction', function() { |
| 46 | + var pq = new Libpq(); |
| 47 | + pq.connectSync(); |
| 48 | + pq.exec('BEGIN'); |
| 49 | + pq.exec('SELECT FROM nonexistent_table_xyz'); |
| 50 | + assert.strictEqual(pq.transactionStatus(), 3); |
| 51 | + pq.exec('ROLLBACK'); |
| 52 | + pq.finish(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns PQTRANS_IDLE after ROLLBACK from error state', function() { |
| 56 | + var pq = new Libpq(); |
| 57 | + pq.connectSync(); |
| 58 | + pq.exec('BEGIN'); |
| 59 | + pq.exec('SELECT FROM nonexistent_table_xyz'); |
| 60 | + assert.strictEqual(pq.transactionStatus(), 3); |
| 61 | + pq.exec('ROLLBACK'); |
| 62 | + assert.strictEqual(pq.transactionStatus(), 0); |
| 63 | + pq.finish(); |
| 64 | + }); |
| 65 | + |
| 66 | +}); |
0 commit comments