From 5f9326d1656633051ac202b08fc97d8522660159 Mon Sep 17 00:00:00 2001 From: Chris Rhodes Date: Wed, 20 Jan 2016 23:38:17 -0800 Subject: [PATCH] Clean up tests further. --- discord_test.go | 45 ++++++++++++--------------------------------- restapi_test.go | 38 ++++++++++++++------------------------ 2 files changed, 26 insertions(+), 57 deletions(-) diff --git a/discord_test.go b/discord_test.go index 77620d7..28c21e9 100644 --- a/discord_test.go +++ b/discord_test.go @@ -21,6 +21,16 @@ var ( envAdmin string = os.Getenv("DG_ADMIN") // User ID of admin user to use for tests ) +func init() { + if envEmail == "" || envPassword == "" || envToken == "" { + return + } + + if d, err := New(envEmail, envPassword, envToken); err == nil { + dg = d + } +} + ////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////// HELPER FUNCTIONS USED FOR TESTING @@ -76,15 +86,10 @@ func TestNew(t *testing.T) { // TestInvalidToken tests the New() function with an invalid token func TestInvalidToken(t *testing.T) { - d, err := New("asjkldhflkjasdh") + _, err := New("asjkldhflkjasdh") if err != nil { t.Fatalf("New(InvalidToken) returned error: %+v", err) } - - if err = d.OpenAndListen(); err == nil { - t.Fatalf("New(InvalidToken), d.OpenAndListen did not fail.") - } - } // TestInvalidUserPass tests the New() function with an invalid Email and Pass @@ -132,20 +137,6 @@ func TestNewUserPass(t *testing.T) { if d.Token == "" { t.Fatal("New(user,pass), d.Token is empty, should be a valid Token.") } - - if err = d.OpenAndListen(); err != nil { - t.Fatalf("New(user,pass), d.OpenAndListen failed: %+v", err) - } - - if !waitBoolEqual(10*time.Second, &d.DataReady, true) { - t.Fatal("New(user,pass), d.DataReady is false after 10 seconds. Should be true.") - } - - t.Log("Successfully connected to Discord via New(user,pass).") - dg = d - if envToken == "" { - envToken = dg.Token - } } // TestNewToken tests the New() function with a Token. This should return @@ -168,21 +159,9 @@ func TestNewToken(t *testing.T) { if d.Token == "" { t.Fatal("New(envToken), d.Token is empty, should be a valid Token.") } - - if err = d.OpenAndListen(); err != nil { - t.Fatalf("New(envToken), d.OpenAndListen failed: %+v", err) - } - - if !waitBoolEqual(10*time.Second, &d.DataReady, true) { - t.Fatal("New(envToken), d.DataReady is false after 10 seconds. Should be true.") - } - - t.Log("Successfully connected to Discord via New(token).") - dg = d - } -func TestClose(t *testing.T) { +func TestOpenClose(t *testing.T) { if envToken == "" { t.Skip("Skipping TestClose, DG_TOKEN not set") } diff --git a/restapi_test.go b/restapi_test.go index b24b61a..d0f3e80 100644 --- a/restapi_test.go +++ b/restapi_test.go @@ -10,8 +10,8 @@ import ( // TestLogout tests the Logout() function. This should not return an error. func TestLogout(t *testing.T) { - if dg == nil || dg.Token == "" { - t.Skip("Cannot test logout, dg.Token not set.") + if dg == nil { + t.Skip("Cannot TestLogout, dg not set.") } err := dg.Logout() @@ -21,8 +21,8 @@ func TestLogout(t *testing.T) { } func TestUserAvatar(t *testing.T) { - if !isConnected() { - t.Skip("Skipped, Not connected to Discord.") + if dg == nil { + t.Skip("Cannot TestUserAvatar, dg not set.") } a, err := dg.UserAvatar("@me") @@ -39,14 +39,8 @@ func TestUserAvatar(t *testing.T) { } func TestUserUpdate(t *testing.T) { - - if envEmail == "" || envPassword == "" { - t.Skip("Skipping, DG_USERNAME or DG_PASSWORD not set") - return - } - - if !isConnected() { - t.Skip("Skipped, Not connected to Discord.") + if dg == nil { + t.Skip("Cannot test logout, dg not set.") } u, err := dg.User("@me") @@ -73,9 +67,8 @@ func TestUserUpdate(t *testing.T) { //func (s *Session) UserChannelCreate(recipientID string) (st *Channel, err error) { func TestUserChannelCreate(t *testing.T) { - - if !isConnected() { - t.Skip("Skipped, Not connected to Discord.") + if dg == nil { + t.Skip("Cannot TestUserChannelCreate, dg not set.") } if envAdmin == "" { @@ -91,9 +84,8 @@ func TestUserChannelCreate(t *testing.T) { } func TestUserChannels(t *testing.T) { - - if !isConnected() { - t.Skip("Skipped, Not connected to Discord.") + if dg == nil { + t.Skip("Cannot TestUserChannels, dg not set.") } _, err := dg.UserChannels() @@ -103,9 +95,8 @@ func TestUserChannels(t *testing.T) { } func TestUserGuilds(t *testing.T) { - - if !isConnected() { - t.Skip("Skipped, Not connected to Discord.") + if dg == nil { + t.Skip("Cannot TestUserGuilds, dg not set.") } _, err := dg.UserGuilds() @@ -115,9 +106,8 @@ func TestUserGuilds(t *testing.T) { } func TestUserSettings(t *testing.T) { - - if !isConnected() { - t.Skip("Skipped, Not connected to Discord.") + if dg == nil { + t.Skip("Cannot TestUserSettings, dg not set.") } _, err := dg.UserSettings()