From 19776db17fc36a0b59caec6ec17435c44c00b84c Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Sat, 25 May 2024 15:47:44 +0100 Subject: [PATCH] improving _TN + adding tests --- lib/helpers/table-name.js | 5 +++- package.json | 2 +- test/help.spec.js | 57 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/lib/helpers/table-name.js b/lib/helpers/table-name.js index 4ad74da3..909ebd23 100644 --- a/lib/helpers/table-name.js +++ b/lib/helpers/table-name.js @@ -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}; diff --git a/package.json b/package.json index 1e126be1..f824d6f5 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/help.spec.js b/test/help.spec.js index 49dc4029..b1c74158 100644 --- a/test/help.spec.js +++ b/test/help.spec.js @@ -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'}); + }); + }); +});