conf_test.go 1.9 KB

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