Comment Resolve Redis Plugin

This commit is contained in:
Atharva Zade
2025-05-14 17:00:37 +05:30
parent 1b79156693
commit 04ba311628
3 changed files with 13 additions and 189 deletions

2
go.mod
View File

@@ -1,6 +1,6 @@
module github.com/beckn/beckn-onix module github.com/beckn/beckn-onix
go 1.23.0 go 1.24
toolchain go1.23.4 toolchain go1.23.4

View File

@@ -3,75 +3,27 @@ package main
import ( import (
"context" "context"
"errors" "errors"
"strconv"
"github.com/beckn/beckn-onix/pkg/plugin/definition" "github.com/beckn/beckn-onix/pkg/plugin/definition"
"github.com/beckn/beckn-onix/pkg/plugin/implementation/cache" "github.com/beckn/beckn-onix/pkg/plugin/implementation/cache"
) )
// cacheProvider implements the definition.CacheProvider interface. // cacheProvider implements the CacheProvider interface for the cache plugin.
type cacheProvider struct{} type cacheProvider struct{}
// Config holds the configuration settings for the Redis cache plugin. // New creates a new cache plugin instance.
type Config struct { func (c cacheProvider) New(ctx context.Context, config map[string]string) (definition.Cache, func() error, error) {
Addr string // Redis address (host:port)
DB int // Redis database number (optional, defaults to 0)
Password string // Redis password (optional, can be empty or from env)
}
// parseConfig converts the string map configuration to a Config struct.
func parseConfig(config map[string]string) (*Config, error) {
addr, ok := config["addr"]
if !ok || addr == "" {
return nil, errors.New("config must contain 'addr'")
}
// Default values
db := 0
password := ""
// Parse DB if provided
if val, ok := config["db"]; ok && val != "" {
if parsedVal, err := strconv.Atoi(val); err == nil {
db = parsedVal
}
}
// Get password if provided
if val, ok := config["password"]; ok {
password = val
}
return &Config{
Addr: addr,
DB: db,
Password: password,
}, nil
}
// convertToRedisConfig converts the plugin Config to redis.Config.
func convertToRedisConfig(cfg *Config) *cache.Config {
return &cache.Config{
Addr: cfg.Addr,
}
}
// New initializes a new Redis cache with the given configuration.
func (p cacheProvider) New(ctx context.Context, config map[string]string) (definition.Cache, func() error, error) {
if ctx == nil { if ctx == nil {
return nil, nil, errors.New("context cannot be nil") return nil, nil, errors.New("context cannot be nil")
} }
cfg, err := parseConfig(config) // Create cache.Config directly from map - validation is handled by cache.New
if err != nil { cacheConfig := &cache.Config{
return nil, nil, err Addr: config["addr"],
} }
// Convert to redis.Config return cache.New(ctx, cacheConfig)
redisConfig := convertToRedisConfig(cfg)
return cache.New(ctx, redisConfig)
} }
// Provider is the exported symbol that the plugin manager will look for. // Provider is the exported plugin instance
var Provider = cacheProvider{} var Provider definition.CacheProvider = cacheProvider{}

View File

@@ -12,79 +12,6 @@ import (
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
) )
// TestParseConfig tests the configuration parsing logic of the plugin
func TestParseConfig(t *testing.T) {
tests := []struct {
name string
config map[string]string
want *Config
wantErr bool
}{
{
name: "missing addr",
config: map[string]string{},
want: nil,
wantErr: true,
},
{
name: "empty addr",
config: map[string]string{"addr": ""},
want: nil,
wantErr: true,
},
{
name: "basic config",
config: map[string]string{"addr": "localhost:6379"},
want: &Config{Addr: "localhost:6379", DB: 0, Password: ""},
wantErr: false,
},
{
name: "with db",
config: map[string]string{"addr": "localhost:6379", "db": "1"},
want: &Config{Addr: "localhost:6379", DB: 1, Password: ""},
wantErr: false,
},
{
name: "with password",
config: map[string]string{"addr": "localhost:6379", "password": "secret"},
want: &Config{Addr: "localhost:6379", DB: 0, Password: "secret"},
wantErr: false,
},
{
name: "invalid db",
config: map[string]string{"addr": "localhost:6379", "db": "invalid"},
want: &Config{Addr: "localhost:6379", DB: 0, Password: ""},
wantErr: false, // Not an error, just defaults to 0
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseConfig(tt.config)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
}
})
}
}
// TestConvertToRedisConfig tests the configuration conversion logic
func TestConvertToRedisConfig(t *testing.T) {
cfg := &Config{
Addr: "localhost:6379",
DB: 1,
Password: "secret",
}
redisConfig := convertToRedisConfig(cfg)
assert.NotNil(t, redisConfig)
assert.Equal(t, cfg.Addr, redisConfig.Addr)
}
// TestProviderNew tests the New method of the cacheProvider // TestProviderNew tests the New method of the cacheProvider
func TestProviderNew(t *testing.T) { func TestProviderNew(t *testing.T) {
provider := cacheProvider{} provider := cacheProvider{}
@@ -138,61 +65,6 @@ func TestProviderNew(t *testing.T) {
} }
} }
// TestProviderIntegration tests the provider with a real Redis server
// func TestProviderIntegration(t *testing.T) {
// // Skip this test if requested
// if os.Getenv("SKIP_REDIS_INTEGRATION_TEST") == "true" {
// t.Skip("Integration test skipped - SKIP_REDIS_INTEGRATION_TEST=true")
// }
// // Set an empty password for testing
// if err := os.Setenv("REDIS_PASSWORD", ""); err != nil {
// 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
// provider := cacheProvider{}
// ctx := context.Background()
// config := map[string]string{
// "addr": "localhost:6379",
// "db": "0",
// }
// cache, cleanup, err := provider.New(ctx, config)
// if err != nil {
// t.Fatalf("Failed to create cache: %v", err)
// }
// 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
// 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")
// }
// TestProviderVariable tests that the Provider variable is correctly initialized // TestProviderVariable tests that the Provider variable is correctly initialized
func TestProviderVariable(t *testing.T) { func TestProviderVariable(t *testing.T) {
assert.NotNil(t, Provider, "Provider should not be nil") assert.NotNil(t, Provider, "Provider should not be nil")
@@ -262,7 +134,7 @@ func TestProviderIntegration(t *testing.T) {
// Create the config and convert it into a map[string]string // Create the config and convert it into a map[string]string
config := &cache.Config{ config := &cache.Config{
Addr: "localhost:35", Addr: "localhost:6379",
} }
// Convert the *cache.Config to map[string]string // Convert the *cache.Config to map[string]string
configMap := map[string]string{ configMap := map[string]string{