81 lines
2.6 KiB
Go
81 lines
2.6 KiB
Go
// Package migrations_test contains source-level contract tests for the
|
|
// 000002_turn_media migration. No real database connection is used.
|
|
package migrations_test
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func readMediaUpMigration(t *testing.T) string {
|
|
t.Helper()
|
|
data, err := os.ReadFile(filepath.Join(".", "000002_turn_media.up.sql"))
|
|
if err != nil {
|
|
t.Fatalf("read up migration: %v", err)
|
|
}
|
|
return string(data)
|
|
}
|
|
|
|
func readMediaDownMigration(t *testing.T) string {
|
|
t.Helper()
|
|
data, err := os.ReadFile(filepath.Join(".", "000002_turn_media.down.sql"))
|
|
if err != nil {
|
|
t.Fatalf("read down migration: %v", err)
|
|
}
|
|
return string(data)
|
|
}
|
|
|
|
// TestUpMediaMigrationAddsMediaJSONColumn enforces that the up migration
|
|
// adds a media_json JSONB column to the turns table with appropriate defaults.
|
|
func TestUpMediaMigrationAddsMediaJSONColumn(t *testing.T) {
|
|
up := readMediaUpMigration(t)
|
|
upper := strings.ToUpper(up)
|
|
|
|
if !strings.Contains(upper, "ADD COLUMN IF NOT EXISTS MEDIA_JSON") {
|
|
t.Error("up migration must use ADD COLUMN IF NOT EXISTS media_json")
|
|
}
|
|
if !strings.Contains(upper, "JSONB NOT NULL DEFAULT '[]'::JSONB") {
|
|
t.Error("up migration must declare media_json JSONB NOT NULL DEFAULT '[]'::JSONB")
|
|
}
|
|
}
|
|
|
|
// TestUpMediaMigrationIsIdempotent enforces that the up migration uses IF NOT
|
|
// EXISTS so it can be safely re-run.
|
|
func TestUpMediaMigrationIsIdempotent(t *testing.T) {
|
|
up := readMediaUpMigration(t)
|
|
upper := strings.ToUpper(up)
|
|
|
|
if !strings.Contains(upper, "IF NOT EXISTS") {
|
|
t.Error("up migration must use IF NOT EXISTS for idempotency")
|
|
}
|
|
}
|
|
|
|
// TestDownMediaMigrationDropsColumn enforces the down migration drops the
|
|
// media_json column with IF EXISTS.
|
|
func TestDownMediaMigrationDropsColumn(t *testing.T) {
|
|
down := readMediaDownMigration(t)
|
|
upper := strings.ToUpper(down)
|
|
|
|
if !strings.Contains(upper, "DROP COLUMN IF EXISTS MEDIA_JSON") {
|
|
t.Error("down migration must use DROP COLUMN IF EXISTS media_json")
|
|
}
|
|
}
|
|
|
|
// TestUpMigrationHasNoReservedWordMedia ensures the column is named media_json
|
|
// not a bare "media" which could be a reserved word in future PostgreSQL.
|
|
func TestUpMigrationHasNoReservedWordMedia(t *testing.T) {
|
|
up := readMediaUpMigration(t)
|
|
|
|
// The bare word "media" as a column name (not media_json) should be avoided
|
|
bare := regexp.MustCompile(`(?i)\bMEDIA\s+JSONB\b`)
|
|
if bare.MatchString(up) {
|
|
t.Error("up migration must not use bare 'media' column name; use media_json")
|
|
}
|
|
if !regexp.MustCompile(`(?i)\bMEDIA_JSON\s+JSONB\b`).MatchString(up) {
|
|
t.Error("up migration must use media_json column name")
|
|
}
|
|
}
|