2019-09-21 12:18:34 +03:00
package twitterscraper
import (
2020-06-12 21:31:08 +08:00
"context"
2019-09-21 12:18:34 +03:00
"testing"
2021-03-09 10:40:22 +02:00
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
2019-09-21 12:18:34 +03:00
)
func TestGetTweets ( t * testing . T ) {
count := 0
2020-12-11 20:58:49 +02:00
maxTweetsNbr := 300
dupcheck := make ( map [ string ] bool )
2020-06-15 16:16:08 +03:00
for tweet := range GetTweets ( context . Background ( ) , "Twitter" , maxTweetsNbr ) {
2019-09-21 12:18:34 +03:00
if tweet . Error != nil {
t . Error ( tweet . Error )
} else {
count ++
if tweet . ID == "" {
2021-04-22 20:35:33 +03:00
t . Error ( "Expected tweet ID is empty" )
2020-12-11 20:58:49 +02:00
} else {
if dupcheck [ tweet . ID ] {
t . Errorf ( "Detect duplicated tweet ID: %s" , tweet . ID )
} else {
dupcheck [ tweet . ID ] = true
}
}
if tweet . UserID == "" {
2021-04-22 20:35:33 +03:00
t . Error ( "Expected tweet UserID is empty" )
2019-09-21 12:18:34 +03:00
}
2020-06-15 16:38:51 +03:00
if tweet . Username == "" {
2021-04-22 20:35:33 +03:00
t . Error ( "Expected tweet Username is empty" )
2020-06-15 16:38:51 +03:00
}
2019-09-21 12:18:34 +03:00
if tweet . PermanentURL == "" {
2021-04-22 20:35:33 +03:00
t . Error ( "Expected tweet PermanentURL is empty" )
2019-09-21 12:18:34 +03:00
}
if tweet . Text == "" {
2021-04-22 20:35:33 +03:00
t . Error ( "Expected tweet Text is empty" )
2019-09-21 12:18:34 +03:00
}
if tweet . TimeParsed . IsZero ( ) {
2021-04-22 20:35:33 +03:00
t . Error ( "Expected tweet TimeParsed is zero" )
2019-09-21 12:18:34 +03:00
}
if tweet . Timestamp == 0 {
t . Error ( "Expected tweet Timestamp is greater than zero" )
}
2020-12-11 20:58:49 +02:00
for _ , video := range tweet . Videos {
if video . ID == "" {
2021-04-22 20:35:33 +03:00
t . Error ( "Expected tweet video ID is empty" )
2020-12-11 20:58:49 +02:00
}
if video . Preview == "" {
2021-04-22 20:35:33 +03:00
t . Error ( "Expected tweet video Preview is empty" )
2020-12-11 20:58:49 +02:00
}
if video . URL == "" {
2021-04-22 20:35:33 +03:00
t . Error ( "Expected tweet video URL is empty" )
2020-12-11 20:58:49 +02:00
}
}
2019-09-21 12:18:34 +03:00
}
}
2020-06-15 16:16:08 +03:00
if count != maxTweetsNbr {
t . Errorf ( "Expected tweets count=%v, got: %v" , maxTweetsNbr , count )
2019-09-21 12:18:34 +03:00
}
}
2021-03-09 10:40:22 +02:00
func TestGetTweet ( t * testing . T ) {
sample := Tweet {
HTML : "That thing you didn’ t Tweet but wanted to but didn’ t 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 didn’ t Tweet but wanted to but didn’ t 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 )
}
}
}
2021-03-28 12:25:55 +03:00
func TestRetweet ( t * testing . T ) {
2021-07-16 11:37:43 +03:00
sample := & Tweet {
HTML : "We’ ve seen an increase in attacks against Asian communities and individuals around the world. It’ s important to know that this isn’ t new; throughout history, Asians have experienced violence and exclusion. However, their diverse lived experiences have largely been overlooked." ,
ID : "1359151057872580612" ,
Likes : 6683 ,
PermanentURL : "https://twitter.com/TwitterTogether/status/1359151057872580612" ,
Replies : 455 ,
Retweets : 1495 ,
Text : "We’ ve seen an increase in attacks against Asian communities and individuals around the world. It’ s important to know that this isn’ t new; throughout history, Asians have experienced violence and exclusion. However, their diverse lived experiences have largely been overlooked." ,
TimeParsed : time . Date ( 2021 , 02 , 9 , 14 , 43 , 58 , 0 , time . FixedZone ( "UTC" , 0 ) ) ,
Timestamp : 1612881838 ,
UserID : "773578328498372608" ,
Username : "TwitterTogether" ,
2021-03-28 12:25:55 +03:00
}
tweet , err := defaultScraper . GetTweet ( "1362849141248974853" )
if err != nil {
t . Error ( err )
} else {
if diff := cmp . Diff ( sample , tweet . Retweet ) ; diff != "" {
t . Error ( "Resulting retweet does not match the sample" , diff )
}
}
}