mirror of
https://gitlab.com/commento/commento.git
synced 2025-06-29 22:56:37 -04:00
api: Add go files
I know this is a huge commit, but I can't be bothered to check this in part by part.
This commit is contained in:
128
api/testing.go
Normal file
128
api/testing.go
Normal file
@ -0,0 +1,128 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user