Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 0d46495

Browse files
committed
add stream tests
1 parent ca85149 commit 0d46495

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

tests/test_sanity.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
auth=noauth,
5252
api_version='1.1')
5353

54+
twitter11_stream = TwitterStream(domain='stream.twitter.com',
55+
auth=oauth,
56+
api_version='1.1')
57+
5458
twitter2 = Twitter(domain='api.twitter.com',
5559
auth=oauth,
5660
api_version='2',
@@ -61,6 +65,11 @@
6165
api_version='2',
6266
format='')
6367

68+
twitter2_stream = TwitterStream2(domain='api.twitter.com',
69+
auth=oauth2,
70+
api_version='2',
71+
format='')
72+
6473
AZaz = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
6574

6675
b64_image_data = b"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94JFhMBAJv5kaUAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAA4UlEQVQoz7WSIZLGIAxG6c5OFZjianBcIOfgPkju1DsEBWfAUEcNGGpY8Xe7dDoVFRvHfO8NJGRorZE39UVe1nd/WNfVObcsi3OOEAIASikAmOf5D2q/FWPUWgshKKWfiFIqhNBaxxhPjPQ05/z+Bs557xw9hBC89ymlu5BS8t6HEC5NW2sR8alRRLTWXoRSSinlSejT12M9BAAAgCeoTw9BSimlfBIu6WdYtVZEVErdaaUUItZaL/9wOsaY83YAMMb0dGtt6Jdv3/ec87ZtOWdCCGNsmibG2DiOJzP8+7b+AAOmsiPxyHWCAAAAAElFTkSuQmCC"
@@ -185,6 +194,54 @@ def test_search():
185194
assert results
186195

187196

197+
def test_stream():
198+
# Sometimes Twitter matches tweets with keywords from urls or users
199+
# which are more complex to check so we allow a few mismatches
200+
mismatch = 0
201+
for tweet in twitter11_stream.statuses.filter(track="gaga,bieber", filter_level='none', stall_warnings='true'):
202+
if "timeout" in tweet:
203+
continue
204+
assert "text" in tweet
205+
lowtext = tweet["text"].lower()
206+
if "gaga" in lowtext or "bieber" in lowtext:
207+
break
208+
mismatch += 1
209+
if mismatch > 5:
210+
assert "gaga" in lowtext or "bieber" in lowtext
211+
212+
213+
def test_stream_v2():
214+
rules = twitter2_app.tweets.search.stream.rules()
215+
assert "data" in rules and "meta" in rules and len(rules["data"]) == rules["meta"].get("result_count", 0)
216+
217+
if len(rules["data"]):
218+
remove = twitter2_app.tweets.search.stream.rules(
219+
_json={"delete": {"ids": [rule["id"] for rule in rules["data"]]}}
220+
)
221+
assert "meta" in remove and "summary" in remove["meta"] and remove["meta"]["summary"].get("deleted", 0) == len(rules["data"])
222+
223+
add = twitter2_app.tweets.search.stream.rules(
224+
_json={"add": [{"value": '"gaga"'}, {"value": '"bieber"'}]}
225+
)
226+
assert "meta" in add and "summary" in add["meta"] and add["meta"]["summary"].get("created", 0) == 2
227+
assert "data" in add and len(add["data"]) == 2
228+
229+
# Sometimes Twitter matches tweets with keywords from urls or users
230+
# which are more complex to check so we allow a few mismatches
231+
mismatch = 0
232+
for tweet in twitter2_stream.tweets.search.stream(
233+
expansions="referenced_tweets.id",
234+
params={"tweet.fields": "referenced_tweets,text"}
235+
):
236+
assert "data" in tweet and "text" in tweet["data"]
237+
lowtext = tweet["data"]["text"].lower()
238+
if "gaga" in lowtext or "bieber" in lowtext:
239+
break
240+
mismatch += 1
241+
if mismatch > 5:
242+
assert "gaga" in lowtext or "bieber" in lowtext
243+
244+
188245
def test_get_trends():
189246
# This is one method of inserting parameters, using named
190247
# underscore params.

0 commit comments

Comments
 (0)