Last active
May 28, 2023 18:16
-
-
Save ashleyconnor/732ce2b2ccce5f1b3dd9a0dcc9e77c79 to your computer and use it in GitHub Desktop.
Testing a redis client against real redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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