first commit
This commit is contained in:
45
configs/redis_test.go
Normal file
45
configs/redis_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user