Initial Commit of Redis Plugin with Unit Test cases

This commit is contained in:
Atharva Zade
2025-05-13 14:59:19 +05:30
parent ec69ecd50d
commit afd157d123
2 changed files with 25 additions and 97 deletions

View File

@@ -8,82 +8,8 @@ import (
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
) )
// mockRedisClient is a mock implementation of the Redis client for testing
type mockRedisClient struct {
mock.Mock
}
// Create a mock of all Redis client methods we use
func (m *mockRedisClient) Get(ctx context.Context, key string) *redis.StringCmd {
args := m.Called(ctx, key)
return args.Get(0).(*redis.StringCmd)
}
func (m *mockRedisClient) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd {
args := m.Called(ctx, key, value, expiration)
return args.Get(0).(*redis.StatusCmd)
}
func (m *mockRedisClient) Del(ctx context.Context, keys ...string) *redis.IntCmd {
args := m.Called(ctx, keys)
return args.Get(0).(*redis.IntCmd)
}
func (m *mockRedisClient) FlushDB(ctx context.Context) *redis.StatusCmd {
args := m.Called(ctx)
return args.Get(0).(*redis.StatusCmd)
}
func (m *mockRedisClient) Ping(ctx context.Context) *redis.StatusCmd {
args := m.Called(ctx)
return args.Get(0).(*redis.StatusCmd)
}
func (m *mockRedisClient) Close() error {
args := m.Called()
return args.Error(0)
}
// Test helpers for creating Redis command responses
func stringCmdWithValue(val string) *redis.StringCmd {
cmd := redis.NewStringCmd(context.Background())
cmd.SetVal(val)
return cmd
}
func stringCmdWithError(err error) *redis.StringCmd {
cmd := redis.NewStringCmd(context.Background())
cmd.SetErr(err)
return cmd
}
func statusCmdSuccess() *redis.StatusCmd {
cmd := redis.NewStatusCmd(context.Background(), "OK")
return cmd
}
func statusCmdWithError(err error) *redis.StatusCmd {
cmd := redis.NewStatusCmd(context.Background())
cmd.SetErr(err)
return cmd
}
func intCmdWithValue(val int64) *redis.IntCmd {
cmd := redis.NewIntCmd(context.Background())
cmd.SetVal(val)
return cmd
}
func intCmdWithError(err error) *redis.IntCmd {
cmd := redis.NewIntCmd(context.Background())
cmd.SetErr(err)
return cmd
}
// TestValidate tests the validation function for Cache configurations // TestValidate tests the validation function for Cache configurations
func TestValidate(t *testing.T) { func TestValidate(t *testing.T) {
tests := []struct { tests := []struct {
@@ -209,8 +135,9 @@ func TestCacheIntegration(t *testing.T) {
} }
// Set empty password for local testing // Set empty password for local testing
os.Setenv("REDIS_PASSWORD", "") if err := os.Setenv("REDIS_PASSWORD", ""); err != nil {
t.Fatalf("Failed to set environment variable: %v", err)
}
// Create a new cache // Create a new cache
cache, cleanup, err := New(ctx, cfg) cache, cleanup, err := New(ctx, cfg)
if err != nil { if err != nil {
@@ -225,7 +152,6 @@ func TestCacheIntegration(t *testing.T) {
err = cache.Set(ctx, key, value, ttl) err = cache.Set(ctx, key, value, ttl)
assert.NoError(t, err, "Set should not return an error") assert.NoError(t, err, "Set should not return an error")
got, err := cache.Get(ctx, key) got, err := cache.Get(ctx, key)
assert.NoError(t, err, "Get should not return an error") assert.NoError(t, err, "Get should not return an error")
assert.Equal(t, value, got, "Get should return the set value") assert.Equal(t, value, got, "Get should return the set value")

View File

@@ -137,7 +137,9 @@ func TestProviderIntegration(t *testing.T) {
} }
// Set an empty password for testing // Set an empty password for testing
os.Setenv("REDIS_PASSWORD", "") if err := os.Setenv("REDIS_PASSWORD", ""); err != nil {
t.Fatalf("Failed to set REDIS_PASSWORD: %v", err)
}
// Create provider and test with real Redis // Create provider and test with real Redis
provider := cacheProvider{} provider := cacheProvider{}