Skip to content

Commit

Permalink
Rewrite gomigrate test using testcontainer (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
sudiptob2 authored Dec 3, 2024
1 parent 05e6982 commit 7ad9eb3
Showing 1 changed file with 44 additions and 24 deletions.
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))
}

0 comments on commit 7ad9eb3

Please sign in to comment.