2017-11-28 01:41:18 +01:00
|
|
|
import pytest
|
|
|
|
from contextlib import contextmanager
|
2019-04-15 15:48:45 +02:00
|
|
|
try:
|
|
|
|
from mock import MagicMock
|
|
|
|
except ImportError:
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
import requests_mock
|
2017-11-28 01:41:18 +01:00
|
|
|
|
2017-11-29 23:53:35 +01:00
|
|
|
UNLIKELY_HASHTAG = "fgiztsshwiaqqiztpmmjbtvmescsculuvmgjgopwoeidbcrixp"
|
|
|
|
|
2017-11-28 01:41:18 +01:00
|
|
|
|
|
|
|
@contextmanager
|
2017-11-28 01:49:15 +01:00
|
|
|
def many_statuses(api, n=10, suffix=''):
|
2017-11-28 01:41:18 +01:00
|
|
|
statuses = list()
|
|
|
|
for i in range(n):
|
2017-11-28 01:49:15 +01:00
|
|
|
status = api.status_post("Toot number {}!{}".format(i, suffix))
|
2017-11-28 01:41:18 +01:00
|
|
|
statuses.append(status)
|
|
|
|
yield statuses
|
|
|
|
for status in statuses:
|
|
|
|
api.status_delete(status['id'])
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.vcr()
|
|
|
|
def test_fetch_next_previous(api):
|
|
|
|
account = api.account_verify_credentials()
|
|
|
|
with many_statuses(api):
|
|
|
|
statuses = api.account_statuses(account['id'], limit=5)
|
|
|
|
next_statuses = api.fetch_next(statuses)
|
|
|
|
assert next_statuses
|
|
|
|
previous_statuses = api.fetch_previous(next_statuses)
|
|
|
|
assert previous_statuses
|
2017-11-28 01:49:15 +01:00
|
|
|
|
|
|
|
|
2017-11-29 23:53:35 +01:00
|
|
|
@pytest.mark.vcr()
|
|
|
|
def test_fetch_next_previous_from_pagination_info(api):
|
|
|
|
account = api.account_verify_credentials()
|
|
|
|
with many_statuses(api):
|
|
|
|
statuses = api.account_statuses(account['id'], limit=5)
|
2018-06-04 16:48:20 +02:00
|
|
|
next_statuses = api.fetch_next(statuses[-1]._pagination_next)
|
2017-11-29 23:53:35 +01:00
|
|
|
assert next_statuses
|
2018-06-04 16:48:20 +02:00
|
|
|
previous_statuses = api.fetch_previous(next_statuses[0]._pagination_prev)
|
2017-11-29 23:53:35 +01:00
|
|
|
assert previous_statuses
|
|
|
|
|
|
|
|
|
2017-11-28 01:49:15 +01:00
|
|
|
@pytest.mark.vcr()
|
|
|
|
def test_fetch_remaining(api):
|
|
|
|
with many_statuses(api, n=30, suffix=' #'+UNLIKELY_HASHTAG):
|
|
|
|
hashtag = api.timeline_hashtag(UNLIKELY_HASHTAG, limit=10)
|
|
|
|
hashtag_remaining = api.fetch_remaining(hashtag)
|
|
|
|
assert hashtag_remaining
|
|
|
|
assert len(hashtag_remaining) >= 30
|
2019-04-15 15:48:45 +02:00
|
|
|
|
|
|
|
def test_link_headers(api):
|
|
|
|
rmock = requests_mock.Adapter()
|
|
|
|
api.session.mount(api.api_base_url, rmock)
|
|
|
|
|
|
|
|
_id='abc1234'
|
|
|
|
|
|
|
|
rmock.register_uri('GET', requests_mock.ANY, json=[{"foo": "bar"}], headers={"link":"""
|
|
|
|
<{base}/api/v1/timelines/tag/{tag}?max_id={_id}>; rel="next", <{base}/api/v1/timelines/tag/{tag}?since_id={_id}>; rel="prev"
|
|
|
|
""".format(base=api.api_base_url, tag=UNLIKELY_HASHTAG, _id=_id).strip()
|
|
|
|
})
|
|
|
|
|
|
|
|
resp = api.timeline_hashtag(UNLIKELY_HASHTAG)
|
|
|
|
assert resp[0]._pagination_next['max_id'] == _id
|
|
|
|
assert resp[0]._pagination_prev['since_id'] == _id
|