commands_test.go 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938
  1. package redis_test
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "reflect"
  6. "time"
  7. . "github.com/onsi/ginkgo"
  8. . "github.com/onsi/gomega"
  9. "gopkg.in/redis.v5"
  10. )
  11. var _ = Describe("Commands", func() {
  12. var client *redis.Client
  13. BeforeEach(func() {
  14. client = redis.NewClient(redisOptions())
  15. Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
  16. })
  17. AfterEach(func() {
  18. Expect(client.Close()).NotTo(HaveOccurred())
  19. })
  20. Describe("server", func() {
  21. It("should Auth", func() {
  22. _, err := client.Pipelined(func(pipe *redis.Pipeline) error {
  23. pipe.Auth("password")
  24. return nil
  25. })
  26. Expect(err).To(MatchError("ERR Client sent AUTH, but no password is set"))
  27. })
  28. It("should Echo", func() {
  29. pipe := client.Pipeline()
  30. echo := pipe.Echo("hello")
  31. _, err := pipe.Exec()
  32. Expect(err).NotTo(HaveOccurred())
  33. Expect(echo.Err()).NotTo(HaveOccurred())
  34. Expect(echo.Val()).To(Equal("hello"))
  35. })
  36. It("should Ping", func() {
  37. ping := client.Ping()
  38. Expect(ping.Err()).NotTo(HaveOccurred())
  39. Expect(ping.Val()).To(Equal("PONG"))
  40. })
  41. It("should Wait", func() {
  42. // assume testing on single redis instance
  43. start := time.Now()
  44. wait := client.Wait(1, time.Second)
  45. Expect(wait.Err()).NotTo(HaveOccurred())
  46. Expect(wait.Val()).To(Equal(int64(0)))
  47. Expect(time.Now()).To(BeTemporally("~", start.Add(time.Second), 800*time.Millisecond))
  48. })
  49. It("should Select", func() {
  50. pipe := client.Pipeline()
  51. sel := pipe.Select(1)
  52. _, err := pipe.Exec()
  53. Expect(err).NotTo(HaveOccurred())
  54. Expect(sel.Err()).NotTo(HaveOccurred())
  55. Expect(sel.Val()).To(Equal("OK"))
  56. })
  57. It("should BgRewriteAOF", func() {
  58. Skip("flaky test")
  59. val, err := client.BgRewriteAOF().Result()
  60. Expect(err).NotTo(HaveOccurred())
  61. Expect(val).To(ContainSubstring("Background append only file rewriting"))
  62. })
  63. It("should BgSave", func() {
  64. Skip("flaky test")
  65. // workaround for "ERR Can't BGSAVE while AOF log rewriting is in progress"
  66. Eventually(func() string {
  67. return client.BgSave().Val()
  68. }, "30s").Should(Equal("Background saving started"))
  69. })
  70. It("should ClientKill", func() {
  71. r := client.ClientKill("1.1.1.1:1111")
  72. Expect(r.Err()).To(MatchError("ERR No such client"))
  73. Expect(r.Val()).To(Equal(""))
  74. })
  75. It("should ClientPause", func() {
  76. err := client.ClientPause(time.Second).Err()
  77. Expect(err).NotTo(HaveOccurred())
  78. start := time.Now()
  79. err = client.Ping().Err()
  80. Expect(err).NotTo(HaveOccurred())
  81. Expect(time.Now()).To(BeTemporally("~", start.Add(time.Second), 800*time.Millisecond))
  82. })
  83. It("should ClientSetName and ClientGetName", func() {
  84. pipe := client.Pipeline()
  85. set := pipe.ClientSetName("theclientname")
  86. get := pipe.ClientGetName()
  87. _, err := pipe.Exec()
  88. Expect(err).NotTo(HaveOccurred())
  89. Expect(set.Err()).NotTo(HaveOccurred())
  90. Expect(set.Val()).To(BeTrue())
  91. Expect(get.Err()).NotTo(HaveOccurred())
  92. Expect(get.Val()).To(Equal("theclientname"))
  93. })
  94. It("should ConfigGet", func() {
  95. r := client.ConfigGet("*")
  96. Expect(r.Err()).NotTo(HaveOccurred())
  97. Expect(r.Val()).NotTo(BeEmpty())
  98. })
  99. It("should ConfigResetStat", func() {
  100. r := client.ConfigResetStat()
  101. Expect(r.Err()).NotTo(HaveOccurred())
  102. Expect(r.Val()).To(Equal("OK"))
  103. })
  104. It("should ConfigSet", func() {
  105. configGet := client.ConfigGet("maxmemory")
  106. Expect(configGet.Err()).NotTo(HaveOccurred())
  107. Expect(configGet.Val()).To(HaveLen(2))
  108. Expect(configGet.Val()[0]).To(Equal("maxmemory"))
  109. configSet := client.ConfigSet("maxmemory", configGet.Val()[1].(string))
  110. Expect(configSet.Err()).NotTo(HaveOccurred())
  111. Expect(configSet.Val()).To(Equal("OK"))
  112. })
  113. It("should DbSize", func() {
  114. dbSize := client.DbSize()
  115. Expect(dbSize.Err()).NotTo(HaveOccurred())
  116. Expect(dbSize.Val()).To(Equal(int64(0)))
  117. })
  118. It("should Info", func() {
  119. info := client.Info()
  120. Expect(info.Err()).NotTo(HaveOccurred())
  121. Expect(info.Val()).NotTo(Equal(""))
  122. })
  123. It("should Info cpu", func() {
  124. info := client.Info("cpu")
  125. Expect(info.Err()).NotTo(HaveOccurred())
  126. Expect(info.Val()).NotTo(Equal(""))
  127. Expect(info.Val()).To(ContainSubstring(`used_cpu_sys`))
  128. })
  129. It("should LastSave", func() {
  130. lastSave := client.LastSave()
  131. Expect(lastSave.Err()).NotTo(HaveOccurred())
  132. Expect(lastSave.Val()).NotTo(Equal(0))
  133. })
  134. It("should Save", func() {
  135. // workaround for "ERR Background save already in progress"
  136. Eventually(func() string {
  137. return client.Save().Val()
  138. }, "10s").Should(Equal("OK"))
  139. })
  140. It("should SlaveOf", func() {
  141. slaveOf := client.SlaveOf("localhost", "8888")
  142. Expect(slaveOf.Err()).NotTo(HaveOccurred())
  143. Expect(slaveOf.Val()).To(Equal("OK"))
  144. slaveOf = client.SlaveOf("NO", "ONE")
  145. Expect(slaveOf.Err()).NotTo(HaveOccurred())
  146. Expect(slaveOf.Val()).To(Equal("OK"))
  147. })
  148. It("should Time", func() {
  149. tm, err := client.Time().Result()
  150. Expect(err).NotTo(HaveOccurred())
  151. Expect(tm).To(BeTemporally("~", time.Now(), 3*time.Second))
  152. })
  153. })
  154. Describe("debugging", func() {
  155. It("should DebugObject", func() {
  156. debug := client.DebugObject("foo")
  157. Expect(debug.Err()).To(HaveOccurred())
  158. Expect(debug.Err().Error()).To(Equal("ERR no such key"))
  159. client.Set("foo", "bar", 0)
  160. debug = client.DebugObject("foo")
  161. Expect(debug.Err()).NotTo(HaveOccurred())
  162. Expect(debug.Val()).To(ContainSubstring(`serializedlength:4`))
  163. })
  164. })
  165. Describe("keys", func() {
  166. It("should Del", func() {
  167. err := client.Set("key1", "Hello", 0).Err()
  168. Expect(err).NotTo(HaveOccurred())
  169. err = client.Set("key2", "World", 0).Err()
  170. Expect(err).NotTo(HaveOccurred())
  171. n, err := client.Del("key1", "key2", "key3").Result()
  172. Expect(err).NotTo(HaveOccurred())
  173. Expect(n).To(Equal(int64(2)))
  174. })
  175. It("should Unlink", func() {
  176. err := client.Set("key1", "Hello", 0).Err()
  177. Expect(err).NotTo(HaveOccurred())
  178. err = client.Set("key2", "World", 0).Err()
  179. Expect(err).NotTo(HaveOccurred())
  180. n, err := client.Unlink("key1", "key2", "key3").Result()
  181. Expect(err).NotTo(HaveOccurred())
  182. Expect(n).To(Equal(int64(2)))
  183. })
  184. It("should Dump", func() {
  185. set := client.Set("key", "hello", 0)
  186. Expect(set.Err()).NotTo(HaveOccurred())
  187. Expect(set.Val()).To(Equal("OK"))
  188. dump := client.Dump("key")
  189. Expect(dump.Err()).NotTo(HaveOccurred())
  190. Expect(dump.Val()).NotTo(BeEmpty())
  191. })
  192. It("should Exists", func() {
  193. set := client.Set("key1", "Hello", 0)
  194. Expect(set.Err()).NotTo(HaveOccurred())
  195. Expect(set.Val()).To(Equal("OK"))
  196. exists := client.Exists("key1")
  197. Expect(exists.Err()).NotTo(HaveOccurred())
  198. Expect(exists.Val()).To(Equal(true))
  199. exists = client.Exists("key2")
  200. Expect(exists.Err()).NotTo(HaveOccurred())
  201. Expect(exists.Val()).To(Equal(false))
  202. existsMul := client.ExistsMulti("key1", "key2")
  203. Expect(existsMul.Err()).NotTo(HaveOccurred())
  204. Expect(existsMul.Val()).To(Equal(int64(1)))
  205. existsMul = client.ExistsMulti("key1", "key1")
  206. Expect(existsMul.Err()).NotTo(HaveOccurred())
  207. Expect(existsMul.Val()).To(Equal(int64(2)))
  208. })
  209. It("should Expire", func() {
  210. set := client.Set("key", "Hello", 0)
  211. Expect(set.Err()).NotTo(HaveOccurred())
  212. Expect(set.Val()).To(Equal("OK"))
  213. expire := client.Expire("key", 10*time.Second)
  214. Expect(expire.Err()).NotTo(HaveOccurred())
  215. Expect(expire.Val()).To(Equal(true))
  216. ttl := client.TTL("key")
  217. Expect(ttl.Err()).NotTo(HaveOccurred())
  218. Expect(ttl.Val()).To(Equal(10 * time.Second))
  219. set = client.Set("key", "Hello World", 0)
  220. Expect(set.Err()).NotTo(HaveOccurred())
  221. Expect(set.Val()).To(Equal("OK"))
  222. ttl = client.TTL("key")
  223. Expect(ttl.Err()).NotTo(HaveOccurred())
  224. Expect(ttl.Val() < 0).To(Equal(true))
  225. })
  226. It("should ExpireAt", func() {
  227. set := client.Set("key", "Hello", 0)
  228. Expect(set.Err()).NotTo(HaveOccurred())
  229. Expect(set.Val()).To(Equal("OK"))
  230. exists := client.Exists("key")
  231. Expect(exists.Err()).NotTo(HaveOccurred())
  232. Expect(exists.Val()).To(Equal(true))
  233. expireAt := client.ExpireAt("key", time.Now().Add(-time.Hour))
  234. Expect(expireAt.Err()).NotTo(HaveOccurred())
  235. Expect(expireAt.Val()).To(Equal(true))
  236. exists = client.Exists("key")
  237. Expect(exists.Err()).NotTo(HaveOccurred())
  238. Expect(exists.Val()).To(Equal(false))
  239. })
  240. It("should Keys", func() {
  241. mset := client.MSet("one", "1", "two", "2", "three", "3", "four", "4")
  242. Expect(mset.Err()).NotTo(HaveOccurred())
  243. Expect(mset.Val()).To(Equal("OK"))
  244. keys := client.Keys("*o*")
  245. Expect(keys.Err()).NotTo(HaveOccurred())
  246. Expect(keys.Val()).To(ConsistOf([]string{"four", "one", "two"}))
  247. keys = client.Keys("t??")
  248. Expect(keys.Err()).NotTo(HaveOccurred())
  249. Expect(keys.Val()).To(Equal([]string{"two"}))
  250. keys = client.Keys("*")
  251. Expect(keys.Err()).NotTo(HaveOccurred())
  252. Expect(keys.Val()).To(ConsistOf([]string{"four", "one", "three", "two"}))
  253. })
  254. It("should Migrate", func() {
  255. migrate := client.Migrate("localhost", redisSecondaryPort, "key", 0, 0)
  256. Expect(migrate.Err()).NotTo(HaveOccurred())
  257. Expect(migrate.Val()).To(Equal("NOKEY"))
  258. set := client.Set("key", "hello", 0)
  259. Expect(set.Err()).NotTo(HaveOccurred())
  260. Expect(set.Val()).To(Equal("OK"))
  261. migrate = client.Migrate("localhost", redisSecondaryPort, "key", 0, 0)
  262. Expect(migrate.Err()).To(MatchError("IOERR error or timeout writing to target instance"))
  263. Expect(migrate.Val()).To(Equal(""))
  264. })
  265. It("should Move", func() {
  266. move := client.Move("key", 2)
  267. Expect(move.Err()).NotTo(HaveOccurred())
  268. Expect(move.Val()).To(Equal(false))
  269. set := client.Set("key", "hello", 0)
  270. Expect(set.Err()).NotTo(HaveOccurred())
  271. Expect(set.Val()).To(Equal("OK"))
  272. move = client.Move("key", 2)
  273. Expect(move.Err()).NotTo(HaveOccurred())
  274. Expect(move.Val()).To(Equal(true))
  275. get := client.Get("key")
  276. Expect(get.Err()).To(Equal(redis.Nil))
  277. Expect(get.Val()).To(Equal(""))
  278. pipe := client.Pipeline()
  279. pipe.Select(2)
  280. get = pipe.Get("key")
  281. pipe.FlushDb()
  282. _, err := pipe.Exec()
  283. Expect(err).NotTo(HaveOccurred())
  284. Expect(get.Val()).To(Equal("hello"))
  285. })
  286. It("should Object", func() {
  287. set := client.Set("key", "hello", 0)
  288. Expect(set.Err()).NotTo(HaveOccurred())
  289. Expect(set.Val()).To(Equal("OK"))
  290. refCount := client.ObjectRefCount("key")
  291. Expect(refCount.Err()).NotTo(HaveOccurred())
  292. Expect(refCount.Val()).To(Equal(int64(1)))
  293. err := client.ObjectEncoding("key").Err()
  294. Expect(err).NotTo(HaveOccurred())
  295. idleTime := client.ObjectIdleTime("key")
  296. Expect(idleTime.Err()).NotTo(HaveOccurred())
  297. Expect(idleTime.Val()).To(Equal(time.Duration(0)))
  298. })
  299. It("should Persist", func() {
  300. set := client.Set("key", "Hello", 0)
  301. Expect(set.Err()).NotTo(HaveOccurred())
  302. Expect(set.Val()).To(Equal("OK"))
  303. expire := client.Expire("key", 10*time.Second)
  304. Expect(expire.Err()).NotTo(HaveOccurred())
  305. Expect(expire.Val()).To(Equal(true))
  306. ttl := client.TTL("key")
  307. Expect(ttl.Err()).NotTo(HaveOccurred())
  308. Expect(ttl.Val()).To(Equal(10 * time.Second))
  309. persist := client.Persist("key")
  310. Expect(persist.Err()).NotTo(HaveOccurred())
  311. Expect(persist.Val()).To(Equal(true))
  312. ttl = client.TTL("key")
  313. Expect(ttl.Err()).NotTo(HaveOccurred())
  314. Expect(ttl.Val() < 0).To(Equal(true))
  315. })
  316. It("should PExpire", func() {
  317. set := client.Set("key", "Hello", 0)
  318. Expect(set.Err()).NotTo(HaveOccurred())
  319. Expect(set.Val()).To(Equal("OK"))
  320. expiration := 900 * time.Millisecond
  321. pexpire := client.PExpire("key", expiration)
  322. Expect(pexpire.Err()).NotTo(HaveOccurred())
  323. Expect(pexpire.Val()).To(Equal(true))
  324. ttl := client.TTL("key")
  325. Expect(ttl.Err()).NotTo(HaveOccurred())
  326. Expect(ttl.Val()).To(Equal(time.Second))
  327. pttl := client.PTTL("key")
  328. Expect(pttl.Err()).NotTo(HaveOccurred())
  329. Expect(pttl.Val()).To(BeNumerically("~", expiration, 10*time.Millisecond))
  330. })
  331. It("should PExpireAt", func() {
  332. set := client.Set("key", "Hello", 0)
  333. Expect(set.Err()).NotTo(HaveOccurred())
  334. Expect(set.Val()).To(Equal("OK"))
  335. expiration := 900 * time.Millisecond
  336. pexpireat := client.PExpireAt("key", time.Now().Add(expiration))
  337. Expect(pexpireat.Err()).NotTo(HaveOccurred())
  338. Expect(pexpireat.Val()).To(Equal(true))
  339. ttl := client.TTL("key")
  340. Expect(ttl.Err()).NotTo(HaveOccurred())
  341. Expect(ttl.Val()).To(Equal(time.Second))
  342. pttl := client.PTTL("key")
  343. Expect(pttl.Err()).NotTo(HaveOccurred())
  344. Expect(pttl.Val()).To(BeNumerically("~", expiration, 10*time.Millisecond))
  345. })
  346. It("should PTTL", func() {
  347. set := client.Set("key", "Hello", 0)
  348. Expect(set.Err()).NotTo(HaveOccurred())
  349. Expect(set.Val()).To(Equal("OK"))
  350. expiration := time.Second
  351. expire := client.Expire("key", expiration)
  352. Expect(expire.Err()).NotTo(HaveOccurred())
  353. Expect(set.Val()).To(Equal("OK"))
  354. pttl := client.PTTL("key")
  355. Expect(pttl.Err()).NotTo(HaveOccurred())
  356. Expect(pttl.Val()).To(BeNumerically("~", expiration, 10*time.Millisecond))
  357. })
  358. It("should RandomKey", func() {
  359. randomKey := client.RandomKey()
  360. Expect(randomKey.Err()).To(Equal(redis.Nil))
  361. Expect(randomKey.Val()).To(Equal(""))
  362. set := client.Set("key", "hello", 0)
  363. Expect(set.Err()).NotTo(HaveOccurred())
  364. Expect(set.Val()).To(Equal("OK"))
  365. randomKey = client.RandomKey()
  366. Expect(randomKey.Err()).NotTo(HaveOccurred())
  367. Expect(randomKey.Val()).To(Equal("key"))
  368. })
  369. It("should Rename", func() {
  370. set := client.Set("key", "hello", 0)
  371. Expect(set.Err()).NotTo(HaveOccurred())
  372. Expect(set.Val()).To(Equal("OK"))
  373. status := client.Rename("key", "key1")
  374. Expect(status.Err()).NotTo(HaveOccurred())
  375. Expect(status.Val()).To(Equal("OK"))
  376. get := client.Get("key1")
  377. Expect(get.Err()).NotTo(HaveOccurred())
  378. Expect(get.Val()).To(Equal("hello"))
  379. })
  380. It("should RenameNX", func() {
  381. set := client.Set("key", "hello", 0)
  382. Expect(set.Err()).NotTo(HaveOccurred())
  383. Expect(set.Val()).To(Equal("OK"))
  384. renameNX := client.RenameNX("key", "key1")
  385. Expect(renameNX.Err()).NotTo(HaveOccurred())
  386. Expect(renameNX.Val()).To(Equal(true))
  387. get := client.Get("key1")
  388. Expect(get.Err()).NotTo(HaveOccurred())
  389. Expect(get.Val()).To(Equal("hello"))
  390. })
  391. It("should Restore", func() {
  392. err := client.Set("key", "hello", 0).Err()
  393. Expect(err).NotTo(HaveOccurred())
  394. dump := client.Dump("key")
  395. Expect(dump.Err()).NotTo(HaveOccurred())
  396. err = client.Del("key").Err()
  397. Expect(err).NotTo(HaveOccurred())
  398. restore, err := client.Restore("key", 0, dump.Val()).Result()
  399. Expect(err).NotTo(HaveOccurred())
  400. Expect(restore).To(Equal("OK"))
  401. type_, err := client.Type("key").Result()
  402. Expect(err).NotTo(HaveOccurred())
  403. Expect(type_).To(Equal("string"))
  404. val, err := client.Get("key").Result()
  405. Expect(err).NotTo(HaveOccurred())
  406. Expect(val).To(Equal("hello"))
  407. })
  408. It("should RestoreReplace", func() {
  409. err := client.Set("key", "hello", 0).Err()
  410. Expect(err).NotTo(HaveOccurred())
  411. dump := client.Dump("key")
  412. Expect(dump.Err()).NotTo(HaveOccurred())
  413. restore, err := client.RestoreReplace("key", 0, dump.Val()).Result()
  414. Expect(err).NotTo(HaveOccurred())
  415. Expect(restore).To(Equal("OK"))
  416. type_, err := client.Type("key").Result()
  417. Expect(err).NotTo(HaveOccurred())
  418. Expect(type_).To(Equal("string"))
  419. val, err := client.Get("key").Result()
  420. Expect(err).NotTo(HaveOccurred())
  421. Expect(val).To(Equal("hello"))
  422. })
  423. It("should Sort", func() {
  424. size, err := client.LPush("list", "1").Result()
  425. Expect(err).NotTo(HaveOccurred())
  426. Expect(size).To(Equal(int64(1)))
  427. size, err = client.LPush("list", "3").Result()
  428. Expect(err).NotTo(HaveOccurred())
  429. Expect(size).To(Equal(int64(2)))
  430. size, err = client.LPush("list", "2").Result()
  431. Expect(err).NotTo(HaveOccurred())
  432. Expect(size).To(Equal(int64(3)))
  433. els, err := client.Sort("list", redis.Sort{
  434. Offset: 0,
  435. Count: 2,
  436. Order: "ASC",
  437. }).Result()
  438. Expect(err).NotTo(HaveOccurred())
  439. Expect(els).To(Equal([]string{"1", "2"}))
  440. })
  441. It("should Sort and Get", func() {
  442. size, err := client.LPush("list", "1").Result()
  443. Expect(err).NotTo(HaveOccurred())
  444. Expect(size).To(Equal(int64(1)))
  445. size, err = client.LPush("list", "3").Result()
  446. Expect(err).NotTo(HaveOccurred())
  447. Expect(size).To(Equal(int64(2)))
  448. size, err = client.LPush("list", "2").Result()
  449. Expect(err).NotTo(HaveOccurred())
  450. Expect(size).To(Equal(int64(3)))
  451. err = client.Set("object_2", "value2", 0).Err()
  452. Expect(err).NotTo(HaveOccurred())
  453. {
  454. els, err := client.Sort("list", redis.Sort{
  455. Get: []string{"object_*"},
  456. }).Result()
  457. Expect(err).NotTo(HaveOccurred())
  458. Expect(els).To(Equal([]string{"", "value2", ""}))
  459. }
  460. {
  461. els, err := client.SortInterfaces("list", redis.Sort{
  462. Get: []string{"object_*"},
  463. }).Result()
  464. Expect(err).NotTo(HaveOccurred())
  465. Expect(els).To(Equal([]interface{}{nil, "value2", nil}))
  466. }
  467. })
  468. It("should TTL", func() {
  469. ttl := client.TTL("key")
  470. Expect(ttl.Err()).NotTo(HaveOccurred())
  471. Expect(ttl.Val() < 0).To(Equal(true))
  472. set := client.Set("key", "hello", 0)
  473. Expect(set.Err()).NotTo(HaveOccurred())
  474. Expect(set.Val()).To(Equal("OK"))
  475. expire := client.Expire("key", 60*time.Second)
  476. Expect(expire.Err()).NotTo(HaveOccurred())
  477. Expect(expire.Val()).To(Equal(true))
  478. ttl = client.TTL("key")
  479. Expect(ttl.Err()).NotTo(HaveOccurred())
  480. Expect(ttl.Val()).To(Equal(60 * time.Second))
  481. })
  482. It("should Type", func() {
  483. set := client.Set("key", "hello", 0)
  484. Expect(set.Err()).NotTo(HaveOccurred())
  485. Expect(set.Val()).To(Equal("OK"))
  486. type_ := client.Type("key")
  487. Expect(type_.Err()).NotTo(HaveOccurred())
  488. Expect(type_.Val()).To(Equal("string"))
  489. })
  490. })
  491. Describe("scanning", func() {
  492. It("should Scan", func() {
  493. for i := 0; i < 1000; i++ {
  494. set := client.Set(fmt.Sprintf("key%d", i), "hello", 0)
  495. Expect(set.Err()).NotTo(HaveOccurred())
  496. }
  497. keys, cursor, err := client.Scan(0, "", 0).Result()
  498. Expect(err).NotTo(HaveOccurred())
  499. Expect(keys).NotTo(BeEmpty())
  500. Expect(cursor).NotTo(BeZero())
  501. })
  502. It("should SScan", func() {
  503. for i := 0; i < 1000; i++ {
  504. sadd := client.SAdd("myset", fmt.Sprintf("member%d", i))
  505. Expect(sadd.Err()).NotTo(HaveOccurred())
  506. }
  507. keys, cursor, err := client.SScan("myset", 0, "", 0).Result()
  508. Expect(err).NotTo(HaveOccurred())
  509. Expect(keys).NotTo(BeEmpty())
  510. Expect(cursor).NotTo(BeZero())
  511. })
  512. It("should HScan", func() {
  513. for i := 0; i < 1000; i++ {
  514. sadd := client.HSet("myhash", fmt.Sprintf("key%d", i), "hello")
  515. Expect(sadd.Err()).NotTo(HaveOccurred())
  516. }
  517. keys, cursor, err := client.HScan("myhash", 0, "", 0).Result()
  518. Expect(err).NotTo(HaveOccurred())
  519. Expect(keys).NotTo(BeEmpty())
  520. Expect(cursor).NotTo(BeZero())
  521. })
  522. It("should ZScan", func() {
  523. for i := 0; i < 1000; i++ {
  524. sadd := client.ZAdd("myset", redis.Z{float64(i), fmt.Sprintf("member%d", i)})
  525. Expect(sadd.Err()).NotTo(HaveOccurred())
  526. }
  527. keys, cursor, err := client.ZScan("myset", 0, "", 0).Result()
  528. Expect(err).NotTo(HaveOccurred())
  529. Expect(keys).NotTo(BeEmpty())
  530. Expect(cursor).NotTo(BeZero())
  531. })
  532. })
  533. Describe("strings", func() {
  534. It("should Append", func() {
  535. exists := client.Exists("key")
  536. Expect(exists.Err()).NotTo(HaveOccurred())
  537. Expect(exists.Val()).To(Equal(false))
  538. append := client.Append("key", "Hello")
  539. Expect(append.Err()).NotTo(HaveOccurred())
  540. Expect(append.Val()).To(Equal(int64(5)))
  541. append = client.Append("key", " World")
  542. Expect(append.Err()).NotTo(HaveOccurred())
  543. Expect(append.Val()).To(Equal(int64(11)))
  544. get := client.Get("key")
  545. Expect(get.Err()).NotTo(HaveOccurred())
  546. Expect(get.Val()).To(Equal("Hello World"))
  547. })
  548. It("should BitCount", func() {
  549. set := client.Set("key", "foobar", 0)
  550. Expect(set.Err()).NotTo(HaveOccurred())
  551. Expect(set.Val()).To(Equal("OK"))
  552. bitCount := client.BitCount("key", nil)
  553. Expect(bitCount.Err()).NotTo(HaveOccurred())
  554. Expect(bitCount.Val()).To(Equal(int64(26)))
  555. bitCount = client.BitCount("key", &redis.BitCount{0, 0})
  556. Expect(bitCount.Err()).NotTo(HaveOccurred())
  557. Expect(bitCount.Val()).To(Equal(int64(4)))
  558. bitCount = client.BitCount("key", &redis.BitCount{1, 1})
  559. Expect(bitCount.Err()).NotTo(HaveOccurred())
  560. Expect(bitCount.Val()).To(Equal(int64(6)))
  561. })
  562. It("should BitOpAnd", func() {
  563. set := client.Set("key1", "1", 0)
  564. Expect(set.Err()).NotTo(HaveOccurred())
  565. Expect(set.Val()).To(Equal("OK"))
  566. set = client.Set("key2", "0", 0)
  567. Expect(set.Err()).NotTo(HaveOccurred())
  568. Expect(set.Val()).To(Equal("OK"))
  569. bitOpAnd := client.BitOpAnd("dest", "key1", "key2")
  570. Expect(bitOpAnd.Err()).NotTo(HaveOccurred())
  571. Expect(bitOpAnd.Val()).To(Equal(int64(1)))
  572. get := client.Get("dest")
  573. Expect(get.Err()).NotTo(HaveOccurred())
  574. Expect(get.Val()).To(Equal("0"))
  575. })
  576. It("should BitOpOr", func() {
  577. set := client.Set("key1", "1", 0)
  578. Expect(set.Err()).NotTo(HaveOccurred())
  579. Expect(set.Val()).To(Equal("OK"))
  580. set = client.Set("key2", "0", 0)
  581. Expect(set.Err()).NotTo(HaveOccurred())
  582. Expect(set.Val()).To(Equal("OK"))
  583. bitOpOr := client.BitOpOr("dest", "key1", "key2")
  584. Expect(bitOpOr.Err()).NotTo(HaveOccurred())
  585. Expect(bitOpOr.Val()).To(Equal(int64(1)))
  586. get := client.Get("dest")
  587. Expect(get.Err()).NotTo(HaveOccurred())
  588. Expect(get.Val()).To(Equal("1"))
  589. })
  590. It("should BitOpXor", func() {
  591. set := client.Set("key1", "\xff", 0)
  592. Expect(set.Err()).NotTo(HaveOccurred())
  593. Expect(set.Val()).To(Equal("OK"))
  594. set = client.Set("key2", "\x0f", 0)
  595. Expect(set.Err()).NotTo(HaveOccurred())
  596. Expect(set.Val()).To(Equal("OK"))
  597. bitOpXor := client.BitOpXor("dest", "key1", "key2")
  598. Expect(bitOpXor.Err()).NotTo(HaveOccurred())
  599. Expect(bitOpXor.Val()).To(Equal(int64(1)))
  600. get := client.Get("dest")
  601. Expect(get.Err()).NotTo(HaveOccurred())
  602. Expect(get.Val()).To(Equal("\xf0"))
  603. })
  604. It("should BitOpNot", func() {
  605. set := client.Set("key1", "\x00", 0)
  606. Expect(set.Err()).NotTo(HaveOccurred())
  607. Expect(set.Val()).To(Equal("OK"))
  608. bitOpNot := client.BitOpNot("dest", "key1")
  609. Expect(bitOpNot.Err()).NotTo(HaveOccurred())
  610. Expect(bitOpNot.Val()).To(Equal(int64(1)))
  611. get := client.Get("dest")
  612. Expect(get.Err()).NotTo(HaveOccurred())
  613. Expect(get.Val()).To(Equal("\xff"))
  614. })
  615. It("should BitPos", func() {
  616. err := client.Set("mykey", "\xff\xf0\x00", 0).Err()
  617. Expect(err).NotTo(HaveOccurred())
  618. pos, err := client.BitPos("mykey", 0).Result()
  619. Expect(err).NotTo(HaveOccurred())
  620. Expect(pos).To(Equal(int64(12)))
  621. pos, err = client.BitPos("mykey", 1).Result()
  622. Expect(err).NotTo(HaveOccurred())
  623. Expect(pos).To(Equal(int64(0)))
  624. pos, err = client.BitPos("mykey", 0, 2).Result()
  625. Expect(err).NotTo(HaveOccurred())
  626. Expect(pos).To(Equal(int64(16)))
  627. pos, err = client.BitPos("mykey", 1, 2).Result()
  628. Expect(err).NotTo(HaveOccurred())
  629. Expect(pos).To(Equal(int64(-1)))
  630. pos, err = client.BitPos("mykey", 0, -1).Result()
  631. Expect(err).NotTo(HaveOccurred())
  632. Expect(pos).To(Equal(int64(16)))
  633. pos, err = client.BitPos("mykey", 1, -1).Result()
  634. Expect(err).NotTo(HaveOccurred())
  635. Expect(pos).To(Equal(int64(-1)))
  636. pos, err = client.BitPos("mykey", 0, 2, 1).Result()
  637. Expect(err).NotTo(HaveOccurred())
  638. Expect(pos).To(Equal(int64(-1)))
  639. pos, err = client.BitPos("mykey", 0, 0, -3).Result()
  640. Expect(err).NotTo(HaveOccurred())
  641. Expect(pos).To(Equal(int64(-1)))
  642. pos, err = client.BitPos("mykey", 0, 0, 0).Result()
  643. Expect(err).NotTo(HaveOccurred())
  644. Expect(pos).To(Equal(int64(-1)))
  645. })
  646. It("should Decr", func() {
  647. set := client.Set("key", "10", 0)
  648. Expect(set.Err()).NotTo(HaveOccurred())
  649. Expect(set.Val()).To(Equal("OK"))
  650. decr := client.Decr("key")
  651. Expect(decr.Err()).NotTo(HaveOccurred())
  652. Expect(decr.Val()).To(Equal(int64(9)))
  653. set = client.Set("key", "234293482390480948029348230948", 0)
  654. Expect(set.Err()).NotTo(HaveOccurred())
  655. Expect(set.Val()).To(Equal("OK"))
  656. decr = client.Decr("key")
  657. Expect(decr.Err()).To(MatchError("ERR value is not an integer or out of range"))
  658. Expect(decr.Val()).To(Equal(int64(0)))
  659. })
  660. It("should DecrBy", func() {
  661. set := client.Set("key", "10", 0)
  662. Expect(set.Err()).NotTo(HaveOccurred())
  663. Expect(set.Val()).To(Equal("OK"))
  664. decrBy := client.DecrBy("key", 5)
  665. Expect(decrBy.Err()).NotTo(HaveOccurred())
  666. Expect(decrBy.Val()).To(Equal(int64(5)))
  667. })
  668. It("should Get", func() {
  669. get := client.Get("_")
  670. Expect(get.Err()).To(Equal(redis.Nil))
  671. Expect(get.Val()).To(Equal(""))
  672. set := client.Set("key", "hello", 0)
  673. Expect(set.Err()).NotTo(HaveOccurred())
  674. Expect(set.Val()).To(Equal("OK"))
  675. get = client.Get("key")
  676. Expect(get.Err()).NotTo(HaveOccurred())
  677. Expect(get.Val()).To(Equal("hello"))
  678. })
  679. It("should GetBit", func() {
  680. setBit := client.SetBit("key", 7, 1)
  681. Expect(setBit.Err()).NotTo(HaveOccurred())
  682. Expect(setBit.Val()).To(Equal(int64(0)))
  683. getBit := client.GetBit("key", 0)
  684. Expect(getBit.Err()).NotTo(HaveOccurred())
  685. Expect(getBit.Val()).To(Equal(int64(0)))
  686. getBit = client.GetBit("key", 7)
  687. Expect(getBit.Err()).NotTo(HaveOccurred())
  688. Expect(getBit.Val()).To(Equal(int64(1)))
  689. getBit = client.GetBit("key", 100)
  690. Expect(getBit.Err()).NotTo(HaveOccurred())
  691. Expect(getBit.Val()).To(Equal(int64(0)))
  692. })
  693. It("should GetRange", func() {
  694. set := client.Set("key", "This is a string", 0)
  695. Expect(set.Err()).NotTo(HaveOccurred())
  696. Expect(set.Val()).To(Equal("OK"))
  697. getRange := client.GetRange("key", 0, 3)
  698. Expect(getRange.Err()).NotTo(HaveOccurred())
  699. Expect(getRange.Val()).To(Equal("This"))
  700. getRange = client.GetRange("key", -3, -1)
  701. Expect(getRange.Err()).NotTo(HaveOccurred())
  702. Expect(getRange.Val()).To(Equal("ing"))
  703. getRange = client.GetRange("key", 0, -1)
  704. Expect(getRange.Err()).NotTo(HaveOccurred())
  705. Expect(getRange.Val()).To(Equal("This is a string"))
  706. getRange = client.GetRange("key", 10, 100)
  707. Expect(getRange.Err()).NotTo(HaveOccurred())
  708. Expect(getRange.Val()).To(Equal("string"))
  709. })
  710. It("should GetSet", func() {
  711. incr := client.Incr("key")
  712. Expect(incr.Err()).NotTo(HaveOccurred())
  713. Expect(incr.Val()).To(Equal(int64(1)))
  714. getSet := client.GetSet("key", "0")
  715. Expect(getSet.Err()).NotTo(HaveOccurred())
  716. Expect(getSet.Val()).To(Equal("1"))
  717. get := client.Get("key")
  718. Expect(get.Err()).NotTo(HaveOccurred())
  719. Expect(get.Val()).To(Equal("0"))
  720. })
  721. It("should Incr", func() {
  722. set := client.Set("key", "10", 0)
  723. Expect(set.Err()).NotTo(HaveOccurred())
  724. Expect(set.Val()).To(Equal("OK"))
  725. incr := client.Incr("key")
  726. Expect(incr.Err()).NotTo(HaveOccurred())
  727. Expect(incr.Val()).To(Equal(int64(11)))
  728. get := client.Get("key")
  729. Expect(get.Err()).NotTo(HaveOccurred())
  730. Expect(get.Val()).To(Equal("11"))
  731. })
  732. It("should IncrBy", func() {
  733. set := client.Set("key", "10", 0)
  734. Expect(set.Err()).NotTo(HaveOccurred())
  735. Expect(set.Val()).To(Equal("OK"))
  736. incrBy := client.IncrBy("key", 5)
  737. Expect(incrBy.Err()).NotTo(HaveOccurred())
  738. Expect(incrBy.Val()).To(Equal(int64(15)))
  739. })
  740. It("should IncrByFloat", func() {
  741. set := client.Set("key", "10.50", 0)
  742. Expect(set.Err()).NotTo(HaveOccurred())
  743. Expect(set.Val()).To(Equal("OK"))
  744. incrByFloat := client.IncrByFloat("key", 0.1)
  745. Expect(incrByFloat.Err()).NotTo(HaveOccurred())
  746. Expect(incrByFloat.Val()).To(Equal(10.6))
  747. set = client.Set("key", "5.0e3", 0)
  748. Expect(set.Err()).NotTo(HaveOccurred())
  749. Expect(set.Val()).To(Equal("OK"))
  750. incrByFloat = client.IncrByFloat("key", 2.0e2)
  751. Expect(incrByFloat.Err()).NotTo(HaveOccurred())
  752. Expect(incrByFloat.Val()).To(Equal(float64(5200)))
  753. })
  754. It("should IncrByFloatOverflow", func() {
  755. incrByFloat := client.IncrByFloat("key", 996945661)
  756. Expect(incrByFloat.Err()).NotTo(HaveOccurred())
  757. Expect(incrByFloat.Val()).To(Equal(float64(996945661)))
  758. })
  759. It("should MSetMGet", func() {
  760. mSet := client.MSet("key1", "hello1", "key2", "hello2")
  761. Expect(mSet.Err()).NotTo(HaveOccurred())
  762. Expect(mSet.Val()).To(Equal("OK"))
  763. mGet := client.MGet("key1", "key2", "_")
  764. Expect(mGet.Err()).NotTo(HaveOccurred())
  765. Expect(mGet.Val()).To(Equal([]interface{}{"hello1", "hello2", nil}))
  766. })
  767. It("should MSetNX", func() {
  768. mSetNX := client.MSetNX("key1", "hello1", "key2", "hello2")
  769. Expect(mSetNX.Err()).NotTo(HaveOccurred())
  770. Expect(mSetNX.Val()).To(Equal(true))
  771. mSetNX = client.MSetNX("key2", "hello1", "key3", "hello2")
  772. Expect(mSetNX.Err()).NotTo(HaveOccurred())
  773. Expect(mSetNX.Val()).To(Equal(false))
  774. })
  775. It("should Set with expiration", func() {
  776. err := client.Set("key", "hello", 100*time.Millisecond).Err()
  777. Expect(err).NotTo(HaveOccurred())
  778. val, err := client.Get("key").Result()
  779. Expect(err).NotTo(HaveOccurred())
  780. Expect(val).To(Equal("hello"))
  781. Eventually(func() error {
  782. return client.Get("foo").Err()
  783. }, "1s", "100ms").Should(Equal(redis.Nil))
  784. })
  785. It("should SetGet", func() {
  786. set := client.Set("key", "hello", 0)
  787. Expect(set.Err()).NotTo(HaveOccurred())
  788. Expect(set.Val()).To(Equal("OK"))
  789. get := client.Get("key")
  790. Expect(get.Err()).NotTo(HaveOccurred())
  791. Expect(get.Val()).To(Equal("hello"))
  792. })
  793. It("should SetNX", func() {
  794. setNX := client.SetNX("key", "hello", 0)
  795. Expect(setNX.Err()).NotTo(HaveOccurred())
  796. Expect(setNX.Val()).To(Equal(true))
  797. setNX = client.SetNX("key", "hello2", 0)
  798. Expect(setNX.Err()).NotTo(HaveOccurred())
  799. Expect(setNX.Val()).To(Equal(false))
  800. get := client.Get("key")
  801. Expect(get.Err()).NotTo(HaveOccurred())
  802. Expect(get.Val()).To(Equal("hello"))
  803. })
  804. It("should SetNX with expiration", func() {
  805. isSet, err := client.SetNX("key", "hello", time.Second).Result()
  806. Expect(err).NotTo(HaveOccurred())
  807. Expect(isSet).To(Equal(true))
  808. isSet, err = client.SetNX("key", "hello2", time.Second).Result()
  809. Expect(err).NotTo(HaveOccurred())
  810. Expect(isSet).To(Equal(false))
  811. val, err := client.Get("key").Result()
  812. Expect(err).NotTo(HaveOccurred())
  813. Expect(val).To(Equal("hello"))
  814. })
  815. It("should SetXX", func() {
  816. isSet, err := client.SetXX("key", "hello2", 0).Result()
  817. Expect(err).NotTo(HaveOccurred())
  818. Expect(isSet).To(Equal(false))
  819. err = client.Set("key", "hello", 0).Err()
  820. Expect(err).NotTo(HaveOccurred())
  821. isSet, err = client.SetXX("key", "hello2", 0).Result()
  822. Expect(err).NotTo(HaveOccurred())
  823. Expect(isSet).To(Equal(true))
  824. val, err := client.Get("key").Result()
  825. Expect(err).NotTo(HaveOccurred())
  826. Expect(val).To(Equal("hello2"))
  827. })
  828. It("should SetXX with expiration", func() {
  829. isSet, err := client.SetXX("key", "hello2", time.Second).Result()
  830. Expect(err).NotTo(HaveOccurred())
  831. Expect(isSet).To(Equal(false))
  832. err = client.Set("key", "hello", time.Second).Err()
  833. Expect(err).NotTo(HaveOccurred())
  834. isSet, err = client.SetXX("key", "hello2", time.Second).Result()
  835. Expect(err).NotTo(HaveOccurred())
  836. Expect(isSet).To(Equal(true))
  837. val, err := client.Get("key").Result()
  838. Expect(err).NotTo(HaveOccurred())
  839. Expect(val).To(Equal("hello2"))
  840. })
  841. It("should SetRange", func() {
  842. set := client.Set("key", "Hello World", 0)
  843. Expect(set.Err()).NotTo(HaveOccurred())
  844. Expect(set.Val()).To(Equal("OK"))
  845. range_ := client.SetRange("key", 6, "Redis")
  846. Expect(range_.Err()).NotTo(HaveOccurred())
  847. Expect(range_.Val()).To(Equal(int64(11)))
  848. get := client.Get("key")
  849. Expect(get.Err()).NotTo(HaveOccurred())
  850. Expect(get.Val()).To(Equal("Hello Redis"))
  851. })
  852. It("should StrLen", func() {
  853. set := client.Set("key", "hello", 0)
  854. Expect(set.Err()).NotTo(HaveOccurred())
  855. Expect(set.Val()).To(Equal("OK"))
  856. strLen := client.StrLen("key")
  857. Expect(strLen.Err()).NotTo(HaveOccurred())
  858. Expect(strLen.Val()).To(Equal(int64(5)))
  859. strLen = client.StrLen("_")
  860. Expect(strLen.Err()).NotTo(HaveOccurred())
  861. Expect(strLen.Val()).To(Equal(int64(0)))
  862. })
  863. })
  864. Describe("hashes", func() {
  865. It("should HDel", func() {
  866. hSet := client.HSet("hash", "key", "hello")
  867. Expect(hSet.Err()).NotTo(HaveOccurred())
  868. hDel := client.HDel("hash", "key")
  869. Expect(hDel.Err()).NotTo(HaveOccurred())
  870. Expect(hDel.Val()).To(Equal(int64(1)))
  871. hDel = client.HDel("hash", "key")
  872. Expect(hDel.Err()).NotTo(HaveOccurred())
  873. Expect(hDel.Val()).To(Equal(int64(0)))
  874. })
  875. It("should HExists", func() {
  876. hSet := client.HSet("hash", "key", "hello")
  877. Expect(hSet.Err()).NotTo(HaveOccurred())
  878. hExists := client.HExists("hash", "key")
  879. Expect(hExists.Err()).NotTo(HaveOccurred())
  880. Expect(hExists.Val()).To(Equal(true))
  881. hExists = client.HExists("hash", "key1")
  882. Expect(hExists.Err()).NotTo(HaveOccurred())
  883. Expect(hExists.Val()).To(Equal(false))
  884. })
  885. It("should HGet", func() {
  886. hSet := client.HSet("hash", "key", "hello")
  887. Expect(hSet.Err()).NotTo(HaveOccurred())
  888. hGet := client.HGet("hash", "key")
  889. Expect(hGet.Err()).NotTo(HaveOccurred())
  890. Expect(hGet.Val()).To(Equal("hello"))
  891. hGet = client.HGet("hash", "key1")
  892. Expect(hGet.Err()).To(Equal(redis.Nil))
  893. Expect(hGet.Val()).To(Equal(""))
  894. })
  895. It("should HGetAll", func() {
  896. err := client.HSet("hash", "key1", "hello1").Err()
  897. Expect(err).NotTo(HaveOccurred())
  898. err = client.HSet("hash", "key2", "hello2").Err()
  899. Expect(err).NotTo(HaveOccurred())
  900. m, err := client.HGetAll("hash").Result()
  901. Expect(err).NotTo(HaveOccurred())
  902. Expect(m).To(Equal(map[string]string{"key1": "hello1", "key2": "hello2"}))
  903. })
  904. It("should HIncrBy", func() {
  905. hSet := client.HSet("hash", "key", "5")
  906. Expect(hSet.Err()).NotTo(HaveOccurred())
  907. hIncrBy := client.HIncrBy("hash", "key", 1)
  908. Expect(hIncrBy.Err()).NotTo(HaveOccurred())
  909. Expect(hIncrBy.Val()).To(Equal(int64(6)))
  910. hIncrBy = client.HIncrBy("hash", "key", -1)
  911. Expect(hIncrBy.Err()).NotTo(HaveOccurred())
  912. Expect(hIncrBy.Val()).To(Equal(int64(5)))
  913. hIncrBy = client.HIncrBy("hash", "key", -10)
  914. Expect(hIncrBy.Err()).NotTo(HaveOccurred())
  915. Expect(hIncrBy.Val()).To(Equal(int64(-5)))
  916. })
  917. It("should HIncrByFloat", func() {
  918. hSet := client.HSet("hash", "field", "10.50")
  919. Expect(hSet.Err()).NotTo(HaveOccurred())
  920. Expect(hSet.Val()).To(Equal(true))
  921. hIncrByFloat := client.HIncrByFloat("hash", "field", 0.1)
  922. Expect(hIncrByFloat.Err()).NotTo(HaveOccurred())
  923. Expect(hIncrByFloat.Val()).To(Equal(10.6))
  924. hSet = client.HSet("hash", "field", "5.0e3")
  925. Expect(hSet.Err()).NotTo(HaveOccurred())
  926. Expect(hSet.Val()).To(Equal(false))
  927. hIncrByFloat = client.HIncrByFloat("hash", "field", 2.0e2)
  928. Expect(hIncrByFloat.Err()).NotTo(HaveOccurred())
  929. Expect(hIncrByFloat.Val()).To(Equal(float64(5200)))
  930. })
  931. It("should HKeys", func() {
  932. hkeys := client.HKeys("hash")
  933. Expect(hkeys.Err()).NotTo(HaveOccurred())
  934. Expect(hkeys.Val()).To(Equal([]string{}))
  935. hset := client.HSet("hash", "key1", "hello1")
  936. Expect(hset.Err()).NotTo(HaveOccurred())
  937. hset = client.HSet("hash", "key2", "hello2")
  938. Expect(hset.Err()).NotTo(HaveOccurred())
  939. hkeys = client.HKeys("hash")
  940. Expect(hkeys.Err()).NotTo(HaveOccurred())
  941. Expect(hkeys.Val()).To(Equal([]string{"key1", "key2"}))
  942. })
  943. It("should HLen", func() {
  944. hSet := client.HSet("hash", "key1", "hello1")
  945. Expect(hSet.Err()).NotTo(HaveOccurred())
  946. hSet = client.HSet("hash", "key2", "hello2")
  947. Expect(hSet.Err()).NotTo(HaveOccurred())
  948. hLen := client.HLen("hash")
  949. Expect(hLen.Err()).NotTo(HaveOccurred())
  950. Expect(hLen.Val()).To(Equal(int64(2)))
  951. })
  952. It("should HMGet", func() {
  953. err := client.HSet("hash", "key1", "hello1").Err()
  954. Expect(err).NotTo(HaveOccurred())
  955. err = client.HSet("hash", "key2", "hello2").Err()
  956. Expect(err).NotTo(HaveOccurred())
  957. vals, err := client.HMGet("hash", "key1", "key2", "_").Result()
  958. Expect(err).NotTo(HaveOccurred())
  959. Expect(vals).To(Equal([]interface{}{"hello1", "hello2", nil}))
  960. })
  961. It("should HMSet", func() {
  962. ok, err := client.HMSet("hash", map[string]string{
  963. "key1": "hello1",
  964. "key2": "hello2",
  965. }).Result()
  966. Expect(err).NotTo(HaveOccurred())
  967. Expect(ok).To(Equal("OK"))
  968. v, err := client.HGet("hash", "key1").Result()
  969. Expect(err).NotTo(HaveOccurred())
  970. Expect(v).To(Equal("hello1"))
  971. v, err = client.HGet("hash", "key2").Result()
  972. Expect(err).NotTo(HaveOccurred())
  973. Expect(v).To(Equal("hello2"))
  974. })
  975. It("should HSet", func() {
  976. hSet := client.HSet("hash", "key", "hello")
  977. Expect(hSet.Err()).NotTo(HaveOccurred())
  978. Expect(hSet.Val()).To(Equal(true))
  979. hGet := client.HGet("hash", "key")
  980. Expect(hGet.Err()).NotTo(HaveOccurred())
  981. Expect(hGet.Val()).To(Equal("hello"))
  982. })
  983. It("should HSetNX", func() {
  984. hSetNX := client.HSetNX("hash", "key", "hello")
  985. Expect(hSetNX.Err()).NotTo(HaveOccurred())
  986. Expect(hSetNX.Val()).To(Equal(true))
  987. hSetNX = client.HSetNX("hash", "key", "hello")
  988. Expect(hSetNX.Err()).NotTo(HaveOccurred())
  989. Expect(hSetNX.Val()).To(Equal(false))
  990. hGet := client.HGet("hash", "key")
  991. Expect(hGet.Err()).NotTo(HaveOccurred())
  992. Expect(hGet.Val()).To(Equal("hello"))
  993. })
  994. It("should HVals", func() {
  995. err := client.HSet("hash", "key1", "hello1").Err()
  996. Expect(err).NotTo(HaveOccurred())
  997. err = client.HSet("hash", "key2", "hello2").Err()
  998. Expect(err).NotTo(HaveOccurred())
  999. v, err := client.HVals("hash").Result()
  1000. Expect(err).NotTo(HaveOccurred())
  1001. Expect(v).To(Equal([]string{"hello1", "hello2"}))
  1002. var slice []string
  1003. err = client.HVals("hash").ScanSlice(&slice)
  1004. Expect(err).NotTo(HaveOccurred())
  1005. Expect(slice).To(Equal([]string{"hello1", "hello2"}))
  1006. })
  1007. })
  1008. Describe("hyperloglog", func() {
  1009. It("should PFMerge", func() {
  1010. pfAdd := client.PFAdd("hll1", "1", "2", "3", "4", "5")
  1011. Expect(pfAdd.Err()).NotTo(HaveOccurred())
  1012. pfCount := client.PFCount("hll1")
  1013. Expect(pfCount.Err()).NotTo(HaveOccurred())
  1014. Expect(pfCount.Val()).To(Equal(int64(5)))
  1015. pfAdd = client.PFAdd("hll2", "a", "b", "c", "d", "e")
  1016. Expect(pfAdd.Err()).NotTo(HaveOccurred())
  1017. pfMerge := client.PFMerge("hllMerged", "hll1", "hll2")
  1018. Expect(pfMerge.Err()).NotTo(HaveOccurred())
  1019. pfCount = client.PFCount("hllMerged")
  1020. Expect(pfCount.Err()).NotTo(HaveOccurred())
  1021. Expect(pfCount.Val()).To(Equal(int64(10)))
  1022. pfCount = client.PFCount("hll1", "hll2")
  1023. Expect(pfCount.Err()).NotTo(HaveOccurred())
  1024. Expect(pfCount.Val()).To(Equal(int64(10)))
  1025. })
  1026. })
  1027. Describe("lists", func() {
  1028. It("should BLPop", func() {
  1029. rPush := client.RPush("list1", "a", "b", "c")
  1030. Expect(rPush.Err()).NotTo(HaveOccurred())
  1031. bLPop := client.BLPop(0, "list1", "list2")
  1032. Expect(bLPop.Err()).NotTo(HaveOccurred())
  1033. Expect(bLPop.Val()).To(Equal([]string{"list1", "a"}))
  1034. })
  1035. It("should BLPopBlocks", func() {
  1036. started := make(chan bool)
  1037. done := make(chan bool)
  1038. go func() {
  1039. defer GinkgoRecover()
  1040. started <- true
  1041. bLPop := client.BLPop(0, "list")
  1042. Expect(bLPop.Err()).NotTo(HaveOccurred())
  1043. Expect(bLPop.Val()).To(Equal([]string{"list", "a"}))
  1044. done <- true
  1045. }()
  1046. <-started
  1047. select {
  1048. case <-done:
  1049. Fail("BLPop is not blocked")
  1050. case <-time.After(time.Second):
  1051. // ok
  1052. }
  1053. rPush := client.RPush("list", "a")
  1054. Expect(rPush.Err()).NotTo(HaveOccurred())
  1055. select {
  1056. case <-done:
  1057. // ok
  1058. case <-time.After(time.Second):
  1059. Fail("BLPop is still blocked")
  1060. }
  1061. })
  1062. It("should BLPop timeout", func() {
  1063. val, err := client.BLPop(time.Second, "list1").Result()
  1064. Expect(err).To(Equal(redis.Nil))
  1065. Expect(val).To(BeNil())
  1066. Expect(client.Ping().Err()).NotTo(HaveOccurred())
  1067. stats := client.PoolStats()
  1068. Expect(stats.Requests).To(Equal(uint32(3)))
  1069. Expect(stats.Hits).To(Equal(uint32(1)))
  1070. Expect(stats.Timeouts).To(Equal(uint32(0)))
  1071. })
  1072. It("should BRPop", func() {
  1073. rPush := client.RPush("list1", "a", "b", "c")
  1074. Expect(rPush.Err()).NotTo(HaveOccurred())
  1075. bRPop := client.BRPop(0, "list1", "list2")
  1076. Expect(bRPop.Err()).NotTo(HaveOccurred())
  1077. Expect(bRPop.Val()).To(Equal([]string{"list1", "c"}))
  1078. })
  1079. It("should BRPop blocks", func() {
  1080. started := make(chan bool)
  1081. done := make(chan bool)
  1082. go func() {
  1083. defer GinkgoRecover()
  1084. started <- true
  1085. brpop := client.BRPop(0, "list")
  1086. Expect(brpop.Err()).NotTo(HaveOccurred())
  1087. Expect(brpop.Val()).To(Equal([]string{"list", "a"}))
  1088. done <- true
  1089. }()
  1090. <-started
  1091. select {
  1092. case <-done:
  1093. Fail("BRPop is not blocked")
  1094. case <-time.After(time.Second):
  1095. // ok
  1096. }
  1097. rPush := client.RPush("list", "a")
  1098. Expect(rPush.Err()).NotTo(HaveOccurred())
  1099. select {
  1100. case <-done:
  1101. // ok
  1102. case <-time.After(time.Second):
  1103. Fail("BRPop is still blocked")
  1104. // ok
  1105. }
  1106. })
  1107. It("should BRPopLPush", func() {
  1108. _, err := client.BRPopLPush("list1", "list2", time.Second).Result()
  1109. Expect(err).To(Equal(redis.Nil))
  1110. err = client.RPush("list1", "a", "b", "c").Err()
  1111. Expect(err).NotTo(HaveOccurred())
  1112. v, err := client.BRPopLPush("list1", "list2", 0).Result()
  1113. Expect(err).NotTo(HaveOccurred())
  1114. Expect(v).To(Equal("c"))
  1115. })
  1116. It("should LIndex", func() {
  1117. lPush := client.LPush("list", "World")
  1118. Expect(lPush.Err()).NotTo(HaveOccurred())
  1119. lPush = client.LPush("list", "Hello")
  1120. Expect(lPush.Err()).NotTo(HaveOccurred())
  1121. lIndex := client.LIndex("list", 0)
  1122. Expect(lIndex.Err()).NotTo(HaveOccurred())
  1123. Expect(lIndex.Val()).To(Equal("Hello"))
  1124. lIndex = client.LIndex("list", -1)
  1125. Expect(lIndex.Err()).NotTo(HaveOccurred())
  1126. Expect(lIndex.Val()).To(Equal("World"))
  1127. lIndex = client.LIndex("list", 3)
  1128. Expect(lIndex.Err()).To(Equal(redis.Nil))
  1129. Expect(lIndex.Val()).To(Equal(""))
  1130. })
  1131. It("should LInsert", func() {
  1132. rPush := client.RPush("list", "Hello")
  1133. Expect(rPush.Err()).NotTo(HaveOccurred())
  1134. rPush = client.RPush("list", "World")
  1135. Expect(rPush.Err()).NotTo(HaveOccurred())
  1136. lInsert := client.LInsert("list", "BEFORE", "World", "There")
  1137. Expect(lInsert.Err()).NotTo(HaveOccurred())
  1138. Expect(lInsert.Val()).To(Equal(int64(3)))
  1139. lRange := client.LRange("list", 0, -1)
  1140. Expect(lRange.Err()).NotTo(HaveOccurred())
  1141. Expect(lRange.Val()).To(Equal([]string{"Hello", "There", "World"}))
  1142. })
  1143. It("should LLen", func() {
  1144. lPush := client.LPush("list", "World")
  1145. Expect(lPush.Err()).NotTo(HaveOccurred())
  1146. lPush = client.LPush("list", "Hello")
  1147. Expect(lPush.Err()).NotTo(HaveOccurred())
  1148. lLen := client.LLen("list")
  1149. Expect(lLen.Err()).NotTo(HaveOccurred())
  1150. Expect(lLen.Val()).To(Equal(int64(2)))
  1151. })
  1152. It("should LPop", func() {
  1153. rPush := client.RPush("list", "one")
  1154. Expect(rPush.Err()).NotTo(HaveOccurred())
  1155. rPush = client.RPush("list", "two")
  1156. Expect(rPush.Err()).NotTo(HaveOccurred())
  1157. rPush = client.RPush("list", "three")
  1158. Expect(rPush.Err()).NotTo(HaveOccurred())
  1159. lPop := client.LPop("list")
  1160. Expect(lPop.Err()).NotTo(HaveOccurred())
  1161. Expect(lPop.Val()).To(Equal("one"))
  1162. lRange := client.LRange("list", 0, -1)
  1163. Expect(lRange.Err()).NotTo(HaveOccurred())
  1164. Expect(lRange.Val()).To(Equal([]string{"two", "three"}))
  1165. })
  1166. It("should LPush", func() {
  1167. lPush := client.LPush("list", "World")
  1168. Expect(lPush.Err()).NotTo(HaveOccurred())
  1169. lPush = client.LPush("list", "Hello")
  1170. Expect(lPush.Err()).NotTo(HaveOccurred())
  1171. lRange := client.LRange("list", 0, -1)
  1172. Expect(lRange.Err()).NotTo(HaveOccurred())
  1173. Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
  1174. })
  1175. It("should LPushX", func() {
  1176. lPush := client.LPush("list", "World")
  1177. Expect(lPush.Err()).NotTo(HaveOccurred())
  1178. lPushX := client.LPushX("list", "Hello")
  1179. Expect(lPushX.Err()).NotTo(HaveOccurred())
  1180. Expect(lPushX.Val()).To(Equal(int64(2)))
  1181. lPushX = client.LPushX("list2", "Hello")
  1182. Expect(lPushX.Err()).NotTo(HaveOccurred())
  1183. Expect(lPushX.Val()).To(Equal(int64(0)))
  1184. lRange := client.LRange("list", 0, -1)
  1185. Expect(lRange.Err()).NotTo(HaveOccurred())
  1186. Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
  1187. lRange = client.LRange("list2", 0, -1)
  1188. Expect(lRange.Err()).NotTo(HaveOccurred())
  1189. Expect(lRange.Val()).To(Equal([]string{}))
  1190. })
  1191. It("should LRange", func() {
  1192. rPush := client.RPush("list", "one")
  1193. Expect(rPush.Err()).NotTo(HaveOccurred())
  1194. rPush = client.RPush("list", "two")
  1195. Expect(rPush.Err()).NotTo(HaveOccurred())
  1196. rPush = client.RPush("list", "three")
  1197. Expect(rPush.Err()).NotTo(HaveOccurred())
  1198. lRange := client.LRange("list", 0, 0)
  1199. Expect(lRange.Err()).NotTo(HaveOccurred())
  1200. Expect(lRange.Val()).To(Equal([]string{"one"}))
  1201. lRange = client.LRange("list", -3, 2)
  1202. Expect(lRange.Err()).NotTo(HaveOccurred())
  1203. Expect(lRange.Val()).To(Equal([]string{"one", "two", "three"}))
  1204. lRange = client.LRange("list", -100, 100)
  1205. Expect(lRange.Err()).NotTo(HaveOccurred())
  1206. Expect(lRange.Val()).To(Equal([]string{"one", "two", "three"}))
  1207. lRange = client.LRange("list", 5, 10)
  1208. Expect(lRange.Err()).NotTo(HaveOccurred())
  1209. Expect(lRange.Val()).To(Equal([]string{}))
  1210. })
  1211. It("should LRem", func() {
  1212. rPush := client.RPush("list", "hello")
  1213. Expect(rPush.Err()).NotTo(HaveOccurred())
  1214. rPush = client.RPush("list", "hello")
  1215. Expect(rPush.Err()).NotTo(HaveOccurred())
  1216. rPush = client.RPush("list", "key")
  1217. Expect(rPush.Err()).NotTo(HaveOccurred())
  1218. rPush = client.RPush("list", "hello")
  1219. Expect(rPush.Err()).NotTo(HaveOccurred())
  1220. lRem := client.LRem("list", -2, "hello")
  1221. Expect(lRem.Err()).NotTo(HaveOccurred())
  1222. Expect(lRem.Val()).To(Equal(int64(2)))
  1223. lRange := client.LRange("list", 0, -1)
  1224. Expect(lRange.Err()).NotTo(HaveOccurred())
  1225. Expect(lRange.Val()).To(Equal([]string{"hello", "key"}))
  1226. })
  1227. It("should LSet", func() {
  1228. rPush := client.RPush("list", "one")
  1229. Expect(rPush.Err()).NotTo(HaveOccurred())
  1230. rPush = client.RPush("list", "two")
  1231. Expect(rPush.Err()).NotTo(HaveOccurred())
  1232. rPush = client.RPush("list", "three")
  1233. Expect(rPush.Err()).NotTo(HaveOccurred())
  1234. lSet := client.LSet("list", 0, "four")
  1235. Expect(lSet.Err()).NotTo(HaveOccurred())
  1236. Expect(lSet.Val()).To(Equal("OK"))
  1237. lSet = client.LSet("list", -2, "five")
  1238. Expect(lSet.Err()).NotTo(HaveOccurred())
  1239. Expect(lSet.Val()).To(Equal("OK"))
  1240. lRange := client.LRange("list", 0, -1)
  1241. Expect(lRange.Err()).NotTo(HaveOccurred())
  1242. Expect(lRange.Val()).To(Equal([]string{"four", "five", "three"}))
  1243. })
  1244. It("should LTrim", func() {
  1245. rPush := client.RPush("list", "one")
  1246. Expect(rPush.Err()).NotTo(HaveOccurred())
  1247. rPush = client.RPush("list", "two")
  1248. Expect(rPush.Err()).NotTo(HaveOccurred())
  1249. rPush = client.RPush("list", "three")
  1250. Expect(rPush.Err()).NotTo(HaveOccurred())
  1251. lTrim := client.LTrim("list", 1, -1)
  1252. Expect(lTrim.Err()).NotTo(HaveOccurred())
  1253. Expect(lTrim.Val()).To(Equal("OK"))
  1254. lRange := client.LRange("list", 0, -1)
  1255. Expect(lRange.Err()).NotTo(HaveOccurred())
  1256. Expect(lRange.Val()).To(Equal([]string{"two", "three"}))
  1257. })
  1258. It("should RPop", func() {
  1259. rPush := client.RPush("list", "one")
  1260. Expect(rPush.Err()).NotTo(HaveOccurred())
  1261. rPush = client.RPush("list", "two")
  1262. Expect(rPush.Err()).NotTo(HaveOccurred())
  1263. rPush = client.RPush("list", "three")
  1264. Expect(rPush.Err()).NotTo(HaveOccurred())
  1265. rPop := client.RPop("list")
  1266. Expect(rPop.Err()).NotTo(HaveOccurred())
  1267. Expect(rPop.Val()).To(Equal("three"))
  1268. lRange := client.LRange("list", 0, -1)
  1269. Expect(lRange.Err()).NotTo(HaveOccurred())
  1270. Expect(lRange.Val()).To(Equal([]string{"one", "two"}))
  1271. })
  1272. It("should RPopLPush", func() {
  1273. rPush := client.RPush("list", "one")
  1274. Expect(rPush.Err()).NotTo(HaveOccurred())
  1275. rPush = client.RPush("list", "two")
  1276. Expect(rPush.Err()).NotTo(HaveOccurred())
  1277. rPush = client.RPush("list", "three")
  1278. Expect(rPush.Err()).NotTo(HaveOccurred())
  1279. rPopLPush := client.RPopLPush("list", "list2")
  1280. Expect(rPopLPush.Err()).NotTo(HaveOccurred())
  1281. Expect(rPopLPush.Val()).To(Equal("three"))
  1282. lRange := client.LRange("list", 0, -1)
  1283. Expect(lRange.Err()).NotTo(HaveOccurred())
  1284. Expect(lRange.Val()).To(Equal([]string{"one", "two"}))
  1285. lRange = client.LRange("list2", 0, -1)
  1286. Expect(lRange.Err()).NotTo(HaveOccurred())
  1287. Expect(lRange.Val()).To(Equal([]string{"three"}))
  1288. })
  1289. It("should RPush", func() {
  1290. rPush := client.RPush("list", "Hello")
  1291. Expect(rPush.Err()).NotTo(HaveOccurred())
  1292. Expect(rPush.Val()).To(Equal(int64(1)))
  1293. rPush = client.RPush("list", "World")
  1294. Expect(rPush.Err()).NotTo(HaveOccurred())
  1295. Expect(rPush.Val()).To(Equal(int64(2)))
  1296. lRange := client.LRange("list", 0, -1)
  1297. Expect(lRange.Err()).NotTo(HaveOccurred())
  1298. Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
  1299. })
  1300. It("should RPushX", func() {
  1301. rPush := client.RPush("list", "Hello")
  1302. Expect(rPush.Err()).NotTo(HaveOccurred())
  1303. Expect(rPush.Val()).To(Equal(int64(1)))
  1304. rPushX := client.RPushX("list", "World")
  1305. Expect(rPushX.Err()).NotTo(HaveOccurred())
  1306. Expect(rPushX.Val()).To(Equal(int64(2)))
  1307. rPushX = client.RPushX("list2", "World")
  1308. Expect(rPushX.Err()).NotTo(HaveOccurred())
  1309. Expect(rPushX.Val()).To(Equal(int64(0)))
  1310. lRange := client.LRange("list", 0, -1)
  1311. Expect(lRange.Err()).NotTo(HaveOccurred())
  1312. Expect(lRange.Val()).To(Equal([]string{"Hello", "World"}))
  1313. lRange = client.LRange("list2", 0, -1)
  1314. Expect(lRange.Err()).NotTo(HaveOccurred())
  1315. Expect(lRange.Val()).To(Equal([]string{}))
  1316. })
  1317. })
  1318. Describe("sets", func() {
  1319. It("should SAdd", func() {
  1320. sAdd := client.SAdd("set", "Hello")
  1321. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1322. Expect(sAdd.Val()).To(Equal(int64(1)))
  1323. sAdd = client.SAdd("set", "World")
  1324. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1325. Expect(sAdd.Val()).To(Equal(int64(1)))
  1326. sAdd = client.SAdd("set", "World")
  1327. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1328. Expect(sAdd.Val()).To(Equal(int64(0)))
  1329. sMembers := client.SMembers("set")
  1330. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1331. Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"}))
  1332. })
  1333. It("should SCard", func() {
  1334. sAdd := client.SAdd("set", "Hello")
  1335. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1336. Expect(sAdd.Val()).To(Equal(int64(1)))
  1337. sAdd = client.SAdd("set", "World")
  1338. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1339. Expect(sAdd.Val()).To(Equal(int64(1)))
  1340. sCard := client.SCard("set")
  1341. Expect(sCard.Err()).NotTo(HaveOccurred())
  1342. Expect(sCard.Val()).To(Equal(int64(2)))
  1343. })
  1344. It("should SDiff", func() {
  1345. sAdd := client.SAdd("set1", "a")
  1346. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1347. sAdd = client.SAdd("set1", "b")
  1348. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1349. sAdd = client.SAdd("set1", "c")
  1350. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1351. sAdd = client.SAdd("set2", "c")
  1352. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1353. sAdd = client.SAdd("set2", "d")
  1354. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1355. sAdd = client.SAdd("set2", "e")
  1356. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1357. sDiff := client.SDiff("set1", "set2")
  1358. Expect(sDiff.Err()).NotTo(HaveOccurred())
  1359. Expect(sDiff.Val()).To(ConsistOf([]string{"a", "b"}))
  1360. })
  1361. It("should SDiffStore", func() {
  1362. sAdd := client.SAdd("set1", "a")
  1363. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1364. sAdd = client.SAdd("set1", "b")
  1365. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1366. sAdd = client.SAdd("set1", "c")
  1367. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1368. sAdd = client.SAdd("set2", "c")
  1369. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1370. sAdd = client.SAdd("set2", "d")
  1371. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1372. sAdd = client.SAdd("set2", "e")
  1373. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1374. sDiffStore := client.SDiffStore("set", "set1", "set2")
  1375. Expect(sDiffStore.Err()).NotTo(HaveOccurred())
  1376. Expect(sDiffStore.Val()).To(Equal(int64(2)))
  1377. sMembers := client.SMembers("set")
  1378. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1379. Expect(sMembers.Val()).To(ConsistOf([]string{"a", "b"}))
  1380. })
  1381. It("should SInter", func() {
  1382. sAdd := client.SAdd("set1", "a")
  1383. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1384. sAdd = client.SAdd("set1", "b")
  1385. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1386. sAdd = client.SAdd("set1", "c")
  1387. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1388. sAdd = client.SAdd("set2", "c")
  1389. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1390. sAdd = client.SAdd("set2", "d")
  1391. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1392. sAdd = client.SAdd("set2", "e")
  1393. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1394. sInter := client.SInter("set1", "set2")
  1395. Expect(sInter.Err()).NotTo(HaveOccurred())
  1396. Expect(sInter.Val()).To(Equal([]string{"c"}))
  1397. })
  1398. It("should SInterStore", func() {
  1399. sAdd := client.SAdd("set1", "a")
  1400. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1401. sAdd = client.SAdd("set1", "b")
  1402. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1403. sAdd = client.SAdd("set1", "c")
  1404. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1405. sAdd = client.SAdd("set2", "c")
  1406. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1407. sAdd = client.SAdd("set2", "d")
  1408. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1409. sAdd = client.SAdd("set2", "e")
  1410. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1411. sInterStore := client.SInterStore("set", "set1", "set2")
  1412. Expect(sInterStore.Err()).NotTo(HaveOccurred())
  1413. Expect(sInterStore.Val()).To(Equal(int64(1)))
  1414. sMembers := client.SMembers("set")
  1415. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1416. Expect(sMembers.Val()).To(Equal([]string{"c"}))
  1417. })
  1418. It("should IsMember", func() {
  1419. sAdd := client.SAdd("set", "one")
  1420. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1421. sIsMember := client.SIsMember("set", "one")
  1422. Expect(sIsMember.Err()).NotTo(HaveOccurred())
  1423. Expect(sIsMember.Val()).To(Equal(true))
  1424. sIsMember = client.SIsMember("set", "two")
  1425. Expect(sIsMember.Err()).NotTo(HaveOccurred())
  1426. Expect(sIsMember.Val()).To(Equal(false))
  1427. })
  1428. It("should SMembers", func() {
  1429. sAdd := client.SAdd("set", "Hello")
  1430. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1431. sAdd = client.SAdd("set", "World")
  1432. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1433. sMembers := client.SMembers("set")
  1434. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1435. Expect(sMembers.Val()).To(ConsistOf([]string{"Hello", "World"}))
  1436. })
  1437. It("should SMove", func() {
  1438. sAdd := client.SAdd("set1", "one")
  1439. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1440. sAdd = client.SAdd("set1", "two")
  1441. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1442. sAdd = client.SAdd("set2", "three")
  1443. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1444. sMove := client.SMove("set1", "set2", "two")
  1445. Expect(sMove.Err()).NotTo(HaveOccurred())
  1446. Expect(sMove.Val()).To(Equal(true))
  1447. sMembers := client.SMembers("set1")
  1448. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1449. Expect(sMembers.Val()).To(Equal([]string{"one"}))
  1450. sMembers = client.SMembers("set2")
  1451. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1452. Expect(sMembers.Val()).To(ConsistOf([]string{"three", "two"}))
  1453. })
  1454. It("should SPop", func() {
  1455. sAdd := client.SAdd("set", "one")
  1456. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1457. sAdd = client.SAdd("set", "two")
  1458. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1459. sAdd = client.SAdd("set", "three")
  1460. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1461. sPop := client.SPop("set")
  1462. Expect(sPop.Err()).NotTo(HaveOccurred())
  1463. Expect(sPop.Val()).NotTo(Equal(""))
  1464. sMembers := client.SMembers("set")
  1465. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1466. Expect(sMembers.Val()).To(HaveLen(2))
  1467. })
  1468. It("should SPopN", func() {
  1469. sAdd := client.SAdd("set", "one")
  1470. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1471. sAdd = client.SAdd("set", "two")
  1472. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1473. sAdd = client.SAdd("set", "three")
  1474. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1475. sAdd = client.SAdd("set", "four")
  1476. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1477. sPopN := client.SPopN("set", 1)
  1478. Expect(sPopN.Err()).NotTo(HaveOccurred())
  1479. Expect(sPopN.Val()).NotTo(Equal([]string{""}))
  1480. sMembers := client.SMembers("set")
  1481. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1482. Expect(sMembers.Val()).To(HaveLen(3))
  1483. sPopN = client.SPopN("set", 4)
  1484. Expect(sPopN.Err()).NotTo(HaveOccurred())
  1485. Expect(sPopN.Val()).To(HaveLen(3))
  1486. sMembers = client.SMembers("set")
  1487. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1488. Expect(sMembers.Val()).To(HaveLen(0))
  1489. })
  1490. It("should SRandMember and SRandMemberN", func() {
  1491. err := client.SAdd("set", "one").Err()
  1492. Expect(err).NotTo(HaveOccurred())
  1493. err = client.SAdd("set", "two").Err()
  1494. Expect(err).NotTo(HaveOccurred())
  1495. err = client.SAdd("set", "three").Err()
  1496. Expect(err).NotTo(HaveOccurred())
  1497. members, err := client.SMembers("set").Result()
  1498. Expect(err).NotTo(HaveOccurred())
  1499. Expect(members).To(HaveLen(3))
  1500. member, err := client.SRandMember("set").Result()
  1501. Expect(err).NotTo(HaveOccurred())
  1502. Expect(member).NotTo(Equal(""))
  1503. members, err = client.SRandMemberN("set", 2).Result()
  1504. Expect(err).NotTo(HaveOccurred())
  1505. Expect(members).To(HaveLen(2))
  1506. })
  1507. It("should SRem", func() {
  1508. sAdd := client.SAdd("set", "one")
  1509. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1510. sAdd = client.SAdd("set", "two")
  1511. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1512. sAdd = client.SAdd("set", "three")
  1513. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1514. sRem := client.SRem("set", "one")
  1515. Expect(sRem.Err()).NotTo(HaveOccurred())
  1516. Expect(sRem.Val()).To(Equal(int64(1)))
  1517. sRem = client.SRem("set", "four")
  1518. Expect(sRem.Err()).NotTo(HaveOccurred())
  1519. Expect(sRem.Val()).To(Equal(int64(0)))
  1520. sMembers := client.SMembers("set")
  1521. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1522. Expect(sMembers.Val()).To(ConsistOf([]string{"three", "two"}))
  1523. })
  1524. It("should SUnion", func() {
  1525. sAdd := client.SAdd("set1", "a")
  1526. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1527. sAdd = client.SAdd("set1", "b")
  1528. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1529. sAdd = client.SAdd("set1", "c")
  1530. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1531. sAdd = client.SAdd("set2", "c")
  1532. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1533. sAdd = client.SAdd("set2", "d")
  1534. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1535. sAdd = client.SAdd("set2", "e")
  1536. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1537. sUnion := client.SUnion("set1", "set2")
  1538. Expect(sUnion.Err()).NotTo(HaveOccurred())
  1539. Expect(sUnion.Val()).To(HaveLen(5))
  1540. })
  1541. It("should SUnionStore", func() {
  1542. sAdd := client.SAdd("set1", "a")
  1543. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1544. sAdd = client.SAdd("set1", "b")
  1545. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1546. sAdd = client.SAdd("set1", "c")
  1547. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1548. sAdd = client.SAdd("set2", "c")
  1549. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1550. sAdd = client.SAdd("set2", "d")
  1551. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1552. sAdd = client.SAdd("set2", "e")
  1553. Expect(sAdd.Err()).NotTo(HaveOccurred())
  1554. sUnionStore := client.SUnionStore("set", "set1", "set2")
  1555. Expect(sUnionStore.Err()).NotTo(HaveOccurred())
  1556. Expect(sUnionStore.Val()).To(Equal(int64(5)))
  1557. sMembers := client.SMembers("set")
  1558. Expect(sMembers.Err()).NotTo(HaveOccurred())
  1559. Expect(sMembers.Val()).To(HaveLen(5))
  1560. })
  1561. })
  1562. Describe("sorted sets", func() {
  1563. It("should ZAdd", func() {
  1564. added, err := client.ZAdd("zset", redis.Z{1, "one"}).Result()
  1565. Expect(err).NotTo(HaveOccurred())
  1566. Expect(added).To(Equal(int64(1)))
  1567. added, err = client.ZAdd("zset", redis.Z{1, "uno"}).Result()
  1568. Expect(err).NotTo(HaveOccurred())
  1569. Expect(added).To(Equal(int64(1)))
  1570. added, err = client.ZAdd("zset", redis.Z{2, "two"}).Result()
  1571. Expect(err).NotTo(HaveOccurred())
  1572. Expect(added).To(Equal(int64(1)))
  1573. added, err = client.ZAdd("zset", redis.Z{3, "two"}).Result()
  1574. Expect(err).NotTo(HaveOccurred())
  1575. Expect(added).To(Equal(int64(0)))
  1576. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1577. Expect(err).NotTo(HaveOccurred())
  1578. Expect(vals).To(Equal([]redis.Z{{1, "one"}, {1, "uno"}, {3, "two"}}))
  1579. })
  1580. It("should ZAdd bytes", func() {
  1581. added, err := client.ZAdd("zset", redis.Z{1, []byte("one")}).Result()
  1582. Expect(err).NotTo(HaveOccurred())
  1583. Expect(added).To(Equal(int64(1)))
  1584. added, err = client.ZAdd("zset", redis.Z{1, []byte("uno")}).Result()
  1585. Expect(err).NotTo(HaveOccurred())
  1586. Expect(added).To(Equal(int64(1)))
  1587. added, err = client.ZAdd("zset", redis.Z{2, []byte("two")}).Result()
  1588. Expect(err).NotTo(HaveOccurred())
  1589. Expect(added).To(Equal(int64(1)))
  1590. added, err = client.ZAdd("zset", redis.Z{3, []byte("two")}).Result()
  1591. Expect(err).NotTo(HaveOccurred())
  1592. Expect(added).To(Equal(int64(0)))
  1593. val, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1594. Expect(err).NotTo(HaveOccurred())
  1595. Expect(val).To(Equal([]redis.Z{{1, "one"}, {1, "uno"}, {3, "two"}}))
  1596. })
  1597. It("should ZAddNX", func() {
  1598. added, err := client.ZAddNX("zset", redis.Z{1, "one"}).Result()
  1599. Expect(err).NotTo(HaveOccurred())
  1600. Expect(added).To(Equal(int64(1)))
  1601. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1602. Expect(err).NotTo(HaveOccurred())
  1603. Expect(vals).To(Equal([]redis.Z{{1, "one"}}))
  1604. added, err = client.ZAddNX("zset", redis.Z{2, "one"}).Result()
  1605. Expect(err).NotTo(HaveOccurred())
  1606. Expect(added).To(Equal(int64(0)))
  1607. vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
  1608. Expect(err).NotTo(HaveOccurred())
  1609. Expect(vals).To(Equal([]redis.Z{{1, "one"}}))
  1610. })
  1611. It("should ZAddXX", func() {
  1612. added, err := client.ZAddXX("zset", redis.Z{1, "one"}).Result()
  1613. Expect(err).NotTo(HaveOccurred())
  1614. Expect(added).To(Equal(int64(0)))
  1615. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1616. Expect(err).NotTo(HaveOccurred())
  1617. Expect(vals).To(BeEmpty())
  1618. added, err = client.ZAdd("zset", redis.Z{1, "one"}).Result()
  1619. Expect(err).NotTo(HaveOccurred())
  1620. Expect(added).To(Equal(int64(1)))
  1621. added, err = client.ZAddXX("zset", redis.Z{2, "one"}).Result()
  1622. Expect(err).NotTo(HaveOccurred())
  1623. Expect(added).To(Equal(int64(0)))
  1624. vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
  1625. Expect(err).NotTo(HaveOccurred())
  1626. Expect(vals).To(Equal([]redis.Z{{2, "one"}}))
  1627. })
  1628. It("should ZAddCh", func() {
  1629. changed, err := client.ZAddCh("zset", redis.Z{1, "one"}).Result()
  1630. Expect(err).NotTo(HaveOccurred())
  1631. Expect(changed).To(Equal(int64(1)))
  1632. changed, err = client.ZAddCh("zset", redis.Z{1, "one"}).Result()
  1633. Expect(err).NotTo(HaveOccurred())
  1634. Expect(changed).To(Equal(int64(0)))
  1635. })
  1636. It("should ZAddNXCh", func() {
  1637. changed, err := client.ZAddNXCh("zset", redis.Z{1, "one"}).Result()
  1638. Expect(err).NotTo(HaveOccurred())
  1639. Expect(changed).To(Equal(int64(1)))
  1640. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1641. Expect(err).NotTo(HaveOccurred())
  1642. Expect(vals).To(Equal([]redis.Z{{1, "one"}}))
  1643. changed, err = client.ZAddNXCh("zset", redis.Z{2, "one"}).Result()
  1644. Expect(err).NotTo(HaveOccurred())
  1645. Expect(changed).To(Equal(int64(0)))
  1646. vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
  1647. Expect(err).NotTo(HaveOccurred())
  1648. Expect(vals).To(Equal([]redis.Z{{1, "one"}}))
  1649. })
  1650. It("should ZAddXXCh", func() {
  1651. changed, err := client.ZAddXXCh("zset", redis.Z{1, "one"}).Result()
  1652. Expect(err).NotTo(HaveOccurred())
  1653. Expect(changed).To(Equal(int64(0)))
  1654. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1655. Expect(err).NotTo(HaveOccurred())
  1656. Expect(vals).To(BeEmpty())
  1657. added, err := client.ZAdd("zset", redis.Z{1, "one"}).Result()
  1658. Expect(err).NotTo(HaveOccurred())
  1659. Expect(added).To(Equal(int64(1)))
  1660. changed, err = client.ZAddXXCh("zset", redis.Z{2, "one"}).Result()
  1661. Expect(err).NotTo(HaveOccurred())
  1662. Expect(changed).To(Equal(int64(1)))
  1663. vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
  1664. Expect(err).NotTo(HaveOccurred())
  1665. Expect(vals).To(Equal([]redis.Z{{2, "one"}}))
  1666. })
  1667. It("should ZIncr", func() {
  1668. score, err := client.ZIncr("zset", redis.Z{1, "one"}).Result()
  1669. Expect(err).NotTo(HaveOccurred())
  1670. Expect(score).To(Equal(float64(1)))
  1671. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1672. Expect(err).NotTo(HaveOccurred())
  1673. Expect(vals).To(Equal([]redis.Z{{1, "one"}}))
  1674. score, err = client.ZIncr("zset", redis.Z{1, "one"}).Result()
  1675. Expect(err).NotTo(HaveOccurred())
  1676. Expect(score).To(Equal(float64(2)))
  1677. vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
  1678. Expect(err).NotTo(HaveOccurred())
  1679. Expect(vals).To(Equal([]redis.Z{{2, "one"}}))
  1680. })
  1681. It("should ZIncrNX", func() {
  1682. score, err := client.ZIncrNX("zset", redis.Z{1, "one"}).Result()
  1683. Expect(err).NotTo(HaveOccurred())
  1684. Expect(score).To(Equal(float64(1)))
  1685. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1686. Expect(err).NotTo(HaveOccurred())
  1687. Expect(vals).To(Equal([]redis.Z{{1, "one"}}))
  1688. score, err = client.ZIncrNX("zset", redis.Z{1, "one"}).Result()
  1689. Expect(err).To(Equal(redis.Nil))
  1690. Expect(score).To(Equal(float64(0)))
  1691. vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
  1692. Expect(err).NotTo(HaveOccurred())
  1693. Expect(vals).To(Equal([]redis.Z{{1, "one"}}))
  1694. })
  1695. It("should ZIncrXX", func() {
  1696. score, err := client.ZIncrXX("zset", redis.Z{1, "one"}).Result()
  1697. Expect(err).To(Equal(redis.Nil))
  1698. Expect(score).To(Equal(float64(0)))
  1699. vals, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1700. Expect(err).NotTo(HaveOccurred())
  1701. Expect(vals).To(BeEmpty())
  1702. added, err := client.ZAdd("zset", redis.Z{1, "one"}).Result()
  1703. Expect(err).NotTo(HaveOccurred())
  1704. Expect(added).To(Equal(int64(1)))
  1705. score, err = client.ZIncrXX("zset", redis.Z{1, "one"}).Result()
  1706. Expect(err).NotTo(HaveOccurred())
  1707. Expect(score).To(Equal(float64(2)))
  1708. vals, err = client.ZRangeWithScores("zset", 0, -1).Result()
  1709. Expect(err).NotTo(HaveOccurred())
  1710. Expect(vals).To(Equal([]redis.Z{{2, "one"}}))
  1711. })
  1712. It("should ZCard", func() {
  1713. zAdd := client.ZAdd("zset", redis.Z{1, "one"})
  1714. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1715. zAdd = client.ZAdd("zset", redis.Z{2, "two"})
  1716. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1717. zCard := client.ZCard("zset")
  1718. Expect(zCard.Err()).NotTo(HaveOccurred())
  1719. Expect(zCard.Val()).To(Equal(int64(2)))
  1720. })
  1721. It("should ZCount", func() {
  1722. zAdd := client.ZAdd("zset", redis.Z{1, "one"})
  1723. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1724. zAdd = client.ZAdd("zset", redis.Z{2, "two"})
  1725. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1726. zAdd = client.ZAdd("zset", redis.Z{3, "three"})
  1727. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1728. zCount := client.ZCount("zset", "-inf", "+inf")
  1729. Expect(zCount.Err()).NotTo(HaveOccurred())
  1730. Expect(zCount.Val()).To(Equal(int64(3)))
  1731. zCount = client.ZCount("zset", "(1", "3")
  1732. Expect(zCount.Err()).NotTo(HaveOccurred())
  1733. Expect(zCount.Val()).To(Equal(int64(2)))
  1734. })
  1735. It("should ZIncrBy", func() {
  1736. zAdd := client.ZAdd("zset", redis.Z{1, "one"})
  1737. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1738. zAdd = client.ZAdd("zset", redis.Z{2, "two"})
  1739. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1740. zIncrBy := client.ZIncrBy("zset", 2, "one")
  1741. Expect(zIncrBy.Err()).NotTo(HaveOccurred())
  1742. Expect(zIncrBy.Val()).To(Equal(float64(3)))
  1743. val, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1744. Expect(err).NotTo(HaveOccurred())
  1745. Expect(val).To(Equal([]redis.Z{{2, "two"}, {3, "one"}}))
  1746. })
  1747. It("should ZInterStore", func() {
  1748. zAdd := client.ZAdd("zset1", redis.Z{1, "one"})
  1749. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1750. zAdd = client.ZAdd("zset1", redis.Z{2, "two"})
  1751. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1752. zAdd = client.ZAdd("zset2", redis.Z{1, "one"})
  1753. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1754. zAdd = client.ZAdd("zset2", redis.Z{2, "two"})
  1755. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1756. zAdd = client.ZAdd("zset3", redis.Z{3, "two"})
  1757. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1758. zInterStore := client.ZInterStore(
  1759. "out", redis.ZStore{Weights: []float64{2, 3}}, "zset1", "zset2")
  1760. Expect(zInterStore.Err()).NotTo(HaveOccurred())
  1761. Expect(zInterStore.Val()).To(Equal(int64(2)))
  1762. val, err := client.ZRangeWithScores("out", 0, -1).Result()
  1763. Expect(err).NotTo(HaveOccurred())
  1764. Expect(val).To(Equal([]redis.Z{{5, "one"}, {10, "two"}}))
  1765. })
  1766. It("should ZRange", func() {
  1767. zAdd := client.ZAdd("zset", redis.Z{1, "one"})
  1768. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1769. zAdd = client.ZAdd("zset", redis.Z{2, "two"})
  1770. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1771. zAdd = client.ZAdd("zset", redis.Z{3, "three"})
  1772. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1773. zRange := client.ZRange("zset", 0, -1)
  1774. Expect(zRange.Err()).NotTo(HaveOccurred())
  1775. Expect(zRange.Val()).To(Equal([]string{"one", "two", "three"}))
  1776. zRange = client.ZRange("zset", 2, 3)
  1777. Expect(zRange.Err()).NotTo(HaveOccurred())
  1778. Expect(zRange.Val()).To(Equal([]string{"three"}))
  1779. zRange = client.ZRange("zset", -2, -1)
  1780. Expect(zRange.Err()).NotTo(HaveOccurred())
  1781. Expect(zRange.Val()).To(Equal([]string{"two", "three"}))
  1782. })
  1783. It("should ZRangeWithScores", func() {
  1784. zAdd := client.ZAdd("zset", redis.Z{1, "one"})
  1785. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1786. zAdd = client.ZAdd("zset", redis.Z{2, "two"})
  1787. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1788. zAdd = client.ZAdd("zset", redis.Z{3, "three"})
  1789. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1790. val, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1791. Expect(err).NotTo(HaveOccurred())
  1792. Expect(val).To(Equal([]redis.Z{{1, "one"}, {2, "two"}, {3, "three"}}))
  1793. val, err = client.ZRangeWithScores("zset", 2, 3).Result()
  1794. Expect(err).NotTo(HaveOccurred())
  1795. Expect(val).To(Equal([]redis.Z{{3, "three"}}))
  1796. val, err = client.ZRangeWithScores("zset", -2, -1).Result()
  1797. Expect(err).NotTo(HaveOccurred())
  1798. Expect(val).To(Equal([]redis.Z{{2, "two"}, {3, "three"}}))
  1799. })
  1800. It("should ZRangeByScore", func() {
  1801. zAdd := client.ZAdd("zset", redis.Z{1, "one"})
  1802. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1803. zAdd = client.ZAdd("zset", redis.Z{2, "two"})
  1804. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1805. zAdd = client.ZAdd("zset", redis.Z{3, "three"})
  1806. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1807. zRangeByScore := client.ZRangeByScore("zset", redis.ZRangeBy{
  1808. Min: "-inf",
  1809. Max: "+inf",
  1810. })
  1811. Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
  1812. Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two", "three"}))
  1813. zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{
  1814. Min: "1",
  1815. Max: "2",
  1816. })
  1817. Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
  1818. Expect(zRangeByScore.Val()).To(Equal([]string{"one", "two"}))
  1819. zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{
  1820. Min: "(1",
  1821. Max: "2",
  1822. })
  1823. Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
  1824. Expect(zRangeByScore.Val()).To(Equal([]string{"two"}))
  1825. zRangeByScore = client.ZRangeByScore("zset", redis.ZRangeBy{
  1826. Min: "(1",
  1827. Max: "(2",
  1828. })
  1829. Expect(zRangeByScore.Err()).NotTo(HaveOccurred())
  1830. Expect(zRangeByScore.Val()).To(Equal([]string{}))
  1831. })
  1832. It("should ZRangeByLex", func() {
  1833. zAdd := client.ZAdd("zset", redis.Z{0, "a"})
  1834. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1835. zAdd = client.ZAdd("zset", redis.Z{0, "b"})
  1836. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1837. zAdd = client.ZAdd("zset", redis.Z{0, "c"})
  1838. zRangeByLex := client.ZRangeByLex("zset", redis.ZRangeBy{
  1839. Min: "-",
  1840. Max: "+",
  1841. })
  1842. Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
  1843. Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b", "c"}))
  1844. zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{
  1845. Min: "[a",
  1846. Max: "[b",
  1847. })
  1848. Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
  1849. Expect(zRangeByLex.Val()).To(Equal([]string{"a", "b"}))
  1850. zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{
  1851. Min: "(a",
  1852. Max: "[b",
  1853. })
  1854. Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
  1855. Expect(zRangeByLex.Val()).To(Equal([]string{"b"}))
  1856. zRangeByLex = client.ZRangeByLex("zset", redis.ZRangeBy{
  1857. Min: "(a",
  1858. Max: "(b",
  1859. })
  1860. Expect(zRangeByLex.Err()).NotTo(HaveOccurred())
  1861. Expect(zRangeByLex.Val()).To(Equal([]string{}))
  1862. })
  1863. It("should ZRangeByScoreWithScoresMap", func() {
  1864. zAdd := client.ZAdd("zset", redis.Z{1, "one"})
  1865. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1866. zAdd = client.ZAdd("zset", redis.Z{2, "two"})
  1867. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1868. zAdd = client.ZAdd("zset", redis.Z{3, "three"})
  1869. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1870. val, err := client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
  1871. Min: "-inf",
  1872. Max: "+inf",
  1873. }).Result()
  1874. Expect(err).NotTo(HaveOccurred())
  1875. Expect(val).To(Equal([]redis.Z{{1, "one"}, {2, "two"}, {3, "three"}}))
  1876. val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
  1877. Min: "1",
  1878. Max: "2",
  1879. }).Result()
  1880. Expect(err).NotTo(HaveOccurred())
  1881. Expect(val).To(Equal([]redis.Z{{1, "one"}, {2, "two"}}))
  1882. val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
  1883. Min: "(1",
  1884. Max: "2",
  1885. }).Result()
  1886. Expect(err).NotTo(HaveOccurred())
  1887. Expect(val).To(Equal([]redis.Z{{2, "two"}}))
  1888. val, err = client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{
  1889. Min: "(1",
  1890. Max: "(2",
  1891. }).Result()
  1892. Expect(err).NotTo(HaveOccurred())
  1893. Expect(val).To(Equal([]redis.Z{}))
  1894. })
  1895. It("should ZRank", func() {
  1896. zAdd := client.ZAdd("zset", redis.Z{1, "one"})
  1897. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1898. zAdd = client.ZAdd("zset", redis.Z{2, "two"})
  1899. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1900. zAdd = client.ZAdd("zset", redis.Z{3, "three"})
  1901. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1902. zRank := client.ZRank("zset", "three")
  1903. Expect(zRank.Err()).NotTo(HaveOccurred())
  1904. Expect(zRank.Val()).To(Equal(int64(2)))
  1905. zRank = client.ZRank("zset", "four")
  1906. Expect(zRank.Err()).To(Equal(redis.Nil))
  1907. Expect(zRank.Val()).To(Equal(int64(0)))
  1908. })
  1909. It("should ZRem", func() {
  1910. zAdd := client.ZAdd("zset", redis.Z{1, "one"})
  1911. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1912. zAdd = client.ZAdd("zset", redis.Z{2, "two"})
  1913. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1914. zAdd = client.ZAdd("zset", redis.Z{3, "three"})
  1915. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1916. zRem := client.ZRem("zset", "two")
  1917. Expect(zRem.Err()).NotTo(HaveOccurred())
  1918. Expect(zRem.Val()).To(Equal(int64(1)))
  1919. val, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1920. Expect(err).NotTo(HaveOccurred())
  1921. Expect(val).To(Equal([]redis.Z{{1, "one"}, {3, "three"}}))
  1922. })
  1923. It("should ZRemRangeByRank", func() {
  1924. zAdd := client.ZAdd("zset", redis.Z{1, "one"})
  1925. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1926. zAdd = client.ZAdd("zset", redis.Z{2, "two"})
  1927. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1928. zAdd = client.ZAdd("zset", redis.Z{3, "three"})
  1929. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1930. zRemRangeByRank := client.ZRemRangeByRank("zset", 0, 1)
  1931. Expect(zRemRangeByRank.Err()).NotTo(HaveOccurred())
  1932. Expect(zRemRangeByRank.Val()).To(Equal(int64(2)))
  1933. val, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1934. Expect(err).NotTo(HaveOccurred())
  1935. Expect(val).To(Equal([]redis.Z{{3, "three"}}))
  1936. })
  1937. It("should ZRemRangeByScore", func() {
  1938. zAdd := client.ZAdd("zset", redis.Z{1, "one"})
  1939. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1940. zAdd = client.ZAdd("zset", redis.Z{2, "two"})
  1941. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1942. zAdd = client.ZAdd("zset", redis.Z{3, "three"})
  1943. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1944. zRemRangeByScore := client.ZRemRangeByScore("zset", "-inf", "(2")
  1945. Expect(zRemRangeByScore.Err()).NotTo(HaveOccurred())
  1946. Expect(zRemRangeByScore.Val()).To(Equal(int64(1)))
  1947. val, err := client.ZRangeWithScores("zset", 0, -1).Result()
  1948. Expect(err).NotTo(HaveOccurred())
  1949. Expect(val).To(Equal([]redis.Z{{2, "two"}, {3, "three"}}))
  1950. })
  1951. It("should ZRemRangeByLex", func() {
  1952. zz := []redis.Z{
  1953. {0, "aaaa"},
  1954. {0, "b"},
  1955. {0, "c"},
  1956. {0, "d"},
  1957. {0, "e"},
  1958. {0, "foo"},
  1959. {0, "zap"},
  1960. {0, "zip"},
  1961. {0, "ALPHA"},
  1962. {0, "alpha"},
  1963. }
  1964. for _, z := range zz {
  1965. err := client.ZAdd("zset", z).Err()
  1966. Expect(err).NotTo(HaveOccurred())
  1967. }
  1968. n, err := client.ZRemRangeByLex("zset", "[alpha", "[omega").Result()
  1969. Expect(err).NotTo(HaveOccurred())
  1970. Expect(n).To(Equal(int64(6)))
  1971. vals, err := client.ZRange("zset", 0, -1).Result()
  1972. Expect(err).NotTo(HaveOccurred())
  1973. Expect(vals).To(Equal([]string{"ALPHA", "aaaa", "zap", "zip"}))
  1974. })
  1975. It("should ZRevRange", func() {
  1976. zAdd := client.ZAdd("zset", redis.Z{1, "one"})
  1977. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1978. zAdd = client.ZAdd("zset", redis.Z{2, "two"})
  1979. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1980. zAdd = client.ZAdd("zset", redis.Z{3, "three"})
  1981. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1982. zRevRange := client.ZRevRange("zset", 0, -1)
  1983. Expect(zRevRange.Err()).NotTo(HaveOccurred())
  1984. Expect(zRevRange.Val()).To(Equal([]string{"three", "two", "one"}))
  1985. zRevRange = client.ZRevRange("zset", 2, 3)
  1986. Expect(zRevRange.Err()).NotTo(HaveOccurred())
  1987. Expect(zRevRange.Val()).To(Equal([]string{"one"}))
  1988. zRevRange = client.ZRevRange("zset", -2, -1)
  1989. Expect(zRevRange.Err()).NotTo(HaveOccurred())
  1990. Expect(zRevRange.Val()).To(Equal([]string{"two", "one"}))
  1991. })
  1992. It("should ZRevRangeWithScoresMap", func() {
  1993. zAdd := client.ZAdd("zset", redis.Z{1, "one"})
  1994. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1995. zAdd = client.ZAdd("zset", redis.Z{2, "two"})
  1996. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1997. zAdd = client.ZAdd("zset", redis.Z{3, "three"})
  1998. Expect(zAdd.Err()).NotTo(HaveOccurred())
  1999. val, err := client.ZRevRangeWithScores("zset", 0, -1).Result()
  2000. Expect(err).NotTo(HaveOccurred())
  2001. Expect(val).To(Equal([]redis.Z{{3, "three"}, {2, "two"}, {1, "one"}}))
  2002. val, err = client.ZRevRangeWithScores("zset", 2, 3).Result()
  2003. Expect(err).NotTo(HaveOccurred())
  2004. Expect(val).To(Equal([]redis.Z{{1, "one"}}))
  2005. val, err = client.ZRevRangeWithScores("zset", -2, -1).Result()
  2006. Expect(err).NotTo(HaveOccurred())
  2007. Expect(val).To(Equal([]redis.Z{{2, "two"}, {1, "one"}}))
  2008. })
  2009. It("should ZRevRangeByScore", func() {
  2010. zadd := client.ZAdd("zset", redis.Z{1, "one"})
  2011. Expect(zadd.Err()).NotTo(HaveOccurred())
  2012. zadd = client.ZAdd("zset", redis.Z{2, "two"})
  2013. Expect(zadd.Err()).NotTo(HaveOccurred())
  2014. zadd = client.ZAdd("zset", redis.Z{3, "three"})
  2015. Expect(zadd.Err()).NotTo(HaveOccurred())
  2016. vals, err := client.ZRevRangeByScore(
  2017. "zset", redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result()
  2018. Expect(err).NotTo(HaveOccurred())
  2019. Expect(vals).To(Equal([]string{"three", "two", "one"}))
  2020. vals, err = client.ZRevRangeByScore(
  2021. "zset", redis.ZRangeBy{Max: "2", Min: "(1"}).Result()
  2022. Expect(err).NotTo(HaveOccurred())
  2023. Expect(vals).To(Equal([]string{"two"}))
  2024. vals, err = client.ZRevRangeByScore(
  2025. "zset", redis.ZRangeBy{Max: "(2", Min: "(1"}).Result()
  2026. Expect(err).NotTo(HaveOccurred())
  2027. Expect(vals).To(Equal([]string{}))
  2028. })
  2029. It("should ZRevRangeByLex", func() {
  2030. zadd := client.ZAdd("zset", redis.Z{0, "a"})
  2031. Expect(zadd.Err()).NotTo(HaveOccurred())
  2032. zadd = client.ZAdd("zset", redis.Z{0, "b"})
  2033. Expect(zadd.Err()).NotTo(HaveOccurred())
  2034. zadd = client.ZAdd("zset", redis.Z{0, "c"})
  2035. Expect(zadd.Err()).NotTo(HaveOccurred())
  2036. vals, err := client.ZRevRangeByLex(
  2037. "zset", redis.ZRangeBy{Max: "+", Min: "-"}).Result()
  2038. Expect(err).NotTo(HaveOccurred())
  2039. Expect(vals).To(Equal([]string{"c", "b", "a"}))
  2040. vals, err = client.ZRevRangeByLex(
  2041. "zset", redis.ZRangeBy{Max: "[b", Min: "(a"}).Result()
  2042. Expect(err).NotTo(HaveOccurred())
  2043. Expect(vals).To(Equal([]string{"b"}))
  2044. vals, err = client.ZRevRangeByLex(
  2045. "zset", redis.ZRangeBy{Max: "(b", Min: "(a"}).Result()
  2046. Expect(err).NotTo(HaveOccurred())
  2047. Expect(vals).To(Equal([]string{}))
  2048. })
  2049. It("should ZRevRangeByScoreWithScores", func() {
  2050. zadd := client.ZAdd("zset", redis.Z{1, "one"})
  2051. Expect(zadd.Err()).NotTo(HaveOccurred())
  2052. zadd = client.ZAdd("zset", redis.Z{2, "two"})
  2053. Expect(zadd.Err()).NotTo(HaveOccurred())
  2054. zadd = client.ZAdd("zset", redis.Z{3, "three"})
  2055. Expect(zadd.Err()).NotTo(HaveOccurred())
  2056. vals, err := client.ZRevRangeByScoreWithScores(
  2057. "zset", redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result()
  2058. Expect(err).NotTo(HaveOccurred())
  2059. Expect(vals).To(Equal([]redis.Z{{3, "three"}, {2, "two"}, {1, "one"}}))
  2060. })
  2061. It("should ZRevRangeByScoreWithScoresMap", func() {
  2062. zAdd := client.ZAdd("zset", redis.Z{1, "one"})
  2063. Expect(zAdd.Err()).NotTo(HaveOccurred())
  2064. zAdd = client.ZAdd("zset", redis.Z{2, "two"})
  2065. Expect(zAdd.Err()).NotTo(HaveOccurred())
  2066. zAdd = client.ZAdd("zset", redis.Z{3, "three"})
  2067. Expect(zAdd.Err()).NotTo(HaveOccurred())
  2068. val, err := client.ZRevRangeByScoreWithScores(
  2069. "zset", redis.ZRangeBy{Max: "+inf", Min: "-inf"}).Result()
  2070. Expect(err).NotTo(HaveOccurred())
  2071. Expect(val).To(Equal([]redis.Z{{3, "three"}, {2, "two"}, {1, "one"}}))
  2072. val, err = client.ZRevRangeByScoreWithScores(
  2073. "zset", redis.ZRangeBy{Max: "2", Min: "(1"}).Result()
  2074. Expect(err).NotTo(HaveOccurred())
  2075. Expect(val).To(Equal([]redis.Z{{2, "two"}}))
  2076. val, err = client.ZRevRangeByScoreWithScores(
  2077. "zset", redis.ZRangeBy{Max: "(2", Min: "(1"}).Result()
  2078. Expect(err).NotTo(HaveOccurred())
  2079. Expect(val).To(Equal([]redis.Z{}))
  2080. })
  2081. It("should ZRevRank", func() {
  2082. zAdd := client.ZAdd("zset", redis.Z{1, "one"})
  2083. Expect(zAdd.Err()).NotTo(HaveOccurred())
  2084. zAdd = client.ZAdd("zset", redis.Z{2, "two"})
  2085. Expect(zAdd.Err()).NotTo(HaveOccurred())
  2086. zAdd = client.ZAdd("zset", redis.Z{3, "three"})
  2087. Expect(zAdd.Err()).NotTo(HaveOccurred())
  2088. zRevRank := client.ZRevRank("zset", "one")
  2089. Expect(zRevRank.Err()).NotTo(HaveOccurred())
  2090. Expect(zRevRank.Val()).To(Equal(int64(2)))
  2091. zRevRank = client.ZRevRank("zset", "four")
  2092. Expect(zRevRank.Err()).To(Equal(redis.Nil))
  2093. Expect(zRevRank.Val()).To(Equal(int64(0)))
  2094. })
  2095. It("should ZScore", func() {
  2096. zAdd := client.ZAdd("zset", redis.Z{1.001, "one"})
  2097. Expect(zAdd.Err()).NotTo(HaveOccurred())
  2098. zScore := client.ZScore("zset", "one")
  2099. Expect(zScore.Err()).NotTo(HaveOccurred())
  2100. Expect(zScore.Val()).To(Equal(float64(1.001)))
  2101. })
  2102. It("should ZUnionStore", func() {
  2103. zAdd := client.ZAdd("zset1", redis.Z{1, "one"})
  2104. Expect(zAdd.Err()).NotTo(HaveOccurred())
  2105. zAdd = client.ZAdd("zset1", redis.Z{2, "two"})
  2106. Expect(zAdd.Err()).NotTo(HaveOccurred())
  2107. zAdd = client.ZAdd("zset2", redis.Z{1, "one"})
  2108. Expect(zAdd.Err()).NotTo(HaveOccurred())
  2109. zAdd = client.ZAdd("zset2", redis.Z{2, "two"})
  2110. Expect(zAdd.Err()).NotTo(HaveOccurred())
  2111. zAdd = client.ZAdd("zset2", redis.Z{3, "three"})
  2112. Expect(zAdd.Err()).NotTo(HaveOccurred())
  2113. zUnionStore := client.ZUnionStore(
  2114. "out", redis.ZStore{Weights: []float64{2, 3}}, "zset1", "zset2")
  2115. Expect(zUnionStore.Err()).NotTo(HaveOccurred())
  2116. Expect(zUnionStore.Val()).To(Equal(int64(3)))
  2117. val, err := client.ZRangeWithScores("out", 0, -1).Result()
  2118. Expect(err).NotTo(HaveOccurred())
  2119. Expect(val).To(Equal([]redis.Z{{5, "one"}, {9, "three"}, {10, "two"}}))
  2120. })
  2121. })
  2122. Describe("Geo add and radius search", func() {
  2123. BeforeEach(func() {
  2124. geoAdd := client.GeoAdd(
  2125. "Sicily",
  2126. &redis.GeoLocation{Longitude: 13.361389, Latitude: 38.115556, Name: "Palermo"},
  2127. &redis.GeoLocation{Longitude: 15.087269, Latitude: 37.502669, Name: "Catania"},
  2128. )
  2129. Expect(geoAdd.Err()).NotTo(HaveOccurred())
  2130. Expect(geoAdd.Val()).To(Equal(int64(2)))
  2131. })
  2132. It("should not add same geo location", func() {
  2133. geoAdd := client.GeoAdd(
  2134. "Sicily",
  2135. &redis.GeoLocation{Longitude: 13.361389, Latitude: 38.115556, Name: "Palermo"},
  2136. )
  2137. Expect(geoAdd.Err()).NotTo(HaveOccurred())
  2138. Expect(geoAdd.Val()).To(Equal(int64(0)))
  2139. })
  2140. It("should search geo radius", func() {
  2141. res, err := client.GeoRadius("Sicily", 15, 37, &redis.GeoRadiusQuery{
  2142. Radius: 200,
  2143. }).Result()
  2144. Expect(err).NotTo(HaveOccurred())
  2145. Expect(res).To(HaveLen(2))
  2146. Expect(res[0].Name).To(Equal("Palermo"))
  2147. Expect(res[1].Name).To(Equal("Catania"))
  2148. })
  2149. It("should search geo radius with options", func() {
  2150. res, err := client.GeoRadius("Sicily", 15, 37, &redis.GeoRadiusQuery{
  2151. Radius: 200,
  2152. Unit: "km",
  2153. WithGeoHash: true,
  2154. WithCoord: true,
  2155. WithDist: true,
  2156. Count: 2,
  2157. Sort: "ASC",
  2158. }).Result()
  2159. Expect(err).NotTo(HaveOccurred())
  2160. Expect(res).To(HaveLen(2))
  2161. Expect(res[1].Name).To(Equal("Palermo"))
  2162. Expect(res[1].Dist).To(Equal(190.4424))
  2163. Expect(res[1].GeoHash).To(Equal(int64(3479099956230698)))
  2164. Expect(res[1].Longitude).To(Equal(13.361389338970184))
  2165. Expect(res[1].Latitude).To(Equal(38.115556395496299))
  2166. Expect(res[0].Name).To(Equal("Catania"))
  2167. Expect(res[0].Dist).To(Equal(56.4413))
  2168. Expect(res[0].GeoHash).To(Equal(int64(3479447370796909)))
  2169. Expect(res[0].Longitude).To(Equal(15.087267458438873))
  2170. Expect(res[0].Latitude).To(Equal(37.50266842333162))
  2171. })
  2172. It("should search geo radius with WithDist=false", func() {
  2173. res, err := client.GeoRadius("Sicily", 15, 37, &redis.GeoRadiusQuery{
  2174. Radius: 200,
  2175. Unit: "km",
  2176. WithGeoHash: true,
  2177. WithCoord: true,
  2178. Count: 2,
  2179. Sort: "ASC",
  2180. }).Result()
  2181. Expect(err).NotTo(HaveOccurred())
  2182. Expect(res).To(HaveLen(2))
  2183. Expect(res[1].Name).To(Equal("Palermo"))
  2184. Expect(res[1].Dist).To(Equal(float64(0)))
  2185. Expect(res[1].GeoHash).To(Equal(int64(3479099956230698)))
  2186. Expect(res[1].Longitude).To(Equal(13.361389338970184))
  2187. Expect(res[1].Latitude).To(Equal(38.115556395496299))
  2188. Expect(res[0].Name).To(Equal("Catania"))
  2189. Expect(res[0].Dist).To(Equal(float64(0)))
  2190. Expect(res[0].GeoHash).To(Equal(int64(3479447370796909)))
  2191. Expect(res[0].Longitude).To(Equal(15.087267458438873))
  2192. Expect(res[0].Latitude).To(Equal(37.50266842333162))
  2193. })
  2194. It("should search geo radius by member with options", func() {
  2195. res, err := client.GeoRadiusByMember("Sicily", "Catania", &redis.GeoRadiusQuery{
  2196. Radius: 200,
  2197. Unit: "km",
  2198. WithGeoHash: true,
  2199. WithCoord: true,
  2200. WithDist: true,
  2201. Count: 2,
  2202. Sort: "ASC",
  2203. }).Result()
  2204. Expect(err).NotTo(HaveOccurred())
  2205. Expect(res).To(HaveLen(2))
  2206. Expect(res[0].Name).To(Equal("Catania"))
  2207. Expect(res[0].Dist).To(Equal(0.0))
  2208. Expect(res[0].GeoHash).To(Equal(int64(3479447370796909)))
  2209. Expect(res[0].Longitude).To(Equal(15.087267458438873))
  2210. Expect(res[0].Latitude).To(Equal(37.50266842333162))
  2211. Expect(res[1].Name).To(Equal("Palermo"))
  2212. Expect(res[1].Dist).To(Equal(166.2742))
  2213. Expect(res[1].GeoHash).To(Equal(int64(3479099956230698)))
  2214. Expect(res[1].Longitude).To(Equal(13.361389338970184))
  2215. Expect(res[1].Latitude).To(Equal(38.115556395496299))
  2216. })
  2217. It("should search geo radius with no results", func() {
  2218. res, err := client.GeoRadius("Sicily", 99, 37, &redis.GeoRadiusQuery{
  2219. Radius: 200,
  2220. Unit: "km",
  2221. WithGeoHash: true,
  2222. WithCoord: true,
  2223. WithDist: true,
  2224. }).Result()
  2225. Expect(err).NotTo(HaveOccurred())
  2226. Expect(res).To(HaveLen(0))
  2227. })
  2228. It("should get geo distance with unit options", func() {
  2229. // From Redis CLI, note the difference in rounding in m vs
  2230. // km on Redis itself.
  2231. //
  2232. // GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
  2233. // GEODIST Sicily Palermo Catania m
  2234. // "166274.15156960033"
  2235. // GEODIST Sicily Palermo Catania km
  2236. // "166.27415156960032"
  2237. dist, err := client.GeoDist("Sicily", "Palermo", "Catania", "km").Result()
  2238. Expect(err).NotTo(HaveOccurred())
  2239. Expect(dist).To(BeNumerically("~", 166.27, 0.01))
  2240. dist, err = client.GeoDist("Sicily", "Palermo", "Catania", "m").Result()
  2241. Expect(err).NotTo(HaveOccurred())
  2242. Expect(dist).To(BeNumerically("~", 166274.15, 0.01))
  2243. })
  2244. It("should get geo hash in string representation", func() {
  2245. hashes, err := client.GeoHash("Sicily", "Palermo", "Catania").Result()
  2246. Expect(err).NotTo(HaveOccurred())
  2247. Expect(hashes).To(ConsistOf([]string{"sqc8b49rny0", "sqdtr74hyu0"}))
  2248. })
  2249. It("should return geo position", func() {
  2250. pos, err := client.GeoPos("Sicily", "Palermo", "Catania", "NonExisting").Result()
  2251. Expect(err).NotTo(HaveOccurred())
  2252. Expect(pos).To(ConsistOf([]*redis.GeoPos{
  2253. {
  2254. Longitude: 13.361389338970184,
  2255. Latitude: 38.1155563954963,
  2256. },
  2257. {
  2258. Longitude: 15.087267458438873,
  2259. Latitude: 37.50266842333162,
  2260. },
  2261. nil,
  2262. }))
  2263. })
  2264. })
  2265. Describe("marshaling/unmarshaling", func() {
  2266. type convTest struct {
  2267. value interface{}
  2268. wanted string
  2269. dest interface{}
  2270. }
  2271. convTests := []convTest{
  2272. {nil, "", nil},
  2273. {"hello", "hello", new(string)},
  2274. {[]byte("hello"), "hello", new([]byte)},
  2275. {int(1), "1", new(int)},
  2276. {int8(1), "1", new(int8)},
  2277. {int16(1), "1", new(int16)},
  2278. {int32(1), "1", new(int32)},
  2279. {int64(1), "1", new(int64)},
  2280. {uint(1), "1", new(uint)},
  2281. {uint8(1), "1", new(uint8)},
  2282. {uint16(1), "1", new(uint16)},
  2283. {uint32(1), "1", new(uint32)},
  2284. {uint64(1), "1", new(uint64)},
  2285. {float32(1.0), "1", new(float32)},
  2286. {float64(1.0), "1", new(float64)},
  2287. {true, "1", new(bool)},
  2288. {false, "0", new(bool)},
  2289. }
  2290. It("should convert to string", func() {
  2291. for _, test := range convTests {
  2292. err := client.Set("key", test.value, 0).Err()
  2293. Expect(err).NotTo(HaveOccurred())
  2294. s, err := client.Get("key").Result()
  2295. Expect(err).NotTo(HaveOccurred())
  2296. Expect(s).To(Equal(test.wanted))
  2297. if test.dest == nil {
  2298. continue
  2299. }
  2300. err = client.Get("key").Scan(test.dest)
  2301. Expect(err).NotTo(HaveOccurred())
  2302. Expect(deref(test.dest)).To(Equal(test.value))
  2303. }
  2304. })
  2305. })
  2306. Describe("json marshaling/unmarshaling", func() {
  2307. BeforeEach(func() {
  2308. value := &numberStruct{Number: 42}
  2309. err := client.Set("key", value, 0).Err()
  2310. Expect(err).NotTo(HaveOccurred())
  2311. })
  2312. It("should marshal custom values using json", func() {
  2313. s, err := client.Get("key").Result()
  2314. Expect(err).NotTo(HaveOccurred())
  2315. Expect(s).To(Equal(`{"Number":42}`))
  2316. })
  2317. It("should scan custom values using json", func() {
  2318. value := &numberStruct{}
  2319. err := client.Get("key").Scan(value)
  2320. Expect(err).NotTo(HaveOccurred())
  2321. Expect(value.Number).To(Equal(42))
  2322. })
  2323. })
  2324. Describe("Command", func() {
  2325. It("returns map of commands", func() {
  2326. cmds, err := client.Command().Result()
  2327. Expect(err).NotTo(HaveOccurred())
  2328. Expect(len(cmds)).To(BeNumerically("~", 173, 5))
  2329. cmd := cmds["mget"]
  2330. Expect(cmd.Name).To(Equal("mget"))
  2331. Expect(cmd.Arity).To(Equal(int8(-2)))
  2332. Expect(cmd.Flags).To(Equal([]string{"readonly"}))
  2333. Expect(cmd.FirstKeyPos).To(Equal(int8(1)))
  2334. Expect(cmd.LastKeyPos).To(Equal(int8(-1)))
  2335. Expect(cmd.StepCount).To(Equal(int8(1)))
  2336. })
  2337. })
  2338. Describe("Eval", func() {
  2339. It("returns keys and values", func() {
  2340. vals, err := client.Eval(
  2341. "return {KEYS[1],ARGV[1]}",
  2342. []string{"key"},
  2343. "hello",
  2344. ).Result()
  2345. Expect(err).NotTo(HaveOccurred())
  2346. Expect(vals).To(Equal([]interface{}{"key", "hello"}))
  2347. })
  2348. })
  2349. })
  2350. type numberStruct struct {
  2351. Number int
  2352. }
  2353. func (s *numberStruct) MarshalBinary() ([]byte, error) {
  2354. return json.Marshal(s)
  2355. }
  2356. func (s *numberStruct) UnmarshalBinary(b []byte) error {
  2357. return json.Unmarshal(b, s)
  2358. }
  2359. func deref(viface interface{}) interface{} {
  2360. v := reflect.ValueOf(viface)
  2361. for v.Kind() == reflect.Ptr {
  2362. v = v.Elem()
  2363. }
  2364. return v.Interface()
  2365. }