|
| 1 | +package redissug_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math/rand" |
| 6 | + "os" |
| 7 | + "slices" |
| 8 | + "strconv" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/redis/go-redis/v9" |
| 12 | + |
| 13 | + redissug "github.com/ndx-technologies/go-redis-suggest" |
| 14 | +) |
| 15 | + |
| 16 | +func TestRedisSuggest(t *testing.T) { |
| 17 | + if testing.Short() { |
| 18 | + t.Skip("network; redis;") |
| 19 | + } |
| 20 | + |
| 21 | + addr := os.Getenv("REDIS_ADDR") |
| 22 | + if addr == "" { |
| 23 | + addr = "localhost:6379" |
| 24 | + } |
| 25 | + |
| 26 | + rdb := redis.NewClient(&redis.Options{ |
| 27 | + Addr: addr, |
| 28 | + Password: os.Getenv("REDIS_PASSWORD"), |
| 29 | + }) |
| 30 | + |
| 31 | + ctx := t.Context() |
| 32 | + id := "test-sug:" + strconv.Itoa(rand.Int()) |
| 33 | + s := redissug.RedisSuggestionClient{DB: rdb} |
| 34 | + t.Cleanup(func() { s.DelAll(context.Background(), id) }) |
| 35 | + |
| 36 | + t.Run("when adding suggestions", func(t *testing.T) { |
| 37 | + size, err := s.SugAdd(ctx, id, "text", 1, false, "payload1") |
| 38 | + if err != nil { |
| 39 | + t.Error("SugAdd failed:", err) |
| 40 | + } |
| 41 | + if size != 1 { |
| 42 | + t.Error("expected size 1, got", size) |
| 43 | + } |
| 44 | + |
| 45 | + size, err = s.SugAdd(ctx, id, "test", 2, false, "payload2") |
| 46 | + if err != nil { |
| 47 | + t.Error("SugAdd failed:", err) |
| 48 | + } |
| 49 | + if size != 2 { |
| 50 | + t.Error("expected size 2, got", size) |
| 51 | + } |
| 52 | + |
| 53 | + size, err = s.SugAdd(ctx, id, "tent", 2, false, "") |
| 54 | + if err != nil { |
| 55 | + t.Error("SugAdd failed:", err) |
| 56 | + } |
| 57 | + if size != 3 { |
| 58 | + t.Error("expected size 3, got", size) |
| 59 | + } |
| 60 | + |
| 61 | + size, err = s.SugAdd(ctx, id, "tent", 1, true, "") |
| 62 | + if err != nil { |
| 63 | + t.Error("SugAdd failed:", err) |
| 64 | + } |
| 65 | + if size != 3 { |
| 66 | + t.Error("expected size 3, got", size) |
| 67 | + } |
| 68 | + |
| 69 | + t.Run("when getting length, then 3", func(t *testing.T) { |
| 70 | + count, err := s.SugLen(ctx, id) |
| 71 | + if err != nil { |
| 72 | + t.Error("SugLen failed:", err) |
| 73 | + } |
| 74 | + if count != 3 { |
| 75 | + t.Error("expected count 3, got", count) |
| 76 | + } |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("when prefix, then return all matched in highest to lowest score order", func(t *testing.T) { |
| 80 | + suggestions, err := s.SugGet(ctx, id, "te", 10, redissug.SugGetOptions{}) |
| 81 | + if err != nil { |
| 82 | + t.Error("SugGet failed:", err) |
| 83 | + } |
| 84 | + exp := []redissug.Suggestion{{Text: "tent"}, {Text: "test"}, {Text: "text"}} |
| 85 | + if !slices.Equal(suggestions, exp) { |
| 86 | + t.Error("expected", exp, "got", suggestions) |
| 87 | + } |
| 88 | + }) |
| 89 | + |
| 90 | + t.Run("when exact prefix, then only exact is returned", func(t *testing.T) { |
| 91 | + suggestions, err := s.SugGet(ctx, id, "tex", 10, redissug.SugGetOptions{}) |
| 92 | + if err != nil { |
| 93 | + t.Error("SugGet failed:", err) |
| 94 | + } |
| 95 | + exp := []redissug.Suggestion{{Text: "text"}} |
| 96 | + if !slices.Equal(suggestions, exp) { |
| 97 | + t.Error("expected", exp, "got", suggestions) |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + t.Run("when fuzzy prefix, then multiple returned", func(t *testing.T) { |
| 102 | + suggestions, err := s.SugGet(ctx, id, "tex", 10, redissug.SugGetOptions{Fuzzy: true}) |
| 103 | + if err != nil { |
| 104 | + t.Error("SugGet failed:", err) |
| 105 | + } |
| 106 | + exp := []redissug.Suggestion{{Text: "text"}, {Text: "tent"}, {Text: "test"}} |
| 107 | + if !slices.Equal(suggestions, exp) { |
| 108 | + t.Error("expected", exp, "got", suggestions) |
| 109 | + } |
| 110 | + }) |
| 111 | + |
| 112 | + t.Run("when getting with payloads, then payloads are returned", func(t *testing.T) { |
| 113 | + suggestions, err := s.SugGet(ctx, id, "te", 10, redissug.SugGetOptions{WithPayloads: true}) |
| 114 | + if err != nil { |
| 115 | + t.Error("SugGet failed:", err) |
| 116 | + } |
| 117 | + exp := []redissug.Suggestion{{Text: "tent"}, {Text: "test", Payload: "payload2"}, {Text: "text", Payload: "payload1"}} |
| 118 | + if !slices.Equal(suggestions, exp) { |
| 119 | + t.Error("expected", exp, "got", suggestions) |
| 120 | + } |
| 121 | + }) |
| 122 | + |
| 123 | + t.Run("when adding same suggestion with different score and payload", func(t *testing.T) { |
| 124 | + size, err := s.SugAdd(ctx, id, "text", 5, false, "new_payload") |
| 125 | + if err != nil { |
| 126 | + t.Error("SugAdd failed:", err) |
| 127 | + } |
| 128 | + if size != 3 { |
| 129 | + t.Error("expected size 3, got", size) |
| 130 | + } |
| 131 | + |
| 132 | + t.Run("then new payload is set", func(t *testing.T) { |
| 133 | + suggestions, err := s.SugGet(ctx, id, "tex", 10, redissug.SugGetOptions{WithPayloads: true}) |
| 134 | + if err != nil { |
| 135 | + t.Error("SugGet failed:", err) |
| 136 | + } |
| 137 | + exp := []redissug.Suggestion{{Text: "text", Payload: "new_payload"}} |
| 138 | + if !slices.Equal(suggestions, exp) { |
| 139 | + t.Error("expected", exp, "got", suggestions) |
| 140 | + } |
| 141 | + }) |
| 142 | + }) |
| 143 | + }) |
| 144 | + |
| 145 | + t.Run("when deleting suggestion", func(t *testing.T) { |
| 146 | + if err := s.SugDel(ctx, id, "test"); err != nil { |
| 147 | + t.Error("SugDel failed:", err) |
| 148 | + } |
| 149 | + |
| 150 | + t.Run("when deleting non existing, then no error and ok", func(t *testing.T) { |
| 151 | + if err := s.SugDel(ctx, id, "asdf"); err != redis.Nil { |
| 152 | + t.Error("expected redis.Nil, got", err) |
| 153 | + } |
| 154 | + }) |
| 155 | + |
| 156 | + t.Run("when getting suggestions, then no deleted entry", func(t *testing.T) { |
| 157 | + suggestions, err := s.SugGet(ctx, id, "te", 10, redissug.SugGetOptions{}) |
| 158 | + if err != nil { |
| 159 | + t.Error("SugGet failed:", err) |
| 160 | + } |
| 161 | + exp := []redissug.Suggestion{{Text: "text"}, {Text: "tent"}} |
| 162 | + if !slices.Equal(suggestions, exp) { |
| 163 | + t.Error("expected", exp, "got", suggestions) |
| 164 | + } |
| 165 | + }) |
| 166 | + |
| 167 | + t.Run("then length is decremented", func(t *testing.T) { |
| 168 | + count, err := s.SugLen(ctx, id) |
| 169 | + if err != nil { |
| 170 | + t.Error("SugLen failed:", err) |
| 171 | + } |
| 172 | + if count != 2 { |
| 173 | + t.Error("expected count 2, got", count) |
| 174 | + } |
| 175 | + }) |
| 176 | + }) |
| 177 | + |
| 178 | + t.Run("when non existing key", func(t *testing.T) { |
| 179 | + nonExistentKey := "non-existent:" + strconv.Itoa(rand.Int()) |
| 180 | + |
| 181 | + t.Run("when getting suggestions, then redis.Nil", func(t *testing.T) { |
| 182 | + suggestions, err := s.SugGet(ctx, nonExistentKey, "te", 10, redissug.SugGetOptions{}) |
| 183 | + if err != redis.Nil { |
| 184 | + t.Error("expected redis.Nil, got", err) |
| 185 | + } |
| 186 | + if len(suggestions) != 0 { |
| 187 | + t.Error("expected empty slice, got", suggestions) |
| 188 | + } |
| 189 | + }) |
| 190 | + |
| 191 | + t.Run("when getting length, then 0", func(t *testing.T) { |
| 192 | + count, err := s.SugLen(ctx, nonExistentKey) |
| 193 | + if err != nil { |
| 194 | + t.Error("SugLen failed:", err) |
| 195 | + } |
| 196 | + if count != 0 { |
| 197 | + t.Error("expected count 0, got", count) |
| 198 | + } |
| 199 | + }) |
| 200 | + |
| 201 | + t.Run("when deleting non existing, then redis nil", func(t *testing.T) { |
| 202 | + if err := s.SugDel(ctx, nonExistentKey, "asdf"); err != redis.Nil { |
| 203 | + t.Error("expected redis.Nil, got", err) |
| 204 | + } |
| 205 | + }) |
| 206 | + }) |
| 207 | +} |
0 commit comments