113 lines
2.6 KiB
Go
113 lines
2.6 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"embed"
|
|
"os"
|
|
"testing"
|
|
|
|
_ "github.com/lib/pq"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func getTestConnString() string {
|
|
connString := os.Getenv("POSTGRES_CONN_STRING")
|
|
if connString == "" {
|
|
connString = "postgres://user:password@localhost:5432/db?sslmode=disable"
|
|
}
|
|
return connString
|
|
}
|
|
|
|
func getTestConn(t *testing.T) *sql.DB {
|
|
conn, err := sql.Open("postgres", getTestConnString())
|
|
require.NoError(t, err)
|
|
|
|
ctx := context.Background()
|
|
require.NoError(t, conn.PingContext(ctx))
|
|
|
|
t.Cleanup(func() {
|
|
cleanTables(t, conn)
|
|
conn.Close()
|
|
})
|
|
|
|
return conn
|
|
}
|
|
|
|
func cleanTables(_ *testing.T, conn *sql.DB) {
|
|
tables := []string{"actions", "notifications", "contacts", "metadata", "chats", "users"}
|
|
for _, table := range tables {
|
|
_, _ = conn.ExecContext(context.Background(), "TRUNCATE TABLE "+table+" CASCADE")
|
|
}
|
|
}
|
|
|
|
func dropSchema(_ *testing.T, conn *sql.DB) {
|
|
tables := []string{"actions", "notifications", "contacts", "metadata", "chats", "users"}
|
|
for _, table := range tables {
|
|
_, _ = conn.ExecContext(context.Background(), "DROP TABLE IF EXISTS "+table+" CASCADE")
|
|
}
|
|
_, _ = conn.ExecContext(context.Background(), "DROP TABLE IF EXISTS schema_migrations")
|
|
}
|
|
|
|
func TestRunMigrations_Success(t *testing.T) {
|
|
conn := getTestConn(t)
|
|
dropSchema(t, conn)
|
|
|
|
err := runMigrations(conn)
|
|
assert.NoError(t, err)
|
|
|
|
var count int
|
|
err = conn.QueryRowContext(context.Background(), "SELECT COUNT(*) FROM users").Scan(&count)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestRunMigrations_AlreadyApplied(t *testing.T) {
|
|
conn := getTestConn(t)
|
|
dropSchema(t, conn)
|
|
|
|
err := runMigrations(conn)
|
|
require.NoError(t, err)
|
|
|
|
err = runMigrations(conn)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestRunMigrations_InvalidConn(t *testing.T) {
|
|
conn, err := sql.Open("postgres", "postgres://invalid:5432/db")
|
|
require.NoError(t, err)
|
|
defer conn.Close()
|
|
|
|
err = runMigrations(conn)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestRunMigrations_FailedCreateIOFSDriver(t *testing.T) {
|
|
holder := migrationsFS
|
|
migrationsFS = embed.FS{}
|
|
|
|
conn, err := sql.Open("postgres", "postgres://invalid:5432/db")
|
|
require.NoError(t, err)
|
|
defer conn.Close()
|
|
|
|
err = runMigrations(conn)
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "create iofs driver")
|
|
|
|
migrationsFS = holder
|
|
}
|
|
|
|
func TestRunMigrations_FailedUp(t *testing.T) {
|
|
conn := getTestConn(t)
|
|
dropSchema(t, conn)
|
|
|
|
_, err := conn.ExecContext(context.Background(), `CREATE TABLE users ()`)
|
|
require.NoError(t, err)
|
|
|
|
err = runMigrations(conn)
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "run up migrations")
|
|
|
|
dropSchema(t, conn)
|
|
}
|