43 lines
990 B
Go
43 lines
990 B
Go
package sqly
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
"github.com/mattes/migrate/migrate"
|
|
)
|
|
|
|
// BaseModel have to be embeded in all your struct which reflect a table
|
|
type BaseModel struct {
|
|
ID string
|
|
Updated time.Time `db:"updated_at"`
|
|
Created time.Time `db:"created_at"`
|
|
}
|
|
|
|
// RunWithLastestMigration runs your test with database migration set to the lastest
|
|
func RunWithLastestMigration(db *sqlx.DB, pgdsn string, t *testing.T, test func(db *sqlx.DB, t *testing.T)) {
|
|
defer func() {
|
|
allErrors, ok := migrate.DownSync(pgdsn, "../sql")
|
|
if !ok {
|
|
fmt.Println("Oh no ...")
|
|
for _, err := range allErrors {
|
|
t.Log(err)
|
|
t.Fatal("We get some errors when reset the database schema")
|
|
}
|
|
}
|
|
}()
|
|
|
|
allErrors, ok := migrate.UpSync(pgdsn, "../sql")
|
|
if !ok {
|
|
fmt.Println("Oh no ...")
|
|
for _, err := range allErrors {
|
|
t.Log(err)
|
|
t.Fatal("Impossible to run test we get some errors when initialize the database schema")
|
|
}
|
|
}
|
|
|
|
test(db, t)
|
|
}
|