mockredis

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 1, 2024 License: MIT Imports: 10 Imported by: 0

README

Mock Redis

Tests goreportcard

Provide a mock Redis instance and optionally a mock Redis client for testing purposes. This library is built so you can mock Redis instances using real Redis containers. You will need to have Docker running on your local machine or within your CI environment.

This library is built on top of testcontainers.

Usage

Creating a mock instance for creating a customer connection.

func TestXXX(t *testing.T) {
	ctx := context.Background()

	mock, err := mockredis.NewInstance(ctx, t)

	if err != nil {
		t.Fatalf("creating the instance: %v", err)
		return
	}

	// close the mock
	defer mock.Close(ctx)

	// ... my test code
}

Creating a mock redis client for interacting with Redis.

func TestXXX(t *testing.T) {
	ctx := context.Background()

	mock, err := mockredis.NewClient(ctx, t)

	if err != nil {
		t.Fatalf("creating the client: %v", err)
		return
	}

	// close the mock
	defer mock.Close(ctx)

    redisClient := mock.Client()

	// ... my test code
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client wraps the Redis client and the underlying instance for testing. It provides helper methods for accessing the client, closing the client, and closing the instance.

func NewClient

func NewClient(ctx context.Context, t *testing.T) (*Client, error)

NewClient creates a new Redis client that connects to an underlying instance for testing.

func NewClientWithConfig

func NewClientWithConfig(ctx context.Context, t *testing.T, config *redis.Options) (*Client, error)

NewClientWithConfig creates a new Redis client that connects to an underlying instance for testing. This method will override the `Addr` property with the address of the underlying container.

func (*Client) Client

func (r *Client) Client() *redis.Client

Client returns the underlying Redis client.

func (*Client) Close

func (r *Client) Close(ctx context.Context)

Close closes the underlying Redis client and the instance.

type Instance

type Instance struct {
	// contains filtered or unexported fields
}

Instance represents the underlying container that is running the mock redis instance.

func NewInstance

func NewInstance(ctx context.Context, t *testing.T) (*Instance, error)

NewInstance creates a new Redis container. It will exponentially backoff until the container is ready to accept connections so that you can handle throttling within CI environments

func (*Instance) Close

func (r *Instance) Close(ctx context.Context)

Close terminates the underlying container.

func (*Instance) Port

func (r *Instance) Port(ctx context.Context) (nat.Port, error)

Port returns the mapped port of the underlying container.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL