redis

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: May 9, 2020 License: MIT Imports: 3 Imported by: 1

README

Gnomock Redis Build Go Report Card

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

package redis_test

import (
	"fmt"
	"testing"

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

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

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

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

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

	addr := container.Address(gnomock.DefaultPort)
	client := redis.NewClient(&redis.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.StartPreset function to create a configured Redis container to use in tests

Index

Examples

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

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

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

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

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

addr := container.DefaultAddress()
client := redis.NewClient(&redis.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>

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

type P added in v0.3.1

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

P is a Gnomock Preset implementation for Redis storage

func (*P) Image added in v0.3.1

func (r *P) Image() string

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

func (*P) Options added in v0.3.1

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

Options returns a list of options to configure this container

func (*P) Ports added in v0.3.1

func (r *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