From e582957749c46846d6dee6c4b8e314fc77249143 Mon Sep 17 00:00:00 2001 From: alberto Date: Tue, 31 Mar 2020 15:17:05 -0700 Subject: [PATCH 01/12] postgres start, create, delete, close database/jest --- database/postgres/pg.js | 49 ++++++++++++++++++++++++++++++++++++ database/postgres/pg.test.js | 49 ++++++++++++++++++++++++++++++++++++ package.json | 23 +++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 database/postgres/pg.js create mode 100644 database/postgres/pg.test.js create mode 100644 package.json diff --git a/database/postgres/pg.js b/database/postgres/pg.js new file mode 100644 index 00000000..f6b1dca9 --- /dev/null +++ b/database/postgres/pg.js @@ -0,0 +1,49 @@ +const { Client } = require('pg') +require('dotenv').config() + +const pgModule = {} +let client + +pgModule.startPGDB = async ()=>{ + try{ + client = new Client({ + host: process.env.HOST, + port: process.env.PORT, + user: process.env.USERNAME, + password: process.env.PASSWORD, + database: process.env.DATABASE + }) + await client.connect() + }catch(err){ + console.log('connection failed', err) + } +} + +pgModule.closePGDB = async ()=>{ + try{ + await client.end() + }catch(err){ + console.log('connection failed to closed') + } +} + +pgModule.createPgAccount = async (username, password)=>{ + try{ + await client.query(`CREATE DATABASE IF NOT EXISTS ${username}`) + await client.query(`CREATE USER IF NOT EXISTS ${username} WITH ENCRYPTED password '${password}'`) + await client.query(`GRANT ALL PRIVILEGES ON DATABASE ${username} TO ${username}`) + }catch(err){ + console.log('failed to createPgAccount') + } +} + +pgModule.deletePgAccount = async (username)=>{ + try{ + await client.query(`DROP DATABASE IF EXISTS ${username}`) + await client.query(`DROP USER IF EXISTS ${username}`) + }catch(err){ + console.log('failed to deletePgAccount') + } +} + +module.exports = pgModule diff --git a/database/postgres/pg.test.js b/database/postgres/pg.test.js new file mode 100644 index 00000000..293b5d85 --- /dev/null +++ b/database/postgres/pg.test.js @@ -0,0 +1,49 @@ +const pgClient = require('pg') + +describe('Test PG DB', ()=>{ + afterEach(()=>{ + jest.clearAllMocks() + }) + let mockClient = { + query: jest.fn().mockReturnValue(Promise.resolve()), + connect: jest.fn().mockReturnValue(Promise.resolve()), + end: jest.fn().mockReturnValue(Promise.resolve()) + } + pgClient.Client = function(){ + this.query = mockClient.query + this.connect = mockClient.connect + this.end = mockClient.end + } + let {startPGDB, closePGDB, createPgAccount, deletePgAccount} = require('./pg') + describe('Test startPGDB && closePGDB', ()=>{ + it('startPGDB', async ()=>{ + await startPGDB() + expect(mockClient.connect).toHaveBeenCalledTimes(1) + }) + it('closePGDB', async ()=>{ + await closePGDB() + expect(mockClient.end).toHaveBeenCalledTimes(1) + }) + }) + describe('Test createPgAccount', ()=>{ + it('createPgAccount', async ()=>{ + await startPGDB() + await createPgAccount('username', 'password') + expect(mockClient.query).toHaveBeenCalledTimes(3) + expect(mockClient.query).toHaveBeenNthCalledWith(1, `CREATE DATABASE IF NOT EXISTS username`) + expect(mockClient.query).toHaveBeenNthCalledWith(2, `CREATE USER IF NOT EXISTS username WITH ENCRYPTED password 'password'`) + expect(mockClient.query).toHaveBeenNthCalledWith(3, `GRANT ALL PRIVILEGES ON DATABASE username TO username`) + await closePGDB() + }) + }) + describe('Test deletePgAccount', ()=>{ + it('deletePgAccount', async ()=>{ + await startPGDB() + await deletePgAccount('username') + expect(mockClient.query).toHaveBeenCalledTimes(2) + expect(mockClient.query).toHaveBeenNthCalledWith(1, `DROP DATABASE IF EXISTS username`) + expect(mockClient.query).toHaveBeenNthCalledWith(2, `DROP USER IF EXISTS username`) + await closePGDB() + }) + }) +}) diff --git a/package.json b/package.json new file mode 100644 index 00000000..62001483 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "databases", + "version": "1.0.0", + "description": "", + "main": "pg.js", + "scripts": { + "test": "jest" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/allopez7/databases.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/allopez7/databases/issues" + }, + "homepage": "https://github.com/allopez7/databases#readme", + "devDependencies": { + "dotenv": "^8.2.0", + "jest": "^25.2.4" + } +} From 97fddf2e0c7b3db95156018da2c04af9dee8822b Mon Sep 17 00:00:00 2001 From: alberto Date: Tue, 31 Mar 2020 15:28:30 -0700 Subject: [PATCH 02/12] update npm modules and prevent undefined values from running --- database/postgres/pg.js | 2 ++ package.json | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/database/postgres/pg.js b/database/postgres/pg.js index f6b1dca9..f1acc4b7 100644 --- a/database/postgres/pg.js +++ b/database/postgres/pg.js @@ -29,6 +29,7 @@ pgModule.closePGDB = async ()=>{ pgModule.createPgAccount = async (username, password)=>{ try{ + if(!username || !password) return await client.query(`CREATE DATABASE IF NOT EXISTS ${username}`) await client.query(`CREATE USER IF NOT EXISTS ${username} WITH ENCRYPTED password '${password}'`) await client.query(`GRANT ALL PRIVILEGES ON DATABASE ${username} TO ${username}`) @@ -39,6 +40,7 @@ pgModule.createPgAccount = async (username, password)=>{ pgModule.deletePgAccount = async (username)=>{ try{ + if(!username) return await client.query(`DROP DATABASE IF EXISTS ${username}`) await client.query(`DROP USER IF EXISTS ${username}`) }catch(err){ diff --git a/package.json b/package.json index 62001483..7e30c938 100644 --- a/package.json +++ b/package.json @@ -8,16 +8,19 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/allopez7/databases.git" + "url": "git+https://github.com/garageScript/databases.git" }, "author": "", "license": "ISC", "bugs": { - "url": "https://github.com/allopez7/databases/issues" + "url": "https://github.com/gargeScript/databases/issues" }, - "homepage": "https://github.com/allopez7/databases#readme", + "homepage": "https://github.com/garageScript/databases#readme", "devDependencies": { "dotenv": "^8.2.0", "jest": "^25.2.4" + }, + "dependencies": { + "pg": "^8.0.0" } } From a670e2b8b621ef5089f37d2f27c4ceebd7f61132 Mon Sep 17 00:00:00 2001 From: alberto Date: Tue, 31 Mar 2020 15:33:19 -0700 Subject: [PATCH 03/12] updated error management and package.json file --- database/postgres/pg.js | 6 +++--- package.json | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/database/postgres/pg.js b/database/postgres/pg.js index f1acc4b7..94591a41 100644 --- a/database/postgres/pg.js +++ b/database/postgres/pg.js @@ -23,7 +23,7 @@ pgModule.closePGDB = async ()=>{ try{ await client.end() }catch(err){ - console.log('connection failed to closed') + console.log('connection failed to closed', err) } } @@ -34,7 +34,7 @@ pgModule.createPgAccount = async (username, password)=>{ await client.query(`CREATE USER IF NOT EXISTS ${username} WITH ENCRYPTED password '${password}'`) await client.query(`GRANT ALL PRIVILEGES ON DATABASE ${username} TO ${username}`) }catch(err){ - console.log('failed to createPgAccount') + console.log('failed to createPgAccount', err) } } @@ -44,7 +44,7 @@ pgModule.deletePgAccount = async (username)=>{ await client.query(`DROP DATABASE IF EXISTS ${username}`) await client.query(`DROP USER IF EXISTS ${username}`) }catch(err){ - console.log('failed to deletePgAccount') + console.log('failed to deletePgAccount', err) } } diff --git a/package.json b/package.json index 7e30c938..5c5ee0b9 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "databases", "version": "1.0.0", - "description": "", - "main": "pg.js", + "description": "Create databases in postgres, mongoDB and neo4J", + "main": "index.js", "scripts": { "test": "jest" }, @@ -10,7 +10,7 @@ "type": "git", "url": "git+https://github.com/garageScript/databases.git" }, - "author": "", + "author": "garageScript", "license": "ISC", "bugs": { "url": "https://github.com/gargeScript/databases/issues" From f2e2a73ab16e655f9e94a6d13e7efcbae4372377 Mon Sep 17 00:00:00 2001 From: alberto Date: Wed, 1 Apr 2020 20:20:58 -0700 Subject: [PATCH 04/12] updated tests --- database/postgres/pg.js | 16 ++++++++++++++-- database/postgres/pg.test.js | 16 ++++++++++++++-- package.json | 3 ++- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/database/postgres/pg.js b/database/postgres/pg.js index 94591a41..20cfb4a6 100644 --- a/database/postgres/pg.js +++ b/database/postgres/pg.js @@ -7,7 +7,7 @@ let client pgModule.startPGDB = async ()=>{ try{ client = new Client({ - host: process.env.HOST, + host: process.env.HOS, port: process.env.PORT, user: process.env.USERNAME, password: process.env.PASSWORD, @@ -16,6 +16,11 @@ pgModule.startPGDB = async ()=>{ await client.connect() }catch(err){ console.log('connection failed', err) + return await { + error: { + message: 'Connection to PG failed' + } + } } } @@ -23,7 +28,12 @@ pgModule.closePGDB = async ()=>{ try{ await client.end() }catch(err){ - console.log('connection failed to closed', err) + console.log('connection failed to close', err) + return { + error: { + message: 'Connection to PG failed to close' + } + } } } @@ -35,6 +45,7 @@ pgModule.createPgAccount = async (username, password)=>{ await client.query(`GRANT ALL PRIVILEGES ON DATABASE ${username} TO ${username}`) }catch(err){ console.log('failed to createPgAccount', err) + throw new Error(`failed to createPgAccount for user: ${username}`) } } @@ -45,6 +56,7 @@ pgModule.deletePgAccount = async (username)=>{ await client.query(`DROP USER IF EXISTS ${username}`) }catch(err){ console.log('failed to deletePgAccount', err) + throw new Error(`failed to deletePgAccount for database and user: ${username}`) } } diff --git a/database/postgres/pg.test.js b/database/postgres/pg.test.js index 293b5d85..8d368958 100644 --- a/database/postgres/pg.test.js +++ b/database/postgres/pg.test.js @@ -17,11 +17,17 @@ describe('Test PG DB', ()=>{ let {startPGDB, closePGDB, createPgAccount, deletePgAccount} = require('./pg') describe('Test startPGDB && closePGDB', ()=>{ it('startPGDB', async ()=>{ - await startPGDB() + const startRes = await startPGDB() + if(startRes && startRes.error && startRes.error.message){ + expect(startRes.error.message).toBe('Connection to PG failed') + } expect(mockClient.connect).toHaveBeenCalledTimes(1) }) it('closePGDB', async ()=>{ - await closePGDB() + const closeRes = await closePGDB() + if(closeRes && closeRes.error && closeRes.error.message){ + expect(closeRes.error.message).toBe('Connection to PG failed to close') + } expect(mockClient.end).toHaveBeenCalledTimes(1) }) }) @@ -33,6 +39,9 @@ describe('Test PG DB', ()=>{ expect(mockClient.query).toHaveBeenNthCalledWith(1, `CREATE DATABASE IF NOT EXISTS username`) expect(mockClient.query).toHaveBeenNthCalledWith(2, `CREATE USER IF NOT EXISTS username WITH ENCRYPTED password 'password'`) expect(mockClient.query).toHaveBeenNthCalledWith(3, `GRANT ALL PRIVILEGES ON DATABASE username TO username`) + expect(async ()=>{ + await createPgAccount() + }).not.toThrow() await closePGDB() }) }) @@ -43,6 +52,9 @@ describe('Test PG DB', ()=>{ expect(mockClient.query).toHaveBeenCalledTimes(2) expect(mockClient.query).toHaveBeenNthCalledWith(1, `DROP DATABASE IF EXISTS username`) expect(mockClient.query).toHaveBeenNthCalledWith(2, `DROP USER IF EXISTS username`) + expect(async ()=>{ + await deletePgAccount('username') + }).not.toThrow() await closePGDB() }) }) diff --git a/package.json b/package.json index 5c5ee0b9..d3525103 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "homepage": "https://github.com/garageScript/databases#readme", "devDependencies": { "dotenv": "^8.2.0", - "jest": "^25.2.4" + "jest": "^25.2.4", + "pm2": "^4.2.3" }, "dependencies": { "pg": "^8.0.0" From 4b0c933f9df04c1f1f521e661e5bd94612355c37 Mon Sep 17 00:00:00 2001 From: alberto Date: Wed, 1 Apr 2020 20:32:07 -0700 Subject: [PATCH 05/12] fix mistake --- database/postgres/pg.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/postgres/pg.js b/database/postgres/pg.js index 20cfb4a6..ee8b7528 100644 --- a/database/postgres/pg.js +++ b/database/postgres/pg.js @@ -16,7 +16,7 @@ pgModule.startPGDB = async ()=>{ await client.connect() }catch(err){ console.log('connection failed', err) - return await { + return { error: { message: 'Connection to PG failed' } From 1b31f5e6442fe678f6c1288d9dec3413d86b15fe Mon Sep 17 00:00:00 2001 From: alberto Date: Wed, 1 Apr 2020 20:50:41 -0700 Subject: [PATCH 06/12] got rid of try/catch for start and close connection --- database/postgres/pg.js | 26 ++++---------------------- database/postgres/pg.test.js | 10 ++-------- 2 files changed, 6 insertions(+), 30 deletions(-) diff --git a/database/postgres/pg.js b/database/postgres/pg.js index ee8b7528..3b7f209b 100644 --- a/database/postgres/pg.js +++ b/database/postgres/pg.js @@ -5,7 +5,6 @@ const pgModule = {} let client pgModule.startPGDB = async ()=>{ - try{ client = new Client({ host: process.env.HOS, port: process.env.PORT, @@ -13,33 +12,16 @@ pgModule.startPGDB = async ()=>{ password: process.env.PASSWORD, database: process.env.DATABASE }) - await client.connect() - }catch(err){ - console.log('connection failed', err) - return { - error: { - message: 'Connection to PG failed' - } - } - } + return client.connect() } pgModule.closePGDB = async ()=>{ - try{ - await client.end() - }catch(err){ - console.log('connection failed to close', err) - return { - error: { - message: 'Connection to PG failed to close' - } - } - } + return client.end() } pgModule.createPgAccount = async (username, password)=>{ - try{ if(!username || !password) return + try{ await client.query(`CREATE DATABASE IF NOT EXISTS ${username}`) await client.query(`CREATE USER IF NOT EXISTS ${username} WITH ENCRYPTED password '${password}'`) await client.query(`GRANT ALL PRIVILEGES ON DATABASE ${username} TO ${username}`) @@ -50,8 +32,8 @@ pgModule.createPgAccount = async (username, password)=>{ } pgModule.deletePgAccount = async (username)=>{ - try{ if(!username) return + try{ await client.query(`DROP DATABASE IF EXISTS ${username}`) await client.query(`DROP USER IF EXISTS ${username}`) }catch(err){ diff --git a/database/postgres/pg.test.js b/database/postgres/pg.test.js index 8d368958..1740a28b 100644 --- a/database/postgres/pg.test.js +++ b/database/postgres/pg.test.js @@ -17,17 +17,11 @@ describe('Test PG DB', ()=>{ let {startPGDB, closePGDB, createPgAccount, deletePgAccount} = require('./pg') describe('Test startPGDB && closePGDB', ()=>{ it('startPGDB', async ()=>{ - const startRes = await startPGDB() - if(startRes && startRes.error && startRes.error.message){ - expect(startRes.error.message).toBe('Connection to PG failed') - } + await startPGDB() expect(mockClient.connect).toHaveBeenCalledTimes(1) }) it('closePGDB', async ()=>{ - const closeRes = await closePGDB() - if(closeRes && closeRes.error && closeRes.error.message){ - expect(closeRes.error.message).toBe('Connection to PG failed to close') - } + await closePGDB() expect(mockClient.end).toHaveBeenCalledTimes(1) }) }) From c360d87da33a44b30559b549c16eb955e604edea Mon Sep 17 00:00:00 2001 From: alberto Date: Wed, 1 Apr 2020 20:53:35 -0700 Subject: [PATCH 07/12] deleted async from start and close connection --- database/postgres/pg.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/postgres/pg.js b/database/postgres/pg.js index 3b7f209b..d314724e 100644 --- a/database/postgres/pg.js +++ b/database/postgres/pg.js @@ -4,7 +4,7 @@ require('dotenv').config() const pgModule = {} let client -pgModule.startPGDB = async ()=>{ +pgModule.startPGDB = ()=>{ client = new Client({ host: process.env.HOS, port: process.env.PORT, @@ -15,7 +15,7 @@ pgModule.startPGDB = async ()=>{ return client.connect() } -pgModule.closePGDB = async ()=>{ +pgModule.closePGDB = ()=>{ return client.end() } From ab9c8ba12495e4854fd5018bbd9167853c413f1f Mon Sep 17 00:00:00 2001 From: alberto Date: Thu, 2 Apr 2020 14:47:30 -0700 Subject: [PATCH 08/12] added tests to check for undefined inputs and deleted unecessary assertion --- database/postgres/pg.test.js | 40 +++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/database/postgres/pg.test.js b/database/postgres/pg.test.js index 1740a28b..7766ed54 100644 --- a/database/postgres/pg.test.js +++ b/database/postgres/pg.test.js @@ -27,29 +27,35 @@ describe('Test PG DB', ()=>{ }) describe('Test createPgAccount', ()=>{ it('createPgAccount', async ()=>{ - await startPGDB() - await createPgAccount('username', 'password') - expect(mockClient.query).toHaveBeenCalledTimes(3) - expect(mockClient.query).toHaveBeenNthCalledWith(1, `CREATE DATABASE IF NOT EXISTS username`) - expect(mockClient.query).toHaveBeenNthCalledWith(2, `CREATE USER IF NOT EXISTS username WITH ENCRYPTED password 'password'`) - expect(mockClient.query).toHaveBeenNthCalledWith(3, `GRANT ALL PRIVILEGES ON DATABASE username TO username`) - expect(async ()=>{ + await startPGDB() + await createPgAccount('username', 'password') + expect(mockClient.query).toHaveBeenCalledTimes(3) + expect(mockClient.query).toHaveBeenNthCalledWith(1, `CREATE DATABASE IF NOT EXISTS username`) + expect(mockClient.query).toHaveBeenNthCalledWith(2, `CREATE USER IF NOT EXISTS username WITH ENCRYPTED password 'password'`) + expect(mockClient.query).toHaveBeenNthCalledWith(3, `GRANT ALL PRIVILEGES ON DATABASE username TO username`) + await closePGDB() + }) + it('createPgAccount, undefined parameters', async ()=>{ + await startPGDB() await createPgAccount() - }).not.toThrow() - await closePGDB() + expect(mockClient.query).toHaveBeenCalledTimes(0) + await closePGDB() }) }) describe('Test deletePgAccount', ()=>{ it('deletePgAccount', async ()=>{ - await startPGDB() - await deletePgAccount('username') - expect(mockClient.query).toHaveBeenCalledTimes(2) - expect(mockClient.query).toHaveBeenNthCalledWith(1, `DROP DATABASE IF EXISTS username`) - expect(mockClient.query).toHaveBeenNthCalledWith(2, `DROP USER IF EXISTS username`) - expect(async ()=>{ + await startPGDB() await deletePgAccount('username') - }).not.toThrow() - await closePGDB() + expect(mockClient.query).toHaveBeenCalledTimes(2) + expect(mockClient.query).toHaveBeenNthCalledWith(1, `DROP DATABASE IF EXISTS username`) + expect(mockClient.query).toHaveBeenNthCalledWith(2, `DROP USER IF EXISTS username`) + await closePGDB() + }) + it('deletePgAccount, undefined parameters', async ()=>{ + await startPGDB() + await createPgAccount() + expect(mockClient.query).toHaveBeenCalledTimes(0) + await closePGDB() }) }) }) From 9d5ee02a51c367ab88150a57f27b5c00942bd6b2 Mon Sep 17 00:00:00 2001 From: alberto Date: Thu, 2 Apr 2020 17:39:48 -0700 Subject: [PATCH 09/12] fixed comments for tests to make them more explicit --- database/postgres/pg.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/database/postgres/pg.test.js b/database/postgres/pg.test.js index 7766ed54..41551b22 100644 --- a/database/postgres/pg.test.js +++ b/database/postgres/pg.test.js @@ -16,17 +16,17 @@ describe('Test PG DB', ()=>{ } let {startPGDB, closePGDB, createPgAccount, deletePgAccount} = require('./pg') describe('Test startPGDB && closePGDB', ()=>{ - it('startPGDB', async ()=>{ + it('it should call connect when starting PGDB', async ()=>{ await startPGDB() expect(mockClient.connect).toHaveBeenCalledTimes(1) }) - it('closePGDB', async ()=>{ + it('it should call end when closing PGDB', async ()=>{ await closePGDB() expect(mockClient.end).toHaveBeenCalledTimes(1) }) }) describe('Test createPgAccount', ()=>{ - it('createPgAccount', async ()=>{ + it('it should execute all queries if required arguments are passed into createPgAccount', async ()=>{ await startPGDB() await createPgAccount('username', 'password') expect(mockClient.query).toHaveBeenCalledTimes(3) @@ -35,7 +35,7 @@ describe('Test PG DB', ()=>{ expect(mockClient.query).toHaveBeenNthCalledWith(3, `GRANT ALL PRIVILEGES ON DATABASE username TO username`) await closePGDB() }) - it('createPgAccount, undefined parameters', async ()=>{ + it('it should not execute any queries in createPgAccount if required arguments are not passed in', async ()=>{ await startPGDB() await createPgAccount() expect(mockClient.query).toHaveBeenCalledTimes(0) @@ -43,7 +43,7 @@ describe('Test PG DB', ()=>{ }) }) describe('Test deletePgAccount', ()=>{ - it('deletePgAccount', async ()=>{ + it('it should execute all queries if required arguments are passed into deletePgAccount', async ()=>{ await startPGDB() await deletePgAccount('username') expect(mockClient.query).toHaveBeenCalledTimes(2) @@ -51,7 +51,7 @@ describe('Test PG DB', ()=>{ expect(mockClient.query).toHaveBeenNthCalledWith(2, `DROP USER IF EXISTS username`) await closePGDB() }) - it('deletePgAccount, undefined parameters', async ()=>{ + it('it should not execute any queries in deletePgAccount if required arguments are not passed in', async ()=>{ await startPGDB() await createPgAccount() expect(mockClient.query).toHaveBeenCalledTimes(0) From 7204f88018ffbf09f01470dca1870a300568b899 Mon Sep 17 00:00:00 2001 From: alberto Date: Fri, 3 Apr 2020 19:36:25 -0700 Subject: [PATCH 10/12] update description of catch block testing --- database/postgres/pg.js | 2 +- database/postgres/pg.test.js | 67 +++++++++++++++++++++++------------- package.json | 2 +- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/database/postgres/pg.js b/database/postgres/pg.js index d314724e..31ed4c92 100644 --- a/database/postgres/pg.js +++ b/database/postgres/pg.js @@ -37,7 +37,7 @@ pgModule.deletePgAccount = async (username)=>{ await client.query(`DROP DATABASE IF EXISTS ${username}`) await client.query(`DROP USER IF EXISTS ${username}`) }catch(err){ - console.log('failed to deletePgAccount', err) + console.log('failed to deletePgAccount', err) throw new Error(`failed to deletePgAccount for database and user: ${username}`) } } diff --git a/database/postgres/pg.test.js b/database/postgres/pg.test.js index 41551b22..d28a6fae 100644 --- a/database/postgres/pg.test.js +++ b/database/postgres/pg.test.js @@ -1,4 +1,6 @@ -const pgClient = require('pg') +jest.mock('pg') +const {Client} = require('pg') +const {startPGDB, closePGDB, createPgAccount, deletePgAccount} = require('./pg') describe('Test PG DB', ()=>{ afterEach(()=>{ @@ -9,12 +11,10 @@ describe('Test PG DB', ()=>{ connect: jest.fn().mockReturnValue(Promise.resolve()), end: jest.fn().mockReturnValue(Promise.resolve()) } - pgClient.Client = function(){ - this.query = mockClient.query - this.connect = mockClient.connect - this.end = mockClient.end - } - let {startPGDB, closePGDB, createPgAccount, deletePgAccount} = require('./pg') + Client.mockImplementation(function(){ + return mockClient + }) + global.console.log = jest.fn() describe('Test startPGDB && closePGDB', ()=>{ it('it should call connect when starting PGDB', async ()=>{ await startPGDB() @@ -25,37 +25,56 @@ describe('Test PG DB', ()=>{ expect(mockClient.end).toHaveBeenCalledTimes(1) }) }) - describe('Test createPgAccount', ()=>{ - it('it should execute all queries if required arguments are passed into createPgAccount', async ()=>{ - await startPGDB() + describe('Test create and delete pgAccount', ()=>{ + beforeEach(async ()=>{ + await startPGDB() + }) + afterEach(async ()=>{ + await closePGDB() + }) + describe('Test createPgAccount', ()=>{ + it('it should execute all queries if required arguments are passed into createPgAccount', async ()=>{ await createPgAccount('username', 'password') expect(mockClient.query).toHaveBeenCalledTimes(3) expect(mockClient.query).toHaveBeenNthCalledWith(1, `CREATE DATABASE IF NOT EXISTS username`) expect(mockClient.query).toHaveBeenNthCalledWith(2, `CREATE USER IF NOT EXISTS username WITH ENCRYPTED password 'password'`) expect(mockClient.query).toHaveBeenNthCalledWith(3, `GRANT ALL PRIVILEGES ON DATABASE username TO username`) - await closePGDB() - }) - it('it should not execute any queries in createPgAccount if required arguments are not passed in', async ()=>{ - await startPGDB() + }) + it('it should not execute any queries in createPgAccount if required arguments are not passed in', async ()=>{ await createPgAccount() expect(mockClient.query).toHaveBeenCalledTimes(0) - await closePGDB() + }) + it('it should check if console.log is called at throw of createPgAccount', async ()=>{ + try{ + await mockClient.query.mockReturnValue(Promise.reject()) + const resCreatePgAccount = await createPgAccount('username', 'password') + expect(resCreatePgAccount).rejects.toThrow() + }catch(err){ + expect(console.log).toHaveBeenCalledTimes(1) + } + }) }) - }) - describe('Test deletePgAccount', ()=>{ - it('it should execute all queries if required arguments are passed into deletePgAccount', async ()=>{ - await startPGDB() + describe('Test deletePgAccount', ()=>{ + it('it should execute all queries if required arguments are passed into deletePgAccount', async ()=>{ + mockClient.query.mockReturnValue(Promise.resolve()) await deletePgAccount('username') expect(mockClient.query).toHaveBeenCalledTimes(2) expect(mockClient.query).toHaveBeenNthCalledWith(1, `DROP DATABASE IF EXISTS username`) expect(mockClient.query).toHaveBeenNthCalledWith(2, `DROP USER IF EXISTS username`) - await closePGDB() - }) - it('it should not execute any queries in deletePgAccount if required arguments are not passed in', async ()=>{ - await startPGDB() + }) + it('it should not execute any queries in deletePgAccount if required arguments are not passed in', async ()=>{ await createPgAccount() expect(mockClient.query).toHaveBeenCalledTimes(0) - await closePGDB() + }) + it('it should check if console.log is called at throw of deletePgAccount', async ()=>{ + try{ + await mockClient.query.mockReturnValue(Promise.reject()) + const resDeletePgAccount = await deletePgAccount('username', 'password') + expect(resDeletePgAccount).rejects.toThrow() + }catch(err){ + expect(console.log).toHaveBeenCalledTimes(1) + } + }) }) }) }) diff --git a/package.json b/package.json index d3525103..2619c997 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Create databases in postgres, mongoDB and neo4J", "main": "index.js", "scripts": { - "test": "jest" + "test": "jest --coverage" }, "repository": { "type": "git", From 3a651d9bd2965c0eb87b07dd1db6def02b0566d9 Mon Sep 17 00:00:00 2001 From: alberto Date: Mon, 6 Apr 2020 11:01:33 -0700 Subject: [PATCH 11/12] fixed last issue on line 35, test was for deltePgAccount now createPgAccount --- database/postgres/pg.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/postgres/pg.test.js b/database/postgres/pg.test.js index d28a6fae..d9827cc0 100644 --- a/database/postgres/pg.test.js +++ b/database/postgres/pg.test.js @@ -3,7 +3,7 @@ const {Client} = require('pg') const {startPGDB, closePGDB, createPgAccount, deletePgAccount} = require('./pg') describe('Test PG DB', ()=>{ - afterEach(()=>{ + beforeEach(()=>{ jest.clearAllMocks() }) let mockClient = { @@ -63,7 +63,7 @@ describe('Test PG DB', ()=>{ expect(mockClient.query).toHaveBeenNthCalledWith(2, `DROP USER IF EXISTS username`) }) it('it should not execute any queries in deletePgAccount if required arguments are not passed in', async ()=>{ - await createPgAccount() + await deletePgAccount() expect(mockClient.query).toHaveBeenCalledTimes(0) }) it('it should check if console.log is called at throw of deletePgAccount', async ()=>{ From 166f999d406be3397787c63fbec7ccbf831ebe80 Mon Sep 17 00:00:00 2001 From: alberto Date: Mon, 6 Apr 2020 11:24:38 -0700 Subject: [PATCH 12/12] got rid of global.console.log and changed scope of it --- database/postgres/pg.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/database/postgres/pg.test.js b/database/postgres/pg.test.js index d9827cc0..71d4a62b 100644 --- a/database/postgres/pg.test.js +++ b/database/postgres/pg.test.js @@ -14,7 +14,6 @@ describe('Test PG DB', ()=>{ Client.mockImplementation(function(){ return mockClient }) - global.console.log = jest.fn() describe('Test startPGDB && closePGDB', ()=>{ it('it should call connect when starting PGDB', async ()=>{ await startPGDB() @@ -46,6 +45,7 @@ describe('Test PG DB', ()=>{ }) it('it should check if console.log is called at throw of createPgAccount', async ()=>{ try{ + console.log = jest.fn() await mockClient.query.mockReturnValue(Promise.reject()) const resCreatePgAccount = await createPgAccount('username', 'password') expect(resCreatePgAccount).rejects.toThrow() @@ -68,6 +68,7 @@ describe('Test PG DB', ()=>{ }) it('it should check if console.log is called at throw of deletePgAccount', async ()=>{ try{ + console.log = jest.fn() await mockClient.query.mockReturnValue(Promise.reject()) const resDeletePgAccount = await deletePgAccount('username', 'password') expect(resDeletePgAccount).rejects.toThrow()