46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package configs
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
miniredis "github.com/alicebob/miniredis/v2"
|
|
)
|
|
|
|
func TestConnectRedisMissingURL(t *testing.T) {
|
|
t.Setenv("REDIS_URL", "")
|
|
if err := ConnectRedis(); err == nil {
|
|
t.Fatalf("expected error for missing REDIS_URL")
|
|
}
|
|
}
|
|
|
|
func TestConnectRedisInvalidURL(t *testing.T) {
|
|
t.Setenv("REDIS_URL", "%%%")
|
|
if err := ConnectRedis(); err == nil {
|
|
t.Fatalf("expected parse error for invalid REDIS_URL")
|
|
}
|
|
}
|
|
|
|
func TestConnectRedisSuccessAndClose(t *testing.T) {
|
|
mini, err := miniredis.Run()
|
|
if err != nil {
|
|
t.Fatalf("miniredis start failed: %v", err)
|
|
}
|
|
defer mini.Close()
|
|
|
|
t.Setenv("REDIS_URL", "redis://"+mini.Addr()+"/0")
|
|
|
|
if err := ConnectRedis(); err != nil {
|
|
t.Fatalf("ConnectRedis failed: %v", err)
|
|
}
|
|
t.Cleanup(func() { _ = CloseRedis() })
|
|
|
|
if pong, err := RDB.Ping(context.Background()).Result(); err != nil || pong != "PONG" {
|
|
t.Fatalf("expected redis ping PONG, got %q err=%v", pong, err)
|
|
}
|
|
|
|
if err := CloseRedis(); err != nil {
|
|
t.Fatalf("CloseRedis failed: %v", err)
|
|
}
|
|
}
|