Skip to content
This repository has been archived by the owner on Oct 21, 2019. It is now read-only.

Create mock DB setup, postgres integration tests #76

Merged
merged 7 commits into from
Jun 19, 2018
Merged

Conversation

bobheadxi
Copy link
Member

@bobheadxi bobheadxi commented Jun 17, 2018

  • Added mock DB setup, with func newTestDBConnection() (*DAL, func(), error) that begins transactions and rolls back after each test.

  • Bump postgres driver version (current one is more than a year old), update Update() functions accordingly with new WherePK() requirement. Read up on it a bit and there doesn't seem to be any other breaking changes.

  • Change data.dal.db type from *pg.DB to orm.DB, an interface shared between pg.Tx and pg.DB for testing purposes.

  • Updated README documentation.

make test-integration

Coverage dropped a bit because a new package has been introduced to Coveralls (it doesn't automatically calculate coverage against all packages, only those that have at least one test)

@coveralls
Copy link

coveralls commented Jun 17, 2018

Coverage Status

Coverage decreased (-2.8%) to 40.259% when pulling db7583c on test-db into 5b2b077 on master.

@bobheadxi bobheadxi force-pushed the test-db branch 2 times, most recently from fe01004 to 9eeb70b Compare June 17, 2018 22:59
@bobheadxi bobheadxi force-pushed the test-db branch 4 times, most recently from ccf5ae3 to d23defe Compare June 17, 2018 23:54
@bobheadxi bobheadxi changed the title Create mock DB setup Create mock DB setup, postgres integration tests Jun 18, 2018
Copy link
Member

@bfbachmann bfbachmann left a comment

Choose a reason for hiding this comment

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

This looks good, but I really think we should containerize the Postgres setup for integration tests for a few reasons:

1.) Having to set up Postgres on your machine just to run integration tests sucks.
2.) If someone is running Postgres on their machine they probably don't want Rocket's setup and tear-down code to mess with it.
3.) Containerizing integration tests greatly reduces the odds that tests fail because of unrelated issues (like someone screwing up their local Postgres, not having Postgres installed etc.), and it makes the tests more reliable reproducible.
4.) Having clean-up logic run at the end of the tests doesn't guarantee that stuff always gets cleaned up. A simple example is a case where there's a SIGSEGV mid way through the test so it exits before clean-up. Running this in a container would require no clean-up at all.
5.) Postgres already runs in a container in production so it's weird to do it differently in the tests.

Makefile Outdated
clean:
rm rocket
rm -f rocket
pg_ctl -D /usr/local/var/postgres stop -s -m fast
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's safe for a Make target to me shutting down Postgres on people's machines. I think that if you're going to shut down someone's Postgres you should at least make it obvious that you're doing so by making it a separate target like shutdown-db. With this setup your Postgres server gets shut down whenever your run tests.

err = dal.GetMemberBySlackID(memberGet)
assert.Nil(t, err)
assert.False(t, memberGet.IsAdmin)
}
Copy link
Member

Choose a reason for hiding this comment

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

Nice!

mock_db.sh Outdated
createuser -s rocket_test
createdb rocket_test_db
psql -d rocket_test_db -a -f ./schema/tables.sql
echo "MAKE: Local database ready to go!"
Copy link
Member

Choose a reason for hiding this comment

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

I think we could get rid of most of this logic by just containerizing our DB for tests. Also, we don't need to wait for the DB to start with a script like this, go-pg does it for you: https://github.com/ubclaunchpad/rocket/blob/master/data/dal.go#L24.

mock_db.sh Outdated

createuser -s rocket_test
createdb rocket_test_db
psql -d rocket_test_db -a -f ./schema/tables.sql
Copy link
Member

Choose a reason for hiding this comment

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

If this were containerized you could take care of some of this by simply copying the schema to the right location in the Dockerfile:

COPY schema.sql /docker-entrypoint-initdb.d/

Copy link
Member Author

Choose a reason for hiding this comment

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

tbh I've found Docker sucks even more for testing - starting anything is much slower than a simple pg instance, but I'll put this in docker

@bobheadxi
Copy link
Member Author

rebased

Copy link
Member

@bfbachmann bfbachmann left a comment

Choose a reason for hiding this comment

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

🏅 Clean!

if !ok {
return errors.New("dal.db not of type *pg.DB")
}
return database.Close()
Copy link
Member

Choose a reason for hiding this comment

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

This is fine, but maybe we should just panic if this doesn't work. I say that because usually you do checks like this when you've received something and you're not quite sure what it is. But in this case an error would mean that we've fundamentally broken our DB integration so a panic might be appropriate. Up to you, let me know what you think.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a good point, I'll make it panic

Copy link
Member Author

@bobheadxi bobheadxi Jun 19, 2018

Choose a reason for hiding this comment

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

@bfbachmann actually apparently .Close() is never called anywhere in the codebase 😅

And come to think of it, in the event .Close() is called, Rocket should be in the process of shutting down already - it might be fine to leave this as is. I'll try to find a place to put a .Close() somewhere. (edit: see db7583c)

// Create a new member
member := &model.Member{
SlackID: "1234",
Name: "Big Bruno",
Copy link
Member

Choose a reason for hiding this comment

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

Minor: We should probably insert and update as many values for this member as possible, just to make sure that all our columns are configured correctly in the DB and Rocket. If you want you can leave this as-is for now and I can write more tests for this stuff later since you've already done the hard part of setting up the test DB in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I wrote these tests just to make sure the test setup and rollback and everything was working fine, like you say all these tests could use some dedicated love in a separate PR

@bobheadxi bobheadxi merged commit 089187f into master Jun 19, 2018
@bobheadxi bobheadxi deleted the test-db branch June 19, 2018 16:53
@bobheadxi bobheadxi mentioned this pull request Jun 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants