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

Simple tests for db/migration #421

Merged
merged 4 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 17 additions & 9 deletions db/migration/migration.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package migration

import migrate "github.com/rubenv/sql-migrate"
import (
migrate "github.com/rubenv/sql-migrate"
)

var migrations = []func() *migrate.Migration{
Get202009171251,
Get202010071530,
Get202010221010,
Get202012041103,
Get202012091055,
Get2020121691335,
}

func GetMigrations() *migrate.MemoryMigrationSource {
m := make([]*migrate.Migration, len(migrations))
for i := range migrations {
m[i] = migrations[i]()
}
return &migrate.MemoryMigrationSource{
Migrations: []*migrate.Migration{
Get202009171251(),
Get202010071530(),
Get202010221010(),
Get202012041103(),
Get202012091055(),
Get2020121691335(),
},
Migrations: m,
}
}
39 changes: 39 additions & 0 deletions db/migration/migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package migration

import (
"path"
"reflect"
"runtime"
"strings"
"testing"

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

func TestMigrationFuncNamesMatchIDs(t *testing.T) {
timestamps := map[string]bool{}
for _, m := range migrations {
pc := reflect.ValueOf(m).Pointer()
fn := runtime.FuncForPC(pc)

file, _ := fn.FileLine(pc)
file = path.Base(file)
root := strings.TrimSuffix(file, path.Ext(file))

migration := m()
mid := strings.Split(migration.Id, "-")[0]

assert.Equal(t, root, migration.Id, "file root name (%s) and migration id (%s) do not match", root, migration.Id)

fnName := strings.Split(fn.Name(), "migration.")[1]
assert.Equal(t, fnName, "Get"+mid, "migration func name and id timestamp mismatch, func: %s, migration.Id: %s", fnName, mid)

assert.NotContains(t, timestamps, mid)
}

}

func TestGetMigrations(t *testing.T) {
m := GetMigrations()
assert.Len(t, m.Migrations, len(migrations))
}