Created
April 28, 2020 10:41
-
-
Save zepatrik/787ccfd19035e0859225ff79c9643435 to your computer and use it in GitHub Desktop.
SQL migration test go example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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