From babbe087ff44b8ba4302403acfe9795ef11ba3f5 Mon Sep 17 00:00:00 2001 From: Jairo Junior Date: Sun, 13 Oct 2019 16:38:28 -0300 Subject: [PATCH] Convert app/import_test.go t.Fatal calls into assert/require calls (#12228) --- app/import_test.go | 158 +++++++++++++++++---------------------------- 1 file changed, 58 insertions(+), 100 deletions(-) diff --git a/app/import_test.go b/app/import_test.go index 5bb4c1097d..c06aeb4e84 100644 --- a/app/import_test.go +++ b/app/import_test.go @@ -6,7 +6,6 @@ package app import ( "net/http" "path/filepath" - "runtime/debug" "strings" "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) { - if preferences, err := a.Srv.Store.Preference().GetCategory(userId, category); err != nil { - debug.PrintStack() - t.Fatalf("Failed to get preferences for user %v with category %v", userId, category) - } else { - found := false - for _, preference := range preferences { - if preference.Name == name { - found = true - 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) + preferences, err := a.Srv.Store.Preference().GetCategory(userId, category) + require.Nilf(t, err, "Failed to get preferences for user %v with category %v", userId, category) + found := false + for _, preference := range preferences { + if preference.Name == name { + found = true + 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) + break } } + 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) { - if actual, ok := user.NotifyProps[key]; !ok { - debug.PrintStack() - t.Fatalf("Notify prop %v not found. User: %v", key, 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) - } + actual, ok := user.NotifyProps[key] + require.True(t, ok, "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) } func checkError(t *testing.T, err *model.AppError) { - if err == nil { - debug.PrintStack() - t.Fatal("Should have returned an error.") - } + require.NotNil(t, err, "Should have returned an error.") } func checkNoError(t *testing.T, err *model.AppError) { - if err != nil { - debug.PrintStack() - t.Fatalf("Unexpected Error: %v", err.Error()) - } + require.Nil(t, err, "Unexpected Error: %v", err) } 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 { - t.Fatal(err) - } else { - if initialCount+change != result { - debug.PrintStack() - t.Fatalf("Did not find the expected number of posts.") - } - } + result, err := a.Srv.Store.Post().AnalyticsPostCount(teamName, false, false) + require.Nil(t, err) + require.Equal(t, initialCount+change, result, "Did not find the expected number of posts.") } func AssertChannelCount(t *testing.T, a *App, channelType string, expectedCount int64) { - if count, err := a.Srv.Store.Channel().AnalyticsTypeCount("", channelType); err == nil { - if count != expectedCount { - debug.PrintStack() - t.Fatalf("Channel count of type: %v. Expected: %v, Got: %v", channelType, expectedCount, count) - } - } else { - debug.PrintStack() - t.Fatalf("Failed to get channel count.") - } + count, err := a.Srv.Store.Channel().AnalyticsTypeCount("", channelType) + require.Equalf(t, expectedCount, count, "Channel count of type: %v. Expected: %v, Got: %v", channelType, expectedCount, count) + require.Nil(t, err, "Failed to get channel count.") } func TestImportImportLine(t *testing.T) { @@ -112,51 +81,43 @@ func TestImportImportLine(t *testing.T) { Type: "gibberish", } - if err := th.App.ImportLine(line, false); err == nil { - t.Fatalf("Expected an error when importing a line with invalid type.") - } + err := th.App.ImportLine(line, false) + require.NotNil(t, err, "Expected an error when importing a line with invalid type.") // Try import line with team type but nil team. line.Type = "team" - if err := th.App.ImportLine(line, false); err == nil { - t.Fatalf("Expected an error when importing a line of type team with a nil team.") - } + err = th.App.ImportLine(line, false) + 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. line.Type = "channel" - if err := th.App.ImportLine(line, false); err == nil { - t.Fatalf("Expected an error when importing a line with type channel with a nil channel.") - } + err = th.App.ImportLine(line, false) + 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. line.Type = "user" - if err := th.App.ImportLine(line, false); err == nil { - t.Fatalf("Expected an error when importing a line with type uesr with a nil user.") - } + err = th.App.ImportLine(line, false) + 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. line.Type = "post" - if err := th.App.ImportLine(line, false); err == nil { - t.Fatalf("Expected an error when importing a line with type post with a nil post.") - } + err = th.App.ImportLine(line, false) + 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. line.Type = "direct_channel" - if err := th.App.ImportLine(line, false); err == nil { - t.Fatalf("Expected an error when importing a line with type direct_channel with a nil direct_channel.") - } + err = th.App.ImportLine(line, false) + 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. line.Type = "direct_post" - if err := th.App.ImportLine(line, false); err == nil { - t.Fatalf("Expected an error when importing a line with type direct_post with a nil direct_post.") - } + err = th.App.ImportLine(line, false) + 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. line.Type = "scheme" - if err := th.App.ImportLine(line, false); err == nil { - t.Fatalf("Expected an error when importing a line with type scheme with a nil scheme.") - } + err = th.App.ImportLine(line, false) + require.NotNil(t, err, "Expected an error when importing a line with type scheme with a nil scheme.") } 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": "emoji", "emoji": {"name": "` + emojiName + `", "image": "` + testImage + `"}}` - if err, line := th.App.BulkImport(strings.NewReader(data1), false, 2); err != nil || line != 0 { - t.Fatalf("BulkImport should have succeeded: %v, %v", err.Error(), line) - } + err, line := th.App.BulkImport(strings.NewReader(data1), false, 2) + 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. data2 := `{"type": "version", "version": 1` - if err, line := th.App.BulkImport(strings.NewReader(data2), false, 2); err == nil || line != 1 { - t.Fatalf("Should have failed due to invalid JSON on line 1.") - } + err, line = th.App.BulkImport(strings.NewReader(data2), false, 2) + 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. data3 := `{"type": "team", "team": {"type": "O", "display_name": "lskmw2d7a5ao7ppwqh5ljchvr4", "name": "` + teamName + `"}} {"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": "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 { - t.Fatalf("Should have failed due to missing version line on line 1.") - } + err, line = th.App.BulkImport(strings.NewReader(data3), false, 2) + 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) { data := `{"type": "version", "version": 1} @@ -241,20 +202,18 @@ func TestImportProcessImportDataFileVersionLine(t *testing.T) { Type: "version", Version: ptrInt(1), } - if version, err := processImportDataFileVersionLine(data); err != nil || version != 1 { - t.Fatalf("Expected no error and version 1.") - } + version, err := processImportDataFileVersionLine(data) + require.Nil(t, err, "Expected no error") + require.Equal(t, 1, version, "Expected version 1") data.Type = "NotVersion" - if _, err := processImportDataFileVersionLine(data); err == nil { - t.Fatalf("Expected error on invalid version line.") - } + _, err = processImportDataFileVersionLine(data) + require.NotNil(t, err, "Expected error on invalid version line.") data.Type = "version" data.Version = nil - if _, err := processImportDataFileVersionLine(data); err == nil { - t.Fatalf("Expected error on invalid version line.") - } + _, err = processImportDataFileVersionLine(data) + require.NotNil(t, err, "Expected error on invalid version line.") } 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 assert.NotNil(t, postId) - if posts, err := th.App.Srv.Store.Post().GetPostsByIds([]string{postId}); err != nil { - t.Fatal(err.Error()) - } else { - assert.Equal(t, len(posts), 1) - for _, file := range files { - assert.Contains(t, posts[0].FileIds, file.Id) - } + posts, err := th.App.Srv.Store.Post().GetPostsByIds([]string{postId}) + require.Nil(t, err) + + assert.Equal(t, len(posts), 1) + for _, file := range files { + assert.Contains(t, posts[0].FileIds, file.Id) } }