1
0
mirror of https://github.com/xorcare/testing-go-code-with-postgres.git synced 2024-12-24 16:28:34 +02:00
The example suggests a solution to the problem of cleaning the database after running tests and the problem of running tests in parallel. It also shows how to organize integration testing of Go code with Postgres.
Go to file
Vasiliy Vasilyuk e9de025f7e
Migrate to Compose V2
From July 2023 Compose V1 stopped receiving updates. It’s also no
longer available in new releases of Docker Desktop.

Compose V2, which was first released in 2020, is included with all
currently supported versions of Docker Desktop. It offers an improved
CLI experience, improved build performance with BuildKit, and continued
new-feature development.

See https://docs.docker.com/compose/migrate
See https://docs.docker.com/compose/reference
2023-09-22 22:16:15 +03:00
.github/workflows Add GitLab CI and mirroring to GitLab 2023-07-11 22:53:23 +03:00
migrations Publish an example testing go code with Postgres 2023-07-05 01:31:59 +03:00
testingpg Make human-readable database name 2023-07-16 17:18:30 +03:00
.editorconfig Publish an example testing go code with Postgres 2023-07-05 01:31:59 +03:00
.gitignore Publish an example testing go code with Postgres 2023-07-05 01:31:59 +03:00
.gitlab-ci.yml Migrate to Compose V2 2023-09-22 22:16:15 +03:00
.golangci.yml Add lint to CI 2023-07-10 23:10:36 +03:00
docker-compose.yml Speed up your integration tests by one line 2023-07-14 17:46:23 +03:00
docker-multiple-databases.sh Fix placeholder for the database template name 2023-07-15 20:34:41 +03:00
go.mod Publish an example testing go code with Postgres 2023-07-05 01:31:59 +03:00
go.sum Publish an example testing go code with Postgres 2023-07-05 01:31:59 +03:00
LICENSE Initial commit 2023-06-17 16:29:50 +03:00
Makefile Migrate to Compose V2 2023-09-22 22:16:15 +03:00
README.md Make human-readable database name 2023-07-16 17:18:30 +03:00
user_model.go Publish an example testing go code with Postgres 2023-07-05 01:31:59 +03:00
user_repository_test.go Add parallel running for tests 2023-07-05 07:57:37 +03:00
user_repository.go Publish an example testing go code with Postgres 2023-07-05 01:31:59 +03:00

Example of testing Go code with Postgres

Go workflow status badge codecov Gitlab CI Pipeline Gitlab CI Coverage Go Report Card

The example suggests a solution to the problem of cleaning the database after running tests and the problem of running tests in parallel. It also shows how to organize integration testing of Go code with Postgres.

What's interesting here?

Generating human-readable database names from t.Name() to simplifying problem investigation. The last 8 characters are a short unique identifier needed to prevent name collision, its necessary because the maximum length of the name is 63 bytes, and the name must be unique.

TestNewPostgres-Changes-are-not-visible-in-different-inWirPQD7J
TestNewPostgres-Changes-are-not-visible-in-different-ineYp0ljjI
TestNewPostgres-Successfully-connect-by-URL-and-get-verzGq4pGza
TestNewPostgres-Successfully-obtained-a-version-using-a20YgZaMf
TestNewPostgres-URL-is-different-at-different-instancesIMDkJgoP
TestNewPostgres-URL-is-different-at-different-instancesjtSsjPR5
TestUserRepository-CreateUser-Cannot-create-a-user-withmgmHFdZe
TestUserRepository-CreateUser-Successfully-created-a-UspTBGNltW
TestUserRepository-ReadUser-Get-an-error-if-the-user-doRqS1GvYh

How to use

Run make test-env-up test and then everything will happen by itself.

Disclaimer

This example is not an example of software architecture!