Skip to content

Instantly share code, notes, and snippets.

@zepatrik
Created April 28, 2020 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zepatrik/787ccfd19035e0859225ff79c9643435 to your computer and use it in GitHub Desktop.
Save zepatrik/787ccfd19035e0859225ff79c9643435 to your computer and use it in GitHub Desktop.
SQL migration test go example
package migratest
import (
"github.com/ory/x/sqlcon/dockertest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestMigrations(t *testing.T) {
// start a test container and create the database connection
connection := dockertest.ConnectToTestPostgreSQLPop(t)
// The test migrator searches for testdata SQL files in the ./testdata directory.
// After applying each migration it runs its corresponding testdata file.
// This test migrator highly depends on the way you apply migrations.
testMigrator := NewTestMigrator(t, connection, "../migrations", "./testdata")
require.NoError(t, testMigrator.Up())
// there are 10 migrations
for i := 0; i < 10; i++ {
// get the expected data for the migration
expect := expectedData(i)
// find the actual data
var actual testData
connection.Find(&actual, expect.ID)
// check if the records are equal
assert.Equal(t, expect, actual)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment