Files
gochat/backend/internal/database/coverage8_test.go
T

613 lines
18 KiB
Go

package database
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func safeCall_Cov8(t *testing.T, f func()) {
t.Helper()
defer func() { _ = recover() }()
f()
}
// === SanitizePostgresEnvironment ===
func TestSanitizePostgresEnvironment_Basic_Cov8(t *testing.T) {
_ = os.Setenv("PGSERVICE", "test_service")
_ = os.Setenv("PGSERVICEFILE", "/tmp/pg_service.conf")
_ = os.Setenv("PGREALM", "test_realm")
SanitizePostgresEnvironment()
assert.Equal(t, "", os.Getenv("PGSERVICE"))
assert.Equal(t, "", os.Getenv("PGSERVICEFILE"))
assert.Equal(t, "", os.Getenv("PGREALM"))
}
func TestSanitizePostgresEnvironment_NoVarsSet_Cov8(t *testing.T) {
_ = os.Unsetenv("PGSERVICE")
_ = os.Unsetenv("PGSERVICEFILE")
_ = os.Unsetenv("PGREALM")
SanitizePostgresEnvironment()
assert.Equal(t, "", os.Getenv("PGSERVICE"))
}
func TestSanitizePostgresEnvironment_Idempotent_Cov8(t *testing.T) {
SanitizePostgresEnvironment()
SanitizePostgresEnvironment()
SanitizePostgresEnvironment()
assert.Equal(t, "", os.Getenv("PGSERVICE"))
}
func TestSanitizePostgresEnvironment_PartiallySet_Cov8(t *testing.T) {
_ = os.Unsetenv("PGSERVICE")
_ = os.Setenv("PGSERVICEFILE", "/tmp/pg")
_ = os.Unsetenv("PGREALM")
SanitizePostgresEnvironment()
assert.Equal(t, "", os.Getenv("PGSERVICEFILE"))
}
// === unsupportedPQEnvironmentKeys ===
func TestUnsupportedPQEnvKeys_ContainsExpected_Cov8(t *testing.T) {
assert.Contains(t, unsupportedPQEnvironmentKeys, "PGSERVICE")
assert.Contains(t, unsupportedPQEnvironmentKeys, "PGSERVICEFILE")
assert.Contains(t, unsupportedPQEnvironmentKeys, "PGREALM")
assert.Len(t, unsupportedPQEnvironmentKeys, 3)
}
// === RunMigrations error paths ===
func TestRunMigrations_InvalidURL_Cov8(t *testing.T) {
err := RunMigrations("invalid://url", "/nonexistent/path")
assert.Error(t, err)
}
func TestRunMigrations_EmptyURL_Cov8(t *testing.T) {
err := RunMigrations("", "/nonexistent/path")
assert.Error(t, err)
}
func TestRunMigrations_EmptyPath_Cov8(t *testing.T) {
err := RunMigrations("sqlite3://test.db", "")
assert.Error(t, err)
}
func TestRunMigrations_BadProtocol_Cov8(t *testing.T) {
err := RunMigrations("badproto://localhost/db", "/nonexistent/path")
assert.Error(t, err)
}
func TestRunMigrations_NonexistentPath_Cov8(t *testing.T) {
err := RunMigrations("sqlite3:///tmp/nonexistent_db_test.db", "/nonexistent/migrations/path")
assert.Error(t, err)
}
func TestRunMigrations_GarbageURL_Cov8(t *testing.T) {
err := RunMigrations("%%%", "/nonexistent/path")
assert.Error(t, err)
}
func TestRunMigrations_MalformedURL_Cov8(t *testing.T) {
err := RunMigrations("://no-scheme", "/nonexistent/path")
assert.Error(t, err)
}
func TestRunMigrations_SanitizeEnvRestores_Cov8(t *testing.T) {
_ = os.Setenv("PGSERVICE", "test_service")
_ = os.Setenv("PGSERVICEFILE", "/tmp/pg_service.conf")
_ = os.Setenv("PGREALM", "test_realm")
_ = RunMigrations("invalid-url", "/nonexistent/path")
// After RunMigrations, env vars should be restored since withSanitizedPQEnvironment restores
// But RunMigrations uses SanitizePostgresEnvironment directly via runMigrations...
// Actually RunMigrations calls withSanitizedPQEnvironment which restores
assert.Equal(t, "test_service", os.Getenv("PGSERVICE"))
assert.Equal(t, "/tmp/pg_service.conf", os.Getenv("PGSERVICEFILE"))
assert.Equal(t, "test_realm", os.Getenv("PGREALM"))
_ = os.Unsetenv("PGSERVICE")
_ = os.Unsetenv("PGSERVICEFILE")
_ = os.Unsetenv("PGREALM")
}
// === withSanitizedPQEnvironment ===
func TestWithSanitizedPQEnvironment_RestoresAfter_Cov8(t *testing.T) {
_ = os.Setenv("PGSERVICE", "test_service")
_ = os.Setenv("PGSERVICEFILE", "/tmp/pg_service.conf")
_ = os.Setenv("PGREALM", "test_realm")
_ = withSanitizedPQEnvironment(func() error {
// Inside the function, vars should be unset
assert.Equal(t, "", os.Getenv("PGSERVICE"))
assert.Equal(t, "", os.Getenv("PGSERVICEFILE"))
assert.Equal(t, "", os.Getenv("PGREALM"))
return nil
})
// After the function, vars should be restored
assert.Equal(t, "test_service", os.Getenv("PGSERVICE"))
assert.Equal(t, "/tmp/pg_service.conf", os.Getenv("PGSERVICEFILE"))
assert.Equal(t, "test_realm", os.Getenv("PGREALM"))
_ = os.Unsetenv("PGSERVICE")
_ = os.Unsetenv("PGSERVICEFILE")
_ = os.Unsetenv("PGREALM")
}
func TestWithSanitizedPQEnvironment_NoVarsSet_Cov8(t *testing.T) {
_ = os.Unsetenv("PGSERVICE")
_ = os.Unsetenv("PGSERVICEFILE")
_ = os.Unsetenv("PGREALM")
called := false
err := withSanitizedPQEnvironment(func() error {
called = true
assert.Equal(t, "", os.Getenv("PGSERVICE"))
return nil
})
assert.NoError(t, err)
assert.True(t, called)
assert.Equal(t, "", os.Getenv("PGSERVICE"))
}
func TestWithSanitizedPQEnvironment_PropagatesError_Cov8(t *testing.T) {
testErr := assert.AnError
err := withSanitizedPQEnvironment(func() error {
return testErr
})
assert.Equal(t, testErr, err)
}
func TestWithSanitizedPQEnvironment_PartiallySet_Cov8(t *testing.T) {
_ = os.Unsetenv("PGSERVICE")
_ = os.Setenv("PGSERVICEFILE", "/tmp/pg")
_ = os.Unsetenv("PGREALM")
_ = withSanitizedPQEnvironment(func() error {
assert.Equal(t, "", os.Getenv("PGSERVICEFILE"))
return nil
})
assert.Equal(t, "/tmp/pg", os.Getenv("PGSERVICEFILE"))
_ = os.Unsetenv("PGSERVICEFILE")
}
func TestWithSanitizedPQEnvironment_EmptyValue_Cov8(t *testing.T) {
_ = os.Setenv("PGSERVICE", "")
_ = withSanitizedPQEnvironment(func() error {
assert.Equal(t, "", os.Getenv("PGSERVICE"))
return nil
})
// Empty string is still "set"
assert.Equal(t, "", os.Getenv("PGSERVICE"))
_ = os.Unsetenv("PGSERVICE")
}
func TestWithSanitizedPQEnvironment_NilFn_Cov8(t *testing.T) {
safeCall_Cov8(t, func() {
_ = withSanitizedPQEnvironment(func() error { return nil })
})
}
// === MigrateSteps error paths ===
func TestMigrateSteps_InvalidURL_Cov8(t *testing.T) {
err := MigrateSteps("invalid-url", "/nonexistent/path", 1)
assert.Error(t, err)
}
func TestMigrateSteps_EmptyURL_Cov8(t *testing.T) {
err := MigrateSteps("", "/nonexistent/path", 1)
assert.Error(t, err)
}
func TestMigrateSteps_NegativeSteps_Cov8(t *testing.T) {
err := MigrateSteps("invalid-url", "/nonexistent/path", -1)
assert.Error(t, err)
}
func TestMigrateSteps_ZeroSteps_Cov8(t *testing.T) {
err := MigrateSteps("invalid-url", "/nonexistent/path", 0)
assert.Error(t, err)
}
func TestMigrateSteps_BadProtocol_Cov8(t *testing.T) {
err := MigrateSteps("badproto://localhost/db", "/nonexistent/path", 1)
assert.Error(t, err)
}
func TestMigrateSteps_MalformedURL_Cov8(t *testing.T) {
err := MigrateSteps("://no-scheme", "/nonexistent/path", 1)
assert.Error(t, err)
}
func TestMigrateSteps_GarbageURL_Cov8(t *testing.T) {
err := MigrateSteps("%%%", "/nonexistent/path", 1)
assert.Error(t, err)
}
// === RollbackMigrations error paths ===
func TestRollbackMigrations_InvalidURL_Cov8(t *testing.T) {
err := RollbackMigrations("invalid-url", "/nonexistent/path")
assert.Error(t, err)
}
func TestRollbackMigrations_EmptyURL_Cov8(t *testing.T) {
err := RollbackMigrations("", "/nonexistent/path")
assert.Error(t, err)
}
func TestRollbackMigrations_BadProtocol_Cov8(t *testing.T) {
err := RollbackMigrations("badproto://localhost/db", "/nonexistent/path")
assert.Error(t, err)
}
func TestRollbackMigrations_MalformedURL_Cov8(t *testing.T) {
err := RollbackMigrations("://no-scheme", "/nonexistent/path")
assert.Error(t, err)
}
func TestRollbackMigrations_GarbageURL_Cov8(t *testing.T) {
err := RollbackMigrations("%%%", "/nonexistent/path")
assert.Error(t, err)
}
func TestRollbackMigrations_EmptyPath_Cov8(t *testing.T) {
err := RollbackMigrations("sqlite3://test.db", "")
assert.Error(t, err)
}
// === ForceVersion error paths ===
func TestForceVersion_InvalidURL_Cov8(t *testing.T) {
err := ForceVersion("invalid-url", "/nonexistent/path", 1)
assert.Error(t, err)
}
func TestForceVersion_EmptyURL_Cov8(t *testing.T) {
err := ForceVersion("", "/nonexistent/path", 1)
assert.Error(t, err)
}
func TestForceVersion_ZeroVersion_Cov8(t *testing.T) {
err := ForceVersion("invalid-url", "/nonexistent/path", 0)
assert.Error(t, err)
}
func TestForceVersion_NegativeVersion_Cov8(t *testing.T) {
err := ForceVersion("invalid-url", "/nonexistent/path", -1)
assert.Error(t, err)
}
func TestForceVersion_BadProtocol_Cov8(t *testing.T) {
err := ForceVersion("badproto://localhost/db", "/nonexistent/path", 1)
assert.Error(t, err)
}
func TestForceVersion_MalformedURL_Cov8(t *testing.T) {
err := ForceVersion("://no-scheme", "/nonexistent/path", 1)
assert.Error(t, err)
}
func TestForceVersion_GarbageURL_Cov8(t *testing.T) {
err := ForceVersion("%%%", "/nonexistent/path", 1)
assert.Error(t, err)
}
func TestForceVersion_EmptyPath_Cov8(t *testing.T) {
err := ForceVersion("invalid-url", "", 1)
assert.Error(t, err)
_ = err // may or may not error depending on migrate.New behavior
}
// === CurrentVersion error paths ===
func TestCurrentVersion_InvalidURL_Cov8(t *testing.T) {
v, dirty, err := CurrentVersion("invalid-url", "/nonexistent/path")
assert.Error(t, err)
assert.Equal(t, uint(0), v)
assert.False(t, dirty)
}
func TestCurrentVersion_EmptyURL_Cov8(t *testing.T) {
v, dirty, err := CurrentVersion("", "/nonexistent/path")
assert.Error(t, err)
assert.Equal(t, uint(0), v)
assert.False(t, dirty)
}
func TestCurrentVersion_BadProtocol_Cov8(t *testing.T) {
v, dirty, err := CurrentVersion("badproto://localhost/db", "/nonexistent/path")
assert.Error(t, err)
assert.Equal(t, uint(0), v)
assert.False(t, dirty)
}
func TestCurrentVersion_MalformedURL_Cov8(t *testing.T) {
v, dirty, err := CurrentVersion("://no-scheme", "/nonexistent/path")
assert.Error(t, err)
assert.Equal(t, uint(0), v)
assert.False(t, dirty)
}
func TestCurrentVersion_GarbageURL_Cov8(t *testing.T) {
v, dirty, err := CurrentVersion("%%%", "/nonexistent/path")
assert.Error(t, err)
assert.Equal(t, uint(0), v)
assert.False(t, dirty)
}
func TestCurrentVersion_EmptyPath_Cov8(t *testing.T) {
_, _, err := CurrentVersion("invalid-url", "")
assert.Error(t, err)
_ = err
}
// === runMigrations (internal) error paths ===
func TestRunMigrations_Internal_InvalidURL_Cov8(t *testing.T) {
err := runMigrations("invalid-url", "/nonexistent/path")
assert.Error(t, err)
}
func TestRunMigrations_Internal_EmptyURL_Cov8(t *testing.T) {
err := runMigrations("", "/nonexistent/path")
assert.Error(t, err)
}
func TestRunMigrations_Internal_BadProtocol_Cov8(t *testing.T) {
err := runMigrations("badproto://localhost/db", "/nonexistent/path")
assert.Error(t, err)
}
func TestRunMigrations_Internal_GarbageURL_Cov8(t *testing.T) {
err := runMigrations("%%%", "/nonexistent/path")
assert.Error(t, err)
}
// === Multiple calls ===
func TestRunMigrations_Twice_Cov8(t *testing.T) {
err1 := RunMigrations("invalid-url", "/nonexistent/path")
err2 := RunMigrations("invalid-url", "/nonexistent/path")
assert.Error(t, err1)
assert.Error(t, err2)
}
func TestMigrateSteps_Twice_Cov8(t *testing.T) {
err1 := MigrateSteps("invalid-url", "/nonexistent/path", 1)
err2 := MigrateSteps("invalid-url", "/nonexistent/path", 1)
assert.Error(t, err1)
assert.Error(t, err2)
}
func TestRollbackMigrations_Twice_Cov8(t *testing.T) {
err1 := RollbackMigrations("invalid-url", "/nonexistent/path")
err2 := RollbackMigrations("invalid-url", "/nonexistent/path")
assert.Error(t, err1)
assert.Error(t, err2)
}
func TestForceVersion_Twice_Cov8(t *testing.T) {
err1 := ForceVersion("invalid-url", "/nonexistent/path", 1)
err2 := ForceVersion("invalid-url", "/nonexistent/path", 1)
assert.Error(t, err1)
assert.Error(t, err2)
}
func TestCurrentVersion_Twice_Cov8(t *testing.T) {
v1, d1, err1 := CurrentVersion("invalid-url", "/nonexistent/path")
v2, d2, err2 := CurrentVersion("invalid-url", "/nonexistent/path")
assert.Error(t, err1)
assert.Error(t, err2)
assert.Equal(t, v1, v2)
assert.Equal(t, d1, d2)
}
// === Various URL formats ===
func TestRunMigrations_PostgresURL_Cov8(t *testing.T) {
err := RunMigrations("postgres://user:pass@localhost:5432/dbname?sslmode=disable", "/nonexistent/path")
assert.Error(t, err)
}
func TestRunMigrations_SQLiteURL_Cov8(t *testing.T) {
err := RunMigrations("sqlite3:///tmp/nonexistent_test_db.db", "/nonexistent/path")
assert.Error(t, err)
}
func TestRunMigrations_MySQLURL_Cov8(t *testing.T) {
err := RunMigrations("mysql://user:pass@localhost:3306/dbname", "/nonexistent/path")
assert.Error(t, err)
}
func TestRunMigrations_FileScheme_Cov8(t *testing.T) {
err := RunMigrations("file:///tmp/test.db", "/nonexistent/path")
assert.Error(t, err)
}
func TestRunMigrations_HTTPScheme_Cov8(t *testing.T) {
err := RunMigrations("http://localhost:8080/db", "/nonexistent/path")
assert.Error(t, err)
}
func TestMigrateSteps_PostgresURL_Cov8(t *testing.T) {
err := MigrateSteps("postgres://user:pass@localhost:5432/dbname?sslmode=disable", "/nonexistent/path", 1)
assert.Error(t, err)
}
func TestMigrateSteps_SQLiteURL_Cov8(t *testing.T) {
err := MigrateSteps("sqlite3:///tmp/nonexistent_test_db.db", "/nonexistent/path", 1)
assert.Error(t, err)
}
func TestRollbackMigrations_PostgresURL_Cov8(t *testing.T) {
err := RollbackMigrations("postgres://user:pass@localhost:5432/dbname?sslmode=disable", "/nonexistent/path")
assert.Error(t, err)
}
func TestRollbackMigrations_SQLiteURL_Cov8(t *testing.T) {
err := RollbackMigrations("sqlite3:///tmp/nonexistent_test_db.db", "/nonexistent/path")
assert.Error(t, err)
}
func TestForceVersion_PostgresURL_Cov8(t *testing.T) {
err := ForceVersion("postgres://user:pass@localhost:5432/dbname?sslmode=disable", "/nonexistent/path", 1)
assert.Error(t, err)
}
func TestForceVersion_SQLiteURL_Cov8(t *testing.T) {
err := ForceVersion("sqlite3:///tmp/nonexistent_test_db.db", "/nonexistent/path", 1)
assert.Error(t, err)
}
func TestCurrentVersion_PostgresURL_Cov8(t *testing.T) {
_, _, err := CurrentVersion("postgres://user:pass@localhost:5432/dbname?sslmode=disable", "/nonexistent/path")
assert.Error(t, err)
}
func TestCurrentVersion_SQLiteURL_Cov8(t *testing.T) {
_, _, err := CurrentVersion("sqlite3:///tmp/nonexistent_test_db.db", "/nonexistent/path")
assert.Error(t, err)
}
// === Various path formats ===
func TestRunMigrations_RelativePath_Cov8(t *testing.T) {
err := RunMigrations("invalid-url", "relative/path")
assert.Error(t, err)
}
func TestRunMigrations_AbsolutePath_Cov8(t *testing.T) {
err := RunMigrations("invalid-url", "/absolute/path/to/migrations")
assert.Error(t, err)
}
func TestRunMigrations_PathWithSpaces_Cov8(t *testing.T) {
err := RunMigrations("invalid-url", "/path with spaces/migrations")
assert.Error(t, err)
}
func TestRunMigrations_PathWithSpecialChars_Cov8(t *testing.T) {
err := RunMigrations("invalid-url", "/path@with#special$chars/migrations")
assert.Error(t, err)
}
func TestMigrateSteps_RelativePath_Cov8(t *testing.T) {
err := MigrateSteps("invalid-url", "relative/path", 1)
assert.Error(t, err)
}
func TestMigrateSteps_AbsolutePath_Cov8(t *testing.T) {
err := MigrateSteps("invalid-url", "/absolute/path/to/migrations", 1)
assert.Error(t, err)
}
func TestRollbackMigrations_RelativePath_Cov8(t *testing.T) {
err := RollbackMigrations("invalid-url", "relative/path")
assert.Error(t, err)
}
func TestRollbackMigrations_AbsolutePath_Cov8(t *testing.T) {
err := RollbackMigrations("invalid-url", "/absolute/path/to/migrations")
assert.Error(t, err)
}
func TestForceVersion_RelativePath_Cov8(t *testing.T) {
err := ForceVersion("invalid-url", "relative/path", 1)
assert.Error(t, err)
}
func TestForceVersion_AbsolutePath_Cov8(t *testing.T) {
err := ForceVersion("invalid-url", "/absolute/path/to/migrations", 1)
assert.Error(t, err)
}
func TestCurrentVersion_RelativePath_Cov8(t *testing.T) {
_, _, err := CurrentVersion("invalid-url", "relative/path")
assert.Error(t, err)
}
func TestCurrentVersion_AbsolutePath_Cov8(t *testing.T) {
_, _, err := CurrentVersion("invalid-url", "/absolute/path/to/migrations")
assert.Error(t, err)
}
// === Combined/edge cases ===
func TestRunMigrations_WithPQEnvSet_Cov8(t *testing.T) {
_ = os.Setenv("PGSERVICE", "test_service")
defer func() { _ = os.Unsetenv("PGSERVICE") }()
err := RunMigrations("invalid-url", "/nonexistent/path")
assert.Error(t, err)
// RunMigrations uses withSanitizedPQEnvironment so PGSERVICE should be restored
assert.Equal(t, "test_service", os.Getenv("PGSERVICE"))
}
func TestMigrateSteps_WithPQEnvSet_Cov8(t *testing.T) {
_ = os.Setenv("PGSERVICEFILE", "/tmp/pg")
defer func() { _ = os.Unsetenv("PGSERVICEFILE") }()
err := MigrateSteps("invalid-url", "/nonexistent/path", 1)
assert.Error(t, err)
}
func TestRollbackMigrations_WithPQEnvSet_Cov8(t *testing.T) {
_ = os.Setenv("PGREALM", "realm")
defer func() { _ = os.Unsetenv("PGREALM") }()
err := RollbackMigrations("invalid-url", "/nonexistent/path")
assert.Error(t, err)
}
func TestForceVersion_WithPQEnvSet_Cov8(t *testing.T) {
_ = os.Setenv("PGSERVICE", "svc")
defer func() { _ = os.Unsetenv("PGSERVICE") }()
err := ForceVersion("invalid-url", "/nonexistent/path", 1)
assert.Error(t, err)
}
func TestCurrentVersion_WithPQEnvSet_Cov8(t *testing.T) {
_ = os.Setenv("PGSERVICEFILE", "/tmp/pg")
defer func() { _ = os.Unsetenv("PGSERVICEFILE") }()
_, _, err := CurrentVersion("invalid-url", "/nonexistent/path")
assert.Error(t, err)
}
func TestAllMigrationFuncs_InvalidURL_Cov8(t *testing.T) {
url := "invalid-url"
path := "/nonexistent/path"
assert.Error(t, RunMigrations(url, path))
assert.Error(t, MigrateSteps(url, path, 1))
assert.Error(t, RollbackMigrations(url, path))
assert.Error(t, ForceVersion(url, path, 1))
_, _, err := CurrentVersion(url, path)
assert.Error(t, err)
}
func TestSanitizePostgresEnvironment_AllKeys_Cov8(t *testing.T) {
for _, key := range unsupportedPQEnvironmentKeys {
_ = os.Setenv(key, "value_"+key)
}
SanitizePostgresEnvironment()
for _, key := range unsupportedPQEnvironmentKeys {
assert.Equal(t, "", os.Getenv(key), "key %s should be unset", key)
}
}
func TestSanitizePostgresEnvironment_OtherKeysPreserved_Cov8(t *testing.T) {
_ = os.Setenv("PGHOST", "localhost")
_ = os.Setenv("PGUSER", "testuser")
_ = os.Setenv("PGPASSWORD", "testpass")
_ = os.Setenv("PGDATABASE", "testdb")
SanitizePostgresEnvironment()
assert.Equal(t, "localhost", os.Getenv("PGHOST"))
assert.Equal(t, "testuser", os.Getenv("PGUSER"))
assert.Equal(t, "testpass", os.Getenv("PGPASSWORD"))
assert.Equal(t, "testdb", os.Getenv("PGDATABASE"))
_ = os.Unsetenv("PGHOST")
_ = os.Unsetenv("PGUSER")
_ = os.Unsetenv("PGPASSWORD")
_ = os.Unsetenv("PGDATABASE")
}