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

Rewrite gomigrate test using testcontainer #355

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 44 additions & 24 deletions internal/database/migrations/gomigrate_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,71 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/G-Research/unicorn-history-server/test/config"
"github.com/G-Research/unicorn-history-server/test/database"
)

func TestGoMigrate_Integration(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode")
type GoMigrateIntTest struct {
suite.Suite
tp *database.TestPostgresContainer
}

func (ts *GoMigrateIntTest) SetupSuite() {
ctx := context.Background()
cfg := database.InstanceConfig{
User: "test",
Password: "test",
DBName: "template",
Host: "localhost",
Port: 15439,
}

tp, err := database.NewTestPostgresContainer(ctx, cfg)
require.NoError(ts.T(), err)
ts.tp = tp
}

func (ts *GoMigrateIntTest) TearDownSuite() {
err := ts.tp.Container.Terminate(context.Background())
require.NoError(ts.T(), err)
}

func (ts *GoMigrateIntTest) TestGoMigrate() {
ctx := context.Background()

schema := database.CreateTestSchema(ctx, t)
t.Cleanup(func() {
database.DropTestSchema(ctx, t, schema)
schema := database.CreateTestSchema(ctx, ts.T())
ts.T().Cleanup(func() {
database.DropTestSchema(ctx, ts.T(), schema)
})

cfg := config.GetTestPostgresConfig()
cfg.Schema = schema
m, err := New(cfg, "../../../migrations")
if err != nil {
t.Fatalf("could not create migrator: %v", err)
}
require.NoError(ts.T(), err)

applied, err := m.Up()
assert.Truef(t, applied, "expected up migrations to be applied for the first run")
if err != nil {
t.Fatalf("error running migrations up: %v", err)
}
assert.Truef(ts.T(), applied, "expected up migrations to be applied for the first run")
require.NoError(ts.T(), err)

applied, err = m.Up()
assert.Falsef(t, applied, "expected no up migrations to be applied for the second run")
if err != nil {
t.Fatalf("error running migrations up: %v", err)
}
assert.Falsef(ts.T(), applied, "expected no up migrations to be applied for the second run")
require.NoError(ts.T(), err)

applied, err = m.Down()
assert.Truef(t, applied, "expected down migrations to be applied for the first run")
if err != nil {
t.Fatalf("error running migrations down: %v", err)
}
assert.Truef(ts.T(), applied, "expected down migrations to be applied for the first run")
require.NoError(ts.T(), err)

applied, err = m.Down()
assert.Falsef(t, applied, "expected no down migrations to be applied for the second run")
if err != nil {
t.Fatalf("error running migrations down: %v", err)
assert.Falsef(ts.T(), applied, "expected no down migrations to be applied for the second run")
require.NoError(ts.T(), err)
}

func TestGoMigrateIntegration(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode.")
}
suite.Run(t, new(GoMigrateIntTest))
}
Loading