2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2020-02-01 15:12:21 +08:00
|
|
|
|
|
|
|
"github.com/op/go-logging"
|
2018-05-27 22:40:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func failTestOnError(t *testing.T, err error) {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed test: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPublicTables() ([]string, error) {
|
|
|
|
statement := `
|
2019-12-28 09:31:27 +08:00
|
|
|
SELECT tablename
|
|
|
|
FROM pg_tables
|
|
|
|
WHERE schemaname='public';
|
|
|
|
`
|
2018-05-27 22:40:42 +08:00
|
|
|
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 {
|
2018-06-08 03:09:01 +08:00
|
|
|
if os.Getenv("COMMENTO_POSTGRES") != "" {
|
|
|
|
// set it manually because we need to use commento_test, not commento, by mistake
|
|
|
|
os.Setenv("POSTGRES", os.Getenv("COMMENTO_POSTGRES"))
|
|
|
|
} else {
|
2018-06-08 03:09:27 +08:00
|
|
|
os.Setenv("POSTGRES", "postgres://postgres:postgres@localhost/commento_test?sslmode=disable")
|
2018-06-08 03:09:01 +08:00
|
|
|
}
|
2018-05-27 22:40:42 +08:00
|
|
|
|
2018-07-24 14:50:14 +08:00
|
|
|
if err := dbConnect(0); err != nil {
|
2018-05-27 22:40:42 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := dropTables(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:51:59 +08:00
|
|
|
if err := migrateFromDir("../db/"); err != nil {
|
2018-05-27 22:40:42 +08:00
|
|
|
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
|
|
|
|
|
2018-07-24 14:42:03 +08:00
|
|
|
if err := loggerCreate(); err != nil {
|
2018-05-27 22:40:42 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:43:23 +08:00
|
|
|
if err := markdownRendererCreate(); err != nil {
|
2018-05-27 22:40:42 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := clearTables(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|