diff --git a/pkg/plugin/implementation/cache/cache_test.go b/pkg/plugin/implementation/cache/cache_test.go index 931cb96..ddd7869 100644 --- a/pkg/plugin/implementation/cache/cache_test.go +++ b/pkg/plugin/implementation/cache/cache_test.go @@ -8,82 +8,8 @@ import ( "github.com/redis/go-redis/v9" "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 func TestValidate(t *testing.T) { tests := []struct { @@ -121,7 +47,7 @@ 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 @@ -145,15 +71,15 @@ func TestNew(t *testing.T) { 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) @@ -201,64 +127,64 @@ func TestCacheIntegration(t *testing.T) { if os.Getenv("SKIP_REDIS_INTEGRATION_TEST") == "true" { t.Skip("Integration test skipped - SKIP_REDIS_INTEGRATION_TEST=true") } - + // Set up test environment ctx := context.Background() cfg := &Config{ Addr: "localhost:6379", } - + // 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 cache, cleanup, err := New(ctx, cfg) if err != nil { t.Fatalf("Failed to create cache: %v", err) } defer cleanup() - + // Test Set and Get key := "test_key" value := "test_value" ttl := time.Minute - + err = cache.Set(ctx, key, value, ttl) assert.NoError(t, err, "Set should not return an error") - 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 err = cache.Delete(ctx, key) assert.NoError(t, err, "Delete should not return an error") - + // Verify key is gone _, err = cache.Get(ctx, key) assert.Equal(t, redis.Nil, err, "Get should return redis.Nil after deletion") - + // 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") } diff --git a/pkg/plugin/implementation/cache/cmd/plugin_test.go b/pkg/plugin/implementation/cache/cmd/plugin_test.go index 322cb02..e16c27c 100644 --- a/pkg/plugin/implementation/cache/cmd/plugin_test.go +++ b/pkg/plugin/implementation/cache/cmd/plugin_test.go @@ -137,7 +137,9 @@ func TestProviderIntegration(t *testing.T) { } // 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 provider := cacheProvider{} @@ -156,16 +158,16 @@ func TestProviderIntegration(t *testing.T) { // Verify it works by setting and getting a value testKey := "provider_test_key" testValue := "provider_test_value" - + // Set a value err = cache.Set(ctx, testKey, testValue, 0) assert.NoError(t, err, "Set operation should not fail") - + // Get the value got, err := cache.Get(ctx, testKey) assert.NoError(t, err, "Get operation should not fail") assert.Equal(t, testValue, got, "Should get the value that was set") - + // Clean up err = cache.Delete(ctx, testKey) assert.NoError(t, err, "Delete operation should not fail")