Convert app/import_test.go t.Fatal calls into assert/require calls (#12228)

Этот коммит содержится в:
Jairo Junior
2019-10-13 16:38:28 -03:00
коммит произвёл Jesús Espino
родитель 705090999b
Коммит babbe087ff

Просмотреть файл

@@ -6,7 +6,6 @@ package app
import ( import (
"net/http" "net/http"
"path/filepath" "path/filepath"
"runtime/debug"
"strings" "strings"
"testing" "testing"
@@ -34,73 +33,43 @@ func ptrBool(b bool) *bool {
} }
func checkPreference(t *testing.T, a *App, userId string, category string, name string, value string) { func checkPreference(t *testing.T, a *App, userId string, category string, name string, value string) {
if preferences, err := a.Srv.Store.Preference().GetCategory(userId, category); err != nil { preferences, err := a.Srv.Store.Preference().GetCategory(userId, category)
debug.PrintStack() require.Nilf(t, err, "Failed to get preferences for user %v with category %v", userId, category)
t.Fatalf("Failed to get preferences for user %v with category %v", userId, category) found := false
} else { for _, preference := range preferences {
found := false if preference.Name == name {
for _, preference := range preferences { found = true
if preference.Name == name { require.Equal(t, preference.Value, value, "Preference for user %v in category %v with name %v has value %v, expected %v", userId, category, name, preference.Value, value)
found = true break
if preference.Value != value {
debug.PrintStack()
t.Fatalf("Preference for user %v in category %v with name %v has value %v, expected %v", userId, category, name, preference.Value, value)
}
break
}
}
if !found {
debug.PrintStack()
t.Fatalf("Did not find preference for user %v in category %v with name %v", userId, category, name)
} }
} }
require.Truef(t, found, "Did not find preference for user %v in category %v with name %v", userId, category, name)
} }
func checkNotifyProp(t *testing.T, user *model.User, key string, value string) { func checkNotifyProp(t *testing.T, user *model.User, key string, value string) {
if actual, ok := user.NotifyProps[key]; !ok { actual, ok := user.NotifyProps[key]
debug.PrintStack() require.True(t, ok, "Notify prop %v not found. User: %v", key, user.Id)
t.Fatalf("Notify prop %v not found. User: %v", key, user.Id) require.Equalf(t, actual, value, "Notify Prop %v was %v but expected %v. User: %v", key, actual, value, user.Id)
} else if actual != value {
debug.PrintStack()
t.Fatalf("Notify Prop %v was %v but expected %v. User: %v", key, actual, value, user.Id)
}
} }
func checkError(t *testing.T, err *model.AppError) { func checkError(t *testing.T, err *model.AppError) {
if err == nil { require.NotNil(t, err, "Should have returned an error.")
debug.PrintStack()
t.Fatal("Should have returned an error.")
}
} }
func checkNoError(t *testing.T, err *model.AppError) { func checkNoError(t *testing.T, err *model.AppError) {
if err != nil { require.Nil(t, err, "Unexpected Error: %v", err)
debug.PrintStack()
t.Fatalf("Unexpected Error: %v", err.Error())
}
} }
func AssertAllPostsCount(t *testing.T, a *App, initialCount int64, change int64, teamName string) { func AssertAllPostsCount(t *testing.T, a *App, initialCount int64, change int64, teamName string) {
if result, err := a.Srv.Store.Post().AnalyticsPostCount(teamName, false, false); err != nil { result, err := a.Srv.Store.Post().AnalyticsPostCount(teamName, false, false)
t.Fatal(err) require.Nil(t, err)
} else { require.Equal(t, initialCount+change, result, "Did not find the expected number of posts.")
if initialCount+change != result {
debug.PrintStack()
t.Fatalf("Did not find the expected number of posts.")
}
}
} }
func AssertChannelCount(t *testing.T, a *App, channelType string, expectedCount int64) { func AssertChannelCount(t *testing.T, a *App, channelType string, expectedCount int64) {
if count, err := a.Srv.Store.Channel().AnalyticsTypeCount("", channelType); err == nil { count, err := a.Srv.Store.Channel().AnalyticsTypeCount("", channelType)
if count != expectedCount { require.Equalf(t, expectedCount, count, "Channel count of type: %v. Expected: %v, Got: %v", channelType, expectedCount, count)
debug.PrintStack() require.Nil(t, err, "Failed to get channel count.")
t.Fatalf("Channel count of type: %v. Expected: %v, Got: %v", channelType, expectedCount, count)
}
} else {
debug.PrintStack()
t.Fatalf("Failed to get channel count.")
}
} }
func TestImportImportLine(t *testing.T) { func TestImportImportLine(t *testing.T) {
@@ -112,51 +81,43 @@ func TestImportImportLine(t *testing.T) {
Type: "gibberish", Type: "gibberish",
} }
if err := th.App.ImportLine(line, false); err == nil { err := th.App.ImportLine(line, false)
t.Fatalf("Expected an error when importing a line with invalid type.") require.NotNil(t, err, "Expected an error when importing a line with invalid type.")
}
// Try import line with team type but nil team. // Try import line with team type but nil team.
line.Type = "team" line.Type = "team"
if err := th.App.ImportLine(line, false); err == nil { err = th.App.ImportLine(line, false)
t.Fatalf("Expected an error when importing a line of type team with a nil team.") require.NotNil(t, err, "Expected an error when importing a line of type team with a nil team.")
}
// Try import line with channel type but nil channel. // Try import line with channel type but nil channel.
line.Type = "channel" line.Type = "channel"
if err := th.App.ImportLine(line, false); err == nil { err = th.App.ImportLine(line, false)
t.Fatalf("Expected an error when importing a line with type channel with a nil channel.") require.NotNil(t, err, "Expected an error when importing a line with type channel with a nil channel.")
}
// Try import line with user type but nil user. // Try import line with user type but nil user.
line.Type = "user" line.Type = "user"
if err := th.App.ImportLine(line, false); err == nil { err = th.App.ImportLine(line, false)
t.Fatalf("Expected an error when importing a line with type uesr with a nil user.") require.NotNil(t, err, "Expected an error when importing a line with type user with a nil user.")
}
// Try import line with post type but nil post. // Try import line with post type but nil post.
line.Type = "post" line.Type = "post"
if err := th.App.ImportLine(line, false); err == nil { err = th.App.ImportLine(line, false)
t.Fatalf("Expected an error when importing a line with type post with a nil post.") require.NotNil(t, err, "Expected an error when importing a line with type post with a nil post.")
}
// Try import line with direct_channel type but nil direct_channel. // Try import line with direct_channel type but nil direct_channel.
line.Type = "direct_channel" line.Type = "direct_channel"
if err := th.App.ImportLine(line, false); err == nil { err = th.App.ImportLine(line, false)
t.Fatalf("Expected an error when importing a line with type direct_channel with a nil direct_channel.") require.NotNil(t, err, "Expected an error when importing a line with type direct_channel with a nil direct_channel.")
}
// Try import line with direct_post type but nil direct_post. // Try import line with direct_post type but nil direct_post.
line.Type = "direct_post" line.Type = "direct_post"
if err := th.App.ImportLine(line, false); err == nil { err = th.App.ImportLine(line, false)
t.Fatalf("Expected an error when importing a line with type direct_post with a nil direct_post.") require.NotNil(t, err, "Expected an error when importing a line with type direct_post with a nil direct_post.")
}
// Try import line with scheme type but nil scheme. // Try import line with scheme type but nil scheme.
line.Type = "scheme" line.Type = "scheme"
if err := th.App.ImportLine(line, false); err == nil { err = th.App.ImportLine(line, false)
t.Fatalf("Expected an error when importing a line with type scheme with a nil scheme.") require.NotNil(t, err, "Expected an error when importing a line with type scheme with a nil scheme.")
}
} }
func TestStopOnError(t *testing.T) { func TestStopOnError(t *testing.T) {
@@ -208,24 +169,24 @@ func TestImportBulkImport(t *testing.T) {
{"type": "direct_post", "direct_post": {"channel_members": ["` + username + `", "` + username2 + `", "` + username3 + `"], "user": "` + username + `", "message": "Hello Group Channel", "create_at": 123456789015}} {"type": "direct_post", "direct_post": {"channel_members": ["` + username + `", "` + username2 + `", "` + username3 + `"], "user": "` + username + `", "message": "Hello Group Channel", "create_at": 123456789015}}
{"type": "emoji", "emoji": {"name": "` + emojiName + `", "image": "` + testImage + `"}}` {"type": "emoji", "emoji": {"name": "` + emojiName + `", "image": "` + testImage + `"}}`
if err, line := th.App.BulkImport(strings.NewReader(data1), false, 2); err != nil || line != 0 { err, line := th.App.BulkImport(strings.NewReader(data1), false, 2)
t.Fatalf("BulkImport should have succeeded: %v, %v", err.Error(), line) require.Nil(t, err, "BulkImport should have succeeded")
} require.Equal(t, 0, line, "BulkImport line should be 0")
// Run bulk import using a string that contains a line with invalid json. // Run bulk import using a string that contains a line with invalid json.
data2 := `{"type": "version", "version": 1` data2 := `{"type": "version", "version": 1`
if err, line := th.App.BulkImport(strings.NewReader(data2), false, 2); err == nil || line != 1 { err, line = th.App.BulkImport(strings.NewReader(data2), false, 2)
t.Fatalf("Should have failed due to invalid JSON on line 1.") require.NotNil(t, err, "Should have failed due to invalid JSON on line 1.")
} require.Equal(t, 1, line, "Should have failed due to invalid JSON on line 1.")
// Run bulk import using valid JSON but missing version line at the start. // Run bulk import using valid JSON but missing version line at the start.
data3 := `{"type": "team", "team": {"type": "O", "display_name": "lskmw2d7a5ao7ppwqh5ljchvr4", "name": "` + teamName + `"}} data3 := `{"type": "team", "team": {"type": "O", "display_name": "lskmw2d7a5ao7ppwqh5ljchvr4", "name": "` + teamName + `"}}
{"type": "channel", "channel": {"type": "O", "display_name": "xr6m6udffngark2uekvr3hoeny", "team": "` + teamName + `", "name": "` + channelName + `"}} {"type": "channel", "channel": {"type": "O", "display_name": "xr6m6udffngark2uekvr3hoeny", "team": "` + teamName + `", "name": "` + channelName + `"}}
{"type": "user", "user": {"username": "kufjgnkxkrhhfgbrip6qxkfsaa", "email": "kufjgnkxkrhhfgbrip6qxkfsaa@example.com"}} {"type": "user", "user": {"username": "kufjgnkxkrhhfgbrip6qxkfsaa", "email": "kufjgnkxkrhhfgbrip6qxkfsaa@example.com"}}
{"type": "user", "user": {"username": "bwshaim6qnc2ne7oqkd5b2s2rq", "email": "bwshaim6qnc2ne7oqkd5b2s2rq@example.com", "teams": [{"name": "` + teamName + `", "channels": [{"name": "` + channelName + `"}]}]}}` {"type": "user", "user": {"username": "bwshaim6qnc2ne7oqkd5b2s2rq", "email": "bwshaim6qnc2ne7oqkd5b2s2rq@example.com", "teams": [{"name": "` + teamName + `", "channels": [{"name": "` + channelName + `"}]}]}}`
if err, line := th.App.BulkImport(strings.NewReader(data3), false, 2); err == nil || line != 1 { err, line = th.App.BulkImport(strings.NewReader(data3), false, 2)
t.Fatalf("Should have failed due to missing version line on line 1.") require.NotNil(t, err, "Should have failed due to missing version line on line 1.")
} require.Equal(t, 1, line, "Should have failed due to missing version line on line 1.")
t.Run("First item after version without type", func(t *testing.T) { t.Run("First item after version without type", func(t *testing.T) {
data := `{"type": "version", "version": 1} data := `{"type": "version", "version": 1}
@@ -241,20 +202,18 @@ func TestImportProcessImportDataFileVersionLine(t *testing.T) {
Type: "version", Type: "version",
Version: ptrInt(1), Version: ptrInt(1),
} }
if version, err := processImportDataFileVersionLine(data); err != nil || version != 1 { version, err := processImportDataFileVersionLine(data)
t.Fatalf("Expected no error and version 1.") require.Nil(t, err, "Expected no error")
} require.Equal(t, 1, version, "Expected version 1")
data.Type = "NotVersion" data.Type = "NotVersion"
if _, err := processImportDataFileVersionLine(data); err == nil { _, err = processImportDataFileVersionLine(data)
t.Fatalf("Expected error on invalid version line.") require.NotNil(t, err, "Expected error on invalid version line.")
}
data.Type = "version" data.Type = "version"
data.Version = nil data.Version = nil
if _, err := processImportDataFileVersionLine(data); err == nil { _, err = processImportDataFileVersionLine(data)
t.Fatalf("Expected error on invalid version line.") require.NotNil(t, err, "Expected error on invalid version line.")
}
} }
func GetAttachments(userId string, th *TestHelper, t *testing.T) []*model.FileInfo { func GetAttachments(userId string, th *TestHelper, t *testing.T) []*model.FileInfo {
@@ -267,12 +226,11 @@ func AssertFileIdsInPost(files []*model.FileInfo, th *TestHelper, t *testing.T)
postId := files[0].PostId postId := files[0].PostId
assert.NotNil(t, postId) assert.NotNil(t, postId)
if posts, err := th.App.Srv.Store.Post().GetPostsByIds([]string{postId}); err != nil { posts, err := th.App.Srv.Store.Post().GetPostsByIds([]string{postId})
t.Fatal(err.Error()) require.Nil(t, err)
} else {
assert.Equal(t, len(posts), 1) assert.Equal(t, len(posts), 1)
for _, file := range files { for _, file := range files {
assert.Contains(t, posts[0].FileIds, file.Id) assert.Contains(t, posts[0].FileIds, file.Id)
}
} }
} }