Skip to content
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

feat: support for generating testcontainers-go tests for DB drivers #271

Merged
merged 22 commits into from
Jul 24, 2024

Conversation

mdelapenya
Copy link
Contributor

@mdelapenya mdelapenya commented Jul 3, 2024

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

Problem/Feature

Testing is a mayor concern nowadays, so this PR extends the code generation tool to include integration tests for the DB layer.

It uses Testcontainers for Go (spoiler alert: core maintainer here 👋) to run the test. Given the project already uses Docker Compose for running the project in dev mode, I think it's a good idea to have real services backing the database layer at test time.

Sample ouput of the tests:

go test -timeout 600s -coverprofile=/var/folders/_j/nhbgdck523n3008dd3zlsm5m0000gn/T/vscode-goOsS6fE/go-code-cover testfoo/internal/database -count=1 -v

2024/07/03 17:49:31 github.com/testcontainers/testcontainers-go - Connected to docker:
  Server Version: 78+testcontainerscloud (via Testcontainers Desktop 1.15.0)
  API Version: 1.43
  Operating System: Ubuntu 20.04 LTS
  Total Memory: 7407 MB
  Resolved Docker Host: tcp://127.0.0.1:61939
  Resolved Docker Socket Path: /var/run/docker.sock
  Test SessionID: 044df7af03224e9fe70ad6713b479a3170777bb29ce94d5f4edf802c5afa2aa0
  Test ProcessID: 6d5f6261-a0e1-4e35-b0ba-9c70f33d2c91
2024/07/03 17:49:31 🐳 Creating container for image testcontainers/ryuk:0.7.0
2024/07/03 17:49:31 ✅ Container created: 160fe2de6307
2024/07/03 17:49:31 🐳 Starting container: 160fe2de6307
2024/07/03 17:49:31 ✅ Container started: 160fe2de6307
2024/07/03 17:49:31 🚧 Waiting for container id 160fe2de6307 image: testcontainers/ryuk:0.7.0. Waiting for: &{Port:8080/tcp timeout:<nil> PollInterval:100ms}
2024/07/03 17:49:31 🔔 Container is ready: 160fe2de6307
2024/07/03 17:49:31 🐳 Creating container for image mongo:latest
2024/07/03 17:49:31 ✅ Container created: 908e45f9aa93
2024/07/03 17:49:31 🐳 Starting container: 908e45f9aa93
2024/07/03 17:49:32 ✅ Container started: 908e45f9aa93
2024/07/03 17:49:32 🚧 Waiting for container id 908e45f9aa93 image: mongo:latest. Waiting for: &{timeout:<nil> deadline:<nil> Strategies:[0x140003f0480 0x1400007bf00]}
2024/07/03 17:49:32 🔔 Container is ready: 908e45f9aa93
=== RUN   TestNew
--- PASS: TestNew (0.00s)
=== RUN   TestHealth
--- PASS: TestHealth (0.03s)
PASS
coverage: 80.0% of statements
2024/07/03 17:49:32 🐳 Terminating container: 908e45f9aa93
2024/07/03 17:49:33 🚫 Container terminated: 908e45f9aa93
ok  	testfoo/internal/database	2.840s	coverage: 80.0% of statements

Description of Changes:

  • Add templates for each supported driver: Mongo, Redis, MySQL, Postgres
  • Use the existing Testcontainers for Go modules for such drivers: Mongo, Redis, MySQL, Postgres
  • Include templating mechanism and data structures to render the new templates
  • Document the new capability
  • Include a make goal to run the integration tests
  • Include a Github action workflow for building an app for each DB driver, running the tests for each one

Checklist

@mdelapenya mdelapenya marked this pull request as ready for review July 3, 2024 16:16
@Ujstor
Copy link
Collaborator

Ujstor commented Jul 15, 2024

@mdelapenya Thanks for the PR, and sorry for the delay. We merged some other PRs first that included fixes and had priority. This is a very interesting one.

I think it would be better if we include the DB tests in the core blueprint and bind them to the DB driver flag. It makes more sense to me. The advanced flag is for more niche features. DB integration tests belong in the core.

The current workflow that you created is failing because in v.0.7.0, the core has a git flag that needs to be added: -g commit. After that, the workflow should pass.

Earlier, I saw that linter passed, but now it fails. There was no big change, only a refactor, and git was added into the matrix.

@mdelapenya
Copy link
Contributor Author

Earlier, I saw that linter passed, but now it fails. There was no big change, only a refactor, and git was added into the matrix.

@Ujstor thanks for your feedback! Indeed, the linter passed before and I'm fixing it. We released v0.32.0 past week and it deprecated the RunContainer module APIs in favour of simply Run. Please see testcontainers/testcontainers-go#2610

I'm working on the fix right now.

@mdelapenya
Copy link
Contributor Author

The advanced flag is for more niche features. DB integration tests belong in the core.

I protected the addition of the tests with the advanced flag in the case the team was not sure about bundling testcontainers into the projects as part of the core. I'm glad you see it adding them as part of the core as something positive. Thanks!

@mdelapenya
Copy link
Contributor Author

@Ujstor PR is updated, including the tests in the core. I hope everything is correct. PLMK if anything needs to be changed, thanks!

@mdelapenya mdelapenya changed the title feat: support for generating testcontainers-go tests as an advance feature feat: support for generating testcontainers-go tests for DB drivers Jul 18, 2024
Copy link
Collaborator

@Ujstor Ujstor left a comment

Choose a reason for hiding this comment

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

Just two small things, and after that, we are ready to merge.

.github/workflows/testcontainers.yml Outdated Show resolved Hide resolved
docs/docs/advanced-flag/testcontainers.md Outdated Show resolved Hide resolved
Copy link
Collaborator

@Ujstor Ujstor left a comment

Choose a reason for hiding this comment

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

LGTM

@Ujstor Ujstor merged commit fa8fb47 into Melkeydev:main Jul 24, 2024
159 checks passed
@mdelapenya mdelapenya deleted the testcontainers-integration-testing branch August 12, 2024 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants