Initial Commit of Redis Plugin with Unit Test cases
This commit is contained in:
112
pkg/plugin/implementation/cache/cache_test.go
vendored
112
pkg/plugin/implementation/cache/cache_test.go
vendored
@@ -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 {
|
||||||
@@ -121,7 +47,7 @@ func TestNew(t *testing.T) {
|
|||||||
// Save original env and restore after test
|
// Save original env and restore after test
|
||||||
origPassword := os.Getenv("REDIS_PASSWORD")
|
origPassword := os.Getenv("REDIS_PASSWORD")
|
||||||
defer os.Setenv("REDIS_PASSWORD", origPassword)
|
defer os.Setenv("REDIS_PASSWORD", origPassword)
|
||||||
|
|
||||||
// Test validation errors directly
|
// Test validation errors directly
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -145,15 +71,15 @@ func TestNew(t *testing.T) {
|
|||||||
errorContains: "missing required field",
|
errorContains: "missing required field",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
// Set environment for this test
|
// Set environment for this test
|
||||||
os.Setenv("REDIS_PASSWORD", tt.envPassword)
|
os.Setenv("REDIS_PASSWORD", tt.envPassword)
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
cache, cleanup, err := New(ctx, tt.cfg)
|
cache, cleanup, err := New(ctx, tt.cfg)
|
||||||
|
|
||||||
if tt.expectErr {
|
if tt.expectErr {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Nil(t, cache)
|
assert.Nil(t, cache)
|
||||||
@@ -201,64 +127,64 @@ func TestCacheIntegration(t *testing.T) {
|
|||||||
if os.Getenv("SKIP_REDIS_INTEGRATION_TEST") == "true" {
|
if os.Getenv("SKIP_REDIS_INTEGRATION_TEST") == "true" {
|
||||||
t.Skip("Integration test skipped - SKIP_REDIS_INTEGRATION_TEST=true")
|
t.Skip("Integration test skipped - SKIP_REDIS_INTEGRATION_TEST=true")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up test environment
|
// Set up test environment
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
cfg := &Config{
|
cfg := &Config{
|
||||||
Addr: "localhost:6379",
|
Addr: "localhost:6379",
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
t.Fatalf("Failed to create cache: %v", err)
|
t.Fatalf("Failed to create cache: %v", err)
|
||||||
}
|
}
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
// Test Set and Get
|
// Test Set and Get
|
||||||
key := "test_key"
|
key := "test_key"
|
||||||
value := "test_value"
|
value := "test_value"
|
||||||
ttl := time.Minute
|
ttl := time.Minute
|
||||||
|
|
||||||
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")
|
||||||
|
|
||||||
// Test Delete
|
// Test Delete
|
||||||
err = cache.Delete(ctx, key)
|
err = cache.Delete(ctx, key)
|
||||||
assert.NoError(t, err, "Delete should not return an error")
|
assert.NoError(t, err, "Delete should not return an error")
|
||||||
|
|
||||||
// Verify key is gone
|
// Verify key is gone
|
||||||
_, err = cache.Get(ctx, key)
|
_, err = cache.Get(ctx, key)
|
||||||
assert.Equal(t, redis.Nil, err, "Get should return redis.Nil after deletion")
|
assert.Equal(t, redis.Nil, err, "Get should return redis.Nil after deletion")
|
||||||
|
|
||||||
// Test Clear
|
// Test Clear
|
||||||
// First set multiple keys
|
// First set multiple keys
|
||||||
key1 := "test_key1"
|
key1 := "test_key1"
|
||||||
value1 := "test_value1"
|
value1 := "test_value1"
|
||||||
key2 := "test_key2"
|
key2 := "test_key2"
|
||||||
value2 := "test_value2"
|
value2 := "test_value2"
|
||||||
|
|
||||||
err = cache.Set(ctx, key1, value1, ttl)
|
err = cache.Set(ctx, key1, value1, ttl)
|
||||||
assert.NoError(t, err, "Set should not return an error")
|
assert.NoError(t, err, "Set should not return an error")
|
||||||
|
|
||||||
err = cache.Set(ctx, key2, value2, ttl)
|
err = cache.Set(ctx, key2, value2, ttl)
|
||||||
assert.NoError(t, err, "Set should not return an error")
|
assert.NoError(t, err, "Set should not return an error")
|
||||||
|
|
||||||
// Clear all keys
|
// Clear all keys
|
||||||
err = cache.Clear(ctx)
|
err = cache.Clear(ctx)
|
||||||
assert.NoError(t, err, "Clear should not return an error")
|
assert.NoError(t, err, "Clear should not return an error")
|
||||||
|
|
||||||
// Verify keys are gone
|
// Verify keys are gone
|
||||||
_, err = cache.Get(ctx, key1)
|
_, err = cache.Get(ctx, key1)
|
||||||
assert.Equal(t, redis.Nil, err, "Get should return redis.Nil after clear")
|
assert.Equal(t, redis.Nil, err, "Get should return redis.Nil after clear")
|
||||||
|
|
||||||
_, err = cache.Get(ctx, key2)
|
_, err = cache.Get(ctx, key2)
|
||||||
assert.Equal(t, redis.Nil, err, "Get should return redis.Nil after clear")
|
assert.Equal(t, redis.Nil, err, "Get should return redis.Nil after clear")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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{}
|
||||||
@@ -156,16 +158,16 @@ func TestProviderIntegration(t *testing.T) {
|
|||||||
// 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"
|
||||||
testValue := "provider_test_value"
|
testValue := "provider_test_value"
|
||||||
|
|
||||||
// Set a value
|
// Set a value
|
||||||
err = cache.Set(ctx, testKey, testValue, 0)
|
err = cache.Set(ctx, testKey, testValue, 0)
|
||||||
assert.NoError(t, err, "Set operation should not fail")
|
assert.NoError(t, err, "Set operation should not fail")
|
||||||
|
|
||||||
// Get the value
|
// Get the value
|
||||||
got, err := cache.Get(ctx, testKey)
|
got, err := cache.Get(ctx, testKey)
|
||||||
assert.NoError(t, err, "Get operation should not fail")
|
assert.NoError(t, err, "Get operation should not fail")
|
||||||
assert.Equal(t, testValue, got, "Should get the value that was set")
|
assert.Equal(t, testValue, got, "Should get the value that was set")
|
||||||
|
|
||||||
// Clean up
|
// Clean up
|
||||||
err = cache.Delete(ctx, testKey)
|
err = cache.Delete(ctx, testKey)
|
||||||
assert.NoError(t, err, "Delete operation should not fail")
|
assert.NoError(t, err, "Delete operation should not fail")
|
||||||
|
|||||||
Reference in New Issue
Block a user