Skip to content

fzerorubigd/redimock

Repository files navigation

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)

About

[WIP] redis mock library in tcp level

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages