redis

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2023 License: MIT Imports: 5 Imported by: 7

README

Gnomock Redis

Gnomock Redis is a Gnomock preset for running tests against a real Redis container, without mocks.

package redis_test

import (
	"fmt"
	"testing"

	redisclient "github.com/go-redis/redis/v7"
	"github.com/orlangure/gnomock"
	"github.com/orlangure/gnomock/preset/redis"
)

func ExamplePreset() {
	vs := make(map[string]interface{})

	vs["a"] = "foo"
	vs["b"] = 42
	vs["c"] = true

	p := redis.Preset(redis.WithValues(vs))
	container, _ := gnomock.Start(p)

	defer func() { _ = gnomock.Stop(container) }()

	addr := container.DefaultAddress()
	client := redisclient.NewClient(&redisclient.Options{Addr: addr})

	fmt.Println(client.Get("a").Result())

	var number int

	err := client.Get("b").Scan(&number)
	fmt.Println(number, err)

	var flag bool

	err = client.Get("c").Scan(&flag)
	fmt.Println(flag, err)

	// Output:
	// foo <nil>
	// 42 <nil>
	// true <nil>
}

Documentation

Overview

Package redis includes Redis implementation of Gnomock Preset interface. This Preset can be passed to gnomock.Start() function to create a configured Redis container to use in tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Preset

func Preset(opts ...Option) gnomock.Preset

Preset creates a new Gmomock Redis preset. This preset includes a Redis specific healthcheck function, default Redis image and port, and allows to optionally set up initial state.

Types

type Option

type Option func(*P)

Option is an optional configuration of this Gnomock preset. Use available Options to configure the container.

func WithValues

func WithValues(vs map[string]interface{}) Option

WithValues initializes Redis with the provided key/value pairs. These values never expire. See go-redis/redis package for information on supported value types.

func WithVersion added in v0.9.0

func WithVersion(version string) Option

WithVersion sets image version.

type P

type P struct {
	Values  map[string]interface{} `json:"values"`
	Version string                 `json:"version"`
}

P is a Gnomock Preset implementation for Redis storage.

func (*P) Image

func (p *P) Image() string

Image returns an image that should be pulled to create this container.

func (*P) Options

func (p *P) Options() []gnomock.Option

Options returns a list of options to configure this container.

func (*P) Ports

func (p *P) Ports() gnomock.NamedPorts

Ports returns ports that should be used to access this container.

Jump to

Keyboard shortcuts

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