-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simple tests for db/migration (#421)
## Description Some quick/shallow test for migration definition. ## Why is this needed #371 introduced a migration where none of the names matched :D, the Id & FuncName was fixed in #383 but that one missed the file name. Why make humans do things they're bad at when instead we can make a computer do something its good at? So this is that :D. ## How Has This Been Tested? Ran `go test` and it found the bad file name. I artificially changed an Id and the test caught the FuncName|Id mismatch. ## How are existing users impacted? What migration steps/scripts do we need? N/A ## Checklist: I have: - [ ] updated the documentation and/or roadmap (if required) - [x] added unit or e2e tests - [ ] provided instructions on how to upgrade
- Loading branch information
Showing
3 changed files
with
56 additions
and
9 deletions.
There are no files selected for viewing
File renamed without changes.
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
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, | ||
} | ||
} |
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
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)) | ||
} |