forked from creachadair/twitter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_test.go
More file actions
85 lines (74 loc) · 1.99 KB
/
stream_test.go
File metadata and controls
85 lines (74 loc) · 1.99 KB
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
// Copyright (C) 2020 Michael J. Fromberger. All Rights Reserved.
package twitter_test
import (
"context"
"testing"
"github.com/creachadair/jhttp"
"github.com/nankys/twitter"
"github.com/nankys/twitter/rules"
"github.com/nankys/twitter/tweets"
"github.com/nankys/twitter/types"
)
func TestStream(t *testing.T) {
if *testMode == "record" && *maxBodyBytes == 0 {
t.Fatal("Cannot record streaming methods without a -max-body-size")
}
ctx := context.Background()
req := &jhttp.Request{
Method: "2/tweets/sample/stream",
Params: jhttp.Params{
"tweet.fields": []string{"author_id", "entities"},
},
}
const maxResults = 3
nr := 0
err := cli.Stream(ctx, req, func(rsp *twitter.Reply) error {
nr++
t.Logf("Msg %d: %s", nr, string(rsp.Data))
if nr == maxResults {
return jhttp.ErrStopStreaming
}
return nil
})
if err != nil {
t.Errorf("Error from Stream: %v", err)
}
}
func TestSearchStream(t *testing.T) {
if *testMode == "record" && *maxBodyBytes == 0 {
t.Fatal("Cannot record streaming methods without a -max-body-size")
}
ctx := context.Background()
if *testMode == "record" {
t.Log(`
WARNING: This test may take a while (minutes) to complete in record mode.
Be patient, it is waiting for live data from a search query.
`)
}
r := rules.Adds{{Query: `cat has:images lang:en`}}
rsp, err := rules.Update(r).Invoke(ctx, cli)
if err != nil {
t.Fatalf("Updating rules: %v", err)
}
id := rsp.Rules[0].ID
t.Run("Search", func(t *testing.T) {
err := tweets.SearchStream(func(rsp *tweets.Reply) error {
for _, tw := range rsp.Tweets {
t.Logf("Result: id=%s, author=%s, text=%s", tw.ID, tw.AuthorID, tw.Text)
}
return nil
}, &tweets.StreamOpts{
MaxResults: 3,
Optional: []types.Fields{
types.TweetFields{AuthorID: true},
},
}).Invoke(ctx, cli)
if err != nil {
t.Errorf("SearchStream failed: %v", err)
}
})
del := rules.Deletes{id}
if _, err := rules.Update(del).Invoke(ctx, cli); err != nil {
t.Fatalf("Deleting rules: %v", err)
}
}