129 lines
2.2 KiB
Go
129 lines
2.2 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"github.com/op/go-logging"
|
||
|
"os"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func failTestOnError(t *testing.T, err error) {
|
||
|
if err != nil {
|
||
|
t.Errorf("failed test: %v", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func getPublicTables() ([]string, error) {
|
||
|
statement := `
|
||
|
SELECT tablename
|
||
|
FROM pg_tables
|
||
|
WHERE schemaname='public';
|
||
|
`
|
||
|
rows, err := db.Query(statement)
|
||
|
if err != nil {
|
||
|
fmt.Fprintf(os.Stderr, "cannot query public tables: %v", err)
|
||
|
return []string{}, err
|
||
|
}
|
||
|
|
||
|
defer rows.Close()
|
||
|
|
||
|
tables := []string{}
|
||
|
for rows.Next() {
|
||
|
var table string
|
||
|
if err = rows.Scan(&table); err != nil {
|
||
|
fmt.Fprintf(os.Stderr, "cannot scan table name: %v", err)
|
||
|
return []string{}, err
|
||
|
}
|
||
|
|
||
|
tables = append(tables, table)
|
||
|
}
|
||
|
|
||
|
return tables, nil
|
||
|
}
|
||
|
|
||
|
func dropTables() error {
|
||
|
tables, err := getPublicTables()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
for _, table := range tables {
|
||
|
if table != "migrations" {
|
||
|
_, err = db.Exec(fmt.Sprintf("DROP TABLE %s;", table))
|
||
|
if err != nil {
|
||
|
fmt.Fprintf(os.Stderr, "cannot drop %s: %v", table, err)
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func setupTestDatabase() error {
|
||
|
os.Setenv("POSTGRES", "postgres://postgres:postgres@0.0.0.0/commento_test?sslmode=disable")
|
||
|
|
||
|
if err := connectDB(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if err := dropTables(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if err := performMigrationsFromDir("../db/"); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func clearTables() error {
|
||
|
tables, err := getPublicTables()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
for _, table := range tables {
|
||
|
_, err = db.Exec(fmt.Sprintf("DELETE FROM %s;", table))
|
||
|
if err != nil {
|
||
|
fmt.Fprintf(os.Stderr, "cannot clear %s: %v", table, err)
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
var setupComplete bool
|
||
|
|
||
|
func setupTestEnv() error {
|
||
|
if !setupComplete {
|
||
|
setupComplete = true
|
||
|
|
||
|
if err := createLogger(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Print messages to console only if verbose. Sounds like a good idea to
|
||
|
// keep the console clean on `go test`.
|
||
|
if !testing.Verbose() {
|
||
|
logging.SetLevel(logging.CRITICAL, "")
|
||
|
}
|
||
|
|
||
|
if err := setupTestDatabase(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if err := createMarkdownRenderer(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if err := clearTables(); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|