unexpected type for Strings, got type []string
timpwbaker opened this issue · 3 comments
Hello.
I am trying to add:
conn.Command("ZRANGEBYSCORE", "last_message_at", "-inf", timestamp).Expect([]string{"123"})
Then in my app code:
ids, err := redis.Strings(conn.Do("ZRANGEBYSCORE", "last_message_at", "-inf", timestamp))
And when I try to access in my test it I get
redigo: unexpected type for Strings, got type []string
Application code works ok backed by real redis.
As far as I can tell Strings does return a []string in redigo. Am I misunderstanding something?
Thank you very much in advance.
T
Hi @timpwbaker , I think the line
conn.Command("ZRANGEBYSCORE", "last_message_at", "-inf", timestamp).Expect([]string{"123"})
should be:
conn.Command("ZRANGEBYSCORE", "last_message_at", "-inf", timestamp).Expect([]interface{}{"123"})
Check the example bellow.
issues33.go
package issues33
import "github.com/gomodule/redigo/redis"
// ZRANGEBYSCOREExample follows the example from
// https://redis.io/commands/zrangebyscore
func ZRANGEBYSCOREExample(c redis.Conn) ([]string, error) {
if _, err := c.Do("ZADD", "myzset", 1, "one"); err != nil {
return nil, err
}
if _, err := c.Do("ZADD", "myzset", 2, "two"); err != nil {
return nil, err
}
if _, err := c.Do("ZADD", "myzset", 3, "three"); err != nil {
return nil, err
}
return redis.Strings(c.Do("ZRANGEBYSCORE", "myzset", "-inf", "+inf"))
}
issues33_test.go
package issues33
import (
"reflect"
"testing"
"github.com/rafaeljusto/redigomock"
)
func TestZRANGEBYSCOREExample(t *testing.T) {
c := redigomock.NewConn()
scenarios := []struct {
description string
stub func()
expected []string
expectedError error
}{
{
description: "it should list the elements 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("ZRANGEBYSCORE", "myzset", "-inf", "+inf").Expect([]interface{}{"one", "two", "three"})
},
expected: []string{"one", "two", "three"},
},
}
for _, scenario := range scenarios {
t.Run(scenario.description, func(t *testing.T) {
c.Clear()
scenario.stub()
output, err := ZRANGEBYSCOREExample(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)
}
})
}
}
Thanks @rafaeljusto!
I ended up sticking with ZRANGEBYSCORE
but I needed to upgrade my instance of redigomock. The version I got from dep out of the box did not include support for this.
I will close this one. Let me know if you have any other questions.