5 Star 4 Fork 1

Gitee 极速下载 / rueidis

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/rueian/rueidis
克隆/下载
message_test.go 61.88 KB
一键复制 编辑 原始数据 按行查看 历史
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
package rueidis
import (
"bytes"
"errors"
"fmt"
"io"
"math"
"reflect"
"strconv"
"strings"
"testing"
"time"
)
type wrapped struct {
msg string
err error
}
func (e wrapped) Error() string { return e.msg }
func (e wrapped) Unwrap() error { return e.err }
func TestIsRedisNil(t *testing.T) {
err := Nil
if !IsRedisNil(err) {
t.Fatal("IsRedisNil fail")
}
if IsRedisNil(errors.New("other")) {
t.Fatal("IsRedisNil fail")
}
if err.Error() != "redis nil message" {
t.Fatal("IsRedisNil fail")
}
wrappedErr := wrapped{msg: "wrapped", err: Nil}
if IsRedisNil(wrappedErr) {
t.Fatal("IsRedisNil fail : wrapped error")
}
}
func TestIsRedisErr(t *testing.T) {
err := Nil
if ret, ok := IsRedisErr(err); ok || ret != Nil {
t.Fatal("TestIsRedisErr fail")
}
if ret, ok := IsRedisErr(nil); ok || ret != nil {
t.Fatal("TestIsRedisErr fail")
}
if ret, ok := IsRedisErr(errors.New("other")); ok || ret != nil {
t.Fatal("TestIsRedisErr fail")
}
if ret, ok := IsRedisErr(&RedisError{typ: '-'}); !ok || ret.typ != '-' {
t.Fatal("TestIsRedisErr fail")
}
wrappedErr := wrapped{msg: "wrapped", err: Nil}
if ret, ok := IsRedisErr(wrappedErr); ok || ret == Nil {
t.Fatal("TestIsRedisErr fail : wrapped error")
}
}
func TestIsRedisBusyGroup(t *testing.T) {
err := errors.New("other")
if IsRedisBusyGroup(err) {
t.Fatal("TestIsRedisBusyGroup fail")
}
err = &RedisError{string: "BUSYGROUP Consumer Group name already exists"}
if !IsRedisBusyGroup(err) {
t.Fatal("TestIsRedisBusyGroup fail")
}
}
//gocyclo:ignore
func TestRedisResult(t *testing.T) {
//Add erroneous type
typeNames['t'] = "t"
t.Run("ToInt64", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).ToInt64(); err == nil {
t.Fatal("ToInt64 not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).ToInt64(); err == nil {
t.Fatal("ToInt64 not failed as expected")
}
if v, _ := (RedisResult{val: RedisMessage{typ: ':', integer: 1}}).ToInt64(); v != 1 {
t.Fatal("ToInt64 not get value as expected")
}
})
t.Run("ToBool", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).ToBool(); err == nil {
t.Fatal("ToBool not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).ToBool(); err == nil {
t.Fatal("ToBool not failed as expected")
}
if v, _ := (RedisResult{val: RedisMessage{typ: '#', integer: 1}}).ToBool(); !v {
t.Fatal("ToBool not get value as expected")
}
})
t.Run("AsBool", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsBool(); err == nil {
t.Fatal("ToBool not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsBool(); err == nil {
t.Fatal("ToBool not failed as expected")
}
if v, _ := (RedisResult{val: RedisMessage{typ: '#', integer: 1}}).AsBool(); !v {
t.Fatal("ToBool not get value as expected")
}
if v, _ := (RedisResult{val: RedisMessage{typ: ':', integer: 1}}).AsBool(); !v {
t.Fatal("ToBool not get value as expected")
}
if v, _ := (RedisResult{val: RedisMessage{typ: '+', string: "OK"}}).AsBool(); !v {
t.Fatal("ToBool not get value as expected")
}
if v, _ := (RedisResult{val: RedisMessage{typ: '$', string: "OK"}}).AsBool(); !v {
t.Fatal("ToBool not get value as expected")
}
})
t.Run("ToFloat64", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).ToFloat64(); err == nil {
t.Fatal("ToFloat64 not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).ToFloat64(); err == nil {
t.Fatal("ToFloat64 not failed as expected")
}
if v, _ := (RedisResult{val: RedisMessage{typ: ',', string: "0.1"}}).ToFloat64(); v != 0.1 {
t.Fatal("ToFloat64 not get value as expected")
}
})
t.Run("ToString", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).ToString(); err == nil {
t.Fatal("ToString not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).ToString(); err == nil {
t.Fatal("ToString not failed as expected")
}
if v, _ := (RedisResult{val: RedisMessage{typ: '+', string: "0.1"}}).ToString(); v != "0.1" {
t.Fatal("ToString not get value as expected")
}
})
t.Run("AsReader", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsReader(); err == nil {
t.Fatal("AsReader not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsReader(); err == nil {
t.Fatal("AsReader not failed as expected")
}
r, _ := (RedisResult{val: RedisMessage{typ: '+', string: "0.1"}}).AsReader()
bs, _ := io.ReadAll(r)
if !bytes.Equal(bs, []byte("0.1")) {
t.Fatalf("AsReader not get value as expected %v", bs)
}
})
t.Run("AsBytes", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsBytes(); err == nil {
t.Fatal("AsBytes not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsBytes(); err == nil {
t.Fatal("AsBytes not failed as expected")
}
bs, _ := (RedisResult{val: RedisMessage{typ: '+', string: "0.1"}}).AsBytes()
if !bytes.Equal(bs, []byte("0.1")) {
t.Fatalf("AsBytes not get value as expected %v", bs)
}
})
t.Run("DecodeJSON", func(t *testing.T) {
v := map[string]string{}
if err := (RedisResult{err: errors.New("other")}).DecodeJSON(&v); err == nil {
t.Fatal("DecodeJSON not failed as expected")
}
if err := (RedisResult{val: RedisMessage{typ: '-'}}).DecodeJSON(&v); err == nil {
t.Fatal("DecodeJSON not failed as expected")
}
if _ = (RedisResult{val: RedisMessage{typ: '+', string: `{"k":"v"}`}}).DecodeJSON(&v); v["k"] != "v" {
t.Fatalf("DecodeJSON not get value as expected %v", v)
}
})
t.Run("AsInt64", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsInt64(); err == nil {
t.Fatal("AsInt64 not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsInt64(); err == nil {
t.Fatal("AsInt64 not failed as expected")
}
if v, _ := (RedisResult{val: RedisMessage{typ: '+', string: "1"}}).AsInt64(); v != 1 {
t.Fatal("AsInt64 not get value as expected")
}
if v, _ := (RedisResult{val: RedisMessage{typ: ':', integer: 2}}).AsInt64(); v != 2 {
t.Fatal("AsInt64 not get value as expected")
}
})
t.Run("AsUint64", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsUint64(); err == nil {
t.Fatal("AsUint64 not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsUint64(); err == nil {
t.Fatal("AsUint64 not failed as expected")
}
if v, _ := (RedisResult{val: RedisMessage{typ: '+', string: "1"}}).AsUint64(); v != 1 {
t.Fatal("AsUint64 not get value as expected")
}
if v, _ := (RedisResult{val: RedisMessage{typ: ':', integer: 2}}).AsUint64(); v != 2 {
t.Fatal("AsUint64 not get value as expected")
}
})
t.Run("AsFloat64", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsFloat64(); err == nil {
t.Fatal("AsFloat64 not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsFloat64(); err == nil {
t.Fatal("AsFloat64 not failed as expected")
}
if v, _ := (RedisResult{val: RedisMessage{typ: '+', string: "1.1"}}).AsFloat64(); v != 1.1 {
t.Fatal("AsFloat64 not get value as expected")
}
if v, _ := (RedisResult{val: RedisMessage{typ: ',', string: "2.2"}}).AsFloat64(); v != 2.2 {
t.Fatal("AsFloat64 not get value as expected")
}
})
t.Run("ToArray", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).ToArray(); err == nil {
t.Fatal("ToArray not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).ToArray(); err == nil {
t.Fatal("ToArray not failed as expected")
}
values := []RedisMessage{{string: "item", typ: '+'}}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: values}}).ToArray(); !reflect.DeepEqual(ret, values) {
t.Fatal("ToArray not get value as expected")
}
})
t.Run("AsStrSlice", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsStrSlice(); err == nil {
t.Fatal("AsStrSlice not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsStrSlice(); err == nil {
t.Fatal("AsStrSlice not failed as expected")
}
values := []RedisMessage{{string: "item", typ: '+'}}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: values}}).AsStrSlice(); !reflect.DeepEqual(ret, []string{"item"}) {
t.Fatal("AsStrSlice not get value as expected")
}
})
t.Run("AsIntSlice", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsIntSlice(); err == nil {
t.Fatal("AsIntSlice not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsIntSlice(); err == nil {
t.Fatal("AsIntSlice not failed as expected")
}
values := []RedisMessage{{integer: 2, typ: ':'}}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: values}}).AsIntSlice(); !reflect.DeepEqual(ret, []int64{2}) {
t.Fatal("AsIntSlice not get value as expected")
}
values = []RedisMessage{{string: "3", typ: '+'}}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: values}}).AsIntSlice(); !reflect.DeepEqual(ret, []int64{3}) {
t.Fatal("AsIntSlice not get value as expected")
}
values = []RedisMessage{{string: "ab", typ: '+'}}
if _, err := (RedisResult{val: RedisMessage{typ: '*', values: values}}).AsIntSlice(); err == nil {
t.Fatal("AsIntSlice not failed as expected")
}
})
t.Run("AsFloatSlice", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsFloatSlice(); err == nil {
t.Fatal("AsFloatSlice not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsFloatSlice(); err == nil {
t.Fatal("AsFloatSlice not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{{string: "fff", typ: ','}}}}).AsFloatSlice(); err == nil {
t.Fatal("AsFloatSlice not failed as expected")
}
values := []RedisMessage{{integer: 1, typ: ':'}, {string: "2", typ: '+'}, {string: "3", typ: '$'}, {string: "4", typ: ','}}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: values}}).AsFloatSlice(); !reflect.DeepEqual(ret, []float64{1, 2, 3, 4}) {
t.Fatal("AsFloatSlice not get value as expected")
}
})
t.Run("AsBoolSlice", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsBoolSlice(); err == nil {
t.Fatal("AsBoolSlice not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsBoolSlice(); err == nil {
t.Fatal("AsBoolSlice not failed as expected")
}
values := []RedisMessage{{integer: 1, typ: ':'}, {string: "0", typ: '+'}, {integer: 1, typ: typeBool}}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: values}}).AsBoolSlice(); !reflect.DeepEqual(ret, []bool{true, false, true}) {
t.Fatal("AsBoolSlice not get value as expected")
}
})
t.Run("AsMap", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsMap(); err == nil {
t.Fatal("AsMap not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsMap(); err == nil {
t.Fatal("AsMap not failed as expected")
}
values := []RedisMessage{{string: "key", typ: '+'}, {string: "value", typ: '+'}}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: values}}).AsMap(); !reflect.DeepEqual(map[string]RedisMessage{
values[0].string: values[1],
}, ret) {
t.Fatal("AsMap not get value as expected")
}
})
t.Run("AsStrMap", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsStrMap(); err == nil {
t.Fatal("AsStrMap not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsStrMap(); err == nil {
t.Fatal("AsStrMap not failed as expected")
}
values := []RedisMessage{{string: "key", typ: '+'}, {string: "value", typ: '+'}}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: values}}).AsStrMap(); !reflect.DeepEqual(map[string]string{
values[0].string: values[1].string,
}, ret) {
t.Fatal("AsStrMap not get value as expected")
}
})
t.Run("AsIntMap", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsIntMap(); err == nil {
t.Fatal("AsIntMap not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsIntMap(); err == nil {
t.Fatal("AsIntMap not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{{string: "key", typ: '+'}, {string: "value", typ: '+'}}}}).AsIntMap(); err == nil {
t.Fatal("AsIntMap not failed as expected")
}
values := []RedisMessage{{string: "k1", typ: '+'}, {string: "1", typ: '+'}, {string: "k2", typ: '+'}, {integer: 2, typ: ':'}}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: values}}).AsIntMap(); !reflect.DeepEqual(map[string]int64{
"k1": 1,
"k2": 2,
}, ret) {
t.Fatal("AsIntMap not get value as expected")
}
})
t.Run("ToMap", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).ToMap(); err == nil {
t.Fatal("ToMap not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).ToMap(); err == nil {
t.Fatal("ToMap not failed as expected")
}
values := []RedisMessage{{string: "key", typ: '+'}, {string: "value", typ: '+'}}
if ret, _ := (RedisResult{val: RedisMessage{typ: '%', values: values}}).ToMap(); !reflect.DeepEqual(map[string]RedisMessage{
values[0].string: values[1],
}, ret) {
t.Fatal("ToMap not get value as expected")
}
})
t.Run("ToAny", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).ToAny(); err == nil {
t.Fatal("ToAny not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).ToAny(); err == nil {
t.Fatal("ToAny not failed as expected")
}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '%', values: []RedisMessage{{typ: '+', string: "key"}, {typ: ':', integer: 1}}},
{typ: '%', values: []RedisMessage{{typ: '+', string: "nil"}, {typ: '_'}}},
{typ: '%', values: []RedisMessage{{typ: '+', string: "err"}, {typ: '-', string: "err"}}},
{typ: ',', string: "1.2"},
{typ: '+', string: "str"},
{typ: '#', integer: 0},
{typ: '-', string: "err"},
{typ: '_'},
}}}).ToAny(); !reflect.DeepEqual([]any{
map[string]any{"key": int64(1)},
map[string]any{"nil": nil},
map[string]any{"err": &RedisError{typ: '-', string: "err"}},
1.2,
"str",
false,
&RedisError{typ: '-', string: "err"},
nil,
}, ret) {
t.Fatal("ToAny not get value as expected")
}
})
t.Run("AsXRangeEntry", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsXRangeEntry(); err == nil {
t.Fatal("AsXRangeEntry not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsXRangeEntry(); err == nil {
t.Fatal("AsXRangeEntry not failed as expected")
}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{{string: "id", typ: '+'}, {typ: '*', values: []RedisMessage{{typ: '+', string: "a"}, {typ: '+', string: "b"}}}}}}).AsXRangeEntry(); !reflect.DeepEqual(XRangeEntry{
ID: "id",
FieldValues: map[string]string{"a": "b"},
}, ret) {
t.Fatal("AsXRangeEntry not get value as expected")
}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{{string: "id", typ: '+'}, {typ: '_'}}}}).AsXRangeEntry(); !reflect.DeepEqual(XRangeEntry{
ID: "id",
FieldValues: nil,
}, ret) {
t.Fatal("AsXRangeEntry not get value as expected")
}
})
t.Run("AsXRange", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsXRange(); err == nil {
t.Fatal("AsXRange not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsXRange(); err == nil {
t.Fatal("AsXRange not failed as expected")
}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{{string: "id1", typ: '+'}, {typ: '*', values: []RedisMessage{{typ: '+', string: "a"}, {typ: '+', string: "b"}}}}},
{typ: '*', values: []RedisMessage{{string: "id2", typ: '+'}, {typ: '_'}}},
}}}).AsXRange(); !reflect.DeepEqual([]XRangeEntry{{
ID: "id1",
FieldValues: map[string]string{"a": "b"},
}, {
ID: "id2",
FieldValues: nil,
}}, ret) {
t.Fatal("AsXRange not get value as expected")
}
})
t.Run("AsXRead", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsXRead(); err == nil {
t.Fatal("AsXRead not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsXRead(); err == nil {
t.Fatal("AsXRead not failed as expected")
}
if ret, _ := (RedisResult{val: RedisMessage{typ: '%', values: []RedisMessage{
{typ: '+', string: "stream1"},
{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{{string: "id1", typ: '+'}, {typ: '*', values: []RedisMessage{{typ: '+', string: "a"}, {typ: '+', string: "b"}}}}},
{typ: '*', values: []RedisMessage{{string: "id2", typ: '+'}, {typ: '_'}}},
}},
{typ: '+', string: "stream2"},
{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{{string: "id3", typ: '+'}, {typ: '*', values: []RedisMessage{{typ: '+', string: "c"}, {typ: '+', string: "d"}}}}},
}},
}}}).AsXRead(); !reflect.DeepEqual(map[string][]XRangeEntry{
"stream1": {
{ID: "id1", FieldValues: map[string]string{"a": "b"}},
{ID: "id2", FieldValues: nil}},
"stream2": {
{ID: "id3", FieldValues: map[string]string{"c": "d"}},
},
}, ret) {
t.Fatal("AsXRead not get value as expected")
}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: '+', string: "stream1"},
{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{{string: "id1", typ: '+'}, {typ: '*', values: []RedisMessage{{typ: '+', string: "a"}, {typ: '+', string: "b"}}}}},
{typ: '*', values: []RedisMessage{{string: "id2", typ: '+'}, {typ: '_'}}},
}},
}},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "stream2"},
{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{{string: "id3", typ: '+'}, {typ: '*', values: []RedisMessage{{typ: '+', string: "c"}, {typ: '+', string: "d"}}}}},
}},
}},
}}}).AsXRead(); !reflect.DeepEqual(map[string][]XRangeEntry{
"stream1": {
{ID: "id1", FieldValues: map[string]string{"a": "b"}},
{ID: "id2", FieldValues: nil}},
"stream2": {
{ID: "id3", FieldValues: map[string]string{"c": "d"}},
},
}, ret) {
t.Fatal("AsXRead not get value as expected")
}
})
t.Run("AsZScore", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsZScore(); err == nil {
t.Fatal("AsZScore not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsZScore(); err == nil {
t.Fatal("AsZScore not failed as expected")
}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '+', string: "m1"},
{typ: '+', string: "1"},
}}}).AsZScore(); !reflect.DeepEqual(ZScore{Member: "m1", Score: 1}, ret) {
t.Fatal("AsZScore not get value as expected")
}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '+', string: "m1"},
{typ: ',', string: "1"},
}}}).AsZScore(); !reflect.DeepEqual(ZScore{Member: "m1", Score: 1}, ret) {
t.Fatal("AsZScore not get value as expected")
}
})
t.Run("AsZScores", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsZScores(); err == nil {
t.Fatal("AsZScores not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsZScores(); err == nil {
t.Fatal("AsZScores not failed as expected")
}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '+', string: "m1"},
{typ: '+', string: "1"},
{typ: '+', string: "m2"},
{typ: '+', string: "2"},
}}}).AsZScores(); !reflect.DeepEqual([]ZScore{
{Member: "m1", Score: 1},
{Member: "m2", Score: 2},
}, ret) {
t.Fatal("AsZScores not get value as expected")
}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: '+', string: "m1"},
{typ: ',', string: "1"},
}},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "m2"},
{typ: ',', string: "2"},
}},
}}}).AsZScores(); !reflect.DeepEqual([]ZScore{
{Member: "m1", Score: 1},
{Member: "m2", Score: 2},
}, ret) {
t.Fatal("AsZScores not get value as expected")
}
})
t.Run("AsLMPop", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsLMPop(); err == nil {
t.Fatal("AsLMPop not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsLMPop(); err == nil {
t.Fatal("AsLMPop not failed as expected")
}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '+', string: "k"},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "1"},
{typ: '+', string: "2"},
}},
}}}).AsLMPop(); !reflect.DeepEqual(KeyValues{
Key: "k",
Values: []string{"1", "2"},
}, ret) {
t.Fatal("AsZScores not get value as expected")
}
})
t.Run("AsZMPop", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsZMPop(); err == nil {
t.Fatal("AsZMPop not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsZMPop(); err == nil {
t.Fatal("AsZMPop not failed as expected")
}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '+', string: "k"},
{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: '+', string: "1"},
{typ: ',', string: "1"},
}},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "2"},
{typ: ',', string: "2"},
}},
}},
}}}).AsZMPop(); !reflect.DeepEqual(KeyZScores{
Key: "k",
Values: []ZScore{
{Member: "1", Score: 1},
{Member: "2", Score: 2},
},
}, ret) {
t.Fatal("AsZMPop not get value as expected")
}
})
t.Run("AsFtSearch", func(t *testing.T) {
if _, _, err := (RedisResult{err: errors.New("other")}).AsFtSearch(); err == nil {
t.Fatal("AsFtSearch not failed as expected")
}
if _, _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsFtSearch(); err == nil {
t.Fatal("AsFtSearch not failed as expected")
}
if n, ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: ':', integer: 3},
{typ: '+', string: "a"},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "k1"},
{typ: '+', string: "v1"},
{typ: '+', string: "kk"},
{typ: '+', string: "vv"},
}},
{typ: '+', string: "b"},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "k2"},
{typ: '+', string: "v2"},
{typ: '+', string: "kk"},
{typ: '+', string: "vv"},
}},
}}}).AsFtSearch(); n != 3 || !reflect.DeepEqual([]FtSearchDoc{
{Key: "a", Doc: map[string]string{"k1": "v1", "kk": "vv"}},
{Key: "b", Doc: map[string]string{"k2": "v2", "kk": "vv"}},
}, ret) {
t.Fatal("AsFtSearch not get value as expected")
}
if n, ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: ':', integer: 3},
{typ: '+', string: "a"},
{typ: '+', string: "1"},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "k1"},
{typ: '+', string: "v1"},
}},
{typ: '+', string: "b"},
{typ: '+', string: "2"},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "k2"},
{typ: '+', string: "v2"},
}},
}}}).AsFtSearch(); n != 3 || !reflect.DeepEqual([]FtSearchDoc{
{Key: "a", Doc: map[string]string{"k1": "v1"}, Score: 1},
{Key: "b", Doc: map[string]string{"k2": "v2"}, Score: 2},
}, ret) {
t.Fatal("AsFtSearch not get value as expected")
}
if n, ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: ':', integer: 3},
{typ: '+', string: "a"},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "k1"},
{typ: '+', string: "v1"},
{typ: '+', string: "kk"},
{typ: '+', string: "vv"},
}},
}}}).AsFtSearch(); n != 3 || !reflect.DeepEqual([]FtSearchDoc{
{Key: "a", Doc: map[string]string{"k1": "v1", "kk": "vv"}},
}, ret) {
t.Fatal("AsFtSearch not get value as expected")
}
if n, ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: ':', integer: 3},
{typ: '+', string: "a"},
{typ: '+', string: "b"},
}}}).AsFtSearch(); n != 3 || !reflect.DeepEqual([]FtSearchDoc{
{Key: "a", Doc: nil},
{Key: "b", Doc: nil},
}, ret) {
t.Fatal("AsFtSearch not get value as expected")
}
if n, ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: ':', integer: 3},
{typ: '+', string: "a"},
{typ: '+', string: "1"},
{typ: '+', string: "b"},
{typ: '+', string: "2"},
}}}).AsFtSearch(); n != 3 || !reflect.DeepEqual([]FtSearchDoc{
{Key: "a", Doc: nil, Score: 1},
{Key: "b", Doc: nil, Score: 2},
}, ret) {
t.Fatal("AsFtSearch not get value as expected")
}
if n, ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: ':', integer: 3},
{typ: '+', string: "1"},
{typ: '+', string: "2"},
}}}).AsFtSearch(); n != 3 || !reflect.DeepEqual([]FtSearchDoc{
{Key: "1", Doc: nil},
{Key: "2", Doc: nil},
}, ret) {
t.Fatal("AsFtSearch not get value as expected")
}
if n, ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: ':', integer: 3},
{typ: '+', string: "a"},
}}}).AsFtSearch(); n != 3 || !reflect.DeepEqual([]FtSearchDoc{
{Key: "a", Doc: nil},
}, ret) {
t.Fatal("AsFtSearch not get value as expected")
}
if n, ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: ':', integer: 3},
}}}).AsFtSearch(); n != 3 || !reflect.DeepEqual([]FtSearchDoc{}, ret) {
t.Fatal("AsFtSearch not get value as expected")
}
})
t.Run("AsFtSearch RESP3", func(t *testing.T) {
if n, ret, _ := (RedisResult{val: RedisMessage{typ: '%', values: []RedisMessage{
{typ: '+', string: "total_results"},
{typ: ':', integer: 3},
{typ: '+', string: "results"},
{typ: '*', values: []RedisMessage{
{typ: '%', values: []RedisMessage{
{typ: '+', string: "id"},
{typ: '+', string: "1"},
{typ: '+', string: "extra_attributes"},
{typ: '%', values: []RedisMessage{
{typ: '+', string: "$"},
{typ: '+', string: "1"},
}},
{typ: '+', string: "score"},
{typ: ',', string: "1"},
}},
{typ: '%', values: []RedisMessage{
{typ: '+', string: "id"},
{typ: '+', string: "2"},
{typ: '+', string: "extra_attributes"},
{typ: '%', values: []RedisMessage{
{typ: '+', string: "$"},
{typ: '+', string: "2"},
}},
{typ: '+', string: "score"},
{typ: ',', string: "2"},
}},
}},
{typ: '+', string: "error"},
{typ: '*', values: []RedisMessage{}},
}}}).AsFtSearch(); n != 3 || !reflect.DeepEqual([]FtSearchDoc{
{Key: "1", Doc: map[string]string{"$": "1"}, Score: 1},
{Key: "2", Doc: map[string]string{"$": "2"}, Score: 2},
}, ret) {
t.Fatal("AsFtSearch not get value as expected")
}
if _, _, err := (RedisResult{val: RedisMessage{typ: '%', values: []RedisMessage{
{typ: '+', string: "total_results"},
{typ: ':', integer: 3},
{typ: '+', string: "results"},
{typ: '*', values: []RedisMessage{
{typ: '%', values: []RedisMessage{
{typ: '+', string: "id"},
{typ: '+', string: "1"},
{typ: '+', string: "extra_attributes"},
{typ: '%', values: []RedisMessage{
{typ: '+', string: "$"},
{typ: '+', string: "1"},
}},
{typ: '+', string: "score"},
{typ: ',', string: "1"},
}},
}},
{typ: '+', string: "error"},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "mytimeout"},
}},
}}}).AsFtSearch(); err == nil || err.Error() != "mytimeout" {
t.Fatal("AsFtSearch not get value as expected")
}
})
t.Run("AsFtAggregate", func(t *testing.T) {
if _, _, err := (RedisResult{err: errors.New("other")}).AsFtAggregate(); err == nil {
t.Fatal("AsFtAggregate not failed as expected")
}
if _, _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsFtAggregate(); err == nil {
t.Fatal("AsFtAggregate not failed as expected")
}
if n, ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: ':', integer: 3},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "k1"},
{typ: '+', string: "v1"},
{typ: '+', string: "kk"},
{typ: '+', string: "vv"},
}},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "k2"},
{typ: '+', string: "v2"},
{typ: '+', string: "kk"},
{typ: '+', string: "vv"},
}},
}}}).AsFtAggregate(); n != 3 || !reflect.DeepEqual([]map[string]string{
{"k1": "v1", "kk": "vv"},
{"k2": "v2", "kk": "vv"},
}, ret) {
t.Fatal("AsFtAggregate not get value as expected")
}
if n, ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: ':', integer: 3},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "k1"},
{typ: '+', string: "v1"},
{typ: '+', string: "kk"},
{typ: '+', string: "vv"},
}},
}}}).AsFtAggregate(); n != 3 || !reflect.DeepEqual([]map[string]string{
{"k1": "v1", "kk": "vv"},
}, ret) {
t.Fatal("AsFtAggregate not get value as expected")
}
if n, ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: ':', integer: 3},
}}}).AsFtAggregate(); n != 3 || !reflect.DeepEqual([]map[string]string{}, ret) {
t.Fatal("AsFtAggregate not get value as expected")
}
})
t.Run("AsFtAggregate RESP3", func(t *testing.T) {
if n, ret, _ := (RedisResult{val: RedisMessage{typ: '%', values: []RedisMessage{
{typ: '+', string: "total_results"},
{typ: ':', integer: 3},
{typ: '+', string: "results"},
{typ: '*', values: []RedisMessage{
{typ: '%', values: []RedisMessage{
{typ: '+', string: "extra_attributes"},
{typ: '%', values: []RedisMessage{
{typ: '+', string: "$"},
{typ: '+', string: "1"},
}},
}},
{typ: '%', values: []RedisMessage{
{typ: '+', string: "extra_attributes"},
{typ: '%', values: []RedisMessage{
{typ: '+', string: "$"},
{typ: '+', string: "2"},
}},
}},
}},
{typ: '+', string: "error"},
{typ: '*', values: []RedisMessage{}},
}}}).AsFtAggregate(); n != 3 || !reflect.DeepEqual([]map[string]string{
{"$": "1"},
{"$": "2"},
}, ret) {
t.Fatal("AsFtAggregate not get value as expected")
}
if _, _, err := (RedisResult{val: RedisMessage{typ: '%', values: []RedisMessage{
{typ: '+', string: "total_results"},
{typ: ':', integer: 3},
{typ: '+', string: "results"},
{typ: '*', values: []RedisMessage{
{typ: '%', values: []RedisMessage{
{typ: '+', string: "extra_attributes"},
{typ: '%', values: []RedisMessage{
{typ: '+', string: "$"},
{typ: '+', string: "1"},
}},
}},
}},
{typ: '+', string: "error"},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "mytimeout"},
}},
}}}).AsFtAggregate(); err == nil || err.Error() != "mytimeout" {
t.Fatal("AsFtAggregate not get value as expected")
}
})
t.Run("AsFtAggregate Cursor", func(t *testing.T) {
if _, _, _, err := (RedisResult{err: errors.New("other")}).AsFtAggregateCursor(); err == nil {
t.Fatal("AsFtAggregate not failed as expected")
}
if _, _, _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsFtAggregateCursor(); err == nil {
t.Fatal("AsFtAggregate not failed as expected")
}
if c, n, ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: ':', integer: 3},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "k1"},
{typ: '+', string: "v1"},
{typ: '+', string: "kk"},
{typ: '+', string: "vv"},
}},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "k2"},
{typ: '+', string: "v2"},
{typ: '+', string: "kk"},
{typ: '+', string: "vv"},
}},
}},
{typ: ':', integer: 1},
}}}).AsFtAggregateCursor(); c != 1 || n != 3 || !reflect.DeepEqual([]map[string]string{
{"k1": "v1", "kk": "vv"},
{"k2": "v2", "kk": "vv"},
}, ret) {
t.Fatal("AsFtAggregate not get value as expected")
}
if c, n, ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: ':', integer: 3},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "k1"},
{typ: '+', string: "v1"},
{typ: '+', string: "kk"},
{typ: '+', string: "vv"},
}},
}},
{typ: ':', integer: 1},
}}}).AsFtAggregateCursor(); c != 1 || n != 3 || !reflect.DeepEqual([]map[string]string{
{"k1": "v1", "kk": "vv"},
}, ret) {
t.Fatal("AsFtAggregate not get value as expected")
}
if c, n, ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: ':', integer: 3},
}},
{typ: ':', integer: 1},
}}}).AsFtAggregateCursor(); c != 1 || n != 3 || !reflect.DeepEqual([]map[string]string{}, ret) {
t.Fatal("AsFtAggregate not get value as expected")
}
})
t.Run("AsFtAggregate Cursor RESP3", func(t *testing.T) {
if c, n, ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '%', values: []RedisMessage{
{typ: '+', string: "total_results"},
{typ: ':', integer: 3},
{typ: '+', string: "results"},
{typ: '*', values: []RedisMessage{
{typ: '%', values: []RedisMessage{
{typ: '+', string: "extra_attributes"},
{typ: '%', values: []RedisMessage{
{typ: '+', string: "$"},
{typ: '+', string: "1"},
}},
}},
{typ: '%', values: []RedisMessage{
{typ: '+', string: "extra_attributes"},
{typ: '%', values: []RedisMessage{
{typ: '+', string: "$"},
{typ: '+', string: "2"},
}},
}},
}},
{typ: '+', string: "error"},
{typ: '*', values: []RedisMessage{}},
}},
{typ: ':', integer: 1},
}}}).AsFtAggregateCursor(); c != 1 || n != 3 || !reflect.DeepEqual([]map[string]string{
{"$": "1"},
{"$": "2"},
}, ret) {
t.Fatal("AsFtAggregate not get value as expected")
}
if _, _, _, err := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '%', values: []RedisMessage{
{typ: '+', string: "total_results"},
{typ: ':', integer: 3},
{typ: '+', string: "results"},
{typ: '*', values: []RedisMessage{
{typ: '%', values: []RedisMessage{
{typ: '+', string: "extra_attributes"},
{typ: '%', values: []RedisMessage{
{typ: '+', string: "$"},
{typ: '+', string: "1"},
}},
}},
}},
{typ: '+', string: "error"},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "mytimeout"},
}},
}},
{typ: ':', integer: 1},
}}}).AsFtAggregateCursor(); err == nil || err.Error() != "mytimeout" {
t.Fatal("AsFtAggregate not get value as expected")
}
})
t.Run("AsGeosearch", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsGeosearch(); err == nil {
t.Fatal("AsGeosearch not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsGeosearch(); err == nil {
t.Fatal("AsGeosearch not failed as expected")
}
//WithDist, WithHash, WithCoord
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: '$', string: "k1"},
{typ: ',', string: "2.5"},
{typ: ':', integer: 1},
{typ: '*', values: []RedisMessage{
{typ: ',', string: "28.0473"},
{typ: ',', string: "26.2041"},
}},
}},
{typ: '*', values: []RedisMessage{
{typ: '$', string: "k2"},
{typ: ',', string: "4.5"},
{typ: ':', integer: 4},
{typ: '*', values: []RedisMessage{
{typ: ',', string: "72.4973"},
{typ: ',', string: "13.2263"},
}},
}},
}}}).AsGeosearch(); !reflect.DeepEqual([]GeoLocation{
{Name: "k1", Dist: 2.5, GeoHash: 1, Longitude: 28.0473, Latitude: 26.2041},
{Name: "k2", Dist: 4.5, GeoHash: 4, Longitude: 72.4973, Latitude: 13.2263},
}, ret) {
t.Fatal("AsGeosearch not get value as expected")
}
//WithHash, WithCoord
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: '$', string: "k1"},
{typ: ':', integer: 1},
{typ: '*', values: []RedisMessage{
{typ: ',', string: "84.3877"},
{typ: ',', string: "33.7488"},
}},
}},
{typ: '*', values: []RedisMessage{
{typ: '$', string: "k2"},
{typ: ':', integer: 4},
{typ: '*', values: []RedisMessage{
{typ: ',', string: "115.8613"},
{typ: ',', string: "31.9523"},
}},
}},
}}}).AsGeosearch(); !reflect.DeepEqual([]GeoLocation{
{Name: "k1", GeoHash: 1, Longitude: 84.3877, Latitude: 33.7488},
{Name: "k2", GeoHash: 4, Longitude: 115.8613, Latitude: 31.9523},
}, ret) {
t.Fatal("AsGeosearch not get value as expected")
}
//WithDist, WithCoord
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: '$', string: "k1"},
{typ: ',', string: "2.50076"},
{typ: '*', values: []RedisMessage{
{typ: ',', string: "84.3877"},
{typ: ',', string: "33.7488"},
}},
}},
{typ: '*', values: []RedisMessage{
{typ: '$', string: "k2"},
{typ: ',', string: "1024.96"},
{typ: '*', values: []RedisMessage{
{typ: ',', string: "115.8613"},
{typ: ',', string: "31.9523"},
}},
}},
}}}).AsGeosearch(); !reflect.DeepEqual([]GeoLocation{
{Name: "k1", Dist: 2.50076, Longitude: 84.3877, Latitude: 33.7488},
{Name: "k2", Dist: 1024.96, Longitude: 115.8613, Latitude: 31.9523},
}, ret) {
t.Fatal("AsGeosearch not get value as expected")
}
//WithCoord
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: '$', string: "k1"},
{typ: '*', values: []RedisMessage{
{typ: ',', string: "122.4194"},
{typ: ',', string: "37.7749"},
}},
}},
{typ: '*', values: []RedisMessage{
{typ: '$', string: "k2"},
{typ: '*', values: []RedisMessage{
{typ: ',', string: "35.6762"},
{typ: ',', string: "139.6503"},
}},
}},
}}}).AsGeosearch(); !reflect.DeepEqual([]GeoLocation{
{Name: "k1", Longitude: 122.4194, Latitude: 37.7749},
{Name: "k2", Longitude: 35.6762, Latitude: 139.6503},
}, ret) {
t.Fatal("AsGeosearch not get value as expected")
}
//WithDist
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: '$', string: "k1"},
{typ: ',', string: "2.50076"},
}},
{typ: '*', values: []RedisMessage{
{typ: '$', string: "k2"},
{typ: ',', string: strconv.FormatFloat(math.MaxFloat64, 'E', -1, 64)},
}},
}}}).AsGeosearch(); !reflect.DeepEqual([]GeoLocation{
{Name: "k1", Dist: 2.50076},
{Name: "k2", Dist: math.MaxFloat64},
}, ret) {
t.Fatal("AsGeosearch not get value as expected")
}
//WithHash
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: '$', string: "k1"},
{typ: ':', integer: math.MaxInt64},
}},
{typ: '*', values: []RedisMessage{
{typ: '$', string: "k2"},
{typ: ':', integer: 22296},
}},
}}}).AsGeosearch(); !reflect.DeepEqual([]GeoLocation{
{Name: "k1", GeoHash: math.MaxInt64},
{Name: "k2", GeoHash: 22296},
}, ret) {
t.Fatal("AsGeosearch not get value as expected")
}
//With no additional options
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '$', string: "k1"},
{typ: '$', string: "k2"},
}}}).AsGeosearch(); !reflect.DeepEqual([]GeoLocation{
{Name: "k1"},
{Name: "k2"},
}, ret) {
t.Fatal("AsGeosearch not get value as expected")
}
//With wrong distance
if _, err := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: '$', string: "k1"},
{typ: ',', string: "wrong distance"},
}},
}}}).AsGeosearch(); err == nil {
t.Fatal("AsGeosearch not failed as expected")
}
//With wrong coordinates
if _, err := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: '$', string: "k2"},
{typ: '*', values: []RedisMessage{
{typ: ',', string: "35.6762"},
}},
}},
}}}).AsGeosearch(); err == nil {
t.Fatal("AsGeosearch not failed as expected")
}
})
t.Run("IsCacheHit", func(t *testing.T) {
if (RedisResult{err: errors.New("other")}).IsCacheHit() {
t.Fatal("IsCacheHit not as expected")
}
if !(RedisResult{val: RedisMessage{attrs: cacheMark}}).IsCacheHit() {
t.Fatal("IsCacheHit not as expected")
}
})
t.Run("CacheTTL", func(t *testing.T) {
if (RedisResult{err: errors.New("other")}).CacheTTL() != -1 {
t.Fatal("CacheTTL != -1")
}
m := RedisMessage{}
m.setExpireAt(time.Now().Add(time.Millisecond * 100).UnixMilli())
if (RedisResult{val: m}).CacheTTL() <= 0 {
t.Fatal("CacheTTL <= 0")
}
time.Sleep(150 * time.Millisecond)
if (RedisResult{val: m}).CacheTTL() != 0 {
t.Fatal("CacheTTL != 0")
}
})
t.Run("CachePTTL", func(t *testing.T) {
if (RedisResult{err: errors.New("other")}).CachePTTL() != -1 {
t.Fatal("CachePTTL != -1")
}
m := RedisMessage{}
m.setExpireAt(time.Now().Add(time.Millisecond * 100).UnixMilli())
if (RedisResult{val: m}).CachePTTL() <= 0 {
t.Fatal("CachePTTL <= 0")
}
time.Sleep(150 * time.Millisecond)
if (RedisResult{val: m}).CachePTTL() != 0 {
t.Fatal("CachePTTL != 0")
}
})
t.Run("CachePXAT", func(t *testing.T) {
if (RedisResult{err: errors.New("other")}).CachePXAT() != -1 {
t.Fatal("CachePTTL != -1")
}
m := RedisMessage{}
m.setExpireAt(time.Now().Add(time.Millisecond * 100).UnixMilli())
if (RedisResult{val: m}).CachePXAT() <= 0 {
t.Fatal("CachePXAT <= 0")
}
})
t.Run("Stringer", func(t *testing.T) {
tests := []struct {
input RedisResult
expected string
}{
{
input: RedisResult{
val: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: ':', integer: 0},
{typ: ':', integer: 0},
{typ: '*', values: []RedisMessage{ // master
{typ: '+', string: "127.0.3.1"},
{typ: ':', integer: 3},
{typ: '+', string: ""},
}},
}},
}},
},
expected: `{"Message":{"Value":[{"Value":[{"Value":0,"Type":"int64"},{"Value":0,"Type":"int64"},{"Value":[{"Value":"127.0.3.1","Type":"simple string"},{"Value":3,"Type":"int64"},{"Value":"","Type":"simple string"}],"Type":"array"}],"Type":"array"}],"Type":"array"}}`,
},
{
input: RedisResult{err: errors.New("foo")},
expected: `{"Error":"foo"}`,
},
}
for _, test := range tests {
msg := test.input.String()
if msg != test.expected {
t.Fatalf("unexpected string. got %v expected %v", msg, test.expected)
}
}
})
}
//gocyclo:ignore
func TestRedisMessage(t *testing.T) {
//Add erroneous type
typeNames['t'] = "t"
t.Run("IsNil", func(t *testing.T) {
if !(&RedisMessage{typ: '_'}).IsNil() {
t.Fatal("IsNil fail")
}
})
t.Run("Trim ERR prefix", func(t *testing.T) {
// kvrocks: https://github.com/redis/rueidis/issues/152#issuecomment-1333923750
if (&RedisMessage{typ: '-', string: "ERR no_prefix"}).Error().Error() != "no_prefix" {
t.Fatal("fail to trim ERR")
}
})
t.Run("ToInt64", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).ToInt64(); err == nil {
t.Fatal("ToInt64 not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a RESP3 int64") {
t.Fatal("ToInt64 not panic as expected")
}
}()
(&RedisMessage{typ: 't'}).ToInt64()
})
t.Run("ToBool", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).ToBool(); err == nil {
t.Fatal("ToBool not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a RESP3 bool") {
t.Fatal("ToBool not panic as expected")
}
}()
(&RedisMessage{typ: 't'}).ToBool()
})
t.Run("AsBool", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsBool(); err == nil {
t.Fatal("AsBool not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a int, string or bool") {
t.Fatal("AsBool not panic as expected")
}
}()
(&RedisMessage{typ: 't'}).AsBool()
})
t.Run("ToFloat64", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).ToFloat64(); err == nil {
t.Fatal("ToFloat64 not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a RESP3 float64") {
t.Fatal("ToFloat64 not panic as expected")
}
}()
(&RedisMessage{typ: 't'}).ToFloat64()
})
t.Run("ToString", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).ToString(); err == nil {
t.Fatal("ToString not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), fmt.Sprintf("redis message type %s is not a string", typeNames[':'])) {
t.Fatal("ToString not panic as expected")
}
}()
(&RedisMessage{typ: ':'}).ToString()
})
t.Run("AsReader", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsReader(); err == nil {
t.Fatal("AsReader not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), fmt.Sprintf("redis message type %s is not a string", typeNames[':'])) {
t.Fatal("AsReader not panic as expected")
}
}()
(&RedisMessage{typ: ':'}).AsReader()
})
t.Run("AsBytes", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsBytes(); err == nil {
t.Fatal("AsBytes not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), fmt.Sprintf("redis message type %s is not a string", typeNames[':'])) {
t.Fatal("AsBytes not panic as expected")
}
}()
(&RedisMessage{typ: ':'}).AsBytes()
})
t.Run("DecodeJSON", func(t *testing.T) {
if err := (&RedisMessage{typ: '_'}).DecodeJSON(nil); err == nil {
t.Fatal("DecodeJSON not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), fmt.Sprintf("redis message type %s is not a string", typeNames[':'])) {
t.Fatal("DecodeJSON not panic as expected")
}
}()
(&RedisMessage{typ: ':'}).DecodeJSON(nil)
})
t.Run("AsInt64", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsInt64(); err == nil {
t.Fatal("AsInt64 not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), fmt.Sprintf("redis message type %s is not a string", typeNames['*'])) {
t.Fatal("AsInt64 not panic as expected")
}
}()
(&RedisMessage{typ: '*', values: []RedisMessage{{}}}).AsInt64()
})
t.Run("AsUint64", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsUint64(); err == nil {
t.Fatal("AsUint64 not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), fmt.Sprintf("redis message type %s is not a string", typeNames['*'])) {
t.Fatal("AsUint64 not panic as expected")
}
}()
(&RedisMessage{typ: '*', values: []RedisMessage{{}}}).AsUint64()
})
t.Run("AsFloat64", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsFloat64(); err == nil {
t.Fatal("AsFloat64 not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), fmt.Sprintf("redis message type %s is not a string", typeNames[':'])) {
t.Fatal("AsFloat64 not panic as expected")
}
}()
(&RedisMessage{typ: ':'}).AsFloat64()
})
t.Run("ToArray", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).ToArray(); err == nil {
t.Fatal("ToArray not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a array") {
t.Fatal("ToArray not panic as expected")
}
}()
(&RedisMessage{typ: 't'}).ToArray()
})
t.Run("AsStrSlice", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsStrSlice(); err == nil {
t.Fatal("AsStrSlice not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a array") {
t.Fatal("AsStrSlice not panic as expected")
}
}()
(&RedisMessage{typ: 't'}).AsStrSlice()
})
t.Run("AsIntSlice", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsIntSlice(); err == nil {
t.Fatal("AsIntSlice not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a array") {
t.Fatal("AsIntSlice not panic as expected")
}
}()
(&RedisMessage{typ: 't'}).AsIntSlice()
})
t.Run("AsFloatSlice", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsFloatSlice(); err == nil {
t.Fatal("AsFloatSlice not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a array") {
t.Fatal("AsFloatSlice not panic as expected")
}
}()
(&RedisMessage{typ: 't'}).AsFloatSlice()
})
t.Run("AsBoolSlice", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsBoolSlice(); err == nil {
t.Fatal("AsBoolSlice not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a array") {
t.Fatal("AsBoolSlice not panic as expected")
}
}()
(&RedisMessage{typ: 't'}).AsBoolSlice()
})
t.Run("AsMap", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsMap(); err == nil {
t.Fatal("AsMap not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a map/array/set or its length is not even") {
t.Fatal("AsMap not panic as expected")
}
}()
(&RedisMessage{typ: 't'}).AsMap()
})
t.Run("AsStrMap", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsStrMap(); err == nil {
t.Fatal("AsStrMap not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a map/array/set") {
t.Fatal("AsMap not panic as expected")
}
}()
(&RedisMessage{typ: 't'}).AsStrMap()
})
t.Run("AsIntMap", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsIntMap(); err == nil {
t.Fatal("AsIntMap not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a map/array/set") {
t.Fatal("AsMap not panic as expected")
}
}()
(&RedisMessage{typ: 't'}).AsIntMap()
})
t.Run("ToMap", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).ToMap(); err == nil {
t.Fatal("ToMap not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a RESP3 map") {
t.Fatal("ToMap not panic as expected")
}
}()
(&RedisMessage{typ: 't'}).ToMap()
})
t.Run("ToAny", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).ToAny(); err == nil {
t.Fatal("ToAny not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a supported in ToAny") {
t.Fatal("ToAny not panic as expected")
}
}()
(&RedisMessage{typ: 't'}).ToAny()
})
t.Run("AsXRangeEntry - no range id", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsXRangeEntry(); err == nil {
t.Fatal("AsXRangeEntry not failed as expected")
}
if _, err := (&RedisMessage{typ: '*'}).AsXRangeEntry(); err == nil {
t.Fatal("AsXRangeEntry not failed as expected")
}
if _, err := (&RedisMessage{typ: '*', values: []RedisMessage{{typ: '_'}, {typ: '%'}}}).AsXRangeEntry(); err == nil {
t.Fatal("AsXRangeEntry not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), fmt.Sprintf("redis message type %s is not a string", typeNames[':'])) {
t.Fatal("AsXRangeEntry not panic as expected")
}
}()
(&RedisMessage{typ: '*', values: []RedisMessage{{typ: ':'}, {typ: '%'}}}).AsXRangeEntry()
})
t.Run("AsXRangeEntry - no range field values", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsXRangeEntry(); err == nil {
t.Fatal("AsXRangeEntry not failed as expected")
}
if _, err := (&RedisMessage{typ: '*'}).AsXRangeEntry(); err == nil {
t.Fatal("AsXRangeEntry not failed as expected")
}
if _, err := (&RedisMessage{typ: '*', values: []RedisMessage{{typ: '+'}, {typ: '-'}}}).AsXRangeEntry(); err == nil {
t.Fatal("AsXRangeEntry not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a map/array/set") {
t.Fatal("AsXRangeEntry not panic as expected")
}
}()
(&RedisMessage{typ: '*', values: []RedisMessage{{typ: '+'}, {typ: 't'}}}).AsXRangeEntry()
})
t.Run("AsXRange", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsXRange(); err == nil {
t.Fatal("AsXRange not failed as expected")
}
if _, err := (&RedisMessage{typ: '*', values: []RedisMessage{{typ: '_'}}}).AsXRange(); err == nil {
t.Fatal("AsXRange not failed as expected")
}
})
t.Run("AsXRead", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsXRead(); err == nil {
t.Fatal("AsXRead not failed as expected")
}
if _, err := (&RedisMessage{typ: '%', values: []RedisMessage{
{typ: '+', string: "stream1"},
{typ: '*', values: []RedisMessage{{typ: '*', values: []RedisMessage{{string: "id1", typ: '+'}}}}},
}}).AsXRead(); err == nil {
t.Fatal("AsXRead not failed as expected")
}
if _, err := (&RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: '+', string: "stream1"},
}},
}}).AsXRead(); err == nil {
t.Fatal("AsXRead not failed as expected")
}
if _, err := (&RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: '+', string: "stream1"},
{typ: '*', values: []RedisMessage{{typ: '*', values: []RedisMessage{{string: "id1", typ: '+'}}}}},
}},
}}).AsXRead(); err == nil {
t.Fatal("AsXRead not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message type t is not a map/array/set") {
t.Fatal("AsXRangeEntry not panic as expected")
}
}()
(&RedisMessage{typ: 't'}).AsXRead()
})
t.Run("AsZScore", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsZScore(); err == nil {
t.Fatal("AsZScore not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), "redis message is not a map/array/set or its length is not 2") {
t.Fatal("AsZScore not panic as expected")
}
}()
(&RedisMessage{typ: '*'}).AsZScore()
})
t.Run("AsZScores", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsZScores(); err == nil {
t.Fatal("AsZScore not failed as expected")
}
if _, err := (&RedisMessage{typ: '*', values: []RedisMessage{
{typ: '+', string: "m1"},
{typ: '+', string: "m2"},
}}).AsZScores(); err == nil {
t.Fatal("AsZScores not fails as expected")
}
if _, err := (&RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: '+', string: "m1"},
{typ: '+', string: "m2"},
}},
}}).AsZScores(); err == nil {
t.Fatal("AsZScores not fails as expected")
}
})
t.Run("AsLMPop", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsLMPop(); err == nil {
t.Fatal("AsLMPop not failed as expected")
}
if _, err := (&RedisMessage{typ: '*', values: []RedisMessage{
{typ: '+', string: "k"},
{typ: '_'},
}}).AsLMPop(); err == nil {
t.Fatal("AsLMPop not fails as expected")
}
defer func() {
if !strings.Contains(recover().(string), fmt.Sprintf("redis message type %s is not a LMPOP response", typeNames['*'])) {
t.Fatal("AsLMPop not panic as expected")
}
}()
(&RedisMessage{typ: '*', values: []RedisMessage{
{typ: '+', string: "k"},
}}).AsLMPop()
})
t.Run("AsZMPop", func(t *testing.T) {
if _, err := (&RedisMessage{typ: '_'}).AsZMPop(); err == nil {
t.Fatal("AsZMPop not failed as expected")
}
if _, err := (&RedisMessage{typ: '*', values: []RedisMessage{
{typ: '+', string: "k"},
{typ: '_'},
}}).AsZMPop(); err == nil {
t.Fatal("AsZMPop not fails as expected")
}
defer func() {
if !strings.Contains(recover().(string), fmt.Sprintf("redis message type %s is not a ZMPOP response", typeNames['*'])) {
t.Fatal("AsZMPop not panic as expected")
}
}()
(&RedisMessage{typ: '*', values: []RedisMessage{
{typ: '+', string: "k"},
}}).AsZMPop()
})
t.Run("AsFtSearch", func(t *testing.T) {
if _, _, err := (&RedisMessage{typ: '_'}).AsFtSearch(); err == nil {
t.Fatal("AsFtSearch not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), fmt.Sprintf("redis message type %s is not a FT.SEARCH response", typeNames['*'])) {
t.Fatal("AsFtSearch not panic as expected")
}
}()
(&RedisMessage{typ: '*', values: []RedisMessage{}}).AsFtSearch()
})
t.Run("AsFtAggregate", func(t *testing.T) {
if _, _, err := (&RedisMessage{typ: '_'}).AsFtAggregate(); err == nil {
t.Fatal("AsFtAggregate not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), fmt.Sprintf("redis message type %s is not a FT.AGGREGATE response", typeNames['*'])) {
t.Fatal("AsFtAggregate not panic as expected")
}
}()
(&RedisMessage{typ: '*', values: []RedisMessage{}}).AsFtAggregate()
})
t.Run("AsFtAggregateCursor", func(t *testing.T) {
if _, _, _, err := (&RedisMessage{typ: '_'}).AsFtAggregateCursor(); err == nil {
t.Fatal("AsFtAggregate not failed as expected")
}
defer func() {
if !strings.Contains(recover().(string), fmt.Sprintf("redis message type %s is not a FT.AGGREGATE response", typeNames['*'])) {
t.Fatal("AsFtAggregate not panic as expected")
}
}()
(&RedisMessage{typ: '*', values: []RedisMessage{}}).AsFtAggregateCursor()
})
t.Run("AsScanEntry", func(t *testing.T) {
if _, err := (RedisResult{err: errors.New("other")}).AsScanEntry(); err == nil {
t.Fatal("AsScanEntry not failed as expected")
}
if _, err := (RedisResult{val: RedisMessage{typ: '-'}}).AsScanEntry(); err == nil {
t.Fatal("AsScanEntry not failed as expected")
}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{{string: "1", typ: '+'}, {typ: '*', values: []RedisMessage{{typ: '+', string: "a"}, {typ: '+', string: "b"}}}}}}).AsScanEntry(); !reflect.DeepEqual(ScanEntry{
Cursor: 1,
Elements: []string{"a", "b"},
}, ret) {
t.Fatal("AsScanEntry not get value as expected")
}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: []RedisMessage{{string: "0", typ: '+'}, {typ: '_'}}}}).AsScanEntry(); !reflect.DeepEqual(ScanEntry{}, ret) {
t.Fatal("AsScanEntry not get value as expected")
}
defer func() {
if !strings.Contains(recover().(string), fmt.Sprintf("redis message type %s is not a scan response or its length is not at least 2", typeNames['*'])) {
t.Fatal("AsScanEntry not panic as expected")
}
}()
(&RedisMessage{typ: '*', values: []RedisMessage{{typ: ':'}}}).AsScanEntry()
})
t.Run("ToMap with non string key", func(t *testing.T) {
defer func() {
if !strings.Contains(recover().(string), fmt.Sprintf("redis message type %s as map key is not supported", typeNames[':'])) {
t.Fatal("ToMap not panic as expected")
}
}()
(&RedisMessage{typ: '%', values: []RedisMessage{{typ: ':'}, {typ: ':'}}}).ToMap()
})
t.Run("IsCacheHit", func(t *testing.T) {
if (&RedisMessage{typ: '_'}).IsCacheHit() {
t.Fatal("IsCacheHit not as expected")
}
if !(&RedisMessage{typ: '_', attrs: cacheMark}).IsCacheHit() {
t.Fatal("IsCacheHit not as expected")
}
})
t.Run("CacheTTL", func(t *testing.T) {
if (&RedisMessage{typ: '_'}).CacheTTL() != -1 {
t.Fatal("CacheTTL != -1")
}
m := &RedisMessage{typ: '_'}
m.setExpireAt(time.Now().Add(time.Millisecond * 100).UnixMilli())
if m.CacheTTL() <= 0 {
t.Fatal("CacheTTL <= 0")
}
time.Sleep(100 * time.Millisecond)
if m.CachePTTL() > 0 {
t.Fatal("CachePTTL > 0")
}
})
t.Run("CachePTTL", func(t *testing.T) {
if (&RedisMessage{typ: '_'}).CachePTTL() != -1 {
t.Fatal("CachePTTL != -1")
}
m := &RedisMessage{typ: '_'}
m.setExpireAt(time.Now().Add(time.Millisecond * 100).UnixMilli())
if m.CachePTTL() <= 0 {
t.Fatal("CachePTTL <= 0")
}
time.Sleep(100 * time.Millisecond)
if m.CachePTTL() > 0 {
t.Fatal("CachePTTL > 0")
}
})
t.Run("CachePXAT", func(t *testing.T) {
if (&RedisMessage{typ: '_'}).CachePXAT() != -1 {
t.Fatal("CachePXAT != -1")
}
m := &RedisMessage{typ: '_'}
m.setExpireAt(time.Now().Add(time.Millisecond * 100).UnixMilli())
if m.CachePXAT() <= 0 {
t.Fatal("CachePXAT <= 0")
}
})
t.Run("Stringer", func(t *testing.T) {
tests := []struct {
input RedisMessage
expected string
}{
{
input: RedisMessage{typ: '*', values: []RedisMessage{
{typ: '*', values: []RedisMessage{
{typ: ':', integer: 0},
{typ: ':', integer: 0},
{typ: '*', values: []RedisMessage{
{typ: '+', string: "127.0.3.1"},
{typ: ':', integer: 3},
{typ: '+', string: ""},
}},
}},
}},
expected: `{"Value":[{"Value":[{"Value":0,"Type":"int64"},{"Value":0,"Type":"int64"},{"Value":[{"Value":"127.0.3.1","Type":"simple string"},{"Value":3,"Type":"int64"},{"Value":"","Type":"simple string"}],"Type":"array"}],"Type":"array"}],"Type":"array"}`,
},
{
input: RedisMessage{typ: '+', string: "127.0.3.1", ttl: [7]byte{97, 77, 74, 61, 138, 1, 0}},
expected: `{"Value":"127.0.3.1","Type":"simple string","TTL":"2023-08-28 17:56:34.273 +0000 UTC"}`,
},
{
input: RedisMessage{typ: '0'},
expected: `{"Type":"unknown"}`,
},
{
input: RedisMessage{typ: typeBool, integer: 1},
expected: `{"Value":true,"Type":"boolean"}`,
},
{
input: RedisMessage{typ: typeNull},
expected: `{"Type":"null","Error":"redis nil message"}`,
},
{
input: RedisMessage{typ: typeSimpleErr, string: "ERR foo"},
expected: `{"Type":"simple error","Error":"foo"}`,
},
{
input: RedisMessage{typ: typeBlobErr, string: "ERR foo"},
expected: `{"Type":"blob error","Error":"foo"}`,
},
}
for _, test := range tests {
msg := test.input.String()
if msg != test.expected {
t.Fatalf("unexpected string. got %v expected %v", msg, test.expected)
}
}
})
}
Go
1
https://gitee.com/mirrors/rueidis.git
git@gitee.com:mirrors/rueidis.git
mirrors
rueidis
rueidis
main

搜索帮助