Best Rod code snippet using utils.RandString
keys_test.go
Source:keys_test.go
...9 "time"10)11func TestExists(t *testing.T) {12 FlushAll(testDB, [][]byte{})13 key := utils.RandString(10)14 value := utils.RandString(10)15 Set(testDB, utils.ToBytesList(key, value))16 result := Exists(testDB, utils.ToBytesList(key))17 asserts.AssertIntReply(t, result, 1)18 key = utils.RandString(10)19 result = Exists(testDB, utils.ToBytesList(key))20 asserts.AssertIntReply(t, result, 0)21}22func TestType(t *testing.T) {23 FlushAll(testDB, [][]byte{})24 key := utils.RandString(10)25 value := utils.RandString(10)26 Set(testDB, utils.ToBytesList(key, value))27 result := Type(testDB, utils.ToBytesList(key))28 asserts.AssertStatusReply(t, result, "string")29 Del(testDB, utils.ToBytesList(key))30 result = Type(testDB, utils.ToBytesList(key))31 asserts.AssertStatusReply(t, result, "none")32 RPush(testDB, utils.ToBytesList(key, value))33 result = Type(testDB, utils.ToBytesList(key))34 asserts.AssertStatusReply(t, result, "list")35 Del(testDB, utils.ToBytesList(key))36 HSet(testDB, utils.ToBytesList(key, key, value))37 result = Type(testDB, utils.ToBytesList(key))38 asserts.AssertStatusReply(t, result, "hash")39 Del(testDB, utils.ToBytesList(key))40 SAdd(testDB, utils.ToBytesList(key, value))41 result = Type(testDB, utils.ToBytesList(key))42 asserts.AssertStatusReply(t, result, "set")43 Del(testDB, utils.ToBytesList(key))44 ZAdd(testDB, utils.ToBytesList(key, "1", value))45 result = Type(testDB, utils.ToBytesList(key))46 asserts.AssertStatusReply(t, result, "zset")47}48func TestRename(t *testing.T) {49 FlushAll(testDB, [][]byte{})50 key := utils.RandString(10)51 value := utils.RandString(10)52 newKey := key + utils.RandString(2)53 Set(testDB, utils.ToBytesList(key, value, "ex", "1000"))54 result := Rename(testDB, utils.ToBytesList(key, newKey))55 if _, ok := result.(*reply.OkReply); !ok {56 t.Error("expect ok")57 return58 }59 result = Exists(testDB, utils.ToBytesList(key))60 asserts.AssertIntReply(t, result, 0)61 result = Exists(testDB, utils.ToBytesList(newKey))62 asserts.AssertIntReply(t, result, 1)63 // check ttl64 result = TTL(testDB, utils.ToBytesList(newKey))65 intResult, ok := result.(*reply.IntReply)66 if !ok {67 t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))68 return69 }70 if intResult.Code <= 0 {71 t.Errorf("expected ttl more than 0, actual: %d", intResult.Code)72 return73 }74}75func TestRenameNx(t *testing.T) {76 FlushAll(testDB, [][]byte{})77 key := utils.RandString(10)78 value := utils.RandString(10)79 newKey := key + utils.RandString(2)80 Set(testDB, utils.ToBytesList(key, value, "ex", "1000"))81 result := RenameNx(testDB, utils.ToBytesList(key, newKey))82 asserts.AssertIntReply(t, result, 1)83 result = Exists(testDB, utils.ToBytesList(key))84 asserts.AssertIntReply(t, result, 0)85 result = Exists(testDB, utils.ToBytesList(newKey))86 asserts.AssertIntReply(t, result, 1)87 result = TTL(testDB, utils.ToBytesList(newKey))88 intResult, ok := result.(*reply.IntReply)89 if !ok {90 t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))91 return92 }93 if intResult.Code <= 0 {94 t.Errorf("expected ttl more than 0, actual: %d", intResult.Code)95 return96 }97}98func TestTTL(t *testing.T) {99 FlushAll(testDB, [][]byte{})100 key := utils.RandString(10)101 value := utils.RandString(10)102 Set(testDB, utils.ToBytesList(key, value))103 result := Expire(testDB, utils.ToBytesList(key, "1000"))104 asserts.AssertIntReply(t, result, 1)105 result = TTL(testDB, utils.ToBytesList(key))106 intResult, ok := result.(*reply.IntReply)107 if !ok {108 t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))109 return110 }111 if intResult.Code <= 0 {112 t.Errorf("expected ttl more than 0, actual: %d", intResult.Code)113 return114 }115 result = Persist(testDB, utils.ToBytesList(key))116 asserts.AssertIntReply(t, result, 1)117 result = TTL(testDB, utils.ToBytesList(key))118 asserts.AssertIntReply(t, result, -1)119 result = PExpire(testDB, utils.ToBytesList(key, "1000000"))120 asserts.AssertIntReply(t, result, 1)121 result = PTTL(testDB, utils.ToBytesList(key))122 intResult, ok = result.(*reply.IntReply)123 if !ok {124 t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))125 return126 }127 if intResult.Code <= 0 {128 t.Errorf("expected ttl more than 0, actual: %d", intResult.Code)129 return130 }131}132func TestExpireAt(t *testing.T) {133 FlushAll(testDB, [][]byte{})134 key := utils.RandString(10)135 value := utils.RandString(10)136 Set(testDB, utils.ToBytesList(key, value))137 expireAt := time.Now().Add(time.Minute).Unix()138 result := ExpireAt(testDB, utils.ToBytesList(key, strconv.FormatInt(expireAt, 10)))139 asserts.AssertIntReply(t, result, 1)140 result = TTL(testDB, utils.ToBytesList(key))141 intResult, ok := result.(*reply.IntReply)142 if !ok {143 t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))144 return145 }146 if intResult.Code <= 0 {147 t.Errorf("expected ttl more than 0, actual: %d", intResult.Code)148 return149 }150 expireAt = time.Now().Add(time.Minute).Unix()151 result = PExpireAt(testDB, utils.ToBytesList(key, strconv.FormatInt(expireAt*1000, 10)))152 asserts.AssertIntReply(t, result, 1)153 result = TTL(testDB, utils.ToBytesList(key))154 intResult, ok = result.(*reply.IntReply)155 if !ok {156 t.Error(fmt.Sprintf("expected int reply, actually %s", result.ToBytes()))157 return158 }159 if intResult.Code <= 0 {160 t.Errorf("expected ttl more than 0, actual: %d", intResult.Code)161 return162 }163}164func TestKeys(t *testing.T) {165 FlushAll(testDB, [][]byte{})166 key := utils.RandString(10)167 value := utils.RandString(10)168 Set(testDB, utils.ToBytesList(key, value))169 Set(testDB, utils.ToBytesList("a:"+key, value))170 Set(testDB, utils.ToBytesList("b:"+key, value))171 result := Keys(testDB, utils.ToBytesList("*"))172 asserts.AssertMultiBulkReplySize(t, result, 3)173 result = Keys(testDB, utils.ToBytesList("a:*"))174 asserts.AssertMultiBulkReplySize(t, result, 1)175 result = Keys(testDB, utils.ToBytesList("?:*"))176 asserts.AssertMultiBulkReplySize(t, result, 2)177}...
aof_test.go
Source:aof_test.go
...30 cursor := 031 for i := 0; i < size; i++ {32 key := strconv.Itoa(cursor)33 cursor++34 Set(aofWriteDB, utils.ToBytesList(key, utils.RandString(8), "EX", "10000"))35 keys = append(keys, key)36 }37 for i := 0; i < size; i++ {38 key := strconv.Itoa(cursor)39 cursor++40 RPush(aofWriteDB, utils.ToBytesList(key, utils.RandString(8)))41 keys = append(keys, key)42 }43 for i := 0; i < size; i++ {44 key := strconv.Itoa(cursor)45 cursor++46 HSet(aofWriteDB, utils.ToBytesList(key, utils.RandString(8), utils.RandString(8)))47 keys = append(keys, key)48 }49 for i := 0; i < size; i++ {50 key := strconv.Itoa(cursor)51 cursor++52 SAdd(aofWriteDB, utils.ToBytesList(key, utils.RandString(8)))53 keys = append(keys, key)54 }55 for i := 0; i < size; i++ {56 key := strconv.Itoa(cursor)57 cursor++58 ZAdd(aofWriteDB, utils.ToBytesList(key, "10", utils.RandString(8)))59 keys = append(keys, key)60 }61 aofWriteDB.Close() // wait for aof finished62 aofReadDB := MakeDB() // start new db and read aof file63 for _, key := range keys {64 expect, ok := aofWriteDB.GetEntity(key)65 if !ok {66 t.Errorf("key not found in origin: %s", key)67 continue68 }69 actual, ok := aofReadDB.GetEntity(key)70 if !ok {71 t.Errorf("key not found: %s", key)72 continue73 }74 expectData := EntityToCmd(key, expect).ToBytes()75 actualData := EntityToCmd(key, actual).ToBytes()76 if !utils.BytesEquals(expectData, actualData) {77 t.Errorf("wrong value of key: %s", key)78 }79 }80 aofReadDB.Close()81}82func TestRewriteAOF(t *testing.T) {83 tmpDir, err := ioutil.TempDir("", "godis")84 if err != nil {85 t.Error(err)86 return87 }88 aofFilename := path.Join(tmpDir, "a.aof")89 defer func() {90 _ = os.Remove(aofFilename)91 }()92 config.Properties = &config.ServerProperties{93 AppendOnly: true,94 AppendFilename: aofFilename,95 }96 aofWriteDB := MakeDB()97 size := 198 keys := make([]string, 0)99 ttlKeys := make([]string, 0)100 cursor := 0101 for i := 0; i < size; i++ {102 key := "str" + strconv.Itoa(cursor)103 cursor++104 Set(aofWriteDB, utils.ToBytesList(key, utils.RandString(8)))105 Set(aofWriteDB, utils.ToBytesList(key, utils.RandString(8)))106 keys = append(keys, key)107 }108 // test ttl109 for i := 0; i < size; i++ {110 key := "str" + strconv.Itoa(cursor)111 cursor++112 Set(aofWriteDB, utils.ToBytesList(key, utils.RandString(8), "EX", "1000"))113 ttlKeys = append(ttlKeys, key)114 }115 for i := 0; i < size; i++ {116 key := "list" + strconv.Itoa(cursor)117 cursor++118 RPush(aofWriteDB, utils.ToBytesList(key, utils.RandString(8)))119 RPush(aofWriteDB, utils.ToBytesList(key, utils.RandString(8)))120 keys = append(keys, key)121 }122 for i := 0; i < size; i++ {123 key := "hash" + strconv.Itoa(cursor)124 cursor++125 field := utils.RandString(8)126 HSet(aofWriteDB, utils.ToBytesList(key, field, utils.RandString(8)))127 HSet(aofWriteDB, utils.ToBytesList(key, field, utils.RandString(8)))128 keys = append(keys, key)129 }130 for i := 0; i < size; i++ {131 key := "set" + strconv.Itoa(cursor)132 cursor++133 member := utils.RandString(8)134 SAdd(aofWriteDB, utils.ToBytesList(key, member))135 SAdd(aofWriteDB, utils.ToBytesList(key, member))136 keys = append(keys, key)137 }138 for i := 0; i < size; i++ {139 key := "zset" + strconv.Itoa(cursor)140 cursor++141 ZAdd(aofWriteDB, utils.ToBytesList(key, "10", utils.RandString(8)))142 keys = append(keys, key)143 }144 time.Sleep(time.Second) // wait for async goroutine finish its job145 aofWriteDB.aofRewrite()146 aofWriteDB.Close() // wait for aof finished147 aofReadDB := MakeDB() // start new db and read aof file148 for _, key := range keys {149 expect, ok := aofWriteDB.GetEntity(key)150 if !ok {151 t.Errorf("key not found in origin: %s", key)152 continue153 }154 actual, ok := aofReadDB.GetEntity(key)155 if !ok {...
jwt_test.go
Source:jwt_test.go
...18 var tests []testsStruct19 for i := 1; i < 10; i++ {20 var userBase models.UserBase21 userBase.UID = uuid.New().String()22 userBase.Nickname = utils.RandString(6)23 userBase.Avatar = "https://ruan.co/" + utils.RandString(6)24 userBase.Gender = rand.Intn(2)25 userBase.Signature = utils.RandString(128)26 tests = append(tests, testsStruct{27 name: "randData",28 args: args{&userBase},29 })30 }31 for _, tt := range tests {32 t.Run(tt.name, func(t *testing.T) {33 _, err := GenerateToken(tt.args.base)34 if err != nil {35 t.Errorf("GenerateToken() error = %v", err)36 return37 }38 })39 }40}41// TestParseToken 解ætoken42func TestParseToken(t *testing.T) {43 oldToken := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NzcyNjM2ODYsImlhdCI6MTU3NzI2MzY3NiwidWlkIjoidWlkIiwibmlja25hbWUiOiJuaWNrbmFtZSIsImF2YXRhciI6ImF2YXRhciIsImdlbmRlciI6ImdlbmRlciIsInNpZ25hdHVyZSI6InNpZ25hdHVyZSIsInBlcm1pc3Npb25zIjpudWxsfQ.jUXsL4WMM2lJKu56DMHpHTwtPGvAixQwfxxlyI9X0bM"44 _, err := ParseToken(oldToken)45 if err == nil {46 t.Error("testing oldToken is not error")47 return48 }49 var userBase models.UserBase50 userBase.UID = uuid.New().String()51 userBase.Nickname = utils.RandString(6)52 userBase.Avatar = "https://ruan.co/" + utils.RandString(6)53 userBase.Gender = rand.Intn(2)54 userBase.Signature = utils.RandString(128)55 generateToken, err := GenerateToken(&userBase)56 if err != nil {57 t.Error(err)58 }59 _, err = ParseToken(generateToken)60 if err != nil {61 t.Error(err)62 }63}64// TestVerifyToken65func TestVerifyToken(t *testing.T) {66 type testsStruct struct {67 name string68 args string69 want bool70 }71 var tests []testsStruct72 tests = append(tests, testsStruct{73 name: "error",74 args: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NzcyNjM2ODYsImlhdCI6MTU3NzI2MzY3NiwidWlkIjoidWlkIiwibmlja25hbWUiOiJuaWNrbmFtZSIsImF2YXRhciI6ImF2YXRhciIsImdlbmRlciI6ImdlbmRlciIsInNpZ25hdHVyZSI6InNpZ25hdHVyZSIsInBlcm1pc3Npb25zIjpudWxsfQ.jUXsL4WMM2lJKu56DMHpHTwtPGvAixQwfxxlyI9X0bM",75 want: false,76 })77 for i := 0; i < 10; i++ {78 var userBase models.UserBase79 userBase.UID = uuid.New().String()80 userBase.Nickname = utils.RandString(6)81 userBase.Avatar = "https://ruan.co/" + utils.RandString(6)82 userBase.Gender = rand.Intn(2)83 userBase.Signature = utils.RandString(128)84 generateToken, err := GenerateToken(&userBase)85 if err != nil {86 t.Errorf("GenerateToken() error: %v", err)87 }88 tests = append(tests, testsStruct{89 name: "randData",90 args: generateToken,91 want: true,92 })93 }94 for _, tt := range tests {95 t.Run(tt.name, func(t *testing.T) {96 if got := VerifyToken(tt.args); got != tt.want {97 t.Errorf("VerifyToken() = %v, want %v", got, tt.want)...
RandString
Using AI Code Generation
1import (2func main() {3 fmt.Println(utils.RandString(10))4}5import (6func RandString(n int) string {7 rand.Seed(time.Now().UnixNano())8 var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")9 b := make([]rune, n)10 for i := range b {11 b[i] = letterRunes[rand.Intn(len(letterRunes))]12 }13 return string(b)14}
RandString
Using AI Code Generation
1import (2func main() {3 fmt.Println(utils.RandString(10))4}5import (6func RandString(n int) string {7 var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")8 b := make([]rune, n)9 for i := range b {10 b[i] = letterRunes[rand.Intn(len(letterRunes))]11 }12 return string(b)13}14func init() {15 rand.Seed(time.Now().UnixNano())16}
RandString
Using AI Code Generation
1import (2func main() {3 fmt.Println(utils.RandString(10))4}5import (6func RandString(n int) string {7 rand.Seed(time.Now().UnixNano())8 var letter = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")9 b := make([]rune, n)10 for i := range b {11 b[i] = letter[rand.Intn(len(letter))]12 }13 return string(b)14}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!