Skip to content

Commit

Permalink
improving _TN + adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed May 25, 2024
1 parent 8802545 commit 19776db
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/helpers/table-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ function _TN(a, ...args) {
a = a.map((b, i) => b + (i < args.length ? args[i] : '')).join('');
} // else 'a' is a string
const [schema, table] = a.split('.');
return table === undefined ? {table: schema} : {schema, table};
if(table === undefined) {
return {table: schema};
}
return schema ? {schema, table} : {table};
}

module.exports = {TableName, _TN};
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": "11.7.5",
"version": "11.7.6",
"description": "PostgreSQL interface for Node.js",
"main": "lib/index.js",
"typings": "typescript/pg-promise.d.ts",
Expand Down
57 changes: 57 additions & 0 deletions test/help.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -927,3 +927,60 @@ describe('method \'concat\'', () => {
});
});
});

describe('_TN', () => {
describe('for strings', () => {
it('must support empty', () => {
expect(helpers._TN('')).toEqual({table: ''});
expect(helpers._TN('.')).toEqual({table: ''});
expect(helpers._TN('...')).toEqual({table: ''});
});
it('must ignore extras', () => {
expect(helpers._TN('a.b.c')).toEqual({schema: 'a', table: 'b'});
});
it('must support full names', () => {
expect(helpers._TN('ss.tt')).toEqual({schema: 'ss', table: 'tt'});
});
it('must support table-only', () => {
expect(helpers._TN('t1')).toEqual({table: 't1'});
expect(helpers._TN('.t2')).toEqual({table: 't2'});
});
it('must support schema-only', () => {
expect(helpers._TN('ss.')).toEqual({schema: 'ss', table: ''});
});
});
describe('for templates', () => {
it('must support empty', () => {
expect(helpers._TN``).toEqual({table: ''});
expect(helpers._TN`.`).toEqual({table: ''});
expect(helpers._TN`...`).toEqual({table: ''});
});
it('must ignore extras', () => {
expect(helpers._TN`a.b.c`).toEqual({schema: 'a', table: 'b'});
});
it('must support full names', () => {
expect(helpers._TN`ss.tt`).toEqual({schema: 'ss', table: 'tt'});
});
it('must support table-only', () => {
expect(helpers._TN`t1`).toEqual({table: 't1'});
expect(helpers._TN`.t2`).toEqual({table: 't2'});
});
it('must support schema-only', () => {
expect(helpers._TN`ss.`).toEqual({schema: 'ss', table: ''});
});
it('must support schema-only parameter', () => {
const schema = 'ss';
expect(helpers._TN`${schema}.`).toEqual({schema: 'ss', table: ''});
});
it('must support table-only parameter', () => {
const table = 'tt';
expect(helpers._TN`${table}`).toEqual({table: 'tt'});
expect(helpers._TN`.${table}`).toEqual({table: 'tt'});
});
it('must support all parameters', () => {
const schema = 'ss', table = 'tt';
expect(helpers._TN`${schema}.${table}`).toEqual({schema: 'ss', table: 'tt'});
expect(helpers._TN`${schema}.${table}.`).toEqual({schema: 'ss', table: 'tt'});
});
});
});

0 comments on commit 19776db

Please sign in to comment.