Skip to content

Commit

Permalink
Fixing issue #911 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Mar 21, 2024
1 parent 79199d4 commit 1a4dfe6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
5 changes: 3 additions & 2 deletions lib/formatting.js
Expand Up @@ -839,7 +839,7 @@ const $as = {
* - `obj` - the formatting object, and is the same as `this` context
*
* - For $[Index Variables] formatting:
* - `index` - element's index (starts with 1) that's outside of the input array
* - `index` - element's index (starts with 1) that's outside the input array
* - `arr` - the formatting/input array, and is the same as `this` context
*
* You can tell which type of call it is by checking the type of the first parameter.
Expand Down Expand Up @@ -884,7 +884,8 @@ const $to = {
},
number(num) {
if (typeof num === 'bigint' || Number.isFinite(num)) {
return num.toString();
const s = num.toString();
return num < 0 ? `(${s})` : s;
}
// Converting NaN/+Infinity/-Infinity according to Postgres documentation:
// http://www.postgresql.org/docs/9.6/static/datatype-numeric.html#DATATYPE-FLOAT
Expand Down
10 changes: 0 additions & 10 deletions test/db.spec.js
Expand Up @@ -239,9 +239,6 @@ describe('Connection', () => {
});

if (!isWindows) {
/*
Disabled this test on 14/11/2020, since it started showing crypto-related issues on Windows;
*/
describe('for invalid connection', () => {
const dbErr = pgp('bla-bla');
let error;
Expand All @@ -268,9 +265,6 @@ describe('Connection', () => {
}

if (!isWindows) {
/*
Disabled this test on 14/11/2020, since it started showing crypto-related issues on Windows;
*/
describe('for invalid user name', () => {
const errCN = JSON.parse(JSON.stringify(dbHeader.cn)); // dumb connection cloning;
errCN.user = 'somebody';
Expand Down Expand Up @@ -354,10 +348,6 @@ describe('Connection', () => {
});

if (!isWindows) {
/*
This test has been known to be very flaky, especially on Windows, and keeps
getting in the way of testing the framework locally.
*/
describe('db side closing of the connection pool', () => {
const singleCN = JSON.parse(JSON.stringify(dbHeader.cn)); // dumb connection cloning;
singleCN.max = 1;
Expand Down
10 changes: 5 additions & 5 deletions test/format.spec.js
Expand Up @@ -122,7 +122,7 @@ describe('Method as.number', () => {
expect(pgp.as.number(0)).toBe('0');
expect(pgp.as.number(1)).toBe('1');
expect(pgp.as.number(1234567890)).toBe('1234567890');
expect(pgp.as.number(-123.456)).toBe('-123.456');
expect(pgp.as.number(-123.456)).toBe('(-123.456)');
expect(pgp.as.number(NaN)).toBe('\'NaN\'');
expect(pgp.as.number(Number.NaN)).toBe('\'NaN\'');
expect(pgp.as.number(1 / 0)).toBe('\'+Infinity\'');
Expand Down Expand Up @@ -326,8 +326,8 @@ describe('Method as.csv', () => {

expect(pgp.as.csv(0)).toBe('0'); // test zero;
expect(pgp.as.csv([0])).toBe('0'); // test zero in array;
expect(pgp.as.csv(-123.456)).toBe('-123.456'); // test a float;
expect(pgp.as.csv([-123.456])).toBe('-123.456'); // test a float in array;
expect(pgp.as.csv(-123.456)).toBe('(-123.456)'); // test a float;
expect(pgp.as.csv([-123.456])).toBe('(-123.456)'); // test a float in array;

expect(pgp.as.csv(true)).toBe('true'); // test boolean True;
expect(pgp.as.csv([true])).toBe('true'); // test boolean True in array;
Expand Down Expand Up @@ -678,7 +678,7 @@ describe('Method as.format', () => {
expect(pgp.as.format('$1', [userObj])).toBe(pgp.as.text(JSON.stringify(userObj)));
expect(pgp.as.format('$1^', [userObj])).toBe(JSON.stringify(userObj));

expect(pgp.as.format('$1, $2, $3, $4', [true, -12.34, 'text', dateSample])).toBe(`true, -12.34, 'text', '${$pgUtils.prepareValue(dateSample)}'`);
expect(pgp.as.format('$1, $2, $3, $4', [true, -12.34, 'text', dateSample])).toBe(`true, (-12.34), 'text', '${$pgUtils.prepareValue(dateSample)}'`);

expect(pgp.as.format('$1 $1, $2 $2, $1', [1, 'two'])).toBe('1 1, \'two\' \'two\', 1'); // test for repeated variables;

Expand Down Expand Up @@ -970,7 +970,7 @@ describe('Named Parameters', () => {
three: -123.45,
four: dateSample,
five: () => 'text'
})).toBe('null,true,-123.45,\'' + $pgUtils.prepareValue(dateSample) + '\',\'text\'');
})).toBe('null,true,(-123.45),\'' + $pgUtils.prepareValue(dateSample) + '\',\'text\'');
});

it('must treat null and undefined values equally', () => {
Expand Down

0 comments on commit 1a4dfe6

Please sign in to comment.