github.com/lingyao2333/mo-zero@v1.4.1/core/stores/redis/conf_test.go (about) 1 package redis 2 3 import ( 4 "testing" 5 6 "github.com/lingyao2333/mo-zero/core/stringx" 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestRedisConf(t *testing.T) { 11 tests := []struct { 12 name string 13 RedisConf 14 ok bool 15 }{ 16 { 17 name: "missing host", 18 RedisConf: RedisConf{ 19 Host: "", 20 Type: NodeType, 21 Pass: "", 22 }, 23 ok: false, 24 }, 25 { 26 name: "missing type", 27 RedisConf: RedisConf{ 28 Host: "localhost:6379", 29 Type: "", 30 Pass: "", 31 }, 32 ok: false, 33 }, 34 { 35 name: "ok", 36 RedisConf: RedisConf{ 37 Host: "localhost:6379", 38 Type: NodeType, 39 Pass: "", 40 }, 41 ok: true, 42 }, 43 } 44 45 for _, test := range tests { 46 t.Run(stringx.RandId(), func(t *testing.T) { 47 if test.ok { 48 assert.Nil(t, test.RedisConf.Validate()) 49 assert.NotNil(t, test.RedisConf.NewRedis()) 50 } else { 51 assert.NotNil(t, test.RedisConf.Validate()) 52 } 53 }) 54 } 55 } 56 57 func TestRedisKeyConf(t *testing.T) { 58 tests := []struct { 59 name string 60 RedisKeyConf 61 ok bool 62 }{ 63 { 64 name: "missing host", 65 RedisKeyConf: RedisKeyConf{ 66 RedisConf: RedisConf{ 67 Host: "", 68 Type: NodeType, 69 Pass: "", 70 }, 71 Key: "foo", 72 }, 73 ok: false, 74 }, 75 { 76 name: "missing key", 77 RedisKeyConf: RedisKeyConf{ 78 RedisConf: RedisConf{ 79 Host: "localhost:6379", 80 Type: NodeType, 81 Pass: "", 82 }, 83 Key: "", 84 }, 85 ok: false, 86 }, 87 { 88 name: "ok", 89 RedisKeyConf: RedisKeyConf{ 90 RedisConf: RedisConf{ 91 Host: "localhost:6379", 92 Type: NodeType, 93 Pass: "", 94 }, 95 Key: "foo", 96 }, 97 ok: true, 98 }, 99 } 100 101 for _, test := range tests { 102 t.Run(test.name, func(t *testing.T) { 103 if test.ok { 104 assert.Nil(t, test.RedisKeyConf.Validate()) 105 } else { 106 assert.NotNil(t, test.RedisKeyConf.Validate()) 107 } 108 }) 109 } 110 }