github.com/kaydxh/golang@v0.0.131/pkg/database/redis/redis_set_test.go (about) 1 /* 2 *Copyright (c) 2022, kaydxh 3 * 4 *Permission is hereby granted, free of charge, to any person obtaining a copy 5 *of this software and associated documentation files (the "Software"), to deal 6 *in the Software without restriction, including without limitation the rights 7 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 *copies of the Software, and to permit persons to whom the Software is 9 *furnished to do so, subject to the following conditions: 10 * 11 *The above copyright notice and this permission notice shall be included in all 12 *copies or substantial portions of the Software. 13 * 14 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 *SOFTWARE. 21 */ 22 package redis_test 23 24 import ( 25 "context" 26 "testing" 27 "time" 28 ) 29 30 func TestSAdd(t *testing.T) { 31 db := GetDBOrDie() 32 33 testCases := []struct { 34 Key string 35 Values []string 36 }{ 37 { 38 Key: "set-test-1", 39 Values: []string{ 40 "values-set-test-1-1", 41 "values-set-test-1-2", 42 "values-set-test-1-3", 43 "values-set-test-1-4", 44 "values-set-test-1-5", 45 }, 46 }, 47 { 48 Key: "set-test-20", 49 Values: []string{"values-set-test-1-1", "values-set-test-20-1", "values-set-test-20-2"}, 50 }, 51 } 52 53 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 54 defer cancel() 55 56 for _, testCase := range testCases { 57 val, err := db.SAdd(ctx, testCase.Key, testCase.Values).Result() 58 if err != nil { 59 t.Fatalf("failed to SAdd, err: %v", err) 60 } 61 t.Logf("set: %v, val: %v", testCase.Key, val) 62 } 63 } 64 65 func TestSCard(t *testing.T) { 66 db := GetDBOrDie() 67 68 testCases := []struct { 69 Key string 70 }{ 71 { 72 Key: "set-test-1", 73 }, 74 { 75 Key: "set-test-20", 76 }, 77 } 78 79 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 80 defer cancel() 81 82 for _, testCase := range testCases { 83 count, err := db.SCard(ctx, testCase.Key).Result() 84 if err != nil { 85 t.Fatalf("failed to SCard, err: %v", err) 86 } 87 t.Logf("set: %v, member count: %v", testCase.Key, count) 88 } 89 } 90 91 //diff val from multi sets 92 // return the first set different elements from the other sets 93 func TestSDiff(t *testing.T) { 94 db := GetDBOrDie() 95 96 testCases := []struct { 97 Keys []string 98 }{ 99 { 100 Keys: []string{"set-test-1", "set-test-20"}, 101 }, 102 } 103 104 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 105 defer cancel() 106 107 for _, testCase := range testCases { 108 vals, err := db.SDiff(ctx, testCase.Keys...).Result() 109 if err != nil { 110 t.Fatalf("failed to SDiff, err: %v", err) 111 } 112 t.Logf("sets: %v, diff values: %v", testCase.Keys, vals) 113 } 114 } 115 116 func TestSDiffStore(t *testing.T) { 117 db := GetDBOrDie() 118 119 testCases := []struct { 120 TargetSet string 121 Keys []string 122 }{ 123 { 124 TargetSet: "target-set", 125 Keys: []string{"set-test-1", "set-test-20"}, 126 }, 127 } 128 129 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 130 defer cancel() 131 132 for _, testCase := range testCases { 133 //err: CROSSSLOT Keys in request don't hash to the same slot 134 vals, err := db.SDiffStore(ctx, testCase.TargetSet, testCase.Keys...).Result() 135 if err != nil { 136 t.Fatalf("failed to SDiffStore, err: %v", err) 137 } 138 t.Logf("sets: %v, SDiffStore values: %v", testCase.Keys, vals) 139 } 140 } 141 142 func TestSInter(t *testing.T) { 143 db := GetDBOrDie() 144 145 testCases := []struct { 146 Keys []string 147 }{ 148 { 149 Keys: []string{"set-test-1", "set-test-20"}, 150 }, 151 } 152 153 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 154 defer cancel() 155 156 for _, testCase := range testCases { 157 vals, err := db.SInter(ctx, testCase.Keys...).Result() 158 if err != nil { 159 t.Fatalf("failed to SDiffStore, err: %v", err) 160 } 161 t.Logf("sets: %v, SInter values: %v", testCase.Keys, vals) 162 } 163 } 164 165 func TestSMembers(t *testing.T) { 166 db := GetDBOrDie() 167 168 testCases := []struct { 169 Key string 170 }{ 171 { 172 Key: "set-test-1", 173 }, 174 { 175 Key: "set-test-20", 176 }, 177 } 178 179 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 180 defer cancel() 181 182 for _, testCase := range testCases { 183 vals, err := db.SMembers(ctx, testCase.Key).Result() 184 if err != nil { 185 t.Fatalf("failed to SMembers, err: %v", err) 186 } 187 t.Logf("sets: %v, SMembers values: %v", testCase.Key, vals) 188 } 189 } 190 191 func TestSInterStore(t *testing.T) { 192 db := GetDBOrDie() 193 194 testCases := []struct { 195 TargetSet string 196 Keys []string 197 }{ 198 { 199 TargetSet: "target-inter-set", 200 Keys: []string{"set-test-1", "set-test-20"}, 201 }, 202 } 203 204 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 205 defer cancel() 206 207 for _, testCase := range testCases { 208 //err: CROSSSLOT Keys in request don't hash to the same slot 209 vals, err := db.SInterStore(ctx, testCase.TargetSet, testCase.Keys...).Result() 210 if err != nil { 211 t.Fatalf("failed to SInterStore, err: %v", err) 212 } 213 t.Logf("sets: %v, SInterStore values: %v", testCase.Keys, vals) 214 } 215 } 216 217 //delete values from set 218 func TestSRem(t *testing.T) { 219 db := GetDBOrDie() 220 221 testCases := []struct { 222 Key string 223 Values []string 224 }{ 225 { 226 Key: "set-test-1", 227 Values: []string{"values-set-test-1-2"}, 228 }, 229 } 230 231 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 232 defer cancel() 233 234 for _, testCase := range testCases { 235 //err: CROSSSLOT Keys in request don't hash to the same slot 236 vals, err := db.SRem(ctx, testCase.Key, testCase.Values).Result() 237 if err != nil { 238 t.Fatalf("failed to SRem, err: %v", err) 239 } 240 t.Logf("sets: %v, SRem values: %v", testCase.Key, vals) 241 } 242 } 243 244 // check value is in set 245 //need redis server version >= 6.2.0 246 func TestSMIsMember(t *testing.T) { 247 db := GetDBOrDie() 248 249 testCases := []struct { 250 Key string 251 Values []string 252 }{ 253 { 254 Key: "set-test-1", 255 Values: []string{"values-set-test-1-2"}, 256 }, 257 } 258 259 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 260 defer cancel() 261 262 for _, testCase := range testCases { 263 //err: CROSSSLOT Keys in request don't hash to the same slot 264 vals, err := db.SMIsMember(ctx, testCase.Key, testCase.Values).Result() 265 if err != nil { 266 t.Fatalf("failed to SMIsMember, err: %v", err) 267 } 268 t.Logf("sets: %v, SMIsMember values: %v", testCase.Key, vals) 269 } 270 } 271 272 //random delete elemnt from set 273 func TestSPop(t *testing.T) { 274 db := GetDBOrDie() 275 276 testCases := []struct { 277 Key string 278 }{ 279 { 280 Key: "set-test-1", 281 }, 282 } 283 284 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 285 defer cancel() 286 287 for _, testCase := range testCases { 288 val, err := db.SPop(ctx, testCase.Key).Result() 289 if err != nil { 290 t.Fatalf("failed to SPop, err: %v", err) 291 } 292 t.Logf("sets: %v, SPop value: %v", testCase.Key, val) 293 } 294 } 295 296 // random get value from set 297 func TestSRandMember(t *testing.T) { 298 db := GetDBOrDie() 299 300 testCases := []struct { 301 Key string 302 }{ 303 { 304 Key: "set-test-1", 305 }, 306 } 307 308 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 309 defer cancel() 310 311 for _, testCase := range testCases { 312 val, err := db.SRandMember(ctx, testCase.Key).Result() 313 if err != nil { 314 t.Fatalf("failed to SRandMember, err: %v", err) 315 } 316 t.Logf("sets: %v, SRandMember value: %v", testCase.Key, val) 317 } 318 } 319 320 // random get value from set 321 func TestSRandMemberN(t *testing.T) { 322 db := GetDBOrDie() 323 324 testCases := []struct { 325 Key string 326 Count int64 327 }{ 328 { 329 Key: "set-test-1", 330 Count: 2, 331 }, 332 } 333 334 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 335 defer cancel() 336 337 for _, testCase := range testCases { 338 vals, err := db.SRandMemberN(ctx, testCase.Key, testCase.Count).Result() 339 if err != nil { 340 t.Fatalf("failed to SRandMemberN, err: %v", err) 341 } 342 t.Logf("sets: %v, SRandMemberN values: %v", testCase.Key, vals) 343 } 344 } 345 346 func TestSUnion(t *testing.T) { 347 db := GetDBOrDie() 348 349 testCases := []struct { 350 Keys []string 351 }{ 352 { 353 Keys: []string{"set-test-1", "set-test-20"}, 354 }, 355 } 356 357 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 358 defer cancel() 359 360 for _, testCase := range testCases { 361 //err: CROSSSLOT Keys in request don't hash to the same slot 362 vals, err := db.SUnion(ctx, testCase.Keys...).Result() 363 if err != nil { 364 t.Fatalf("failed to SUnion, err: %v", err) 365 } 366 t.Logf("sets: %v, SUnion values: %v", testCase.Keys, vals) 367 } 368 } 369 370 func TestSMove(t *testing.T) { 371 db := GetDBOrDie() 372 373 testCases := []struct { 374 Source string 375 Destination string 376 Value string 377 }{ 378 { 379 Source: "set-test-1", 380 Destination: "set-test-20", 381 Value: "values-set-test-1-5", 382 }, 383 } 384 385 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 386 defer cancel() 387 388 for _, testCase := range testCases { 389 //err: CROSSSLOT Keys in request don't hash to the same slot 390 ok, err := db.SMove(ctx, testCase.Source, testCase.Destination, testCase.Value).Result() 391 if err != nil { 392 t.Fatalf("failed to SUnion, err: %v", err) 393 } 394 t.Logf("move sets: %v to %v, SUnion values: %v", testCase.Source, testCase.Destination, ok) 395 } 396 } 397 398 func TestSUnionStore(t *testing.T) { 399 db := GetDBOrDie() 400 401 testCases := []struct { 402 TargetSet string 403 Keys []string 404 }{ 405 { 406 TargetSet: "target-set", 407 Keys: []string{"set-test-1", "set-test-20"}, 408 }, 409 } 410 411 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 412 defer cancel() 413 414 for _, testCase := range testCases { 415 //err: CROSSSLOT Keys in request don't hash to the same slot 416 val, err := db.SUnionStore(ctx, testCase.TargetSet, testCase.Keys...).Result() 417 if err != nil { 418 t.Fatalf("failed to SUnionStore, err: %v", err) 419 } 420 t.Logf("sets: %v, SUnionStore values: %v", testCase.Keys, val) 421 } 422 } 423 424 func TestSScan(t *testing.T) { 425 db := GetDBOrDie() 426 427 testCases := []struct { 428 Key string 429 Cursor uint64 430 Match string 431 Count int64 432 }{ 433 { 434 Key: "set-test-1", 435 Cursor: 0, 436 Match: "*set*", 437 Count: 2, 438 }, 439 } 440 441 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 442 defer cancel() 443 444 for _, testCase := range testCases { 445 //err: CROSSSLOT Keys in request don't hash to the same slot 446 keys, cursor, err := db.SScan(ctx, testCase.Key, testCase.Cursor, testCase.Match, testCase.Count).Result() 447 if err != nil { 448 t.Fatalf("failed to SScan, err: %v", err) 449 } 450 t.Logf("sets: %v,SScan keys: %v, cursor: %v", testCase.Key, keys, cursor) 451 } 452 }