twitter-scrapper/tweets_test.go
Alexander Sheiko a8089a552e Add GetTweet
2021-03-09 10:40:22 +02:00

96 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package twitterscraper
import (
"context"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestGetTweets(t *testing.T) {
count := 0
maxTweetsNbr := 300
dupcheck := make(map[string]bool)
for tweet := range GetTweets(context.Background(), "Twitter", maxTweetsNbr) {
if tweet.Error != nil {
t.Error(tweet.Error)
} else {
count++
if tweet.ID == "" {
t.Error("Expected tweet ID is not empty")
} else {
if dupcheck[tweet.ID] {
t.Errorf("Detect duplicated tweet ID: %s", tweet.ID)
} else {
dupcheck[tweet.ID] = true
}
}
if tweet.UserID == "" {
t.Error("Expected tweet UserID is not empty")
}
if tweet.Username == "" {
t.Error("Expected tweet Username is not empty")
}
if tweet.PermanentURL == "" {
t.Error("Expected tweet PermanentURL is not empty")
}
if tweet.Text == "" {
t.Error("Expected tweet Text is not empty")
}
if tweet.TimeParsed.IsZero() {
t.Error("Expected tweet TimeParsed is not zero")
}
if tweet.Timestamp == 0 {
t.Error("Expected tweet Timestamp is greater than zero")
}
for _, video := range tweet.Videos {
if video.ID == "" {
t.Error("Expected tweet video ID is not empty")
}
if video.Preview == "" {
t.Error("Expected tweet video Preview is not empty")
}
if video.URL == "" {
t.Error("Expected tweet video URL is not empty")
}
}
}
}
if count != maxTweetsNbr {
t.Errorf("Expected tweets count=%v, got: %v", maxTweetsNbr, count)
}
}
func TestGetTweet(t *testing.T) {
sample := Tweet{
HTML: "That thing you didnt Tweet but wanted to but didnt but got so close but then were like nah. <br><br>We have a place for that now—Fleets! <br><br>Rolling out to everyone starting today. <br><a href=\"https://t.co/auQAHXZMfH\"><img src=\"https://pbs.twimg.com/amplify_video_thumb/1328684333599756289/img/cP5KwbIXbGunNSBy.jpg\"/></a>",
ID: "1328684389388185600",
PermanentURL: "https://twitter.com/Twitter/status/1328684389388185600",
Photos: []string{"https://pbs.twimg.com/amplify_video_thumb/1328684333599756289/img/cP5KwbIXbGunNSBy.jpg"},
Text: "That thing you didnt Tweet but wanted to but didnt but got so close but then were like nah. \n\nWe have a place for that now—Fleets! \n\nRolling out to everyone starting today. https://t.co/auQAHXZMfH",
TimeParsed: time.Date(2020, 11, 17, 13, 0, 18, 0, time.FixedZone("UTC", 0)),
Timestamp: 1605618018,
UserID: "783214",
Username: "Twitter",
Videos: []Video{{
ID: "1328684333599756289",
Preview: "https://pbs.twimg.com/amplify_video_thumb/1328684333599756289/img/cP5KwbIXbGunNSBy.jpg",
URL: "https://video.twimg.com/amplify_video/1328684333599756289/vid/480x360/Qh70ELAcq-N2RYmZ.mp4?tag=13",
}},
}
tweet, err := defaultScraper.GetTweet("1328684389388185600")
if err != nil {
t.Error(err)
} else {
cmpOptions := cmp.Options{
cmpopts.IgnoreFields(Tweet{}, "Likes"),
cmpopts.IgnoreFields(Tweet{}, "Replies"),
cmpopts.IgnoreFields(Tweet{}, "Retweets"),
}
if diff := cmp.Diff(sample, *tweet, cmpOptions...); diff != "" {
t.Error("Resulting tweet does not match the sample", diff)
}
}
}