Initial Commit of Redis Plugin with Unit Test cases

This commit is contained in:
Atharva Zade
2025-05-13 15:39:31 +05:30
parent afd157d123
commit 21e823b955
3 changed files with 170 additions and 145 deletions

View File

@@ -10,12 +10,21 @@ import (
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
) )
// RedisClient is an interface for Redis operations that allows mocking
type RedisClient interface {
Get(ctx context.Context, key string) *redis.StringCmd
Set(ctx context.Context, key string, value interface{}, ttl time.Duration) *redis.StatusCmd
Del(ctx context.Context, keys ...string) *redis.IntCmd
FlushDB(ctx context.Context) *redis.StatusCmd
Ping(ctx context.Context) *redis.StatusCmd
}
type Config struct { type Config struct {
Addr string Addr string
} }
type Cache struct { type Cache struct {
client *redis.Client client RedisClient
} }
var ( var (
@@ -40,12 +49,7 @@ func New(ctx context.Context, cfg *Config) (*Cache, func() error, error) {
return nil, nil, err return nil, nil, err
} }
// Get password from environment variable
password := os.Getenv("REDIS_PASSWORD") password := os.Getenv("REDIS_PASSWORD")
// Allow empty password for local testing
// if password == "" {
// return nil, nil, ErrCredentialMissing
// }
client := redis.NewClient(&redis.Options{ client := redis.NewClient(&redis.Options{
Addr: cfg.Addr, Addr: cfg.Addr,

View File

@@ -8,9 +8,93 @@ 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"
) )
// TestValidate tests the validation function for Cache configurations // MockRedisClient is a mock implementation of the RedisClient interface
type MockRedisClient struct {
mock.Mock
}
func (m *MockRedisClient) Get(ctx context.Context, key string) *redis.StringCmd {
args := m.Called(ctx, key)
return redis.NewStringResult(args.String(0), args.Error(1))
}
func (m *MockRedisClient) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) *redis.StatusCmd {
args := m.Called(ctx, key, value, ttl)
return redis.NewStatusResult(args.String(0), args.Error(1))
}
func (m *MockRedisClient) Del(ctx context.Context, keys ...string) *redis.IntCmd {
args := m.Called(ctx, keys)
return redis.NewIntCmd(ctx, args.Int(0), args.Error(1))
}
func (m *MockRedisClient) FlushDB(ctx context.Context) *redis.StatusCmd {
args := m.Called(ctx)
return redis.NewStatusResult(args.String(0), args.Error(1))
}
func (m *MockRedisClient) Ping(ctx context.Context) *redis.StatusCmd {
args := m.Called(ctx)
return args.Get(0).(*redis.StatusCmd)
}
// TestCache_Get tests the Get method of the Cache type
func TestCache_Get(t *testing.T) {
mockClient := new(MockRedisClient)
ctx := context.Background()
cache := &Cache{client: mockClient}
mockClient.On("Get", ctx, "my-key").Return("my-value", nil)
value, err := cache.Get(ctx, "my-key")
assert.NoError(t, err)
assert.Equal(t, "my-value", value)
mockClient.AssertExpectations(t)
}
// TestCache_Set tests the Set method of the Cache type
func TestCache_Set(t *testing.T) {
mockClient := new(MockRedisClient)
ctx := context.Background()
cache := &Cache{client: mockClient}
mockClient.On("Set", ctx, "my-key", "my-value", time.Minute).Return("OK", nil)
err := cache.Set(ctx, "my-key", "my-value", time.Minute)
assert.NoError(t, err)
mockClient.AssertExpectations(t)
}
// TestCache_Delete tests the Delete method of the Cache type
func TestCache_Delete(t *testing.T) {
mockClient := new(MockRedisClient)
ctx := context.Background()
cache := &Cache{client: mockClient}
mockClient.On("Del", ctx, []string{"my-key"}).Return(1, nil)
err := cache.Delete(ctx, "my-key")
assert.NoError(t, err)
mockClient.AssertExpectations(t)
}
// TestCache_Clear tests the Clear method of the Cache type
func TestCache_Clear(t *testing.T) {
mockClient := new(MockRedisClient)
ctx := context.Background()
cache := &Cache{client: mockClient}
mockClient.On("FlushDB", ctx).Return("OK", nil)
err := cache.Clear(ctx)
assert.NoError(t, err)
mockClient.AssertExpectations(t)
}
// TestValidate tests the validate function
func TestValidate(t *testing.T) { func TestValidate(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@@ -37,154 +121,75 @@ func TestValidate(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
err := validate(tt.cfg) err := validate(tt.cfg)
assert.Equal(t, tt.wantErr, err) if tt.wantErr != nil {
}) assert.Equal(t, tt.wantErr, err)
}
}
// TestNew tests the validation behavior of the constructor
func TestNew(t *testing.T) {
// Save original env and restore after test
origPassword := os.Getenv("REDIS_PASSWORD")
defer os.Setenv("REDIS_PASSWORD", origPassword)
// Test validation errors directly
tests := []struct {
name string
cfg *Config
envPassword string
expectErr bool
errorContains string
}{
{
name: "nil config",
cfg: nil,
envPassword: "password",
expectErr: true,
errorContains: "empty config",
},
{
name: "empty address",
cfg: &Config{Addr: ""},
envPassword: "password",
expectErr: true,
errorContains: "missing required field",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set environment for this test
os.Setenv("REDIS_PASSWORD", tt.envPassword)
ctx := context.Background()
cache, cleanup, err := New(ctx, tt.cfg)
if tt.expectErr {
assert.Error(t, err)
assert.Nil(t, cache)
assert.Nil(t, cleanup)
if tt.errorContains != "" {
assert.Contains(t, err.Error(), tt.errorContains)
}
} else { } else {
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, cache)
assert.NotNil(t, cleanup)
} }
}) })
} }
} }
// TestCache_Get tests the Get method of the Cache type // TestNew_Validation tests the validation parts of the New function
func TestCache_Get(t *testing.T) { func TestNew_Validation(t *testing.T) {
// Skip for now as we need to refactor to inject our mocks testCases := []struct {
t.Skip("Cache.Get test skipped - cannot inject mocks at this time") name string
} cfg *Config
wantErr bool
// TestCache_Set tests the Set method of the Cache type errType error
func TestCache_Set(t *testing.T) { }{
// Skip for now as we need to refactor to inject our mocks {
t.Skip("Cache.Set test skipped - cannot inject mocks at this time") name: "nil config",
} cfg: nil,
wantErr: true,
// TestCache_Delete tests the Delete method of the Cache type errType: ErrEmptyConfig,
func TestCache_Delete(t *testing.T) { },
// Skip for now as we need to refactor to inject our mocks {
t.Skip("Cache.Delete test skipped - cannot inject mocks at this time") name: "empty addr",
} cfg: &Config{Addr: ""},
wantErr: true,
// TestCache_Clear tests the Clear method of the Cache type errType: ErrAddrMissing,
func TestCache_Clear(t *testing.T) { },
// Skip for now as we need to refactor to inject our mocks
t.Skip("Cache.Clear test skipped - cannot inject mocks at this time")
}
// Integration test that tests all Redis operations with a real Redis server
func TestCacheIntegration(t *testing.T) {
// Run this test by default since we have a Redis server available
// To skip, set SKIP_REDIS_INTEGRATION_TEST=true
if os.Getenv("SKIP_REDIS_INTEGRATION_TEST") == "true" {
t.Skip("Integration test skipped - SKIP_REDIS_INTEGRATION_TEST=true")
} }
// Set up test environment for _, tc := range testCases {
ctx := context.Background() t.Run(tc.name, func(t *testing.T) {
cfg := &Config{ _, _, err := New(context.Background(), tc.cfg)
Addr: "localhost:6379",
}
// Set empty password for local testing if tc.wantErr {
if err := os.Setenv("REDIS_PASSWORD", ""); err != nil { assert.Error(t, err)
t.Fatalf("Failed to set environment variable: %v", err) if tc.errType != nil {
assert.ErrorIs(t, err, tc.errType)
}
} else {
assert.NoError(t, err)
}
})
} }
// Create a new cache }
cache, cleanup, err := New(ctx, cfg)
// TestNew_ConnectionFailure tests the connection failure in New function
func TestNew_ConnectionFailure(t *testing.T) {
// Set test env var
err := os.Setenv("REDIS_PASSWORD", "")
if err != nil { if err != nil {
t.Fatalf("Failed to create cache: %v", err) t.Fatalf("Failed to set REDIS_PASSWORD environment variable: %v", err)
} }
defer cleanup()
// Test Set and Get defer func() {
key := "test_key" err := os.Unsetenv("REDIS_PASSWORD")
value := "test_value" if err != nil {
ttl := time.Minute t.Fatalf("Failed to unset REDIS_PASSWORD environment variable: %v", err)
}
}()
err = cache.Set(ctx, key, value, ttl) // Use an invalid connection address to force a connection failure
assert.NoError(t, err, "Set should not return an error") cfg := &Config{Addr: "invalid:1234"}
got, err := cache.Get(ctx, key)
assert.NoError(t, err, "Get should not return an error")
assert.Equal(t, value, got, "Get should return the set value")
// Test Delete // Call New which should fail with a connection error
err = cache.Delete(ctx, key) _, _, err = New(context.Background(), cfg)
assert.NoError(t, err, "Delete should not return an error")
// Verify key is gone // Verify error type is connection failure
_, err = cache.Get(ctx, key) assert.Error(t, err)
assert.Equal(t, redis.Nil, err, "Get should return redis.Nil after deletion") assert.ErrorIs(t, err, ErrConnectionFail)
// Test Clear
// First set multiple keys
key1 := "test_key1"
value1 := "test_value1"
key2 := "test_key2"
value2 := "test_value2"
err = cache.Set(ctx, key1, value1, ttl)
assert.NoError(t, err, "Set should not return an error")
err = cache.Set(ctx, key2, value2, ttl)
assert.NoError(t, err, "Set should not return an error")
// Clear all keys
err = cache.Clear(ctx)
assert.NoError(t, err, "Clear should not return an error")
// Verify keys are gone
_, err = cache.Get(ctx, key1)
assert.Equal(t, redis.Nil, err, "Get should return redis.Nil after clear")
_, err = cache.Get(ctx, key2)
assert.Equal(t, redis.Nil, err, "Get should return redis.Nil after clear")
} }

View File

@@ -87,10 +87,15 @@ func TestProviderNew(t *testing.T) {
// Save original environment variable and restore it after test // Save original environment variable and restore it after test
origPassword := os.Getenv("REDIS_PASSWORD") origPassword := os.Getenv("REDIS_PASSWORD")
defer os.Setenv("REDIS_PASSWORD", origPassword) defer func() {
if err := os.Setenv("REDIS_PASSWORD", origPassword); err != nil {
t.Fatalf("Failed to restore REDIS_PASSWORD: %v", err)
}
}()
// 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)
}
tests := []struct { tests := []struct {
name string name string
@@ -141,6 +146,13 @@ func TestProviderIntegration(t *testing.T) {
t.Fatalf("Failed to set REDIS_PASSWORD: %v", err) t.Fatalf("Failed to set REDIS_PASSWORD: %v", err)
} }
// Ensure we clean up the environment variable at the end
defer func() {
if err := os.Unsetenv("REDIS_PASSWORD"); err != nil {
t.Fatalf("Failed to unset REDIS_PASSWORD: %v", err)
}
}()
// Create provider and test with real Redis // Create provider and test with real Redis
provider := cacheProvider{} provider := cacheProvider{}
ctx := context.Background() ctx := context.Background()
@@ -153,7 +165,11 @@ func TestProviderIntegration(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Failed to create cache: %v", err) t.Fatalf("Failed to create cache: %v", err)
} }
defer cleanup() defer func() {
if err := cleanup(); err != nil {
t.Fatalf("Failed to clean up Redis client: %v", err)
}
}()
// Verify it works by setting and getting a value // Verify it works by setting and getting a value
testKey := "provider_test_key" testKey := "provider_test_key"