2019-09-21 11:56:06 +03:00
|
|
|
package twitterscraper
|
|
|
|
|
|
|
|
|
|
import (
|
2021-03-10 09:02:02 +02:00
|
|
|
"fmt"
|
2019-09-21 11:56:06 +03:00
|
|
|
"testing"
|
2020-12-11 20:58:49 +02:00
|
|
|
"time"
|
2019-09-21 11:56:06 +03:00
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestGetProfile(t *testing.T) {
|
2020-12-11 20:58:49 +02:00
|
|
|
loc := time.FixedZone("UTC", 0)
|
|
|
|
|
joined := time.Date(2007, 02, 20, 14, 35, 54, 0, loc)
|
2019-09-21 11:56:06 +03:00
|
|
|
sample := Profile{
|
2021-01-28 10:54:43 +02:00
|
|
|
Avatar: "https://pbs.twimg.com/profile_images/1354479643882004483/Btnfm47p_normal.jpg",
|
2021-04-28 12:08:00 +03:00
|
|
|
Banner: "https://pbs.twimg.com/profile_banners/783214/1619544410",
|
2021-01-04 13:40:06 +02:00
|
|
|
Biography: "What's happening?!",
|
2020-12-11 20:58:49 +02:00
|
|
|
// Birthday: "March 21",
|
|
|
|
|
IsPrivate: false,
|
|
|
|
|
IsVerified: true,
|
|
|
|
|
Joined: &joined,
|
|
|
|
|
Location: "everywhere",
|
|
|
|
|
Name: "Twitter",
|
2020-12-23 10:37:08 +02:00
|
|
|
PinnedTweetIDs: []string{},
|
2020-12-11 20:58:49 +02:00
|
|
|
URL: "https://twitter.com/Twitter",
|
|
|
|
|
UserID: "783214",
|
|
|
|
|
Username: "Twitter",
|
|
|
|
|
Website: "https://about.twitter.com/",
|
2019-09-21 11:56:06 +03:00
|
|
|
}
|
|
|
|
|
|
2020-05-14 18:01:30 +02:00
|
|
|
profile, err := GetProfile("Twitter")
|
2019-09-21 11:56:06 +03:00
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-22 10:17:51 +03:00
|
|
|
cmpOptions := cmp.Options{
|
2019-09-21 11:56:06 +03:00
|
|
|
cmpopts.IgnoreFields(Profile{}, "FollowersCount"),
|
|
|
|
|
cmpopts.IgnoreFields(Profile{}, "FollowingCount"),
|
2020-12-11 20:58:49 +02:00
|
|
|
cmpopts.IgnoreFields(Profile{}, "FriendsCount"),
|
2019-09-21 11:56:06 +03:00
|
|
|
cmpopts.IgnoreFields(Profile{}, "LikesCount"),
|
2020-12-11 20:58:49 +02:00
|
|
|
cmpopts.IgnoreFields(Profile{}, "ListedCount"),
|
2019-09-21 11:56:06 +03:00
|
|
|
cmpopts.IgnoreFields(Profile{}, "TweetsCount"),
|
|
|
|
|
}
|
|
|
|
|
if diff := cmp.Diff(sample, profile, cmpOptions...); diff != "" {
|
|
|
|
|
t.Error("Resulting profile does not match the sample", diff)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if profile.FollowersCount == 0 {
|
2019-09-21 12:06:42 +03:00
|
|
|
t.Error("Expected FollowersCount is greater than zero")
|
2019-09-21 11:56:06 +03:00
|
|
|
}
|
|
|
|
|
if profile.FollowingCount == 0 {
|
2019-09-21 12:06:42 +03:00
|
|
|
t.Error("Expected FollowingCount is greater than zero")
|
2019-09-21 11:56:06 +03:00
|
|
|
}
|
2020-12-11 20:58:49 +02:00
|
|
|
if profile.LikesCount == 0 {
|
|
|
|
|
t.Error("Expected LikesCount is greater than zero")
|
|
|
|
|
}
|
2019-09-21 11:56:06 +03:00
|
|
|
if profile.TweetsCount == 0 {
|
2019-09-21 12:06:42 +03:00
|
|
|
t.Error("Expected TweetsCount is greater than zero")
|
2019-09-21 11:56:06 +03:00
|
|
|
}
|
|
|
|
|
}
|
2021-01-25 10:31:41 +07:00
|
|
|
|
|
|
|
|
func TestGetProfilePrivate(t *testing.T) {
|
|
|
|
|
loc := time.FixedZone("UTC", 0)
|
2021-01-28 10:54:43 +02:00
|
|
|
joined := time.Date(2020, 1, 26, 0, 3, 5, 0, loc)
|
2021-01-25 10:31:41 +07:00
|
|
|
sample := Profile{
|
2021-01-28 10:54:43 +02:00
|
|
|
Avatar: "https://pbs.twimg.com/profile_images/1222218816484020224/ik9P1QZt_normal.jpg",
|
|
|
|
|
Banner: "",
|
|
|
|
|
Biography: "private account",
|
2021-01-25 10:31:41 +07:00
|
|
|
// Birthday: "March 21",
|
|
|
|
|
IsPrivate: true,
|
|
|
|
|
IsVerified: false,
|
|
|
|
|
Joined: &joined,
|
|
|
|
|
Location: "",
|
2021-01-28 10:54:43 +02:00
|
|
|
Name: "private account",
|
2021-01-25 10:31:41 +07:00
|
|
|
PinnedTweetIDs: []string{},
|
2021-01-28 10:54:43 +02:00
|
|
|
URL: "https://twitter.com/tomdumont",
|
|
|
|
|
UserID: "1221221876849995777",
|
|
|
|
|
Username: "tomdumont",
|
|
|
|
|
Website: "",
|
2021-01-25 10:31:41 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// some random private profile (found via google)
|
2021-01-28 10:54:43 +02:00
|
|
|
profile, err := GetProfile("tomdumont")
|
2021-01-25 10:31:41 +07:00
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmpOptions := cmp.Options{
|
|
|
|
|
cmpopts.IgnoreFields(Profile{}, "FollowersCount"),
|
|
|
|
|
cmpopts.IgnoreFields(Profile{}, "FollowingCount"),
|
|
|
|
|
cmpopts.IgnoreFields(Profile{}, "FriendsCount"),
|
|
|
|
|
cmpopts.IgnoreFields(Profile{}, "LikesCount"),
|
|
|
|
|
cmpopts.IgnoreFields(Profile{}, "ListedCount"),
|
|
|
|
|
cmpopts.IgnoreFields(Profile{}, "TweetsCount"),
|
|
|
|
|
}
|
|
|
|
|
if diff := cmp.Diff(sample, profile, cmpOptions...); diff != "" {
|
|
|
|
|
t.Error("Resulting profile does not match the sample", diff)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if profile.FollowingCount == 0 {
|
|
|
|
|
t.Error("Expected FollowingCount is greater than zero")
|
|
|
|
|
}
|
|
|
|
|
if profile.LikesCount == 0 {
|
|
|
|
|
t.Error("Expected LikesCount is greater than zero")
|
|
|
|
|
}
|
|
|
|
|
if profile.TweetsCount == 0 {
|
|
|
|
|
t.Error("Expected TweetsCount is greater than zero")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetProfileErrorSuspended(t *testing.T) {
|
|
|
|
|
_, err := GetProfile("123")
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Error("Expected Error, got success")
|
|
|
|
|
} else {
|
|
|
|
|
if err.Error() != "Authorization: User has been suspended. (63)" {
|
|
|
|
|
t.Errorf("Expected error 'Authorization: User has been suspended. (63)', got '%s'", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGetProfileErrorNotFound(t *testing.T) {
|
2021-03-10 09:02:02 +02:00
|
|
|
neUser := "sample3123131"
|
|
|
|
|
expectedError := fmt.Sprintf("User '%s' not found", neUser)
|
|
|
|
|
_, err := GetProfile(neUser)
|
2021-01-25 10:31:41 +07:00
|
|
|
if err == nil {
|
|
|
|
|
t.Error("Expected Error, got success")
|
|
|
|
|
} else {
|
2021-03-10 09:02:02 +02:00
|
|
|
if err.Error() != expectedError {
|
|
|
|
|
t.Errorf("Expected error '%s', got '%s'", expectedError, err)
|
2021-01-25 10:31:41 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-28 11:12:20 +02:00
|
|
|
|
|
|
|
|
func TestGetUserIDByScreenName(t *testing.T) {
|
|
|
|
|
scraper := New()
|
|
|
|
|
userID, err := scraper.GetUserIDByScreenName("Twitter")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("getUserByScreenName() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
if userID == "" {
|
|
|
|
|
t.Error("Expected non-empty user ID")
|
|
|
|
|
}
|
|
|
|
|
}
|