redimock

package module
v0.0.0-...-6d49372 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2019 License: MIT Imports: 12 Imported by: 0

README

Redimock

GoDoc Build Status Coverage Status Go Report Card

Redimock is the Redis mocking library in TCP level. This is not a Redis clone; it's just a mock. You need to know what command should be expected and provide the output for that commands. This information is available in Redis documents, but also I am adding more helper functions to cover all Redis commands easily.

Usage

For usage, you can see the tests suits of this library itself. Currently, I do test it using redigo and go-redis.

package main 

import (
	"github.com/gomodule/redigo/redis"
)

func ReadRedis(red redis.Conn) error {
	v, err := redis.String(red.Do("GET", "KEY"))
	if err != nil {
		return err
	}

	_, err = red.Do("SET", v, "HI")
	if err != nil {
		return err
	}

	return nil
}

You can write test like this:

package main 

import (
	"context"
	"testing"

	"github.com/fzerorubigd/redimock"
	"github.com/gomodule/redigo/redis"
)

func TestReadRedis(t *testing.T) {
	ctx, cl := context.WithCancel(context.Background())
	defer cl()

	mock, err := redimock.NewServer(ctx, "")
	if err != nil {
		t.FailNow()
	}

	rd, err := redis.Dial("tcp", mock.Addr().String())
	if err != nil {
		t.FailNow()
	}

	mock.ExpectGet("KEY", true, "ANOTHER")
	// Also it works with
	// mock.Expect("GET").WithArgs("KEY").WillReturn(redimock.BulkString("ANOTHER"))
	mock.Expect("SET").WithAnyArgs().WillReturn("OK")

	err = ReadRedis(rd)
	if err != nil {
		t.FailNow()
	}
}

The helper functions are not complete and all are subject to change. (functions inside the commands.go file)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrProtocol = errors.New("invalid request")

ErrProtocol is the general error for unexpected input

Functions

This section is empty.

Types

type BulkString

type BulkString string

BulkString is used to handle the bulk string, normal strings are treated as simple string

type Command

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

Command is a single

func (*Command) Any

func (c *Command) Any() *Command

Any means this can be called 0 to n time

func (*Command) CloseConnection

func (c *Command) CloseConnection() *Command

CloseConnection should close connection after this command

func (*Command) Once

func (c *Command) Once() *Command

Once means it should be called once

func (*Command) Times

func (c *Command) Times(n int) *Command

Times this should be called n times

func (*Command) WillReturn

func (c *Command) WillReturn(ret ...interface{}) *Command

WillReturn set the return value for this command

func (*Command) WillReturnFn

func (c *Command) WillReturnFn(r Result) *Command

WillReturnFn is a helper function for advance return value

func (*Command) WithAnyArgs

func (c *Command) WithAnyArgs() *Command

WithAnyArgs if any argument is ok

func (*Command) WithArgs

func (c *Command) WithArgs(args ...string) *Command

WithArgs add array as arguments

func (*Command) WithDelay

func (c *Command) WithDelay(d time.Duration) *Command

WithDelay return command with delay

func (*Command) WithFnArgs

func (c *Command) WithFnArgs(f func(...string) bool) *Command

WithFnArgs is advanced function compare for arguments

type Error

type Error string

Error is the redis error type

type Result

type Result = func(...string) []interface{}

Result is the function that can be used for advanced result value

type Server

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

Server is the mock server used for handling the connections

func NewServer

func NewServer(ctx context.Context, addr string) (*Server, error)

NewServer makes a server listening on addr. Close with .Close().

func (*Server) Addr

func (s *Server) Addr() *net.TCPAddr

Addr has the net.Addr struct

func (*Server) Expect

func (s *Server) Expect(command string) *Command

Expect return a command

func (*Server) ExpectBLPop

func (s *Server) ExpectBLPop(delay int, topic, resp string, result bool, keys ...string) *Command

ExpectBLPop is the blpop command

func (*Server) ExpectBRPop

func (s *Server) ExpectBRPop(delay int, topic, resp string, result bool, keys ...string) *Command

ExpectBRPop is the brpop command

func (*Server) ExpectGet

func (s *Server) ExpectGet(key string, exists bool, result string) *Command

ExpectGet return a redis GET command

func (*Server) ExpectHGetAll

func (s *Server) ExpectHGetAll(key string, ret map[string]string) *Command

ExpectHGetAll return the HGETALL command

func (*Server) ExpectHSet

func (s *Server) ExpectHSet(key, field, value string, update bool) *Command

ExpectHSet is the command HSET, if the update is true, then it means the key was there already

func (*Server) ExpectLPush

func (s *Server) ExpectLPush(result int, key string, values ...string) *Command

ExpectLPush is helper for lpush command

func (*Server) ExpectPing

func (s *Server) ExpectPing() *Command

ExpectPing is the ping command

func (*Server) ExpectQuit

func (s *Server) ExpectQuit() *Command

ExpectQuit try to return quit command

func (*Server) ExpectRPush

func (s *Server) ExpectRPush(result int, key string, values ...string) *Command

ExpectRPush is helper for lpush command

func (*Server) ExpectSet

func (s *Server) ExpectSet(key string, value string, success bool, extra ...string) *Command

ExpectSet return a redis set command. success could be false only for NX or XX option, otherwise it dose not make sense

func (*Server) ExpectationsWereMet

func (s *Server) ExpectationsWereMet() error

ExpectationsWereMet return nil if the all expects match or error if not

Jump to

Keyboard shortcuts

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