fix error tests
This commit is contained in:
parent
a611486a08
commit
3bfec2211f
2 changed files with 21 additions and 8 deletions
22
profile.go
22
profile.go
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -42,8 +43,9 @@ type user struct {
|
|||
Data struct {
|
||||
User struct {
|
||||
Result struct {
|
||||
RestID string `json:"rest_id"`
|
||||
Legacy legacyUser `json:"legacy"`
|
||||
RestID string `json:"rest_id"`
|
||||
Legacy legacyUser `json:"legacy"`
|
||||
Message string `json:"message"`
|
||||
} `json:"result"`
|
||||
} `json:"user"`
|
||||
} `json:"data"`
|
||||
|
|
@ -91,11 +93,17 @@ func (s *Scraper) GetProfile(username string) (Profile, error) {
|
|||
}
|
||||
|
||||
if len(jsn.Errors) > 0 && jsn.Data.User.Result.RestID == "" {
|
||||
if strings.Contains(jsn.Errors[0].Message, "Missing LdapGroup(visibility-custom-suspension)") {
|
||||
return Profile{}, fmt.Errorf("user is suspended")
|
||||
}
|
||||
return Profile{}, fmt.Errorf("%s", jsn.Errors[0].Message)
|
||||
}
|
||||
|
||||
if jsn.Data.User.Result.RestID == "" {
|
||||
return Profile{}, fmt.Errorf("rest_id not found")
|
||||
if jsn.Data.User.Result.Message == "User is suspended" {
|
||||
return Profile{}, fmt.Errorf("user is suspended")
|
||||
}
|
||||
return Profile{}, fmt.Errorf("user not found")
|
||||
}
|
||||
jsn.Data.User.Result.Legacy.IDStr = jsn.Data.User.Result.RestID
|
||||
|
||||
|
|
@ -142,11 +150,17 @@ func (s *Scraper) GetProfileByID(userID string) (Profile, error) {
|
|||
}
|
||||
|
||||
if len(jsn.Errors) > 0 && jsn.Data.User.Result.RestID == "" {
|
||||
if strings.Contains(jsn.Errors[0].Message, "Missing LdapGroup(visibility-custom-suspension)") {
|
||||
return Profile{}, fmt.Errorf("user is suspended")
|
||||
}
|
||||
return Profile{}, fmt.Errorf("%s", jsn.Errors[0].Message)
|
||||
}
|
||||
|
||||
if jsn.Data.User.Result.RestID == "" {
|
||||
return Profile{}, fmt.Errorf("rest_id not found")
|
||||
if jsn.Data.User.Result.Message == "User is suspended" {
|
||||
return Profile{}, fmt.Errorf("user is suspended")
|
||||
}
|
||||
return Profile{}, fmt.Errorf("user not found")
|
||||
}
|
||||
jsn.Data.User.Result.Legacy.IDStr = jsn.Data.User.Result.RestID
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package twitterscraper_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
|
@ -116,15 +115,15 @@ func TestGetProfileErrorSuspended(t *testing.T) {
|
|||
if err == nil {
|
||||
t.Error("Expected Error, got success")
|
||||
} else {
|
||||
if !strings.Contains(err.Error(), "Missing LdapGroup(visibility-custom-suspension)") {
|
||||
t.Error("Expected error to contain 'Missing LdapGroup(visibility-custom-suspension)', got", err)
|
||||
if !strings.Contains(err.Error(), "suspended") {
|
||||
t.Error("Expected error to contain 'suspended', got", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetProfileErrorNotFound(t *testing.T) {
|
||||
neUser := "sample3123131"
|
||||
expectedError := fmt.Sprintf("User '%s' not found", neUser)
|
||||
expectedError := "user not found"
|
||||
_, err := testScraper.GetProfile(neUser)
|
||||
if err == nil {
|
||||
t.Error("Expected Error, got success")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue