github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/acceptance/testdata/node/json-test.js (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 const assert = require('assert'); 12 const client = require('./client'); 13 const rejectsWithPGError = require('./rejects-with-pg-error'); 14 15 ['JSON', 'JSONB'].forEach(t => { 16 describe(t, () => { 17 describe('round-tripping a value', () => { 18 const cases = [ 19 `123`, 20 `"hello"`, 21 `{}`, 22 `[]`, 23 `0`, 24 `0.0000`, 25 `""`, 26 '"\uD83D\uDE80"', 27 '{"\uD83D\uDE80": "hello"}', 28 `[1, 2, 3]`, 29 `{"foo": 123}`, 30 ]; 31 32 before(() => { 33 return client.query(`CREATE TABLE x (j ${t})`); 34 }); 35 36 after(() => { 37 return client.query(`DROP TABLE x`); 38 }); 39 40 cases.forEach(json => { 41 describe(json, () => { 42 beforeEach(() => { 43 return client.query(`DELETE FROM x`); 44 }); 45 it(`can be selected directly`, () => { 46 return client.query(`SELECT $1::${t} j`, [json]).then(results => { 47 assert.deepStrictEqual(results.rows[0].j, JSON.parse(json)); 48 }); 49 }); 50 51 it(`can be inserted into a table and then retrieved`, () => { 52 return client.query(`INSERT INTO x VALUES ($1)`, [json]) 53 .then(() => client.query(`SELECT j FROM x`)) 54 .then(results => { 55 assert.deepStrictEqual(results.rows[0].j, JSON.parse(json)); 56 }) 57 }); 58 }); 59 }); 60 }); 61 62 it('gives the right error code on invalid JSON', () => { 63 return rejectsWithPGError( 64 client.query({text: `SELECT '{"foo": 123'::JSONB`}), 65 {msg: 'unexpected EOF', code: '22P02'} 66 ); 67 }); 68 }); 69 });