Skip to content

Commit 983f480

Browse files
authored
[TECHDEBT][UTILITY] Fix Unit Tests using testing.M (#282)
## Description Fix unit tests that exist on the main branch. ## Origin Document Fixes conversation in #259: <img width="1059" alt="Screen Shot 2022-10-04 at 4 07 23 PM" src="https://user-images.githubusercontent.com/1892194/193946742-8a9f3914-86f1-4ccb-92fb-a1b025c5258a.png"> ## Issue Rather than the [core Go documentation](https://pkg.go.dev/testing), I believe [this article](https://medium.com/goingogo/why-use-testmain-for-testing-in-go-dafb52b406bc) does a better job at explaining why we had broken tests: <img width="780" alt="Screen Shot 2022-10-04 at 4 12 38 PM" src="https://user-images.githubusercontent.com/1892194/193947366-5cd62ef3-a01a-4d74-95d3-45929ee9348a.png"> Before this change, even when a unit test failed, we still got a pass: <img width="1241" alt="Screen Shot 2022-10-04 at 4 26 09 PM" src="https://user-images.githubusercontent.com/1892194/193948965-9079896e-5ab0-40dc-8f1b-f8bdcf4593c2.png"> ## Type of change Please mark the relevant option(s): - [ ] New feature, functionality or library - [x] Bug fix - [x] Code health or cleanup - [ ] Major breaking change - [ ] Documentation - [ ] Other <!-- add details here if it a different type of change --> ## List of changes - Update `test_utility` target in the `Makefile` - Not ignoring the exit code of `m.Run()` in all the files where we use it (several modules) - Fix tests in the utility module: - Remove redundant tests (e.g. `TestUtilityContext_UnstakesPausedBefore` which was replaced by `TestUtilityContext_UnstakePausedBefore`) - Do not ignore `m.Run` exit code so broken tests are caught - Improve readability of some tests (whitespace) - Do not unnecessarily export helpers - Fix unit tests in the persistence module related to type casting ## Testing - [x] `make develop_test` - [x] [LocalNet](https://github.com/pokt-network/pocket/blob/main/docs/development/README.md) w/ all of the steps outlined in the `README` ## Required Checklist - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have tested my changes using the available tooling - [x] I have updated the corresponding CHANGELOG ### If Applicable Checklist - [x] I have updated the corresponding README(s); local and/or global - [x] I have added tests that prove my fix is effective or that my feature works - [ ] I have added, or updated, [mermaid.js](https://mermaid-js.github.io) diagrams in the corresponding README(s) - [ ] I have added, or updated, documentation and [mermaid.js](https://mermaid-js.github.io) diagrams in `shared/docs/*` if I updated `shared/*`README(s)
1 parent 23055a0 commit 983f480

17 files changed

+111
-127
lines changed

Makefile

+3-8
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,10 @@ test_all_with_coverage: # generate_mocks
263263
test_race: # generate_mocks
264264
go test ${VERBOSE_TEST} -race ./...
265265

266-
.PHONY: test_utility_module
266+
.PHONY: test_utility
267267
## Run all go utility module unit tests
268-
test_utility_module: # generate_mocks
269-
go test ${VERBOSE_TEST} -p 1 -count=1 ./shared/tests/utility_module/...
270-
271-
.PHONY: test_utility_types
272-
## Run all go utility types module unit tests
273-
test_utility_types: # generate_mocks
274-
go test ${VERBOSE_TEST} ./utility/types/...
268+
test_utility: # generate_mocks
269+
go test ${VERBOSE_TEST} -p=1 -count=1 ./utility/...
275270

276271
.PHONY: test_shared
277272
## Run all go unit tests in the shared module

consensus/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.0.0.5] - 2022-10-06
11+
12+
- Don't ignore the exit code of `m.Run()` in the unit tests
13+
1014
## [0.0.0.4] - 2022-09-28
1115

1216
- `consensusModule` stores block directly to prevent shared structure in the `utilityModule`

consensus/consensus_tests/utils_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ import (
3131
)
3232

3333
func TestMain(m *testing.M) {
34-
m.Run()
34+
exitCode := m.Run()
3535
os.Remove(testingConfigFilePath)
3636
os.Remove(testingGenesisFilePath)
37+
os.Exit(exitCode)
3738
}
3839

3940
// If this is set to true, consensus unit tests will fail if additional unexpected messages are received.

p2p/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.0.0.4] - 2022-10-06
11+
12+
- Don't ignore the exit code of `m.Run()` in the unit tests
13+
1014
## [0.0.0.3] - 2022-09-15
1115

1216
**[TECHDEBT] AddrBook management optimization and refactoring** [#246](github.com/pokt-network/pocket/issues/246)

p2p/module_raintree_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,13 @@ func createGenesisState(t *testing.T, valKeys []cryptoPocket.PrivateKey) modules
449449
}
450450

451451
func TestMain(m *testing.M) {
452-
m.Run()
453-
files, _ := filepath.Glob("*.json")
452+
exitCode := m.Run()
453+
files, err := filepath.Glob("*.json")
454+
if err != nil {
455+
log.Fatalf("Error finding json file: %v", err)
456+
}
454457
for _, f := range files {
455458
os.Remove(f)
456459
}
460+
os.Exit(exitCode)
457461
}

persistence/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.0.0.6] - 2022-10-06
11+
12+
- Don't ignore the exit code of `m.Run()` in the unit tests
13+
- Fixed several broken unit tests related to type casting
14+
1015
## [0.0.0.5] - 2022-09-14
1116

1217
- Consolidated `PostgresContext` and `PostgresDb` into a single structure

persistence/shared_sql.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
// for the purpose of being explicit: https://github.com/pokt-network/pocket/pull/140#discussion_r939731342
1515
// TODO Cleanup with #149
1616
const (
17-
UndefinedStakingStatus = 0
18-
UnstakingStatus = 1
19-
StakedStatus = 2
20-
UnstakedStatus = 3
17+
UndefinedStakingStatus = int32(0)
18+
UnstakingStatus = int32(1)
19+
StakedStatus = int32(2)
20+
UnstakedStatus = int32(3)
2121
)
2222

2323
func UnstakingHeightToStatus(unstakingHeight int64) int32 {

persistence/test/account_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package test
33
import (
44
"encoding/hex"
55
"fmt"
6-
"github.com/pokt-network/pocket/persistence/types"
7-
"github.com/pokt-network/pocket/shared/modules"
86
"log"
97
"math/big"
108
"math/rand"
119
"testing"
1210

11+
"github.com/pokt-network/pocket/persistence/types"
12+
"github.com/pokt-network/pocket/shared/modules"
13+
1314
"github.com/pokt-network/pocket/persistence"
1415
"github.com/pokt-network/pocket/shared/crypto"
1516
"github.com/stretchr/testify/require"
@@ -312,7 +313,7 @@ func TestGetAllPools(t *testing.T) {
312313
return db.AddPoolAmount(pool.GetAddress(), "10")
313314
}
314315

315-
getAllActorsTest(t, db, db.GetAllPools, createAndInsertNewPool, updatePool, 6)
316+
getAllActorsTest(t, db, db.GetAllPools, createAndInsertNewPool, updatePool, 7)
316317
}
317318

318319
// --- Helpers ---

persistence/test/application_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ func TestGetAppStatus(t *testing.T) {
135135
// Check status before the app exists
136136
status, err := db.GetAppStatus(addrBz, 0)
137137
require.Error(t, err)
138-
require.Equal(t, status, persistence.UndefinedStakingStatus, "unexpected status")
138+
require.Equal(t, persistence.UndefinedStakingStatus, status, "unexpected status")
139139

140140
// Check status after the app exists
141141
status, err = db.GetAppStatus(addrBz, 1)
142142
require.NoError(t, err)
143-
require.Equal(t, status, DefaultStakeStatus, "unexpected status")
143+
require.Equal(t, DefaultStakeStatus, status, "unexpected status")
144144
}
145145

146146
func TestGetAppPauseHeightIfExists(t *testing.T) {

persistence/test/service_node_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ func TestGetServiceNodeStatus(t *testing.T) {
143143
// Check status before the serviceNode exists
144144
status, err := db.GetServiceNodeStatus(addrBz, 0)
145145
require.Error(t, err)
146-
require.Equal(t, status, persistence.UndefinedStakingStatus, "unexpected status")
146+
require.Equal(t, persistence.UndefinedStakingStatus, status, "unexpected status")
147147

148148
// Check status after the serviceNode exists
149149
status, err = db.GetServiceNodeStatus(addrBz, 1)
150150
require.NoError(t, err)
151-
require.Equal(t, status, DefaultStakeStatus, "unexpected status")
151+
require.Equal(t, DefaultStakeStatus, status, "unexpected status")
152152
}
153153

154154
func TestGetServiceNodePauseHeightIfExists(t *testing.T) {

persistence/test/setup_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ var testPersistenceMod modules.PersistenceModule // initialized in TestMain
5555
func TestMain(m *testing.M) {
5656
pool, resource, dbUrl := sharedTest.SetupPostgresDocker()
5757
testPersistenceMod = newTestPersistenceModule(dbUrl)
58-
m.Run()
58+
exitCode := m.Run()
5959
os.Remove(testingConfigFilePath)
6060
os.Remove(testingGenesisFilePath)
6161
sharedTest.CleanupPostgresDocker(m, pool, resource)
62+
os.Exit(exitCode)
6263
}
6364

6465
func NewTestPostgresContext(t *testing.T, height int64) *persistence.PostgresContext {

persistence/test/validator_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ func TestGetValidatorStatus(t *testing.T) {
140140
// Check status before the validator exists
141141
status, err := db.GetValidatorStatus(addrBz, 0)
142142
require.Error(t, err)
143-
require.Equal(t, status, persistence.UndefinedStakingStatus, "unexpected status")
143+
require.Equal(t, persistence.UndefinedStakingStatus, status, "unexpected status")
144144

145145
// Check status after the validator exists
146146
status, err = db.GetValidatorStatus(addrBz, 1)
147147
require.NoError(t, err)
148-
require.Equal(t, status, DefaultStakeStatus, "unexpected status")
148+
require.Equal(t, DefaultStakeStatus, status, "unexpected status")
149149
}
150150

151151
func TestGetValidatorPauseHeightIfExists(t *testing.T) {

shared/test_artifacts/util.go

-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ func CleanupPostgresDocker(_ *testing.M, pool *dockertest.Pool, resource *docker
9292
if err := pool.Purge(resource); err != nil {
9393
log.Fatalf("could not purge resource: %s", err)
9494
}
95-
os.Exit(0)
9695
}
9796

9897
// TODO(drewsky): Remove this in favor of a golang specific solution

utility/doc/CHANGELOG.md

+22-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.0.0.6] - 2022-10-06
11+
12+
- Don't ignore the exit code of `m.Run()` in the unit tests
13+
- Fixed several broken unit tests related to type casting
14+
- Removed some unit tests (e.g. `TestUtilityContext_UnstakesPausedBefore`) that were legacy and replaced by more general ones (e.g. `TestUtilityContext_UnstakePausedBefore`)
15+
- Avoid exporting unnecessary test helpers
16+
1017
## [0.0.0.5] - 2022-09-29
1118

1219
- Remove unused `StoreBlock` function from the utility module interface
@@ -50,7 +57,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5057
- `UtilityConfig` uses shared interfaces in order to accept `MockUtilityConfig` in test_artifacts
5158
- Moved all utilty tests from shared to tests package
5259
- Left `TODO` for tests package still importing persistence for `NewTestPersistenceModule`
53-
- This is one of the last places where cross-module importing exists
60+
- This is one of the last places where cross-module importing exists
5461

5562
## [0.0.1] - 2022-07-20
5663

@@ -70,18 +77,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7077
### Added
7178

7279
- Added minimal 'proof of stake' implementation with few Pocket Specific terminologies and actors
73-
- Structures
74-
- Accounts
75-
- Validators
76-
- Fishermen
77-
- Applications
78-
- Service Nodes
79-
- Pools
80-
- Messages
81-
- Stake
82-
- Unstake
83-
- EditStake
84-
- Pause
85-
- Unpause
86-
- Send
80+
- Structures
81+
- Accounts
82+
- Validators
83+
- Fishermen
84+
- Applications
85+
- Service Nodes
86+
- Pools
87+
- Messages
88+
- Stake
89+
- Unstake
90+
- EditStake
91+
- Pause
92+
- Unpause
93+
- Send
8794
- Added initial governance params

0 commit comments

Comments
 (0)