rafaeljusto/redigomock

ZREMRANGEBYSCORE not registered

Closed this issue · 2 comments

It seems ZREMRANGEBYSCORE does not work as expected.

I got :
command ZREMRANGEBYSCORE with arguments []interface {}{"key", "0", "1495533556"} not registered in redigomock library

You are missing the arguments of the ZREMRANGEBYSCORE when using Command. Or you could replace Command with GenericCommand call, that ignores all the arguments.

Check the following test example:

redigomock-issue33.go

package main

import (
	"fmt"
	"os"

	"github.com/garyburd/redigo/redis"
)

const (
	redisAddress = "127.0.0.1:6379"
)

// ZREMRANGEBYSCOREExample follows the example from
// https://redis.io/commands/zremrangebyscore
func ZREMRANGEBYSCOREExample(c redis.Conn) (int, error) {
	if _, err := c.Do("ZADD", "myzset", 1, "one"); err != nil {
		return 0, err
	}

	if _, err := c.Do("ZADD", "myzset", 2, "two"); err != nil {
		return 0, err
	}

	if _, err := c.Do("ZADD", "myzset", 3, "three"); err != nil {
		return 0, err
	}

	return redis.Int(c.Do("ZREMRANGEBYSCORE", "myzset", "-inf", "(2"))
}

func main() {
	c, err := redis.Dial("tcp", redisAddress)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer c.Close()

	output, err := ZREMRANGEBYSCOREExample(c)
	if err != nil {
		fmt.Println(err)
		os.Exit(2)
	}

	fmt.Println(output)
}

redigomock-issue33_test.go

package main

import (
	"reflect"
	"testing"

	"github.com/rafaeljusto/redigomock"
)

func TestZREMRANGEBYSCOREExample(t *testing.T) {
	c := redigomock.NewConn()

	scenarios := []struct {
		description   string
		stub          func()
		expected      int
		expectedError error
	}{
		{
			description: "it should remove an element correctly",
			stub: func() {
				c.Command("ZADD", "myzset", 1, "one").Expect(int64(1))
				c.Command("ZADD", "myzset", 2, "two").Expect(int64(1))
				c.Command("ZADD", "myzset", 3, "three").Expect(int64(1))
				c.Command("ZREMRANGEBYSCORE", "myzset", "-inf", "(2").Expect(int64(1))
			},
			expected: 1,
		},
	}

	for _, scenario := range scenarios {
		t.Run(scenario.description, func(t *testing.T) {
			c.Clear()
			scenario.stub()

			output, err := ZREMRANGEBYSCOREExample(c)

			if !reflect.DeepEqual(scenario.expected, output) {
				t.Errorf("unexpected result. Expected “%#v” and got “%#v”", scenario.expected, output)
			}

			if !reflect.DeepEqual(scenario.expectedError, err) {
				t.Errorf("unexpected error. Expected “%#v” and got “%#v”", scenario.expectedError, err)
			}
		})
	}
}

What, I did not make any changes and suddenly my code works today :s

Here is my lines if someone's curious :

c.Command("ZREMRANGEBYSCORE").Expect(int64(1))

_, err := redisClient.Do("ZREMRANGEBYSCORE", someKey, "0", fmt.Sprintf("%d", day7))

Sorry to bother you Rafael, thanks!