66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
package config_test
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/techxcar/backend/internal/config"
|
|
)
|
|
|
|
func TestLoad_defaults(t *testing.T) {
|
|
os.Setenv("DATABASE_URL", "postgres://test:test@localhost/test")
|
|
os.Setenv("REDIS_URL", "redis://localhost:6379")
|
|
os.Setenv("JWT_SECRET", "test-secret-32-chars-minimum-ok!")
|
|
defer func() {
|
|
os.Unsetenv("DATABASE_URL")
|
|
os.Unsetenv("REDIS_URL")
|
|
os.Unsetenv("JWT_SECRET")
|
|
}()
|
|
|
|
cfg, err := config.Load()
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "8080", cfg.Port)
|
|
assert.Equal(t, "development", cfg.AppEnv)
|
|
assert.Equal(t, "postgres://test:test@localhost/test", cfg.DatabaseURL)
|
|
assert.Equal(t, "redis://localhost:6379", cfg.RedisURL)
|
|
}
|
|
|
|
func TestLoad_missingDatabaseURL(t *testing.T) {
|
|
os.Unsetenv("DATABASE_URL")
|
|
os.Setenv("JWT_SECRET", "test-secret-32-chars-minimum-ok!")
|
|
defer os.Unsetenv("JWT_SECRET")
|
|
|
|
_, err := config.Load()
|
|
assert.ErrorContains(t, err, "DATABASE_URL")
|
|
}
|
|
|
|
func TestLoad_missingJWTSecret(t *testing.T) {
|
|
os.Setenv("DATABASE_URL", "postgres://test:test@localhost/test")
|
|
os.Unsetenv("JWT_SECRET")
|
|
defer os.Unsetenv("DATABASE_URL")
|
|
|
|
_, err := config.Load()
|
|
assert.ErrorContains(t, err, "JWT_SECRET")
|
|
}
|
|
|
|
func TestLoad_customPort(t *testing.T) {
|
|
os.Setenv("DATABASE_URL", "postgres://test:test@localhost/test")
|
|
os.Setenv("REDIS_URL", "redis://localhost:6379")
|
|
os.Setenv("JWT_SECRET", "test-secret-32-chars-minimum-ok!")
|
|
os.Setenv("PORT", "9090")
|
|
defer func() {
|
|
os.Unsetenv("DATABASE_URL")
|
|
os.Unsetenv("REDIS_URL")
|
|
os.Unsetenv("JWT_SECRET")
|
|
os.Unsetenv("PORT")
|
|
}()
|
|
|
|
cfg, err := config.Load()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "9090", cfg.Port)
|
|
}
|