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
Этот коммит содержится в:
коммит произвёл
Miguel de la Cruz
родитель
709f407d33
Коммит
4b127cd877
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/mattermost/mattermost-server/model"
|
"github.com/mattermost/mattermost-server/model"
|
||||||
"github.com/mattermost/mattermost-server/store"
|
"github.com/mattermost/mattermost-server/store"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,30 +35,26 @@ func testReactionSave(t *testing.T, ss store.Store) {
|
|||||||
EmojiName: model.NewId(),
|
EmojiName: model.NewId(),
|
||||||
}
|
}
|
||||||
reaction, err := ss.Reaction().Save(reaction1)
|
reaction, err := ss.Reaction().Save(reaction1)
|
||||||
if err != nil {
|
require.Nil(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
} else if saved := reaction; saved.UserId != reaction1.UserId ||
|
saved := reaction
|
||||||
saved.PostId != reaction1.PostId || saved.EmojiName != reaction1.EmojiName {
|
assert.Equal(t, saved.UserId, reaction1.UserId, "should've saved reaction user_id and returned it")
|
||||||
t.Fatal("should've saved reaction 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
|
var secondUpdateAt int64
|
||||||
postList, err := ss.Post().Get(reaction1.PostId, false)
|
postList, err := ss.Post().Get(reaction1.PostId, false)
|
||||||
if err != nil {
|
require.Nil(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
assert.True(t, postList.Posts[post.Id].HasReactions, "should've set HasReactions = true on post")
|
||||||
if !postList.Posts[post.Id].HasReactions {
|
assert.NotEqual(t, postList.Posts[post.Id].UpdateAt, firstUpdateAt, "should've marked post as updated when HasReactions changed")
|
||||||
t.Fatal("should've set HasReactions = true on post")
|
|
||||||
} else if postList.Posts[post.Id].UpdateAt == firstUpdateAt {
|
if postList.Posts[post.Id].HasReactions && postList.Posts[post.Id].UpdateAt != firstUpdateAt {
|
||||||
t.Fatal("should've marked post as updated when HasReactions changed")
|
|
||||||
} else {
|
|
||||||
secondUpdateAt = postList.Posts[post.Id].UpdateAt
|
secondUpdateAt = postList.Posts[post.Id].UpdateAt
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err = ss.Reaction().Save(reaction1); err != nil {
|
_, err = ss.Reaction().Save(reaction1)
|
||||||
t.Log(err)
|
assert.Nil(t, err, "should've allowed saving a duplicate reaction")
|
||||||
t.Fatal("should've allowed saving a duplicate reaction")
|
|
||||||
}
|
|
||||||
|
|
||||||
// different user
|
// different user
|
||||||
reaction2 := &model.Reaction{
|
reaction2 := &model.Reaction{
|
||||||
@@ -65,18 +62,13 @@ func testReactionSave(t *testing.T, ss store.Store) {
|
|||||||
PostId: reaction1.PostId,
|
PostId: reaction1.PostId,
|
||||||
EmojiName: reaction1.EmojiName,
|
EmojiName: reaction1.EmojiName,
|
||||||
}
|
}
|
||||||
if _, err = ss.Reaction().Save(reaction2); err != nil {
|
_, err = ss.Reaction().Save(reaction2)
|
||||||
t.Fatal(err)
|
require.Nil(t, err)
|
||||||
}
|
|
||||||
|
|
||||||
postList, err = ss.Post().Get(reaction2.PostId, false)
|
postList, err = ss.Post().Get(reaction2.PostId, false)
|
||||||
if err != nil {
|
require.Nil(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if postList.Posts[post.Id].UpdateAt == secondUpdateAt {
|
assert.NotEqual(t, postList.Posts[post.Id].UpdateAt, secondUpdateAt, "should've marked post as updated even if HasReactions doesn't change")
|
||||||
t.Fatal("should've marked post as updated even if HasReactions doesn't change")
|
|
||||||
}
|
|
||||||
|
|
||||||
// different post
|
// different post
|
||||||
reaction3 := &model.Reaction{
|
reaction3 := &model.Reaction{
|
||||||
@@ -84,9 +76,8 @@ func testReactionSave(t *testing.T, ss store.Store) {
|
|||||||
PostId: model.NewId(),
|
PostId: model.NewId(),
|
||||||
EmojiName: reaction1.EmojiName,
|
EmojiName: reaction1.EmojiName,
|
||||||
}
|
}
|
||||||
if _, err := ss.Reaction().Save(reaction3); err != nil {
|
_, err = ss.Reaction().Save(reaction3)
|
||||||
t.Fatal(err)
|
require.Nil(t, err)
|
||||||
}
|
|
||||||
|
|
||||||
// different emoji
|
// different emoji
|
||||||
reaction4 := &model.Reaction{
|
reaction4 := &model.Reaction{
|
||||||
@@ -94,18 +85,17 @@ func testReactionSave(t *testing.T, ss store.Store) {
|
|||||||
PostId: reaction1.PostId,
|
PostId: reaction1.PostId,
|
||||||
EmojiName: model.NewId(),
|
EmojiName: model.NewId(),
|
||||||
}
|
}
|
||||||
if _, err := ss.Reaction().Save(reaction4); err != nil {
|
_, err = ss.Reaction().Save(reaction4)
|
||||||
t.Fatal(err)
|
require.Nil(t, err)
|
||||||
}
|
|
||||||
|
|
||||||
// invalid reaction
|
// invalid reaction
|
||||||
reaction5 := &model.Reaction{
|
reaction5 := &model.Reaction{
|
||||||
UserId: reaction1.UserId,
|
UserId: reaction1.UserId,
|
||||||
PostId: reaction1.PostId,
|
PostId: reaction1.PostId,
|
||||||
}
|
}
|
||||||
if _, err := ss.Reaction().Save(reaction5); err == nil {
|
_, err = ss.Reaction().Save(reaction5)
|
||||||
t.Fatal("should've failed for invalid reaction")
|
require.NotNil(t, err, "should've failed for invalid reaction")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testReactionDelete(t *testing.T, ss store.Store) {
|
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)
|
_, err = ss.Reaction().Save(reaction)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|
||||||
result, err := ss.Post().Get(reaction.PostId, false)
|
result, err := ss.Post().Get(reaction.PostId, false)
|
||||||
if err != nil {
|
require.Nil(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
firstUpdateAt := result.Posts[post.Id].UpdateAt
|
firstUpdateAt := result.Posts[post.Id].UpdateAt
|
||||||
|
|
||||||
if _, err = ss.Reaction().Delete(reaction); err != nil {
|
_, err = ss.Reaction().Delete(reaction)
|
||||||
t.Fatal(err)
|
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)
|
postList, err := ss.Post().Get(post.Id, false)
|
||||||
if err != nil {
|
require.Nil(t, err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
assert.False(t, postList.Posts[post.Id].HasReactions, "should've set HasReactions = false on post")
|
||||||
if postList.Posts[post.Id].HasReactions {
|
assert.NotEqual(t, postList.Posts[post.Id].UpdateAt, firstUpdateAt, "should mark post as updated after deleting reactions")
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testReactionGetForPost(t *testing.T, ss store.Store) {
|
func testReactionGetForPost(t *testing.T, ss store.Store) {
|
||||||
@@ -182,53 +167,49 @@ func testReactionGetForPost(t *testing.T, ss store.Store) {
|
|||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if returned, err := ss.Reaction().GetForPost(postId, false); err != nil {
|
returned, err := ss.Reaction().GetForPost(postId, false)
|
||||||
t.Fatal(err)
|
require.Nil(t, err)
|
||||||
} else if len(returned) != 3 {
|
require.Len(t, returned, 3, "should've returned 3 reactions")
|
||||||
t.Fatal("should've returned 3 reactions")
|
|
||||||
} else {
|
|
||||||
for _, reaction := range reactions {
|
|
||||||
found := false
|
|
||||||
|
|
||||||
for _, returnedReaction := range returned {
|
for _, reaction := range reactions {
|
||||||
if returnedReaction.UserId == reaction.UserId && returnedReaction.PostId == reaction.PostId &&
|
found := false
|
||||||
returnedReaction.EmojiName == reaction.EmojiName {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !found && reaction.PostId == postId {
|
for _, returnedReaction := range returned {
|
||||||
t.Fatalf("should've returned reaction for post %v", reaction)
|
if returnedReaction.UserId == reaction.UserId && returnedReaction.PostId == reaction.PostId &&
|
||||||
} else if found && reaction.PostId != postId {
|
returnedReaction.EmojiName == reaction.EmojiName {
|
||||||
t.Fatal("shouldn't have returned reaction for another post")
|
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
|
// Should return cached item
|
||||||
if returned, err := ss.Reaction().GetForPost(postId, true); err != nil {
|
returned, err = ss.Reaction().GetForPost(postId, true)
|
||||||
t.Fatal(err)
|
require.Nil(t, err)
|
||||||
} else if len(returned) != 3 {
|
require.Len(t, returned, 3, "should've returned 3 reactions")
|
||||||
t.Fatal("should've returned 3 reactions")
|
|
||||||
} else {
|
|
||||||
for _, reaction := range reactions {
|
|
||||||
found := false
|
|
||||||
|
|
||||||
for _, returnedReaction := range returned {
|
for _, reaction := range reactions {
|
||||||
if returnedReaction.UserId == reaction.UserId && returnedReaction.PostId == reaction.PostId &&
|
found := false
|
||||||
returnedReaction.EmojiName == reaction.EmojiName {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !found && reaction.PostId == postId {
|
for _, returnedReaction := range returned {
|
||||||
t.Fatalf("should've returned reaction for post %v", reaction)
|
if returnedReaction.UserId == reaction.UserId && returnedReaction.PostId == reaction.PostId &&
|
||||||
} else if found && reaction.PostId != postId {
|
returnedReaction.EmojiName == reaction.EmojiName {
|
||||||
t.Fatal("shouldn't have returned reaction for another post")
|
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)
|
require.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ss.Reaction().DeleteAllWithEmojiName(emojiToDelete); err != nil {
|
err := ss.Reaction().DeleteAllWithEmojiName(emojiToDelete)
|
||||||
t.Fatal(err)
|
require.Nil(t, err)
|
||||||
}
|
|
||||||
|
|
||||||
// check that the reactions were deleted
|
// check that the reactions were deleted
|
||||||
if returned, err := ss.Reaction().GetForPost(post.Id, false); err != nil {
|
returned, err := ss.Reaction().GetForPost(post.Id, false)
|
||||||
t.Fatal(err)
|
require.Nil(t, err)
|
||||||
} else if len(returned) != 1 {
|
require.Len(t, returned, 1, "should've only removed reactions with emoji name")
|
||||||
t.Fatal("should've only removed reactions with emoji name")
|
|
||||||
} else {
|
for _, reaction := range returned {
|
||||||
for _, reaction := range returned {
|
assert.NotEqual(t, reaction.EmojiName, "smile", "should've removed reaction with emoji name")
|
||||||
if reaction.EmojiName == "smile" {
|
|
||||||
t.Fatal("should've removed reaction with emoji name")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if returned, err := ss.Reaction().GetForPost(post2.Id, false); err != nil {
|
returned, err = ss.Reaction().GetForPost(post2.Id, false)
|
||||||
t.Fatal(err)
|
require.Nil(t, err)
|
||||||
} else if len(returned) != 1 {
|
assert.Len(t, returned, 1, "should've only removed reactions with emoji name")
|
||||||
t.Fatal("should've only removed reactions with emoji name")
|
|
||||||
}
|
|
||||||
|
|
||||||
if returned, err := ss.Reaction().GetForPost(post3.Id, false); err != nil {
|
returned, err = ss.Reaction().GetForPost(post3.Id, false)
|
||||||
t.Fatal(err)
|
require.Nil(t, err)
|
||||||
} else if len(returned) != 0 {
|
assert.Len(t, returned, 0, "should've only removed reactions with emoji name")
|
||||||
t.Fatal("should've only removed reactions with emoji name")
|
|
||||||
}
|
|
||||||
|
|
||||||
// check that the posts are updated
|
// check that the posts are updated
|
||||||
postList, err := ss.Post().Get(post.Id, false)
|
postList, err := ss.Post().Get(post.Id, false)
|
||||||
if err != nil {
|
require.Nil(t, err)
|
||||||
t.Fatal(err)
|
assert.True(t, postList.Posts[post.Id].HasReactions, "post should still have reactions")
|
||||||
}
|
|
||||||
if !postList.Posts[post.Id].HasReactions {
|
|
||||||
t.Fatal("post should still have reactions")
|
|
||||||
}
|
|
||||||
|
|
||||||
postList, err = ss.Post().Get(post2.Id, false)
|
postList, err = ss.Post().Get(post2.Id, false)
|
||||||
if err != nil {
|
require.Nil(t, err)
|
||||||
t.Fatal(err)
|
assert.True(t, postList.Posts[post2.Id].HasReactions, "post should still have reactions")
|
||||||
}
|
|
||||||
if !postList.Posts[post2.Id].HasReactions {
|
|
||||||
t.Fatal("post should still have reactions")
|
|
||||||
}
|
|
||||||
|
|
||||||
postList, err = ss.Post().Get(post3.Id, false)
|
postList, err = ss.Post().Get(post3.Id, false)
|
||||||
if err != nil {
|
require.Nil(t, err)
|
||||||
t.Fatal(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) {
|
func testReactionStorePermanentDeleteBatch(t *testing.T, ss store.Store) {
|
||||||
@@ -384,24 +344,20 @@ func testReactionStorePermanentDeleteBatch(t *testing.T, ss store.Store) {
|
|||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if returned, err := ss.Reaction().GetForPost(post.Id, false); err != nil {
|
returned, err := ss.Reaction().GetForPost(post.Id, false)
|
||||||
t.Fatal(err)
|
require.Nil(t, err)
|
||||||
} else if len(returned) != 4 {
|
require.Len(t, returned, 4, "expected 4 reactions")
|
||||||
t.Fatal("expected 4 reactions")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := ss.Reaction().PermanentDeleteBatch(1800, 1000)
|
_, err = ss.Reaction().PermanentDeleteBatch(1800, 1000)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|
||||||
// This is to force a clear of the cache.
|
// This is to force a clear of the cache.
|
||||||
_, err = ss.Reaction().Delete(lastReaction)
|
_, err = ss.Reaction().Delete(lastReaction)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
|
|
||||||
if returned, err := ss.Reaction().GetForPost(post.Id, false); err != nil {
|
returned, err = ss.Reaction().GetForPost(post.Id, false)
|
||||||
t.Fatal(err)
|
require.Nil(t, err)
|
||||||
} else if len(returned) != 1 {
|
require.Len(t, returned, 1, "expected 1 reaction. Got: %v", len(returned))
|
||||||
t.Fatalf("expected 1 reaction. Got: %v", len(returned))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testReactionBulkGetForPosts(t *testing.T, ss store.Store) {
|
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}
|
postIds := []string{postId, post2Id, post3Id}
|
||||||
if returned, err := ss.Reaction().BulkGetForPosts(postIds); err != nil {
|
returned, err := ss.Reaction().BulkGetForPosts(postIds)
|
||||||
t.Fatal(err)
|
require.Nil(t, err)
|
||||||
} else if len(returned) != 5 {
|
require.Len(t, returned, 5, "should've returned 5 reactions")
|
||||||
t.Fatal("should've returned 5 reactions")
|
|
||||||
} else {
|
|
||||||
post4IdFound := false
|
|
||||||
for _, reaction := range returned {
|
|
||||||
if reaction.PostId == post4Id {
|
|
||||||
post4IdFound = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if post4IdFound {
|
post4IdFound := false
|
||||||
t.Fatal("Wrong reaction returned")
|
for _, reaction := range returned {
|
||||||
|
if reaction.PostId == post4Id {
|
||||||
|
post4IdFound = true
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
require.False(t, post4IdFound, "Wrong reaction returned")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user