Skip to content

Commit

Permalink
updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Nov 14, 2020
1 parent 75075a1 commit 5b792d2
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 103 deletions.
4 changes: 3 additions & 1 deletion lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ function $stream(ctx, qs, initCB, config) {
if (ctx.options.pgNative) {
return $p.reject(new Error(npm.text.nativeStreaming));
}
// Stream class was renamed again, see the following issue:
// https://github.com/brianc/node-postgres/issues/2412
if (!qs || !qs.constructor || qs.constructor.name !== `QueryStream`) {
// stream object wasn't passed in correctly;
// invalid or missing stream object;
return $p.reject(new TypeError(npm.text.invalidStream));
}
if (qs._reading || qs._closed) {
Expand Down
2 changes: 1 addition & 1 deletion lib/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = {

/* streaming support */
nativeStreaming: `Streaming doesn't work with Native Bindings.`,
invalidStream: `Invalid or missing stream object.`,
invalidStream: `Invalid or missing stream object: pg-query-stream >= v3.4.2 was expected`,
invalidStreamState: `Invalid stream state.`,
invalidStreamCB: `Invalid or missing stream initialization callback.`,

Expand Down
9 changes: 8 additions & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
const npm = {
path: require(`path`),
util: require(`util`),
patterns: require(`../patterns`)
patterns: require(`../patterns`),
os: require(`os`)
};

////////////////////////////////////////////
Expand Down Expand Up @@ -202,6 +203,8 @@ function toJson(data) {
}
}

const platform = npm.os.platform();

const exp = {
toJson,
getIfHas,
Expand All @@ -212,6 +215,10 @@ const exp = {
isText,
isNull,
isDev,
platform: {
isWindows: platform === `win32`,
isMac: platform === `darwin`
},
addReadProp,
addReadProperties,
getSafeConnection,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-promise",
"version": "10.7.4",
"version": "10.7.5-beta.0",
"description": "PostgreSQL interface for Node.js",
"main": "lib/index.js",
"typings": "typescript/pg-promise.d.ts",
Expand Down
207 changes: 108 additions & 99 deletions test/db.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const pgResult = require(`pg/lib/result`);
const header = require(`./db/header`);
const tools = require(`./db/tools`);

const isMacOS = require(`os`).platform() === `darwin`;
const {isMac, isWindows} = require(`../lib/utils`).platform;

const promise = header.defPromise;
const options = {
Expand Down Expand Up @@ -164,7 +164,7 @@ describe(`Connection`, () => {
.finally(done);
});
it(`must report the right error`, () => {
if (!isMacOS) {
if (!isMac) {
// we do not test this on MacOS, because it requires use of password, so the test will fail.
expect(log.e.cn).toEqual(errCN);
}
Expand Down Expand Up @@ -239,50 +239,60 @@ describe(`Connection`, () => {
});
});

describe(`for invalid connection`, () => {
const dbErr = pgp(`bla-bla`);
let error;
beforeEach(done => {
dbErr.connect()
.catch(err => {
error = err;
})
.finally(done);
});
it(`must report the right error`, () => {
const oldStyleError = `database "bla-bla" does not exist`; // Before PostgreSQL v.10
const newStyleError = `role ` + JSON.stringify(pgp.pg.defaults.user) + ` does not exist`;
expect(error instanceof Error).toBe(true);
if (isMacOS) {
expect(error.message).toContain(`password authentication failed for user`);
} else {
expect(error.message.indexOf(oldStyleError) >= 0 || error.message.indexOf(newStyleError) >= 0).toBe(true);
}
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;
beforeEach(done => {
dbErr.connect()
.catch(err => {
error = err;
})
.finally(done);
});
it(`must report the right error`, () => {
const oldStyleError = `database "bla-bla" does not exist`; // Before PostgreSQL v.10
const newStyleError = `role ` + JSON.stringify(pgp.pg.defaults.user) + ` does not exist`;
expect(error instanceof Error).toBe(true);
if (isMac) {
expect(error.message).toContain(`password authentication failed for user`);
} else {
expect(error.message.indexOf(oldStyleError) >= 0 || error.message.indexOf(newStyleError) >= 0).toBe(true);
}
});
});
});
}

describe(`for invalid user name`, () => {
const errCN = JSON.parse(JSON.stringify(dbHeader.cn)); // dumb connection cloning;
errCN.user = `somebody`;
const dbErr = pgp(errCN);
let error;
beforeEach(done => {
dbErr.connect()
.catch(err => {
error = err;
})
.finally(done);
});
it(`must report the right error`, () => {
expect(error instanceof Error).toBe(true);
const macError = `password authentication failed for user "somebody"`;
if (isMacOS) {
expect(error.message).toContain(macError);
} else {
expect(error.message).toContain(`role "somebody" does not exist`);
}
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`;
const dbErr = pgp(errCN);
let error;
beforeEach(done => {
dbErr.connect()
.catch(err => {
error = err;
})
.finally(done);
});
it(`must report the right error`, () => {
expect(error instanceof Error).toBe(true);
const macError = `password authentication failed for user "somebody"`;
if (isMac) {
expect(error.message).toContain(macError);
} else {
expect(error.message).toContain(`role "somebody" does not exist`);
}
});
});
});
}

describe(`on repeated disconnection`, () => {
let error;
Expand Down Expand Up @@ -342,69 +352,68 @@ describe(`Connection`, () => {
});
});

/*
Commenting this out again, on July 11, 2019, because 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;
const dbSingleCN = pgp(singleCN);
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;
const dbSingleCN = pgp(singleCN);

let error;
let error;

beforeEach(done => {
dbSingleCN.connect()
.then(obj => {
obj.func('pg_backend_pid')
.then(res => {
const pid = res[0].pg_backend_pid;
return promise.all([
obj.proc('pg_sleep', [2])
.catch(reason => {
error = reason;
}),
// Terminate connection after a short delay, before the query finishes
promise.delay(1000)
.then(() =>
db.one('SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid = $1', pid)
)
])
.finally(() => {
obj.done(error);
done();
});
});
});
});
beforeEach(done => {
dbSingleCN.connect()
.then(obj => {
obj.func(`pg_backend_pid`)
.then(res => {
const pid = res[0].pg_backend_pid;
return promise.all([
obj.proc(`pg_sleep`, [2])
.catch(reason => {
error = reason;
}),
// Terminate connection after a short delay, before the query finishes
promise.delay(1000)
.then(() =>
db.one(`SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid = $1`, pid)
)
])
.finally(() => {
obj.done(error);
done();
});
});
});
});

it('returns the postgres error', () => {
expect(error instanceof Error).toBe(true);
expect(error.code).toEqual('57P01');
expect(error.message).toEqual('terminating connection due to administrator command');
});
it(`returns the postgres error`, () => {
expect(error instanceof Error).toBe(true);
expect(error.code).toEqual(`57P01`);
expect(error.message).toEqual(`terminating connection due to administrator command`);
});

it('releases the client from the pool', done => {
let result, singleError;
it(`releases the client from the pool`, done => {
let result, singleError;

dbSingleCN.one('SELECT 1 as test;')
.then(data => {
result = data;
})
.catch(reason => {
singleError = reason;
})
.then(() => {
expect(singleError).not.toBeDefined();
expect(result).toEqual({test: 1});
done();
});
dbSingleCN.one(`SELECT 1 as test`)
.then(data => {
result = data;
})
.catch(reason => {
singleError = reason;
})
.then(() => {
expect(singleError).not.toBeDefined();
expect(result).toEqual({test: 1});
done();
});

});
});
});
*/

}
});

describe(`Direct Connection`, () => {
Expand Down

0 comments on commit 5b792d2

Please sign in to comment.