base_redis_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (c) [2022] [巴拉迪维 BaratSemet]
  2. // [ohUrlShortener] is licensed under Mulan PSL v2.
  3. // You can use this software according to the terms and conditions of the Mulan PSL v2.
  4. // You may obtain a copy of Mulan PSL v2 at:
  5. // http://license.coscl.org.cn/MulanPSL2
  6. // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  7. // See the Mulan PSL v2 for more details.
  8. package storage
  9. import (
  10. "ohurlshortener/utils"
  11. "reflect"
  12. "testing"
  13. "time"
  14. )
  15. func testInitRedisSettings(t *testing.T) {
  16. _, err := utils.InitConfig("../config.ini")
  17. if err != nil {
  18. t.Error(err)
  19. return
  20. }
  21. _, err = InitRedisService()
  22. if err != nil {
  23. t.Error(err)
  24. return
  25. }
  26. }
  27. func TestRedisSet(t *testing.T) {
  28. testInitRedisSettings(t)
  29. duration, _ := time.ParseDuration("30m")
  30. type args struct {
  31. key string
  32. value interface{}
  33. ttl time.Duration
  34. }
  35. tests := []struct {
  36. name string
  37. args args
  38. wantErr bool
  39. }{
  40. {name: "Test1", args: args{key: "hello", value: "test", ttl: duration}, wantErr: false},
  41. {name: "Test1", args: args{key: "world", value: "test1111", ttl: duration}, wantErr: false},
  42. }
  43. for _, tt := range tests {
  44. t.Run(tt.name, func(t *testing.T) {
  45. if err := RedisSet(tt.args.key, tt.args.value, tt.args.ttl); (err != nil) != tt.wantErr {
  46. t.Errorf("RedisSet() error = %v, wantErr %v", err, tt.wantErr)
  47. }
  48. })
  49. }
  50. }
  51. func TestRedisScan4Keys(t *testing.T) {
  52. want1 := []string{"hello"}
  53. want2 := []string{"world"}
  54. testInitRedisSettings(t)
  55. type args struct {
  56. prefix string
  57. }
  58. tests := []struct {
  59. name string
  60. args args
  61. want []string
  62. wantErr bool
  63. }{
  64. {name: "Test1", args: args{prefix: "hel*"}, want: want1, wantErr: false},
  65. {name: "Test2", args: args{prefix: "wo*"}, want: want2, wantErr: false},
  66. {name: "Test2", args: args{prefix: "aa*"}, want: nil, wantErr: false},
  67. }
  68. for _, tt := range tests {
  69. t.Run(tt.name, func(t *testing.T) {
  70. got, err := RedisScan4Keys(tt.args.prefix)
  71. if (err != nil) != tt.wantErr {
  72. t.Errorf("RedisScan4Keys() error = %v, wantErr %v", err, tt.wantErr)
  73. return
  74. }
  75. if !reflect.DeepEqual(got, tt.want) {
  76. t.Errorf("RedisScan4Keys() = %v, want %v", got, tt.want)
  77. }
  78. })
  79. }
  80. }
  81. func TestRedisGetString(t *testing.T) {
  82. testInitRedisSettings(t)
  83. want1 := "test"
  84. want2 := "test1111"
  85. type args struct {
  86. key string
  87. }
  88. tests := []struct {
  89. name string
  90. args args
  91. want string
  92. wantErr bool
  93. }{
  94. {name: "Test1", args: args{key: "hello"}, want: want1, wantErr: false},
  95. {name: "Test2", args: args{key: "world"}, want: want2, wantErr: false},
  96. }
  97. for _, tt := range tests {
  98. t.Run(tt.name, func(t *testing.T) {
  99. got, err := RedisGetString(tt.args.key)
  100. if (err != nil) != tt.wantErr {
  101. t.Errorf("RedisGetString() error = %v, wantErr %v", err, tt.wantErr)
  102. return
  103. }
  104. if got != tt.want {
  105. t.Errorf("RedisGetString() = %v, want %v", got, tt.want)
  106. }
  107. })
  108. }
  109. }
  110. func TestRedisFlushDB(t *testing.T) {
  111. testInitRedisSettings(t)
  112. tests := []struct {
  113. name string
  114. wantErr bool
  115. }{
  116. {name: "Test1", wantErr: false},
  117. }
  118. for _, tt := range tests {
  119. t.Run(tt.name, func(t *testing.T) {
  120. if err := RedisFlushDB(); (err != nil) != tt.wantErr {
  121. t.Errorf("RedisFlushDB() error = %v, wantErr %v", err, tt.wantErr)
  122. }
  123. })
  124. }
  125. }
  126. func TestRedisDelete(t *testing.T) {
  127. testInitRedisSettings(t)
  128. type args struct {
  129. key []string
  130. }
  131. tests := []struct {
  132. name string
  133. args args
  134. wantErr bool
  135. }{
  136. {name: "Test1", args: args{key: []string{"hello", "world"}}, wantErr: false},
  137. }
  138. for _, tt := range tests {
  139. t.Run(tt.name, func(t *testing.T) {
  140. if err := RedisDelete(tt.args.key...); (err != nil) != tt.wantErr {
  141. t.Errorf("RedisDelete() error = %v, wantErr %v", err, tt.wantErr)
  142. }
  143. })
  144. }
  145. }