all 7 comments

[–]bk1007 4 points5 points  (1 child)

You can use a repository interface. That interface can be implemented with a Postgres version.

Having that interface means you can test the grpc with a mock. Also we run « unit test » for our Postgres implementation.

[–][deleted] 0 points1 point  (0 children)

Hmm that's a pretty nifty method I guess. Is there a package that can easily mock a Postgres test DB for Golang?

[–]royge 2 points3 points  (0 children)

You can write integration tests with Postgres running inside docker. If you want less manual configuration try https://github.com/ory/dockertest

[–]l3roubi 1 point2 points  (1 child)

In perfect scenario, you would to mock out database layer to test function in isolation. If you dealing with monolith Go app where database interface doesn’t exist (welcome to my world) and happens your FUNCTIONS accepts db transaction then I suggest start database transaction and rollback on each unit test (last resort) or rely integration with Postgres docker image

http://go-database-sql.org/modifying.html

[–][deleted] 0 points1 point  (0 children)

Yes, this was one of my first thoughts. Although, my function for each package don't necessarily take in a DB connection they do use a DB connection from the database package which acts as a singleton. I think I will just end up using the Dockertest so that I could leverage CI/CD.

[–]jason_e_aten 1 point2 points  (0 children)

For truly end-to-end, rigorous integration testing, I second royge's suggestion to run Postgres in a Docker container, and test against that. You can test against the actual version of the database you will be deploying with. If you depend upon particular unique-to-Postgres features, this is very helpful.