conf_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package redis
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/tal-tech/go-zero/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. for _, test := range tests {
  42. t.Run(stringx.RandId(), func(t *testing.T) {
  43. if test.ok {
  44. assert.Nil(t, test.RedisConf.Validate())
  45. assert.NotNil(t, test.RedisConf.NewRedis())
  46. } else {
  47. assert.NotNil(t, test.RedisConf.Validate())
  48. }
  49. })
  50. }
  51. }
  52. func TestRedisKeyConf(t *testing.T) {
  53. tests := []struct {
  54. name string
  55. RedisKeyConf
  56. ok bool
  57. }{
  58. {
  59. name: "missing host",
  60. RedisKeyConf: RedisKeyConf{
  61. RedisConf: RedisConf{
  62. Host: "",
  63. Type: NodeType,
  64. Pass: "",
  65. },
  66. Key: "foo",
  67. },
  68. ok: false,
  69. },
  70. {
  71. name: "missing key",
  72. RedisKeyConf: RedisKeyConf{
  73. RedisConf: RedisConf{
  74. Host: "localhost:6379",
  75. Type: NodeType,
  76. Pass: "",
  77. },
  78. Key: "",
  79. },
  80. ok: false,
  81. },
  82. {
  83. name: "ok",
  84. RedisKeyConf: RedisKeyConf{
  85. RedisConf: RedisConf{
  86. Host: "localhost:6379",
  87. Type: NodeType,
  88. Pass: "",
  89. },
  90. Key: "foo",
  91. },
  92. ok: true,
  93. },
  94. }
  95. for _, test := range tests {
  96. t.Run(test.name, func(t *testing.T) {
  97. if test.ok {
  98. assert.Nil(t, test.RedisKeyConf.Validate())
  99. } else {
  100. assert.NotNil(t, test.RedisKeyConf.Validate())
  101. }
  102. })
  103. }
  104. }