aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Diener <[email protected]>2018-05-06 01:37:13 +0200
committerLorenz Diener <[email protected]>2018-05-06 01:37:13 +0200
commita15d28d9d60128fd5551f05eccd8999b46f70f5e (patch)
tree84c2e92799983594138a9a4fc4ec7dab429e52bf /tests/test_streaming.py
parentad6b8eeb044760adf4b420199164b245012350f4 (diff)
downloadmastodon.py-a15d28d9d60128fd5551f05eccd8999b46f70f5e.tar.gz
Testing updates, can now test streaming API
Diffstat (limited to 'tests/test_streaming.py')
-rw-r--r--tests/test_streaming.py90
1 files changed, 86 insertions, 4 deletions
diff --git a/tests/test_streaming.py b/tests/test_streaming.py
index ac8e691..8c2160a 100644
--- a/tests/test_streaming.py
+++ b/tests/test_streaming.py
@@ -1,11 +1,51 @@
1import six 1import six
2import pytest 2import pytest
3import itertools 3import itertools
4from mastodon.streaming import StreamListener 4from mastodon.streaming import StreamListener, CallbackStreamListener
5from mastodon.Mastodon import MastodonMalformedEventError 5from mastodon.Mastodon import MastodonMalformedEventError
6 6from mastodon import Mastodon
7 7
8 8import threading
9import time
10
11# For monkeypatching so we can make vcrpy better
12import vcr.stubs
13
14streamingIsPatched = False
15realConnections = []
16
17def patchStreaming():
18 global streamingIsPatched
19 if streamingIsPatched == True:
20 return
21 streamingIsPatched = True
22
23 realGetResponse = vcr.stubs.VCRConnection.getresponse
24 def fakeGetResponse(*args, **kwargs):
25 if args[0]._vcr_request.path.startswith("/api/v1/streaming/"):
26 realConnections.append(args[0].real_connection)
27 realConnectionRealGetresponse = args[0].real_connection.getresponse
28 def fakeRealConnectionGetresponse(*args, **kwargs):
29 response = realConnectionRealGetresponse(*args, **kwargs)
30 real_body = b""
31 try:
32 while True:
33 chunk = response.read(1)
34 real_body += chunk
35 except AttributeError:
36 pass # Connection closed
37 response.read = (lambda: real_body)
38 return response
39 args[0].real_connection.getresponse = fakeRealConnectionGetresponse
40 return realGetResponse(*args, **kwargs)
41 vcr.stubs.VCRConnection.getresponse = fakeGetResponse
42
43def streamingClose():
44 global realConnections
45 for connection in realConnections:
46 connection.close()
47 realConnections = []
48
9class Listener(StreamListener): 49class Listener(StreamListener):
10 def __init__(self): 50 def __init__(self):
11 self.updates = [] 51 self.updates = []
@@ -210,3 +250,45 @@ def test_multiline_payload():
210 '', 250 '',
211 ]) 251 ])
212 assert listener.updates == [{"foo": "bar"}] 252 assert listener.updates == [{"foo": "bar"}]
253
254@pytest.mark.vcr(match_on=['path'])
255def test_stream_user(api, api2):
256 patchStreaming()
257
258 updates = []
259 notifications = []
260 listener = CallbackStreamListener(
261 update_handler = lambda x: updates.append(x),
262 notification_handler = lambda x: notifications.append(x)
263 )
264
265 posted = []
266 def do_activities():
267 time.sleep(5)
268 posted.append(api.status_post("only real cars respond."))
269 posted.append(api2.status_post("@mastodonpy_test beep beep I'm a jeep"))
270 posted.append(api2.status_post("on the internet, nobody knows you're a plane"))
271 time.sleep(3)
272 streamingClose()
273
274 t = threading.Thread(args=(), target=do_activities)
275 t.start()
276
277 stream = None
278 try:
279 stream = api.stream_user(listener, run_async=True)
280 time.sleep(13)
281 finally:
282 if stream != None:
283 stream.close()
284
285 assert len(updates) == 2
286 assert len(notifications) == 1
287
288 assert updates[0].id == posted[0].id
289 assert updates[1].id == posted[0].id
290 assert notifications[0].status.id == posted[1].id
291
292 t.join()
293
294 \ No newline at end of file
Powered by cgit v1.2.3 (git 2.41.0)