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

Restructures Go tests to use BaseTestSuite and PopTestSuite #1557

Merged
merged 7 commits into from Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions pkg/auth/auth_test.go
@@ -1,14 +1,17 @@
package auth

import (
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
"log"
"testing"

"github.com/stretchr/testify/suite"
"go.uber.org/zap"

"github.com/transcom/mymove/pkg/testingsuite"
)

type authSuite struct {
suite.Suite
testingsuite.BaseTestSuite
logger *zap.Logger
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like most of our test suites have a *zap.Logger, although we sometimes initialize it to NewDevelopment() and other times to NewNop() (not sure if that's intentional or not). Should we consider moving logger up to the BaseTestSuite?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to leave BaseTestSuite purposefully kind of plain and single focused. I think logger is mostly used for passing into handler contexts and other child structs anyway, so I'm okay with leaving logger usage up to the test suite author

}

Expand Down
41 changes: 12 additions & 29 deletions pkg/auth/authentication/auth_test.go
@@ -1,10 +1,6 @@
package authentication

import (
"github.com/gobuffalo/pop"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
"log"
"net/http"
"net/http/httptest"
Expand All @@ -13,46 +9,33 @@ import (
"strconv"
"testing"

"github.com/gofrs/uuid"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"

"github.com/transcom/mymove/pkg/auth"
"github.com/transcom/mymove/pkg/models"
"github.com/transcom/mymove/pkg/testingsuite"
)

type AuthSuite struct {
suite.Suite
db *pop.Connection
testingsuite.PopTestSuite
logger *zap.Logger
}

func (suite *AuthSuite) SetupTest() {
suite.db.TruncateAll()
}

func (suite *AuthSuite) mustSave(model interface{}) {
t := suite.T()
t.Helper()

verrs, err := suite.db.ValidateAndSave(model)
if err != nil {
log.Panic(err)
}
if verrs.Count() > 0 {
t.Fatalf("errors encountered saving %v: %v", model, verrs)
}
suite.DB().TruncateAll()
}

func TestAuthSuite(t *testing.T) {
configLocation := "../../../config"
pop.AddLookupPaths(configLocation)
db, err := pop.Connect("test")
if err != nil {
log.Panic(err)
}

logger, err := zap.NewDevelopment()
if err != nil {
log.Panic(err)
}
hs := &AuthSuite{db: db, logger: logger}
hs := &AuthSuite{
PopTestSuite: testingsuite.NewPopTestSuite(),
logger: logger,
}
suite.Run(t, hs)
}

Expand Down Expand Up @@ -122,7 +105,7 @@ func (suite *AuthSuite) TestRequireAuthMiddleware() {
LoginGovUUID: loginGovUUID,
LoginGovEmail: "email@example.com",
}
suite.mustSave(&user)
suite.MustSave(&user)

rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/moves", nil)
Expand Down