Last active
August 14, 2019 18:24
-
-
Save shazow/2c9355d31d41cc0e654d374aed0e69bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using github.com/gomodule/redigo v2.0.0+incompatible | |
func TestMiniRedisScan(t *testing.T) { | |
/* | |
redis> ZADD idx 0 "ccc" | |
(integer) 1 | |
redis> ZRANGEBYLEX idx "[d" "[e" | |
(empty list or set) | |
redis> ZRANGEBYLEX idx "[c" "[d" | |
1) "ccc" | |
*/ | |
mini, err := miniredis.Run() | |
if err != nil { | |
t.Fatal(err) | |
} | |
defer mini.Close() | |
db, err := redigo.Dial("tcp", mini.Addr()) | |
if err != nil { | |
t.Fatal(err) | |
} | |
if _, err := db.Do("FLUSHDB"); err != nil { | |
t.Fatal(err) | |
} | |
if _, err := db.Do("ZADD", "idx", "0", "ccc"); err != nil { | |
t.Fatal(err) | |
} | |
if got, err := db.Do("ZRANGEBYLEX", "idx", "[d", "[e"); err != nil { | |
t.Fatal(err) | |
} else if want := []interface{}{}; !reflect.DeepEqual(got, want) { | |
// This fails with ["ccc"] | |
t.Errorf("got: %q; want: %q", got, want) | |
} | |
if got, err := db.Do("ZRANGEBYLEX", "idx", "[c", "[d"); err != nil { | |
t.Fatal(err) | |
} else if want := []interface{}{[]byte("ccc")}; !reflect.DeepEqual(got, want) { | |
// This passes as expected | |
t.Errorf("got: %q; want: %q", got, want) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using github.com/go-redis/redis v6.15.2+incompatible | |
func TestMiniRedisScan(t *testing.T) { | |
/* | |
redis> ZADD idx 0 "ccc" | |
(integer) 1 | |
redis> ZRANGEBYLEX idx "[d" "[e" | |
(empty list or set) | |
redis> ZRANGEBYLEX idx "[c" "[d" | |
1) "ccc" | |
*/ | |
mini, err := miniredis.Run() | |
if err != nil { | |
t.Fatal(err) | |
} | |
defer mini.Close() | |
db := redis.NewUniversalClient(&redis.UniversalOptions{ | |
Addrs: []string{mini.Addr()}, | |
// Replcaing with an actual Redis server passes the test | |
//Addrs: []string{"localhost:7777"}, | |
}) | |
if err := db.FlushDB().Err(); err != nil { | |
t.Fatal(err) | |
} | |
if err := db.ZAdd("idx", redis.Z{Score: 0, Member: "ccc"}).Err(); err != nil { | |
t.Fatal(err) | |
} | |
if got, err := db.ZRangeByLex("idx", redis.ZRangeBy{ | |
Min: "[d", | |
Max: "[e", | |
}).Result(); err != nil { | |
t.Fatal(err) | |
} else if want := []string{}; !reflect.DeepEqual(got, want) { | |
// This fails with ["ccc"] | |
t.Errorf("got: %q; want: %q", got, want) | |
} | |
if got, err := db.ZRangeByLex("idx", redis.ZRangeBy{ | |
Min: "[c", | |
Max: "[d", | |
}).Result(); err != nil { | |
t.Fatal(err) | |
} else if want := []string{"ccc"}; !reflect.DeepEqual(got, want) { | |
// This passes as expected | |
t.Errorf("got: %q; want: %q", got, want) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment