some basic tests

pull/2/head
Liam Staskawicz 2012-12-15 15:53:08 -08:00
parent b736bb38bf
commit 93d928851d
1 changed files with 73 additions and 0 deletions

73
cmd/migrate_test.go Normal file
View File

@ -0,0 +1,73 @@
package main
import (
"testing"
)
func TestMigrationMapSortUp(t *testing.T) {
mm := &MigrationMap{
Migrations: make(map[int]Migration),
}
// insert in any order
mm.Append(20120000, "test")
mm.Append(20128000, "test")
mm.Append(20129000, "test")
mm.Append(20127000, "test")
mm.Sort(true) // sort Upwards
sorted := []int{20120000, 20127000, 20128000, 20129000}
validateMigrationMapIsSorted(t, mm, sorted)
}
func TestMigrationMapSortDown(t *testing.T) {
mm := &MigrationMap{
Migrations: make(map[int]Migration),
}
// insert in any order
mm.Append(20120000, "test")
mm.Append(20128000, "test")
mm.Append(20129000, "test")
mm.Append(20127000, "test")
mm.Sort(false) // sort Downwards
sorted := []int{20129000, 20128000, 20127000, 20120000}
validateMigrationMapIsSorted(t, mm, sorted)
}
func validateMigrationMapIsSorted(t *testing.T, mm *MigrationMap, sorted []int) {
for i, v := range mm.Versions {
if sorted[i] != v {
t.Error("incorrect sorted version")
}
var next, prev int
if i == 0 {
prev = -1
next = mm.Versions[i+1]
} else if i == len(mm.Versions)-1 {
prev = mm.Versions[i-1]
next = -1
} else {
prev = mm.Versions[i-1]
next = mm.Versions[i+1]
}
if mm.Migrations[v].Next != next {
t.Errorf("mismatched Next. v: %v, got %v, wanted %v\n", v, mm.Migrations[v].Next, next)
}
if mm.Migrations[v].Previous != prev {
t.Errorf("mismatched Previous v: %v, got %v, wanted %v\n", v, mm.Migrations[v].Previous, prev)
}
}
}