81 lines
2.2 KiB
Go
81 lines
2.2 KiB
Go
package app
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
"github.com/go-sql-driver/mysql"
|
|
)
|
|
|
|
func TestRunServerRequiresDSN(t *testing.T) {
|
|
t.Setenv("GITOOCEAN_DSN", "")
|
|
cfgPath := filepath.Join(t.TempDir(), "missing.json")
|
|
err := runServer([]string{"--config", cfgPath, "--addr", ":0"})
|
|
if err == nil || !strings.Contains(err.Error(), "mysql_dsn is required") {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestQuoteMySQLIdentifierWrapper(t *testing.T) {
|
|
if got := quoteMySQLIdentifier("a`b"); got != "`a``b`" {
|
|
t.Fatalf("quoteMySQLIdentifier = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestMigrateSuccessAndDuplicateAlterations(t *testing.T) {
|
|
db, mock, err := sqlmock.New()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
for i := 0; i < 6; i++ {
|
|
mock.ExpectExec("CREATE TABLE IF NOT EXISTS").WillReturnResult(sqlmock.NewResult(0, 0))
|
|
}
|
|
for i := 0; i < 4; i++ {
|
|
mock.ExpectExec("ALTER TABLE").WillReturnError(&mysql.MySQLError{Number: 1060, Message: "Duplicate column"})
|
|
}
|
|
if err := migrate(db); err != nil {
|
|
t.Fatalf("migrate returned error for duplicate column: %v", err)
|
|
}
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestMigrateReturnsCreateAndAlterErrors(t *testing.T) {
|
|
db, mock, err := sqlmock.New()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
mock.ExpectExec("CREATE TABLE IF NOT EXISTS").WillReturnError(assertErr("create failed"))
|
|
if err := migrate(db); err == nil || !strings.Contains(err.Error(), "create failed") {
|
|
t.Fatalf("expected create error, got %v", err)
|
|
}
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
db, mock, err = sqlmock.New()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
for i := 0; i < 6; i++ {
|
|
mock.ExpectExec("CREATE TABLE IF NOT EXISTS").WillReturnResult(sqlmock.NewResult(0, 0))
|
|
}
|
|
mock.ExpectExec("ALTER TABLE").WillReturnError(assertErr("alter failed"))
|
|
if err := migrate(db); err == nil || !strings.Contains(err.Error(), "alter failed") {
|
|
t.Fatalf("expected alter error, got %v", err)
|
|
}
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
type assertErr string
|
|
|
|
func (e assertErr) Error() string { return string(e) }
|