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

Decouple unit test code from business code #17623

Merged
merged 11 commits into from
Nov 12, 2021

Conversation

wxiaoguang
Copy link
Contributor

@wxiaoguang wxiaoguang commented Nov 12, 2021

Many unit test code and business code were coupled together.

This PR decouples them.

After this refactoring, we only import testing and assert in unit test code:

$ grep -nr --include "*.go" --exclude "*_test.go" --exclude-dir vendor 'testing"' .
./models/unittest/testdb.go:12:	"testing"
./integrations/html_helper.go:9:	"testing"
./integrations/testlogger.go:14:	"testing"
./modules/test/context_tests.go:14:	"testing"

$ grep -nr --include "*.go" --exclude "*_test.go" --exclude-dir vendor 'assert"' .
./models/unittest/bridge.go:9:	"github.com/stretchr/testify/assert"
./models/unittest/testdb.go:20:	"github.com/stretchr/testify/assert"
./integrations/html_helper.go:12:	"github.com/stretchr/testify/assert"
./modules/test/context_tests.go:23:	"github.com/stretchr/testify/assert"

Some details:

What are Tester and Asserter?

They are intermediate interfaces for this refactoring. There are many assertions in models.CheckConsistencyFor (which calls db.AssertXxx), so we introduce the Tester/Asserter interfaces to decouple CheckConsistencyFor from testing frameworks. Then we do not need to import any testing frameworks for non-unit-test code. The benefit is that we get a clearer and smaller built binary.

Why don't we refactor CheckConsistencyFor directly to get rid of the assertions?

It will be a huge project. There are too many related code of CheckConsistencyFor, which may cause cycle-import problems. The refactoring of CheckConsistencyFor is very difficult to be done in a few PRs. So we now we just make the first step to decouple it from testing framework, and then we can refactor CheckConsistencyFor and related models one by one.

Then, one day, if CheckConsistencyFor is clean enough, we can remove the intermediate interfaces (Tester/Asserter).

@wxiaoguang wxiaoguang added type/refactoring Existing code has been cleaned up. There should be no new functionality. type/testing labels Nov 12, 2021
@wxiaoguang wxiaoguang added this to the 1.16.0 milestone Nov 12, 2021
@codecov-commenter
Copy link

codecov-commenter commented Nov 12, 2021

Codecov Report

❗ No coverage uploaded for pull request base (main@7f80263). Click here to learn what that means.
The diff coverage is 57.56%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #17623   +/-   ##
=======================================
  Coverage        ?   45.49%           
=======================================
  Files           ?      798           
  Lines           ?    88916           
  Branches        ?        0           
=======================================
  Hits            ?    40452           
  Misses          ?    41965           
  Partials        ?     6499           
Impacted Files Coverage Δ
models/unittest/testdb.go 11.11% <11.11%> (ø)
models/db/engine.go 33.33% <50.00%> (ø)
models/db/unit_tests.go 68.85% <57.57%> (ø)
modules/unittestbridge/unittestbridge.go 66.66% <66.66%> (ø)
models/unittest/bridge.go 75.00% <75.00%> (ø)
models/unittest/fixtures.go 36.11% <83.33%> (ø)
models/consistency.go 47.69% <98.57%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7f80263...e73a172. Read the comment docs.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Nov 12, 2021
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Nov 12, 2021
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Nov 12, 2021
@wxiaoguang wxiaoguang merged commit df64fa4 into go-gitea:main Nov 12, 2021
@wxiaoguang wxiaoguang deleted the decouple-unit-test branch November 12, 2021 14:36
@wxiaoguang wxiaoguang added the skip-changelog This PR is irrelevant for the (next) changelog, for example bug fixes for unreleased features. label Nov 12, 2021
Chianina pushed a commit to Chianina/gitea that referenced this pull request Mar 28, 2022
@go-gitea go-gitea locked and limited conversation to collaborators Apr 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. skip-changelog This PR is irrelevant for the (next) changelog, for example bug fixes for unreleased features. type/refactoring Existing code has been cleaned up. There should be no new functionality. type/testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants