mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
Migrate tests from "store/storetest/reaction_store.go" to use testify (#12752)
* testReactionDelete * testReactionSave * testReactionGetForPost * testReactionDeleteAllWithEmojiName * testReactionStorePermanentDeleteBatch * testReactionBulkGetForPosts * `assert` to follow convention with first parameter as `t` * using semantic assertions instead * removing unnecessary empty lines
This commit is contained in:
committed by
Miguel de la Cruz
parent
709f407d33
commit
4b127cd877
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/store"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
@@ -34,30 +35,26 @@ func testReactionSave(t *testing.T, ss store.Store) {
|
||||
EmojiName: model.NewId(),
|
||||
}
|
||||
reaction, err := ss.Reaction().Save(reaction1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if saved := reaction; saved.UserId != reaction1.UserId ||
|
||||
saved.PostId != reaction1.PostId || saved.EmojiName != reaction1.EmojiName {
|
||||
t.Fatal("should've saved reaction and returned it")
|
||||
}
|
||||
require.Nil(t, err)
|
||||
|
||||
saved := reaction
|
||||
assert.Equal(t, saved.UserId, reaction1.UserId, "should've saved reaction user_id and returned it")
|
||||
assert.Equal(t, saved.PostId, reaction1.PostId, "should've saved reaction post_id and returned it")
|
||||
assert.Equal(t, saved.EmojiName, reaction1.EmojiName, "should've saved reaction emoji_name and returned it")
|
||||
|
||||
var secondUpdateAt int64
|
||||
postList, err := ss.Post().Get(reaction1.PostId, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !postList.Posts[post.Id].HasReactions {
|
||||
t.Fatal("should've set HasReactions = true on post")
|
||||
} else if postList.Posts[post.Id].UpdateAt == firstUpdateAt {
|
||||
t.Fatal("should've marked post as updated when HasReactions changed")
|
||||
} else {
|
||||
require.Nil(t, err)
|
||||
|
||||
assert.True(t, postList.Posts[post.Id].HasReactions, "should've set HasReactions = true on post")
|
||||
assert.NotEqual(t, postList.Posts[post.Id].UpdateAt, firstUpdateAt, "should've marked post as updated when HasReactions changed")
|
||||
|
||||
if postList.Posts[post.Id].HasReactions && postList.Posts[post.Id].UpdateAt != firstUpdateAt {
|
||||
secondUpdateAt = postList.Posts[post.Id].UpdateAt
|
||||
}
|
||||
|
||||
if _, err = ss.Reaction().Save(reaction1); err != nil {
|
||||
t.Log(err)
|
||||
t.Fatal("should've allowed saving a duplicate reaction")
|
||||
}
|
||||
_, err = ss.Reaction().Save(reaction1)
|
||||
assert.Nil(t, err, "should've allowed saving a duplicate reaction")
|
||||
|
||||
// different user
|
||||
reaction2 := &model.Reaction{
|
||||
@@ -65,18 +62,13 @@ func testReactionSave(t *testing.T, ss store.Store) {
|
||||
PostId: reaction1.PostId,
|
||||
EmojiName: reaction1.EmojiName,
|
||||
}
|
||||
if _, err = ss.Reaction().Save(reaction2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = ss.Reaction().Save(reaction2)
|
||||
require.Nil(t, err)
|
||||
|
||||
postList, err = ss.Post().Get(reaction2.PostId, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.Nil(t, err)
|
||||
|
||||
if postList.Posts[post.Id].UpdateAt == secondUpdateAt {
|
||||
t.Fatal("should've marked post as updated even if HasReactions doesn't change")
|
||||
}
|
||||
assert.NotEqual(t, postList.Posts[post.Id].UpdateAt, secondUpdateAt, "should've marked post as updated even if HasReactions doesn't change")
|
||||
|
||||
// different post
|
||||
reaction3 := &model.Reaction{
|
||||
@@ -84,9 +76,8 @@ func testReactionSave(t *testing.T, ss store.Store) {
|
||||
PostId: model.NewId(),
|
||||
EmojiName: reaction1.EmojiName,
|
||||
}
|
||||
if _, err := ss.Reaction().Save(reaction3); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = ss.Reaction().Save(reaction3)
|
||||
require.Nil(t, err)
|
||||
|
||||
// different emoji
|
||||
reaction4 := &model.Reaction{
|
||||
@@ -94,18 +85,17 @@ func testReactionSave(t *testing.T, ss store.Store) {
|
||||
PostId: reaction1.PostId,
|
||||
EmojiName: model.NewId(),
|
||||
}
|
||||
if _, err := ss.Reaction().Save(reaction4); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = ss.Reaction().Save(reaction4)
|
||||
require.Nil(t, err)
|
||||
|
||||
// invalid reaction
|
||||
reaction5 := &model.Reaction{
|
||||
UserId: reaction1.UserId,
|
||||
PostId: reaction1.PostId,
|
||||
}
|
||||
if _, err := ss.Reaction().Save(reaction5); err == nil {
|
||||
t.Fatal("should've failed for invalid reaction")
|
||||
}
|
||||
_, err = ss.Reaction().Save(reaction5)
|
||||
require.NotNil(t, err, "should've failed for invalid reaction")
|
||||
|
||||
}
|
||||
|
||||
func testReactionDelete(t *testing.T, ss store.Store) {
|
||||
@@ -123,30 +113,25 @@ func testReactionDelete(t *testing.T, ss store.Store) {
|
||||
|
||||
_, err = ss.Reaction().Save(reaction)
|
||||
require.Nil(t, err)
|
||||
|
||||
result, err := ss.Post().Get(reaction.PostId, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.Nil(t, err)
|
||||
|
||||
firstUpdateAt := result.Posts[post.Id].UpdateAt
|
||||
|
||||
if _, err = ss.Reaction().Delete(reaction); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = ss.Reaction().Delete(reaction)
|
||||
require.Nil(t, err)
|
||||
|
||||
reactions, rErr := ss.Reaction().GetForPost(post.Id, false)
|
||||
require.Nil(t, rErr)
|
||||
|
||||
assert.Len(t, reactions, 0, "should've deleted reaction")
|
||||
|
||||
if reactions, rErr := ss.Reaction().GetForPost(post.Id, false); rErr != nil {
|
||||
t.Fatal(rErr)
|
||||
} else if len(reactions) != 0 {
|
||||
t.Fatal("should've deleted reaction")
|
||||
}
|
||||
postList, err := ss.Post().Get(post.Id, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if postList.Posts[post.Id].HasReactions {
|
||||
t.Fatal("should've set HasReactions = false on post")
|
||||
} else if postList.Posts[post.Id].UpdateAt == firstUpdateAt {
|
||||
t.Fatal("should mark post as updated after deleting reactions")
|
||||
}
|
||||
require.Nil(t, err)
|
||||
|
||||
assert.False(t, postList.Posts[post.Id].HasReactions, "should've set HasReactions = false on post")
|
||||
assert.NotEqual(t, postList.Posts[post.Id].UpdateAt, firstUpdateAt, "should mark post as updated after deleting reactions")
|
||||
}
|
||||
|
||||
func testReactionGetForPost(t *testing.T, ss store.Store) {
|
||||
@@ -182,53 +167,49 @@ func testReactionGetForPost(t *testing.T, ss store.Store) {
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
if returned, err := ss.Reaction().GetForPost(postId, false); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(returned) != 3 {
|
||||
t.Fatal("should've returned 3 reactions")
|
||||
} else {
|
||||
for _, reaction := range reactions {
|
||||
found := false
|
||||
returned, err := ss.Reaction().GetForPost(postId, false)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, returned, 3, "should've returned 3 reactions")
|
||||
|
||||
for _, returnedReaction := range returned {
|
||||
if returnedReaction.UserId == reaction.UserId && returnedReaction.PostId == reaction.PostId &&
|
||||
returnedReaction.EmojiName == reaction.EmojiName {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
for _, reaction := range reactions {
|
||||
found := false
|
||||
|
||||
if !found && reaction.PostId == postId {
|
||||
t.Fatalf("should've returned reaction for post %v", reaction)
|
||||
} else if found && reaction.PostId != postId {
|
||||
t.Fatal("shouldn't have returned reaction for another post")
|
||||
for _, returnedReaction := range returned {
|
||||
if returnedReaction.UserId == reaction.UserId && returnedReaction.PostId == reaction.PostId &&
|
||||
returnedReaction.EmojiName == reaction.EmojiName {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
assert.NotEqual(t, reaction.PostId, postId, "should've returned reaction for post %v", reaction)
|
||||
} else if found {
|
||||
assert.Equal(t, reaction.PostId, postId, "shouldn't have returned reaction for another post")
|
||||
}
|
||||
}
|
||||
|
||||
// Should return cached item
|
||||
if returned, err := ss.Reaction().GetForPost(postId, true); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(returned) != 3 {
|
||||
t.Fatal("should've returned 3 reactions")
|
||||
} else {
|
||||
for _, reaction := range reactions {
|
||||
found := false
|
||||
returned, err = ss.Reaction().GetForPost(postId, true)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, returned, 3, "should've returned 3 reactions")
|
||||
|
||||
for _, returnedReaction := range returned {
|
||||
if returnedReaction.UserId == reaction.UserId && returnedReaction.PostId == reaction.PostId &&
|
||||
returnedReaction.EmojiName == reaction.EmojiName {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
for _, reaction := range reactions {
|
||||
found := false
|
||||
|
||||
if !found && reaction.PostId == postId {
|
||||
t.Fatalf("should've returned reaction for post %v", reaction)
|
||||
} else if found && reaction.PostId != postId {
|
||||
t.Fatal("shouldn't have returned reaction for another post")
|
||||
for _, returnedReaction := range returned {
|
||||
if returnedReaction.UserId == reaction.UserId && returnedReaction.PostId == reaction.PostId &&
|
||||
returnedReaction.EmojiName == reaction.EmojiName {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
assert.NotEqual(t, reaction.PostId, postId, "should've returned reaction for post %v", reaction)
|
||||
} else if found {
|
||||
assert.Equal(t, reaction.PostId, postId, "shouldn't have returned reaction for another post")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,60 +267,39 @@ func testReactionDeleteAllWithEmojiName(t *testing.T, ss store.Store) {
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
if err := ss.Reaction().DeleteAllWithEmojiName(emojiToDelete); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err := ss.Reaction().DeleteAllWithEmojiName(emojiToDelete)
|
||||
require.Nil(t, err)
|
||||
|
||||
// check that the reactions were deleted
|
||||
if returned, err := ss.Reaction().GetForPost(post.Id, false); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(returned) != 1 {
|
||||
t.Fatal("should've only removed reactions with emoji name")
|
||||
} else {
|
||||
for _, reaction := range returned {
|
||||
if reaction.EmojiName == "smile" {
|
||||
t.Fatal("should've removed reaction with emoji name")
|
||||
}
|
||||
}
|
||||
returned, err := ss.Reaction().GetForPost(post.Id, false)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, returned, 1, "should've only removed reactions with emoji name")
|
||||
|
||||
for _, reaction := range returned {
|
||||
assert.NotEqual(t, reaction.EmojiName, "smile", "should've removed reaction with emoji name")
|
||||
}
|
||||
|
||||
if returned, err := ss.Reaction().GetForPost(post2.Id, false); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(returned) != 1 {
|
||||
t.Fatal("should've only removed reactions with emoji name")
|
||||
}
|
||||
returned, err = ss.Reaction().GetForPost(post2.Id, false)
|
||||
require.Nil(t, err)
|
||||
assert.Len(t, returned, 1, "should've only removed reactions with emoji name")
|
||||
|
||||
if returned, err := ss.Reaction().GetForPost(post3.Id, false); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(returned) != 0 {
|
||||
t.Fatal("should've only removed reactions with emoji name")
|
||||
}
|
||||
returned, err = ss.Reaction().GetForPost(post3.Id, false)
|
||||
require.Nil(t, err)
|
||||
assert.Len(t, returned, 0, "should've only removed reactions with emoji name")
|
||||
|
||||
// check that the posts are updated
|
||||
postList, err := ss.Post().Get(post.Id, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !postList.Posts[post.Id].HasReactions {
|
||||
t.Fatal("post should still have reactions")
|
||||
}
|
||||
require.Nil(t, err)
|
||||
assert.True(t, postList.Posts[post.Id].HasReactions, "post should still have reactions")
|
||||
|
||||
postList, err = ss.Post().Get(post2.Id, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !postList.Posts[post2.Id].HasReactions {
|
||||
t.Fatal("post should still have reactions")
|
||||
}
|
||||
require.Nil(t, err)
|
||||
assert.True(t, postList.Posts[post2.Id].HasReactions, "post should still have reactions")
|
||||
|
||||
postList, err = ss.Post().Get(post3.Id, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.Nil(t, err)
|
||||
assert.False(t, postList.Posts[post3.Id].HasReactions, "post shouldn't have reactions any more")
|
||||
|
||||
if postList.Posts[post3.Id].HasReactions {
|
||||
t.Fatal("post shouldn't have reactions any more")
|
||||
}
|
||||
}
|
||||
|
||||
func testReactionStorePermanentDeleteBatch(t *testing.T, ss store.Store) {
|
||||
@@ -384,24 +344,20 @@ func testReactionStorePermanentDeleteBatch(t *testing.T, ss store.Store) {
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
if returned, err := ss.Reaction().GetForPost(post.Id, false); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(returned) != 4 {
|
||||
t.Fatal("expected 4 reactions")
|
||||
}
|
||||
returned, err := ss.Reaction().GetForPost(post.Id, false)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, returned, 4, "expected 4 reactions")
|
||||
|
||||
_, err := ss.Reaction().PermanentDeleteBatch(1800, 1000)
|
||||
_, err = ss.Reaction().PermanentDeleteBatch(1800, 1000)
|
||||
require.Nil(t, err)
|
||||
|
||||
// This is to force a clear of the cache.
|
||||
_, err = ss.Reaction().Delete(lastReaction)
|
||||
require.Nil(t, err)
|
||||
|
||||
if returned, err := ss.Reaction().GetForPost(post.Id, false); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(returned) != 1 {
|
||||
t.Fatalf("expected 1 reaction. Got: %v", len(returned))
|
||||
}
|
||||
returned, err = ss.Reaction().GetForPost(post.Id, false)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, returned, 1, "expected 1 reaction. Got: %v", len(returned))
|
||||
}
|
||||
|
||||
func testReactionBulkGetForPosts(t *testing.T, ss store.Store) {
|
||||
@@ -451,22 +407,18 @@ func testReactionBulkGetForPosts(t *testing.T, ss store.Store) {
|
||||
}
|
||||
|
||||
postIds := []string{postId, post2Id, post3Id}
|
||||
if returned, err := ss.Reaction().BulkGetForPosts(postIds); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if len(returned) != 5 {
|
||||
t.Fatal("should've returned 5 reactions")
|
||||
} else {
|
||||
post4IdFound := false
|
||||
for _, reaction := range returned {
|
||||
if reaction.PostId == post4Id {
|
||||
post4IdFound = true
|
||||
break
|
||||
}
|
||||
}
|
||||
returned, err := ss.Reaction().BulkGetForPosts(postIds)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, returned, 5, "should've returned 5 reactions")
|
||||
|
||||
if post4IdFound {
|
||||
t.Fatal("Wrong reaction returned")
|
||||
post4IdFound := false
|
||||
for _, reaction := range returned {
|
||||
if reaction.PostId == post4Id {
|
||||
post4IdFound = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
require.False(t, post4IdFound, "Wrong reaction returned")
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user