github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/acceptance/testdata/node/base-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 fs                 = require('fs');
    12  const assert             = require('assert');
    13  const rejectsWithPGError = require('./rejects-with-pg-error');
    14  const client             = require('./client');
    15  
    16  // We orchestrate a failure here to ensure that a failing test actually causes
    17  // the docker build to fail.
    18  if (process.env.SHOULD_FAIL) {
    19    describe('failure smoke test', () => {
    20      it('causes the docker build to fail on a test failure', () => {
    21        assert.fail();
    22      });
    23    });
    24  }
    25  
    26  describe('select', () => {
    27    it('lets you select values', () => {
    28      return client.query("SELECT 1 as first, 2+$1 as second, ARRAY['\"','',''] as third", [3])
    29        .then(results => {
    30          assert.deepEqual(results.rows, [{
    31            first: 1,
    32            second: 5,
    33            third: ['"', '', '']
    34          }]);
    35        });
    36    });
    37  });
    38  
    39  describe('error cases', () => {
    40    const cases = [{
    41      name: 'not enough params',
    42      query: { text: 'SELECT 3', values: ['foo'] },
    43      msg: "expected 0 arguments, got 1",
    44      code: '08P01',
    45    }, {
    46      name: 'invalid utf8',
    47      query: { text: 'SELECT $1::STRING', values: [new Buffer([167])] },
    48      msg: "invalid UTF-8 sequence",
    49      code: '22021',
    50    }];
    51  
    52    cases.forEach(({ name, query, msg, code }) => {
    53      it(`${name} # ${query.text}`, () => {
    54        return rejectsWithPGError(client.query(query), { msg, code });
    55      });
    56    });
    57  });
    58  
    59  const NUMERIC_TYPES = ['INT', 'FLOAT', 'DECIMAL'];
    60  
    61  describe('arrays', () => {
    62    it('can be selected', () => {
    63      return client.query('SELECT ARRAY[1, 2, 3] a')
    64        .then(results => {
    65          assert.deepEqual([1, 2, 3], results.rows[0].a);
    66        });
    67    });
    68  
    69    NUMERIC_TYPES.forEach(t => {
    70      it(`can be passed as a placeholder for a ${t}[]`, () => {
    71        return client.query(`SELECT $1:::${t}[] a`, [[1, 2, 3]])
    72          .then(results => {
    73            assert.deepEqual([1, 2, 3], results.rows[0].a);
    74          });
    75      });
    76    });
    77  });
    78  
    79  describe('regression tests', () => {
    80    it('allows you to switch between format modes for arrays', () => {
    81      return client.query({
    82              text: 'SELECT $1:::int[] as b',
    83              values: [[1, 2, 8]],
    84              binary: false,
    85            }).then(r => {
    86              return client.query({
    87                text: 'SELECT $1:::int[] a',
    88                values: [[4, 5, 6]],
    89                binary: true,
    90              });
    91            }).then(results => {
    92              assert.deepEqual([4, 5, 6], results.rows[0].a);
    93            });
    94    });
    95  })