github.com/therealbill/libredis@v0.0.0-20161227004305-7d50abda5ccf/client/lists_test.go (about) 1 package client 2 3 import ( 4 "testing" 5 ) 6 7 func TestBLPop(t *testing.T) { 8 r.Del("key") 9 result, err := r.BLPop([]string{"key"}, 1) 10 if err != nil { 11 t.Error(err) 12 } 13 if len(result) != 0 { 14 t.Fail() 15 } 16 r.LPush("key", "value") 17 result, err = r.BLPop([]string{"key"}, 0) 18 if err != nil { 19 t.Error(err) 20 } 21 if len(result) == 0 { 22 t.Fail() 23 } 24 if result[0] != "key" || result[1] != "value" { 25 t.Fail() 26 } 27 } 28 29 func TestBRPop(t *testing.T) { 30 r.Del("key") 31 result, err := r.BRPop([]string{"key"}, 1) 32 if err != nil { 33 t.Error(err) 34 } 35 if len(result) != 0 { 36 t.Fail() 37 } 38 r.RPush("key", "value") 39 result, _ = r.BRPop([]string{"key"}, 1) 40 if result == nil { 41 t.Fail() 42 } 43 if result[0] != "key" || result[1] != "value" { 44 t.Fail() 45 } 46 } 47 48 func TestBRPopLPush(t *testing.T) { 49 r.Del("key", "key1") 50 result, err := r.BRPopLPush("key", "key1", 1) 51 if err != nil { 52 t.Error(err) 53 } else if result != nil { 54 t.Fail() 55 } 56 r.RPush("key", "value") 57 result, _ = r.BRPopLPush("key", "key1", 1) 58 if result == nil { 59 t.Fail() 60 } 61 } 62 63 func TestLIndex(t *testing.T) { 64 r.Del("key") 65 r.LPush("key", "world", "hello") 66 if value, err := r.LIndex("key", 0); err != nil { 67 t.Error(err) 68 } else if string(value) != "hello" { 69 t.Fail() 70 } 71 if value, err := r.LIndex("key", -1); err != nil { 72 t.Error(err) 73 } else if string(value) != "world" { 74 t.Fail() 75 } 76 if value, err := r.LIndex("key", 3); err != nil { 77 t.Error(err) 78 } else if value != nil { 79 t.Fail() 80 } 81 } 82 83 func TestLInsert(t *testing.T) { 84 r.Del("key") 85 r.RPush("key", "hello", "world") 86 if n, err := r.LInsert("key", "before", "world", "three"); err != nil { 87 t.Error(err) 88 } else if n != 3 { 89 t.Fail() 90 } 91 } 92 93 func TestLLen(t *testing.T) { 94 r.Del("key") 95 r.RPush("key", "hello", "world") 96 if n, err := r.LLen("key"); err != nil { 97 t.Error(err) 98 } else if n != 2 { 99 t.Fail() 100 } 101 } 102 103 func TestLPop(t *testing.T) { 104 r.Del("key") 105 r.RPush("key", "one", "two", "three") 106 if value, err := r.LPop("key"); err != nil { 107 t.Error(err) 108 } else if string(value) != "one" { 109 t.Fail() 110 } 111 r.Del("key") 112 if value, _ := r.LPop("key"); value != nil { 113 t.Fail() 114 } 115 } 116 117 func TestLPush(t *testing.T) { 118 r.Del("key") 119 if n, err := r.LPush("key", "value"); err != nil { 120 t.Error(err) 121 } else if n != 1 { 122 t.Fail() 123 } 124 } 125 126 func BenchmarkLPush(b *testing.B) { 127 r.Del("key") 128 for i := 0; i < b.N; i++ { 129 r.LPush("key", "value") 130 } 131 } 132 133 func TestLPushx(t *testing.T) { 134 r.Del("key") 135 if n, err := r.LPushx("key", "value"); err != nil { 136 t.Error(err) 137 } else if n != 0 { 138 t.Fail() 139 } 140 r.LPush("key", "value") 141 if n, _ := r.LPushx("key", "value"); n != 2 { 142 t.Fail() 143 } 144 } 145 146 func TestLRange(t *testing.T) { 147 r.Del("key") 148 r.RPush("key", "one", "two", "three") 149 if data, err := r.LRange("key", 0, 0); err != nil { 150 t.Error(err) 151 } else if len(data) != 1 { 152 t.Fail() 153 } else if data[0] != "one" { 154 t.Fail() 155 } 156 if data, _ := r.LRange("key", 5, 10); len(data) != 0 { 157 t.Fail() 158 } 159 } 160 161 func BenchmarkLRange(b *testing.B) { 162 r.Del("key") 163 r.RPush("key", "one", "two", "three") 164 for i := 0; i < b.N; i++ { 165 r.LRange("key", 0, 10) 166 } 167 } 168 169 func TestLRem(t *testing.T) { 170 r.Del("key") 171 r.RPush("key", "hello", "hello", "foo", "hello") 172 if n, err := r.LRem("key", -2, "hello"); err != nil { 173 t.Error(err) 174 } else if n != 2 { 175 t.Fail() 176 } 177 } 178 179 func TestLSet(t *testing.T) { 180 r.Del("key") 181 r.RPush("key", "value") 182 if err := r.LSet("key", 0, "value2"); err != nil { 183 t.Error(err) 184 } 185 if err := r.LSet("key", 1, "value"); err == nil { 186 t.Fail() 187 } 188 } 189 190 func TestLTrim(t *testing.T) { 191 r.Del("key") 192 r.RPush("key", "one", "two", "three") 193 if err := r.LTrim("key", 1, -1); err != nil { 194 t.Error(err) 195 } 196 } 197 198 func TestRPop(t *testing.T) { 199 r.Del("key") 200 r.RPush("key", "one", "two", "three") 201 if value, err := r.RPop("key"); err != nil { 202 t.Error(err) 203 } else if string(value) != "three" { 204 t.Fail() 205 } 206 r.Del("key") 207 if value, _ := r.RPop("key"); value != nil { 208 t.Fail() 209 } 210 } 211 212 func TestRPopLPush(t *testing.T) { 213 r.Del("key") 214 if value, err := r.RPopLPush("key", "key"); err != nil { 215 t.Error(err) 216 } else if value != nil { 217 t.Fail() 218 } 219 } 220 221 func TestRPush(t *testing.T) { 222 r.Del("key") 223 if n, err := r.RPush("key", "one", "two"); err != nil { 224 t.Error(err) 225 } else if n != 2 { 226 t.Fail() 227 } 228 } 229 230 func TestRPushx(t *testing.T) { 231 r.Del("key") 232 if n, err := r.RPushx("key", "value"); err != nil { 233 t.Error(err) 234 } else if n != 0 { 235 t.Fail() 236 } 237 }