Skip to content

Instantly share code, notes, and snippets.

@ashleyconnor
Last active May 28, 2023 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashleyconnor/732ce2b2ccce5f1b3dd9a0dcc9e77c79 to your computer and use it in GitHub Desktop.
Save ashleyconnor/732ce2b2ccce5f1b3dd9a0dcc9e77c79 to your computer and use it in GitHub Desktop.
Testing a redis client against real redis
package main
import (
"context"
"testing"
goredis "github.com/redis/go-redis/v9"
"github.com/testcontainers/testcontainers-go/modules/redis"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWithRedis(t *testing.T) {
ctx := context.Background()
redisContainer, err := redis.RunContainer(ctx)
require.NoError(t, err)
t.Cleanup(func() {
if err := redisContainer.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})
endpoint, err := redisContainer.Endpoint(ctx, "")
require.NoError(t, err)
client := goredis.NewClient(&goredis.Options{
Addr: endpoint,
})
client.Set(ctx, "FOO", "BAR", 0)
result := client.Get(ctx, "FOO")
assert.Equal(t, "BAR", result.Val())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment