Skip to content

#8 Creating database and user & granting user privileges to database #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 6, 2020

Conversation

allopez7
Copy link

@allopez7 allopez7 commented Mar 9, 2020

No description provided.

@allopez7 allopez7 changed the title Postgres Creating database and user & granting user privileges to database Mar 10, 2020
database.js Outdated
});

client.connect().then(() => {
createPgAccount("username", "password", "database");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this doing.

Copy link
Author

@allopez7 allopez7 Mar 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calling the function createPgAccount and passing in dummy data as arguments for now. Eventually, data from the sign-up page will get passed in.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove, or else this script will throw error.

database.js Outdated

const userDatabasePrivileges = await client.query(`GRANT ALL PRIVILEGES ON DATABASE ${database} To ${username}`)

await client.end()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is your client ending when you've created 1 account? By doing this, you can't run this function more than once.

database.js Outdated

const createPgAccount = async (username, password, database) => {
try{
const createDatabase = await client.query(`CREATE DATABASE ${database}`)
Copy link

@hwong0305 hwong0305 Mar 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to assign the return the value to variables when it's not being used?

It should just be await client.query(CREATE DATABASE ${database})

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point

database.js Outdated

client.connect().then(() => {
console.log('connected')
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put these inside a startPgDB function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment

// when the app starts, we should first start all the databases we support (ie PostGres, MongoDb, Neo4J). If the DB can't start up, then our service is useless, so don't even start the app.

@allopez7 allopez7 added the Databases Postgres, mongoDB, neo4j label Mar 25, 2020
@allopez7 allopez7 self-assigned this Mar 25, 2020
package.json Outdated
},
"scripts": {
"test": "jest",
"start": "node database.js"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node index.js <- usually the default

try{
await client.end()
}catch(err){
console.log('connection failed to closed', err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should return an error so whomever calls closePGDB will know that it has failed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add this as a test case

describe('Test startPGDB && closePGDB', ()=>{
it('startPGDB', async ()=>{
const startRes = await startPGDB()
if(startRes && startRes.error && startRes.error.message){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove if statement.

expect(mockClient.query).toHaveBeenNthCalledWith(3, `GRANT ALL PRIVILEGES ON DATABASE username TO username`)
expect(async ()=>{
await createPgAccount()
}).not.toThrow()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expecting it to not throw is not doing anything. because your test doesn't have try catch. If the function threw something, it would not pass

expect(mockClient.query).toHaveBeenNthCalledWith(2, `DROP USER IF EXISTS username`)
expect(async ()=>{
await deletePgAccount('username')
}).not.toThrow()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expecting it to not throw is not doing anything. because your test doesn't have try catch. If the function threw something, it would not pass

})
})
describe('Test createPgAccount', ()=>{
it('createPgAccount', async ()=>{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should run all necessary queries in a successful createPgAccount call

expect(mockClient.query).toHaveBeenNthCalledWith(3, `GRANT ALL PRIVILEGES ON DATABASE username TO username`)
await closePGDB()
})
it('createPgAccount, undefined parameters', async ()=>{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should not execute any queries if required arguments are not passed in.

expect(mockClient.query).toHaveBeenNthCalledWith(2, `DROP USER IF EXISTS username`)
await closePGDB()
})
it('deletePgAccount, undefined parameters', async ()=>{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should not execute any queries in deletePgAccount if required arguments are not passed in.

})
})
describe('Test deletePgAccount', ()=>{
it('deletePgAccount', async ()=>{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should execute all queries if required arguments are passed into deletePgAccount

}
let {startPGDB, closePGDB, createPgAccount, deletePgAccount} = require('./pg')
describe('Test startPGDB && closePGDB', ()=>{
it('startPGDB', async ()=>{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should call connect when starting PGDB

await startPGDB()
expect(mockClient.connect).toHaveBeenCalledTimes(1)
})
it('closePGDB', async ()=>{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should call end when starting PGDB

@@ -0,0 +1,61 @@
const pgClient = require('pg')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jest.mock('pg')
const {Client} = require('pg')
const {startPGDB.....} = require('./pg')

this.query = mockClient.query
this.connect = mockClient.connect
this.end = mockClient.end
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in addition to comment (above), you can do this to be more concise:

Client.mockImplementation(function() {
  return mockClient
})

const {startPGDB, closePGDB, createPgAccount, deletePgAccount} = require('./pg')

describe('Test PG DB', ()=>{
afterEach(()=>{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be beforeEach(...) shoudn't it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be beforeEach as well but on the first test the mocks would be cleared when there is no need to clear.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be beforeEach. As the test suite gets more complex, this will cause issues later on, you can't rely on other ppl to always clear their test.

Client.mockImplementation(function(){
return mockClient
})
global.console.log = jest.fn()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's console.log = jest.fn(), also it'll be clearer if it moves to under 47th line

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but that wouldn't make sense for line 75 where it's called again. Since it's global i'm changing the functionality of console so I wanted to make it clear that this is changing the functionality of console for the whole database file. You do you have a good point so I'm going to take it outside of the postgres test so we can use it in the MongoDB and Neo4J test as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be scoped into the function itself. Its not good for a developer later on to try to console.log something, but nothing shows up. This will essentially remove console.log functionality, which will remove debugging feature for alot of devs. Better to be inside the test.

@songz songz merged commit 039de07 into garageScript:master Apr 6, 2020
@allopez7 allopez7 linked an issue Apr 6, 2020 that may be closed by this pull request
@allopez7 allopez7 removed a link to an issue Apr 6, 2020
@allopez7 allopez7 linked an issue Apr 6, 2020 that may be closed by this pull request
@allopez7 allopez7 removed a link to an issue Apr 6, 2020
@allopez7 allopez7 linked an issue Apr 6, 2020 that may be closed by this pull request
@allopez7 allopez7 changed the title Creating database and user & granting user privileges to database #8 Creating database and user & granting user privileges to database Apr 6, 2020
@allopez7 allopez7 removed a link to an issue Apr 6, 2020
@allopez7 allopez7 linked an issue Apr 6, 2020 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Databases Postgres, mongoDB, neo4j
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement PG DB Module
7 participants