Move testing related code

This commit is contained in:
Nurahmadie 2014-03-27 13:57:53 +00:00
parent fe58917a7c
commit 202ea95368

View file

@ -3,12 +3,14 @@ package migrate_test
import (
"database/sql"
"fmt"
"log"
"os"
"strings"
"testing"
. "github.com/drone/drone/pkg/database/migrate"
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
"github.com/russross/meddler"
)
@ -476,6 +478,10 @@ func TestIndexOperations(t *testing.T) {
}
var esquel []*sqliteMaster
switch {
case driver == "mysql":
default:
// Query sqlite_master, check if index is exists.
query := `SELECT sql FROM sqlite_master WHERE type='index' and tbl_name='samples'`
if err := meddler.QueryAll(db, &esquel, query); err != nil {
@ -515,6 +521,7 @@ func TestIndexOperations(t *testing.T) {
t.Errorf("Expect row length equal to %d, got %d", 1, len(esquel2))
}
}
}
func TestColumnRedundancy(t *testing.T) {
defer tearDown()
@ -527,16 +534,26 @@ func TestColumnRedundancy(t *testing.T) {
t.Fatalf("Can not migrate: %q", err)
}
var tableSql string
query := `SELECT sql FROM sqlite_master where type='table' and name='samples'`
var dummy, query, tableSql string
switch {
case driver == "mysql":
query = `SHOW CREATE TABLE samples`
if err := db.QueryRow(query).Scan(&dummy, &tableSql); err != nil {
t.Fatalf("Can not query table's definition: %q", err)
}
if !strings.Contains(tableSql, "`repository`") {
t.Errorf("Expect column with name repository")
}
default:
query = `SELECT sql FROM sqlite_master where type='table' and name='samples'`
if err := db.QueryRow(query).Scan(&tableSql); err != nil {
t.Fatalf("Can not query sqlite_master: %q", err)
}
if !strings.Contains(tableSql, "repository ") {
t.Errorf("Expect column with name repository")
}
}
}
func TestChangeColumnType(t *testing.T) {
defer tearDown()
@ -549,16 +566,26 @@ func TestChangeColumnType(t *testing.T) {
t.Fatalf("Can not migrate: %q", err)
}
var tableSql string
query := `SELECT sql FROM sqlite_master where type='table' and name='samples'`
var dummy, tableSql, query string
switch {
case driver == "mysql":
query = `SHOW CREATE TABLE samples`
if err := db.QueryRow(query).Scan(&dummy, &tableSql); err != nil {
t.Fatalf("Can not query table's definition: %q", err)
}
if !strings.Contains(tableSql, "`email` varchar(512)") {
t.Errorf("Expect email type to changed: %q", tableSql)
}
default:
query = `SELECT sql FROM sqlite_master where type='table' and name='samples'`
if err := db.QueryRow(query).Scan(&tableSql); err != nil {
t.Fatalf("Can not query sqlite_master: %q", err)
}
if !strings.Contains(tableSql, "email varchar(512) UNIQUE") {
t.Errorf("Expect email type to changed: %q", tableSql)
}
}
}
func init() {
if driver = os.Getenv("DB_ENV"); len(driver) == 0 {
@ -573,15 +600,15 @@ func setUp() error {
var err error
Driver = SQLite
if db, err = sql.Open(driver, dsn); err != nil {
panic("Can't connect to database: ")
log.Fatalf("Can't connect to database: %q", err)
}
if driver == "mysql" {
Driver = MySQL
if _, err := db.Exec(fmt.Sprintf("CREATE DATABASE %s", dbname)); err != nil {
panic("Can't create database: ")
panic("Can't create database")
}
if _, err := db.Exec(fmt.Sprintf("USE %s", dbname)); err != nil {
panic("Can't use database: ")
panic("Can't use database")
}
}
return err