loggalicious/backend/internal/database/migrations/migration_test.go

67 lines
1.5 KiB
Go
Raw Normal View History

2018-02-05 16:18:33 +00:00
package migrations_test
import (
"fmt"
"strings"
"testing"
"git.icedream.tech/icedream/loggalicious/backend/internal/database"
"git.icedream.tech/icedream/loggalicious/backend/internal/database/migrations"
"github.com/fjl/go-couchdb"
. "github.com/smartystreets/goconvey/convey"
)
const couchDBUrl = "http://couchdb:5984"
func Test_Migration(t *testing.T) {
couchDB, err := couchdb.NewClient(couchDBUrl, nil)
if err != nil {
t.Error(err)
return
}
db := &database.DatabaseServer{Client: couchDB}
// Wipe database
dbNames, err := couchDB.AllDBs()
if err != nil {
t.Error(err)
return
}
for _, dbName := range dbNames {
if strings.HasPrefix(dbName, "_") {
continue
}
err = couchDB.DeleteDB(dbName)
if err != nil {
t.Error(err)
return
}
}
Convey("Migrations", t, func() {
Convey("At least one version is registered", func() {
versions := migrations.RegisteredVersions()
So(len(versions), ShouldBeGreaterThan, 0)
for _, version := range versions {
Convey(fmt.Sprintf("Version %s should have at least one migration", version), func() {
So(len(migrations.RegisteredMigrations(version)), ShouldBeGreaterThan, 0)
})
}
})
Convey("Migrations from blank setup works", func() {
oldVersion, _, err := db.GetMigrationDatabase().GetVersion()
So(err, ShouldBeNil)
err = migrations.Run(db)
So(err, ShouldBeNil)
version, _, err := db.GetMigrationDatabase().GetVersion()
So(err, ShouldBeNil)
So(version, ShouldNotEqual, oldVersion)
})
})
}