2020-05-14 14:59:33 +02:00
|
|
|
package twitterscraper
|
|
|
|
|
|
2020-06-12 21:31:08 +08:00
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
2020-05-14 14:59:33 +02:00
|
|
|
|
2021-03-31 15:12:39 +03:00
|
|
|
func TestFetchSearchCursor(t *testing.T) {
|
|
|
|
|
scraper := New()
|
|
|
|
|
maxTweetsNbr := 150
|
|
|
|
|
tweetsNbr := 0
|
|
|
|
|
nextCursor := ""
|
|
|
|
|
for tweetsNbr < maxTweetsNbr {
|
|
|
|
|
tweets, cursor, err := scraper.FetchSearchTweets("twitter", maxTweetsNbr, nextCursor)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if cursor == "" {
|
|
|
|
|
t.Fatal("Expected search cursor is not empty")
|
|
|
|
|
}
|
|
|
|
|
tweetsNbr += len(tweets)
|
|
|
|
|
nextCursor = cursor
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-14 14:59:33 +02:00
|
|
|
func TestGetSearchTweets(t *testing.T) {
|
|
|
|
|
count := 0
|
2020-12-11 20:58:49 +02:00
|
|
|
maxTweetsNbr := 250
|
2021-03-31 15:15:51 +03:00
|
|
|
dupcheck := make(map[string]bool)
|
2021-03-31 15:12:39 +03:00
|
|
|
for tweet := range SearchTweets(context.Background(), "twitter -filter:retweets", maxTweetsNbr) {
|
2020-05-14 14:59:33 +02:00
|
|
|
if tweet.Error != nil {
|
|
|
|
|
t.Error(tweet.Error)
|
|
|
|
|
} else {
|
|
|
|
|
count++
|
|
|
|
|
if tweet.ID == "" {
|
|
|
|
|
t.Error("Expected tweet ID is not 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
|
|
|
|
|
}
|
2020-05-14 14:59:33 +02:00
|
|
|
}
|
|
|
|
|
if tweet.PermanentURL == "" {
|
|
|
|
|
t.Error("Expected tweet PermanentURL is not empty")
|
|
|
|
|
}
|
2020-12-11 20:58:49 +02:00
|
|
|
if tweet.IsRetweet {
|
|
|
|
|
t.Error("Expected tweet IsRetweet is false")
|
|
|
|
|
}
|
2020-05-14 14:59:33 +02:00
|
|
|
if tweet.Text == "" {
|
|
|
|
|
t.Error("Expected tweet Text is not empty")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 17:52:06 +02:00
|
|
|
if count != maxTweetsNbr {
|
|
|
|
|
t.Errorf("Expected tweets count=%v, got: %v", maxTweetsNbr, count)
|
2020-05-14 14:59:33 +02:00
|
|
|
}
|
|
|
|
|
}
|