github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/storage/redis/idalloc_test.go (about) 1 package redis 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/redis/go-redis/v9" 8 ) 9 10 func TestNew(t *testing.T) { 11 InitTestRedis() 12 13 type args struct { 14 conn *redis.Client 15 } 16 tests := []struct { 17 name string 18 args args 19 want *IDAlloc 20 }{ 21 { 22 name: "test new id alloc", 23 args: args{ 24 conn: Client, 25 }, 26 want: &IDAlloc{ 27 client: Client, 28 }, 29 }, 30 } 31 for _, tt := range tests { 32 t.Run(tt.name, func(t *testing.T) { 33 if got := NewIDAlloc(tt.args.conn); !reflect.DeepEqual(got, tt.want) { 34 t.Errorf("New() = %v, want %v", got, tt.want) 35 } 36 }) 37 } 38 } 39 40 func TestIdAlloc_GetCurrentID(t *testing.T) { 41 type fields struct { 42 redisClient *redis.Client 43 } 44 tests := []struct { 45 name string 46 fields fields 47 want int64 48 wantErr bool 49 }{ 50 // TODO: Add test cases. 51 } 52 for _, tt := range tests { 53 t.Run(tt.name, func(t *testing.T) { 54 ia := &IDAlloc{ 55 client: tt.fields.redisClient, 56 } 57 got, err := ia.GetCurrentID("user_id") 58 if (err != nil) != tt.wantErr { 59 t.Errorf("GetCurrentID() error = %v, wantErr %v", err, tt.wantErr) 60 return 61 } 62 if got != tt.want { 63 t.Errorf("GetCurrentID() got = %v, want %v", got, tt.want) 64 } 65 }) 66 } 67 } 68 69 func TestIdAlloc_GetKey(t *testing.T) { 70 type fields struct { 71 redisClient *redis.Client 72 } 73 tests := []struct { 74 name string 75 fields fields 76 want string 77 }{ 78 // TODO: Add test cases. 79 } 80 for _, tt := range tests { 81 t.Run(tt.name, func(t *testing.T) { 82 ia := &IDAlloc{ 83 client: tt.fields.redisClient, 84 } 85 if got := ia.GetKey("user_id"); got != tt.want { 86 t.Errorf("GetKey() = %v, want %v", got, tt.want) 87 } 88 }) 89 } 90 } 91 92 // nolint 93 func TestIdAlloc_GetNewID(t *testing.T) { 94 type fields struct { 95 key string 96 redisClient *redis.Client 97 } 98 type args struct { 99 step int64 100 } 101 tests := []struct { 102 name string 103 fields fields 104 args args 105 want int64 106 wantErr bool 107 }{ 108 // TODO: Add test cases. 109 } 110 for _, tt := range tests { 111 t.Run(tt.name, func(t *testing.T) { 112 ia := &IDAlloc{ 113 client: tt.fields.redisClient, 114 } 115 got, err := ia.GetNewID("user_id", tt.args.step) 116 if (err != nil) != tt.wantErr { 117 t.Errorf("GetNewID() error = %v, wantErr %v", err, tt.wantErr) 118 return 119 } 120 if got != tt.want { 121 t.Errorf("GetNewID() got = %v, want %v", got, tt.want) 122 } 123 }) 124 } 125 }