twitter-scrapper/search_test.go

99 lines
2.3 KiB
Go
Raw Normal View History

2021-12-07 10:18:01 +02:00
package twitterscraper_test
import (
"context"
2023-07-03 14:38:46 +03:00
"os"
"testing"
2021-12-07 10:18:01 +02:00
twitterscraper "github.com/n0madic/twitter-scraper"
)
2021-03-31 15:12:39 +03:00
func TestFetchSearchCursor(t *testing.T) {
2023-07-03 14:38:46 +03:00
if os.Getenv("SKIP_AUTH_TEST") != "" {
t.Skip("Skipping test due to environment variable")
}
2021-03-31 15:12:39 +03:00
maxTweetsNbr := 150
tweetsNbr := 0
nextCursor := ""
for tweetsNbr < maxTweetsNbr {
2023-07-03 15:06:56 +03:00
tweets, cursor, err := testScraper.FetchSearchTweets("twitter", maxTweetsNbr, nextCursor)
2021-03-31 15:12:39 +03:00
if err != nil {
t.Fatal(err)
}
if cursor == "" {
t.Fatal("Expected search cursor is empty")
2021-03-31 15:12:39 +03:00
}
tweetsNbr += len(tweets)
nextCursor = cursor
}
}
func TestGetSearchProfiles(t *testing.T) {
2023-07-03 14:38:46 +03:00
if os.Getenv("SKIP_AUTH_TEST") != "" {
t.Skip("Skipping test due to environment variable")
}
count := 0
maxProfilesNbr := 150
dupcheck := make(map[string]bool)
2023-07-03 15:06:56 +03:00
testScraper.SetSearchMode(twitterscraper.SearchUsers)
for profile := range testScraper.SearchProfiles(context.Background(), "Twitter", maxProfilesNbr) {
if profile.Error != nil {
t.Error(profile.Error)
} else {
count++
if profile.UserID == "" {
t.Error("Expected UserID is empty")
} else {
if dupcheck[profile.UserID] {
t.Errorf("Detect duplicated UserID: %s", profile.UserID)
} else {
dupcheck[profile.UserID] = true
}
}
}
}
if count != maxProfilesNbr {
t.Errorf("Expected profiles count=%v, got: %v", maxProfilesNbr, count)
}
}
func TestGetSearchTweets(t *testing.T) {
2023-07-03 14:38:46 +03:00
if os.Getenv("SKIP_AUTH_TEST") != "" {
t.Skip("Skipping test due to environment variable")
}
count := 0
maxTweetsNbr := 150
2021-03-31 15:15:51 +03:00
dupcheck := make(map[string]bool)
2023-07-03 15:06:56 +03:00
testScraper.SetSearchMode(twitterscraper.SearchLatest)
for tweet := range testScraper.SearchTweets(context.Background(), "twitter", maxTweetsNbr) {
if tweet.Error != nil {
t.Error(tweet.Error)
} else {
count++
if tweet.ID == "" {
t.Error("Expected tweet ID is empty")
2021-03-31 15:15:51 +03:00
} else {
if dupcheck[tweet.ID] {
t.Errorf("Detect duplicated tweet ID: %s", tweet.ID)
} else {
dupcheck[tweet.ID] = true
}
}
if tweet.PermanentURL == "" {
t.Error("Expected tweet PermanentURL is empty")
}
if tweet.IsRetweet {
t.Error("Expected tweet IsRetweet is false")
}
if tweet.Text == "" {
t.Error("Expected tweet Text is empty")
}
}
}
if count != maxTweetsNbr {
t.Errorf("Expected tweets count=%v, got: %v", maxTweetsNbr, count)
}
}