This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 202
/
search_test.go
97 lines (90 loc) · 2.28 KB
/
search_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package twitterscraper_test
import (
"context"
"testing"
twitterscraper "github.com/n0madic/twitter-scraper"
)
func TestFetchSearchCursor(t *testing.T) {
if skipAuthTest {
t.Skip("Skipping test due to environment variable")
}
maxTweetsNbr := 150
tweetsNbr := 0
nextCursor := ""
for tweetsNbr < maxTweetsNbr {
tweets, cursor, err := testScraper.FetchSearchTweets("twitter", maxTweetsNbr, nextCursor)
if err != nil {
t.Fatal(err)
}
if cursor == "" {
t.Fatal("Expected search cursor is empty")
}
tweetsNbr += len(tweets)
nextCursor = cursor
}
}
func TestGetSearchProfiles(t *testing.T) {
if skipAuthTest {
t.Skip("Skipping test due to environment variable")
}
count := 0
maxProfilesNbr := 150
dupcheck := make(map[string]bool)
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) {
if skipAuthTest {
t.Skip("Skipping test due to environment variable")
}
count := 0
maxTweetsNbr := 150
dupcheck := make(map[string]bool)
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")
} 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)
}
}