github.com/mad-app/mattermost-server@v5.11.1+incompatible/store/storetest/emoji_store.go (about) 1 // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package storetest 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/mattermost/mattermost-server/model" 11 "github.com/mattermost/mattermost-server/store" 12 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestEmojiStore(t *testing.T, ss store.Store) { 17 t.Run("EmojiSaveDelete", func(t *testing.T) { testEmojiSaveDelete(t, ss) }) 18 t.Run("EmojiGet", func(t *testing.T) { testEmojiGet(t, ss) }) 19 t.Run("EmojiGetByName", func(t *testing.T) { testEmojiGetByName(t, ss) }) 20 t.Run("EmojiGetMultipleByName", func(t *testing.T) { testEmojiGetMultipleByName(t, ss) }) 21 t.Run("EmojiGetList", func(t *testing.T) { testEmojiGetList(t, ss) }) 22 t.Run("EmojiSearch", func(t *testing.T) { testEmojiSearch(t, ss) }) 23 } 24 25 func testEmojiSaveDelete(t *testing.T, ss store.Store) { 26 emoji1 := &model.Emoji{ 27 CreatorId: model.NewId(), 28 Name: model.NewId(), 29 } 30 31 if result := <-ss.Emoji().Save(emoji1); result.Err != nil { 32 t.Fatal(result.Err) 33 } 34 35 if len(emoji1.Id) != 26 { 36 t.Fatal("should've set id for emoji") 37 } 38 39 emoji2 := model.Emoji{ 40 CreatorId: model.NewId(), 41 Name: emoji1.Name, 42 } 43 if result := <-ss.Emoji().Save(&emoji2); result.Err == nil { 44 t.Fatal("shouldn't be able to save emoji with duplicate name") 45 } 46 47 if result := <-ss.Emoji().Delete(emoji1.Id, time.Now().Unix()); result.Err != nil { 48 t.Fatal(result.Err) 49 } 50 51 if result := <-ss.Emoji().Save(&emoji2); result.Err != nil { 52 t.Fatal("should be able to save emoji with duplicate name now that original has been deleted", result.Err) 53 } 54 55 if result := <-ss.Emoji().Delete(emoji2.Id, time.Now().Unix()+1); result.Err != nil { 56 t.Fatal(result.Err) 57 } 58 } 59 60 func testEmojiGet(t *testing.T, ss store.Store) { 61 emojis := []model.Emoji{ 62 { 63 CreatorId: model.NewId(), 64 Name: model.NewId(), 65 }, 66 { 67 CreatorId: model.NewId(), 68 Name: model.NewId(), 69 }, 70 { 71 CreatorId: model.NewId(), 72 Name: model.NewId(), 73 }, 74 } 75 76 for i, emoji := range emojis { 77 emojis[i] = *store.Must(ss.Emoji().Save(&emoji)).(*model.Emoji) 78 } 79 defer func() { 80 for _, emoji := range emojis { 81 store.Must(ss.Emoji().Delete(emoji.Id, time.Now().Unix())) 82 } 83 }() 84 85 for _, emoji := range emojis { 86 if result := <-ss.Emoji().Get(emoji.Id, false); result.Err != nil { 87 t.Fatalf("failed to get emoji with id %v: %v", emoji.Id, result.Err) 88 } 89 } 90 91 for _, emoji := range emojis { 92 if result := <-ss.Emoji().Get(emoji.Id, true); result.Err != nil { 93 t.Fatalf("failed to get emoji with id %v: %v", emoji.Id, result.Err) 94 } 95 } 96 97 for _, emoji := range emojis { 98 if result := <-ss.Emoji().Get(emoji.Id, true); result.Err != nil { 99 t.Fatalf("failed to get emoji with id %v: %v", emoji.Id, result.Err) 100 } 101 } 102 } 103 104 func testEmojiGetByName(t *testing.T, ss store.Store) { 105 emojis := []model.Emoji{ 106 { 107 CreatorId: model.NewId(), 108 Name: model.NewId(), 109 }, 110 { 111 CreatorId: model.NewId(), 112 Name: model.NewId(), 113 }, 114 { 115 CreatorId: model.NewId(), 116 Name: model.NewId(), 117 }, 118 } 119 120 for i, emoji := range emojis { 121 emojis[i] = *store.Must(ss.Emoji().Save(&emoji)).(*model.Emoji) 122 } 123 defer func() { 124 for _, emoji := range emojis { 125 store.Must(ss.Emoji().Delete(emoji.Id, time.Now().Unix())) 126 } 127 }() 128 129 for _, emoji := range emojis { 130 if result := <-ss.Emoji().GetByName(emoji.Name); result.Err != nil { 131 t.Fatalf("failed to get emoji with name %v: %v", emoji.Name, result.Err) 132 } 133 } 134 } 135 136 func testEmojiGetMultipleByName(t *testing.T, ss store.Store) { 137 emojis := []model.Emoji{ 138 { 139 CreatorId: model.NewId(), 140 Name: model.NewId(), 141 }, 142 { 143 CreatorId: model.NewId(), 144 Name: model.NewId(), 145 }, 146 { 147 CreatorId: model.NewId(), 148 Name: model.NewId(), 149 }, 150 } 151 152 for i, emoji := range emojis { 153 emojis[i] = *store.Must(ss.Emoji().Save(&emoji)).(*model.Emoji) 154 } 155 defer func() { 156 for _, emoji := range emojis { 157 store.Must(ss.Emoji().Delete(emoji.Id, time.Now().Unix())) 158 } 159 }() 160 161 t.Run("one emoji", func(t *testing.T) { 162 if result := <-ss.Emoji().GetMultipleByName([]string{emojis[0].Name}); result.Err != nil { 163 t.Fatal("could not get emoji", result.Err) 164 } else if received := result.Data.([]*model.Emoji); len(received) != 1 || *received[0] != emojis[0] { 165 t.Fatal("got incorrect emoji") 166 } 167 }) 168 169 t.Run("multiple emojis", func(t *testing.T) { 170 if result := <-ss.Emoji().GetMultipleByName([]string{emojis[0].Name, emojis[1].Name, emojis[2].Name}); result.Err != nil { 171 t.Fatal("could not get emojis", result.Err) 172 } else if received := result.Data.([]*model.Emoji); len(received) != 3 { 173 t.Fatal("got incorrect emojis") 174 } 175 }) 176 177 t.Run("one nonexistent emoji", func(t *testing.T) { 178 if result := <-ss.Emoji().GetMultipleByName([]string{"ab"}); result.Err != nil { 179 t.Fatal("could not get emoji", result.Err) 180 } else if received := result.Data.([]*model.Emoji); len(received) != 0 { 181 t.Fatal("got incorrect emoji") 182 } 183 }) 184 185 t.Run("multiple emojis with nonexistent names", func(t *testing.T) { 186 if result := <-ss.Emoji().GetMultipleByName([]string{emojis[0].Name, emojis[1].Name, emojis[2].Name, "abcd", "1234"}); result.Err != nil { 187 t.Fatal("could not get emojis", result.Err) 188 } else if received := result.Data.([]*model.Emoji); len(received) != 3 { 189 t.Fatal("got incorrect emojis") 190 } 191 }) 192 } 193 194 func testEmojiGetList(t *testing.T, ss store.Store) { 195 emojis := []model.Emoji{ 196 { 197 CreatorId: model.NewId(), 198 Name: "00000000000000000000000000a" + model.NewId(), 199 }, 200 { 201 CreatorId: model.NewId(), 202 Name: "00000000000000000000000000b" + model.NewId(), 203 }, 204 { 205 CreatorId: model.NewId(), 206 Name: "00000000000000000000000000c" + model.NewId(), 207 }, 208 } 209 210 for i, emoji := range emojis { 211 emojis[i] = *store.Must(ss.Emoji().Save(&emoji)).(*model.Emoji) 212 } 213 defer func() { 214 for _, emoji := range emojis { 215 store.Must(ss.Emoji().Delete(emoji.Id, time.Now().Unix())) 216 } 217 }() 218 219 if result := <-ss.Emoji().GetList(0, 100, ""); result.Err != nil { 220 t.Fatal(result.Err) 221 } else { 222 for _, emoji := range emojis { 223 found := false 224 225 for _, savedEmoji := range result.Data.([]*model.Emoji) { 226 if emoji.Id == savedEmoji.Id { 227 found = true 228 break 229 } 230 } 231 232 if !found { 233 t.Fatalf("failed to get emoji with id %v", emoji.Id) 234 } 235 } 236 } 237 238 result := <-ss.Emoji().GetList(0, 3, model.EMOJI_SORT_BY_NAME) 239 assert.Nil(t, result.Err) 240 remojis := result.Data.([]*model.Emoji) 241 assert.Equal(t, 3, len(remojis)) 242 assert.Equal(t, emojis[0].Name, remojis[0].Name) 243 assert.Equal(t, emojis[1].Name, remojis[1].Name) 244 assert.Equal(t, emojis[2].Name, remojis[2].Name) 245 246 result = <-ss.Emoji().GetList(1, 2, model.EMOJI_SORT_BY_NAME) 247 assert.Nil(t, result.Err) 248 remojis = result.Data.([]*model.Emoji) 249 assert.Equal(t, 2, len(remojis)) 250 assert.Equal(t, emojis[1].Name, remojis[0].Name) 251 assert.Equal(t, emojis[2].Name, remojis[1].Name) 252 253 } 254 255 func testEmojiSearch(t *testing.T, ss store.Store) { 256 emojis := []model.Emoji{ 257 { 258 CreatorId: model.NewId(), 259 Name: "blargh_" + model.NewId(), 260 }, 261 { 262 CreatorId: model.NewId(), 263 Name: model.NewId() + "_blargh", 264 }, 265 { 266 CreatorId: model.NewId(), 267 Name: model.NewId() + "_blargh_" + model.NewId(), 268 }, 269 { 270 CreatorId: model.NewId(), 271 Name: model.NewId(), 272 }, 273 } 274 275 for i, emoji := range emojis { 276 emojis[i] = *store.Must(ss.Emoji().Save(&emoji)).(*model.Emoji) 277 } 278 defer func() { 279 for _, emoji := range emojis { 280 store.Must(ss.Emoji().Delete(emoji.Id, time.Now().Unix())) 281 } 282 }() 283 284 shouldFind := []bool{true, false, false, false} 285 286 if result := <-ss.Emoji().Search("blargh", true, 100); result.Err != nil { 287 t.Fatal(result.Err) 288 } else { 289 for i, emoji := range emojis { 290 found := false 291 292 for _, savedEmoji := range result.Data.([]*model.Emoji) { 293 if emoji.Id == savedEmoji.Id { 294 found = true 295 break 296 } 297 } 298 299 assert.Equal(t, shouldFind[i], found, emoji.Name) 300 } 301 } 302 303 shouldFind = []bool{true, true, true, false} 304 if result := <-ss.Emoji().Search("blargh", false, 100); result.Err != nil { 305 t.Fatal(result.Err) 306 } else { 307 for i, emoji := range emojis { 308 found := false 309 310 for _, savedEmoji := range result.Data.([]*model.Emoji) { 311 if emoji.Id == savedEmoji.Id { 312 found = true 313 break 314 } 315 } 316 317 assert.Equal(t, shouldFind[i], found, emoji.Name) 318 } 319 } 320 }