|
| 1 | +var helper = require(__dirname + '/test-helper'); |
| 2 | +var assert = require('assert'); |
| 3 | +//if you want binary support, pull request me! |
| 4 | +if (helper.config.binary) { |
| 5 | + console.log('binary mode does not support JSON right now'); |
| 6 | + return; |
| 7 | +} |
| 8 | + |
| 9 | +test('can read and write json', function() { |
| 10 | + helper.pg.connect(helper.config, function(err, client, done) { |
| 11 | + assert.ifError(err); |
| 12 | + helper.versionGTE(client, '9.2.0', assert.success(function(jsonSupported) { |
| 13 | + if(!jsonSupported) { |
| 14 | + console.log('skip json test on older versions of postgres'); |
| 15 | + done(); |
| 16 | + return helper.pg.end(); |
| 17 | + } |
| 18 | + client.query('CREATE TEMP TABLE stuff(id SERIAL PRIMARY KEY, data JSON)'); |
| 19 | + var value ={name: 'Brian', age: 250, alive: true, now: new Date()}; |
| 20 | + client.query('INSERT INTO stuff (data) VALUES ($1)', [value]); |
| 21 | + client.query('SELECT * FROM stuff', assert.success(function(result) { |
| 22 | + assert.equal(result.rows.length, 1); |
| 23 | + assert.equal(typeof result.rows[0].data, 'object'); |
| 24 | + var row = result.rows[0].data; |
| 25 | + assert.strictEqual(row.name, value.name); |
| 26 | + assert.strictEqual(row.age, value.age); |
| 27 | + assert.strictEqual(row.alive, value.alive); |
| 28 | + test('row should have "now" as a date', function() { |
| 29 | + return false; |
| 30 | + assert(row.now instanceof Date, 'row.now should be a date instance but is ' + typeof row.now); |
| 31 | + }); |
| 32 | + assert.equal(JSON.stringify(row.now), JSON.stringify(value.now)); |
| 33 | + done(); |
| 34 | + helper.pg.end(); |
| 35 | + })); |
| 36 | + })); |
| 37 | + }); |
| 38 | +}); |
0 commit comments