|
| 1 | +var timeoutSeconds = 5; |
| 2 | +var tid = setTimeout(function() { |
| 3 | + throw new Error('Tests did not finish within ' + timeoutSeconds + ' seconds'); |
| 4 | +}, timeoutSeconds * 1000); |
| 5 | + |
| 6 | +var assert = require('assert'); |
| 7 | +var async = require('async'); |
| 8 | +var ok = require('okay'); |
| 9 | +var pg = require('pg'); |
| 10 | + |
| 11 | +var setup = function() { |
| 12 | + before(function(done) { |
| 13 | + this.client = new pg.Client(); |
| 14 | + this.client.connect(); |
| 15 | + var client = this.client; |
| 16 | + client.query('CREATE TEMP TABLE floatz(small FLOAT4, big FLOAT8, num NUMERIC)', ok(function(result) { |
| 17 | + client.query('INSERT INTO floatz(small, big, num) VALUES($1, $2, $3)', [1.1, '2.2', 3.3], ok(function(result) { |
| 18 | + done(); |
| 19 | + })); |
| 20 | + })); |
| 21 | + }); |
| 22 | +}; |
| 23 | + |
| 24 | +var teardown = function() { |
| 25 | + after(function(done) { |
| 26 | + this.client.on('end', done); |
| 27 | + this.client.end(); |
| 28 | + }); |
| 29 | +}; |
| 30 | + |
| 31 | +describe('existing (string) functionality', function() { |
| 32 | + setup(); |
| 33 | + |
| 34 | + it('returns floats as strings', function(done) { |
| 35 | + var client = this.client; |
| 36 | + client.query('SELECT * FROM floatz', ok(function(result) { |
| 37 | + assert(result, 'should have returned result'); |
| 38 | + assert(result.rows, 'result should have rows'); |
| 39 | + assert.equal(result.rows.length, 1, 'should have 1 row but returned ' + result.rows.length); |
| 40 | + var row = result.rows.pop(); |
| 41 | + assert.strictEqual(row.small, '1.1'); |
| 42 | + assert.strictEqual(row.big, '2.2'); |
| 43 | + assert.strictEqual(row.num, '3.3'); |
| 44 | + done(); |
| 45 | + })); |
| 46 | + }); |
| 47 | + |
| 48 | + teardown(); |
| 49 | +}); |
| 50 | + |
| 51 | +describe('new (parseFloat) functionality', function() { |
| 52 | + before(function() { |
| 53 | + //load the pg-parse-float module |
| 54 | + require(__dirname)(pg); |
| 55 | + }); |
| 56 | + |
| 57 | + setup(); |
| 58 | + |
| 59 | + it('returns floats as floats', function(done) { |
| 60 | + var client = this.client; |
| 61 | + client.query('SELECT * FROM floatz', ok(function(result) { |
| 62 | + assert(result, 'should have returned result'); |
| 63 | + assert(result.rows, 'result should have rows'); |
| 64 | + assert.equal(result.rows.length, 1, 'should have 1 row but returned ' + result.rows.length); |
| 65 | + var row = result.rows.pop(); |
| 66 | + assert.strictEqual(row.small, 1.1); |
| 67 | + assert.strictEqual(row.big, 2.2); |
| 68 | + assert.strictEqual(row.num, 3.3); |
| 69 | + done(); |
| 70 | + })); |
| 71 | + }); |
| 72 | + |
| 73 | + teardown(); |
| 74 | +}); |
0 commit comments