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

Run go tests in a package-specific database #1776

Draft
wants to merge 20 commits into
base: master
from
Draft
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+154 −102
Diff settings

Always

Just for now

Copy path View file
1 .envrc
@@ -57,6 +57,7 @@ export MYMOVE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export SECURE_MIGRATION_DIR="${MYMOVE_DIR}/local_migrations"
export SECURE_MIGRATION_SOURCE="local"
export DB_PASSWORD=mysecretpassword
export PGPASSWORD=$DB_PASSWORD

This comment has been minimized.

Copy link
@chrisgilmerproj

chrisgilmerproj Feb 22, 2019

Contributor

Setting this explicitly should only cause a problem if we have multiple DB instances with different passwords, right?

export DB_USER=postgres
export DB_HOST=localhost
export DB_PORT=5432
Copy path View file
@@ -289,9 +289,12 @@ endif
.PHONY: server_test
server_test: server_deps server_generate db_test_reset db_test_migrate
# Don't run tests in /cmd or /pkg/gen & pass `-short` to exclude long running tests
# Use -test.parallel 1 to test packages serially and avoid database collisions
# Disable test caching with `-count 1` - caching was masking local test failures
DB_PORT=$(DB_PORT_TEST) go test -p 1 -count 1 -short $$(go list ./... | grep -v \\/pkg\\/gen\\/ | grep -v \\/cmd\\/)
DB_PORT=$(DB_PORT_TEST) go test -count 1 -short $$(go list ./... | grep -v \\/pkg\\/gen\\/ | grep -v \\/cmd\\/)

server_test_build:
# Try to compile tests, but don't run them.
go test -run=nope $$(go list ./... | grep -v \\/pkg\\/gen\\/ | grep -v \\/cmd\\/)

server_test_all: server_deps server_generate db_dev_reset db_dev_migrate
# Like server_test but runs extended tests that may hit external services.
Copy path View file
@@ -1,28 +1,9 @@
# This file is used only by soda, as that tool does not accept database connection information
# in any other way. It is used in both development AND test environments.
development:
dialect: "postgres"
database: {{ env "DB_NAME" }}
host: {{ env "DB_HOST" }}
port: {{ env "DB_PORT" }}
user: {{ env "DB_USER" }}
password: {{ env "DB_PASSWORD" }}

test:
dialect: "postgres"
# Leave the test database name hardcoded, since we run tests in the same
# environment as development, and it's extra confusing to have to swap env
# variables before running tests.
database: "test_db"
host: {{ env "DB_HOST" }}
port: {{ env "DB_PORT" }}
user: {{ env "DB_USER" }}
password: {{ env "DB_PASSWORD" }}

container:
dialect: "postgres"
database: {{ env "DB_NAME" }}
host: {{ env "DB_HOST" }}
port: {{ env "DB_PORT" }}
user: {{ env "DB_USER" }}
password: {{ env "DB_PASSWORD" }}
options:
sslmode: "require"

This comment has been minimized.

Copy link
@chrisgilmerproj

chrisgilmerproj Mar 4, 2019

Contributor

I'm pretty sure this file gets used by our migrations container in AWS. I'd leave it alone or we might see problems.

@@ -1,6 +1,7 @@
package dutystationsloader

import (
"github.com/transcom/mymove/pkg/testingsuite"
"log"
"reflect"
"testing"
@@ -16,44 +17,23 @@ import (
)

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

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

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

verrs, err := suite.db.ValidateAndSave(model)
if err != nil {
suite.T().Errorf("Errors encountered saving %v: %v", model, err)
}
if verrs.HasAny() {
suite.T().Errorf("Validation errors encountered saving %v: %v", model, verrs)
}
suite.DB().TruncateAll()
}

func TestDutyStationsLoaderSuite(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 := &DutyStationsLoaderSuite{
db: db,
logger: logger,
PopTestSuite: testingsuite.NewPopTestSuite(testingsuite.CurrentPackage()),
logger: logger,
}

suite.Run(t, hs)
@@ -63,7 +43,7 @@ func (suite *DutyStationsLoaderSuite) TestParsingFunctions() {
stationsPath := "./testdata/stations.xlsx"
officesPath := "./testdata/offices.xlsx"

builder := NewMigrationBuilder(suite.db, suite.logger)
builder := NewMigrationBuilder(suite.DB(), suite.logger)

stationRows, err := builder.parseStations(stationsPath)
suite.NoError(err)
@@ -81,7 +61,7 @@ func (suite *DutyStationsLoaderSuite) TestParsingFunctions() {
}

func (suite *DutyStationsLoaderSuite) TestInsertionString() {
builder := NewMigrationBuilder(suite.db, suite.logger)
builder := NewMigrationBuilder(suite.DB(), suite.logger)

something := "something"
zeroID := uuid.Nil
@@ -99,7 +79,7 @@ func (suite *DutyStationsLoaderSuite) TestInsertionString() {
}

func (suite *DutyStationsLoaderSuite) TestCreateInsertQuery() {
builder := NewMigrationBuilder(suite.db, suite.logger)
builder := NewMigrationBuilder(suite.DB(), suite.logger)

model := models.User{
ID: uuid.Must(uuid.FromString("cd40c92e-7c8a-4da4-ad58-4480df84b3f0")),
@@ -122,7 +102,7 @@ func (suite *DutyStationsLoaderSuite) TestSeparateStations() {
State: "CA",
PostalCode: postalCode1,
}
suite.mustSave(&address1)
suite.MustSave(&address1)

savedName := "Saved!"
saved := models.DutyStation{
@@ -131,7 +111,7 @@ func (suite *DutyStationsLoaderSuite) TestSeparateStations() {
Name: savedName,
Affiliation: internalmessages.AffiliationARMY,
}
suite.mustSave(&saved)
suite.MustSave(&saved)

postalCode2 := "00002"
address2 := models.Address{
@@ -149,7 +129,7 @@ func (suite *DutyStationsLoaderSuite) TestSeparateStations() {
Affiliation: internalmessages.AffiliationARMY,
}

builder := NewMigrationBuilder(suite.db, suite.logger)
builder := NewMigrationBuilder(suite.DB(), suite.logger)
new, existing, err := builder.separateExistingStations([]DutyStationWrapper{
DutyStationWrapper{
TransportationOfficeName: "Some name",
@@ -178,15 +158,15 @@ func (suite *DutyStationsLoaderSuite) TestCheckForDuplicates() {
State: "CA",
PostalCode: postalCode,
}
suite.mustSave(&address)
suite.MustSave(&address)

savedName := "Some Office"
saved := models.TransportationOffice{
AddressID: address.ID,
Address: address,
Name: savedName,
}
suite.mustSave(&saved)
suite.MustSave(&saved)

postalCode2 := "00002"
address2 := models.Address{
@@ -203,7 +183,7 @@ func (suite *DutyStationsLoaderSuite) TestCheckForDuplicates() {
Name: notSavedName,
}

builder := NewMigrationBuilder(suite.db, suite.logger)
builder := NewMigrationBuilder(suite.DB(), suite.logger)
new, existing, err := builder.separateExistingOffices([]models.TransportationOffice{saved, notSaved})

suite.NoError(err)
@@ -256,7 +236,7 @@ func (suite *DutyStationsLoaderSuite) TestPairStationsOffices() {
},
}

builder := NewMigrationBuilder(suite.db, suite.logger)
builder := NewMigrationBuilder(suite.DB(), suite.logger)
pairs := builder.pairOfficesToStations(
[]DutyStationWrapper{station1, station2, station3},
[]TransportationOfficeWrapper{office1, office2})
@@ -34,7 +34,7 @@ func TestAuthSuite(t *testing.T) {
log.Panic(err)
}
hs := &AuthSuite{
PopTestSuite: testingsuite.NewPopTestSuite(),
PopTestSuite: testingsuite.NewPopTestSuite(testingsuite.CurrentPackage()),
logger: logger,
}
suite.Run(t, hs)
@@ -792,7 +792,7 @@ func TestAwardQueueSuite(t *testing.T) {
}

hs := &AwardQueueSuite{
PopTestSuite: testingsuite.NewPopTestSuite(),
PopTestSuite: testingsuite.NewPopTestSuite(testingsuite.CurrentPackage()),
logger: &hnyzap.Logger{Logger: logger},
}
suite.Run(t, hs)
Copy path View file
@@ -140,7 +140,7 @@ func TestDatesSuite(t *testing.T) {
}

hs := &DatesSuite{
PopTestSuite: testingsuite.NewPopTestSuite(),
PopTestSuite: testingsuite.NewPopTestSuite(testingsuite.CurrentPackage()),
logger: &hnyzap.Logger{Logger: logger},
}
suite.Run(t, hs)
Copy path View file
@@ -22,7 +22,7 @@ func (suite *DBFmtSuite) SetupTest() {

func TestDBFmtSuite(t *testing.T) {
hs := &DBFmtSuite{
PopTestSuite: testingsuite.NewPopTestSuite(),
PopTestSuite: testingsuite.NewPopTestSuite(testingsuite.CurrentPackage()),
}
suite.Run(t, hs)
}
@@ -27,7 +27,7 @@ func TestSequenceSuite(t *testing.T) {
logger := zap.NewNop()

hs := &SequenceSuite{
PopTestSuite: testingsuite.NewPopTestSuite(),
PopTestSuite: testingsuite.NewPopTestSuite(testingsuite.CurrentPackage()),
logger: logger,
}
suite.Run(t, hs)
@@ -315,7 +315,7 @@ func TestInvoiceSuite(t *testing.T) {
v.AutomaticEnv()

hs := &InvoiceSuite{
PopTestSuite: testingsuite.NewPopTestSuite(),
PopTestSuite: testingsuite.NewPopTestSuite(testingsuite.CurrentPackage()),
logger: logger,
Viper: v,
}
Copy path View file
@@ -26,9 +26,9 @@ type BaseHandlerTestSuite struct {
}

// NewBaseHandlerTestSuite returns a new BaseHandlerTestSuite
func NewBaseHandlerTestSuite(logger *zap.Logger, sender notifications.NotificationSender) BaseHandlerTestSuite {
func NewBaseHandlerTestSuite(logger *zap.Logger, sender notifications.NotificationSender, packageName testingsuite.PackageName) BaseHandlerTestSuite {
return BaseHandlerTestSuite{
PopTestSuite: testingsuite.NewPopTestSuite(),
PopTestSuite: testingsuite.NewPopTestSuite(packageName),
logger: logger,
notificationSender: sender,
}
@@ -1,6 +1,7 @@
package dpsapi

import (
"github.com/transcom/mymove/pkg/testingsuite"
"log"
"testing"

@@ -24,7 +25,7 @@ func TestHandlerSuite(t *testing.T) {
}

hs := &HandlerSuite{
BaseHandlerTestSuite: handlers.NewBaseHandlerTestSuite(logger, notifications.NewStubNotificationSender("milmovelocal", logger)),
BaseHandlerTestSuite: handlers.NewBaseHandlerTestSuite(logger, notifications.NewStubNotificationSender("milmovelocal", logger), testingsuite.CurrentPackage()),
}

suite.Run(t, hs)
@@ -1,6 +1,7 @@
package internalapi

import (
"github.com/transcom/mymove/pkg/testingsuite"
"log"
"testing"

@@ -36,7 +37,7 @@ func TestHandlerSuite(t *testing.T) {
}

hs := &HandlerSuite{
BaseHandlerTestSuite: handlers.NewBaseHandlerTestSuite(logger, notifications.NewStubNotificationSender("milmovelocal", logger)),
BaseHandlerTestSuite: handlers.NewBaseHandlerTestSuite(logger, notifications.NewStubNotificationSender("milmovelocal", logger), testingsuite.CurrentPackage()),
}

suite.Run(t, hs)
@@ -1,6 +1,7 @@
package publicapi

import (
"github.com/transcom/mymove/pkg/testingsuite"
"log"
"testing"

@@ -36,7 +37,7 @@ func TestHandlerSuite(t *testing.T) {
}

hs := &HandlerSuite{
BaseHandlerTestSuite: handlers.NewBaseHandlerTestSuite(logger, notifications.NewStubNotificationSender("milmovelocal", logger)),
BaseHandlerTestSuite: handlers.NewBaseHandlerTestSuite(logger, notifications.NewStubNotificationSender("milmovelocal", logger), testingsuite.CurrentPackage()),
}

suite.Run(t, hs)
Copy path View file
@@ -63,7 +63,7 @@ func (suite *ModelSuite) noValidationErrors(verrs *validate.Errors, err error) b
}

func TestModelSuite(t *testing.T) {
hs := &ModelSuite{PopTestSuite: testingsuite.NewPopTestSuite()}
hs := &ModelSuite{PopTestSuite: testingsuite.NewPopTestSuite(testingsuite.CurrentPackage())}
suite.Run(t, hs)
}

@@ -99,7 +99,7 @@ func TestNotificationSuite(t *testing.T) {
logger, _ := zap.NewDevelopment()

s := &NotificationSuite{
PopTestSuite: testingsuite.NewPopTestSuite(),
PopTestSuite: testingsuite.NewPopTestSuite(testingsuite.CurrentPackage()),
logger: logger,
}
suite.Run(t, s)
@@ -65,7 +65,7 @@ func TestPaperworkSuite(t *testing.T) {
}
storer := storageTest.NewFakeS3Storage(true)

popSuite := testingsuite.NewPopTestSuite()
popSuite := testingsuite.NewPopTestSuite(testingsuite.CurrentPackage())
hs := &PaperworkSuite{
PopTestSuite: popSuite,
logger: logger,
@@ -223,7 +223,7 @@ func TestRateEngineSuite(t *testing.T) {
logger, _ := zap.NewDevelopment()

hs := &RateEngineSuite{
PopTestSuite: testingsuite.NewPopTestSuite(),
PopTestSuite: testingsuite.NewPopTestSuite(testingsuite.CurrentPackage()),
logger: logger,
}
suite.Run(t, hs)
@@ -238,7 +238,7 @@ func TestHereTestSuite(t *testing.T) {
}

hs := &HereTestSuite{
testingsuite.NewPopTestSuite(),
testingsuite.NewPopTestSuite(testingsuite.CurrentPackage()),
logger,
}
suite.Run(t, hs)
@@ -33,7 +33,7 @@ func TestInvoiceSuite(t *testing.T) {
fakeS3 := storageTest.NewFakeS3Storage(true)

hs := &InvoiceServiceSuite{
PopTestSuite: testingsuite.NewPopTestSuite(),
PopTestSuite: testingsuite.NewPopTestSuite(testingsuite.CurrentPackage()),
logger: logger,
storer: fakeS3,
}
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.