From 5854c3928accb14c3ec84de4726936514bf50b94 Mon Sep 17 00:00:00 2001 From: matt Date: Thu, 20 Jan 2022 17:03:37 +0000 Subject: [PATCH] Added tests for DatabaseConnectionPool class --- lib/DatabaseConnectionPool.test.js | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 lib/DatabaseConnectionPool.test.js diff --git a/lib/DatabaseConnectionPool.test.js b/lib/DatabaseConnectionPool.test.js new file mode 100644 index 0000000..99ff0aa --- /dev/null +++ b/lib/DatabaseConnectionPool.test.js @@ -0,0 +1,46 @@ +'use strict'; + +const DatabaseConnectionPool = require('./DatabaseConnectionPool'); + +jest.mock('./DatabaseConnectionPool'); + +describe('DatabaseConnectionPool', () => { + test('Should instantiate connection', () => { + new DatabaseConnectionPool(); + + expect(DatabaseConnectionPool).toHaveBeenCalledTimes(1); + }); + + test('Non prepared query should not execute with params', () => { + const dbp = new DatabaseConnectionPool(); + + const sql = `SELECT * FROM class;`; + dbp.runQuery(sql); + + expect(dbp.runQuery.mock.results[0].value).toEqual({ + sql: sql + }); + }); + + test('Prepared query should execute along with params', () => { + const dbp = new DatabaseConnectionPool(); + + const sql = `SELECT * FROM class where name = ?;`; + const params = [ 'bob' ]; + dbp.runQuery(sql, params); + + expect(dbp.runQuery.mock.results[0].value).toEqual({ + sql: sql, + params: params + }); + }); + + test('Query without trailing ; should throw error', () => { + const dbp = new DatabaseConnectionPool(); + + const sql = `SELECT * FROM class where name = ?`; + const params = [ 'bob' ]; + + expect(() => dbp.runQuery(sql, params)).toThrow(); + }); +});