Esempio n. 1
0
async def test_close(event_loop):
    client = BasePeonyClient("", "", loop=event_loop)
    await client.setup()

    def dummy_func(*args, **kwargs):
        pass

    client._gathered_tasks = asyncio.gather(dummy())
    with patch.object(client.loop, 'run_until_complete',
                      side_effect=dummy_func) as run:
        with patch.object(client._gathered_tasks, 'cancel') as cancel:
            with patch.object(client._session, 'close') as close:
                client.close()
                run.assert_called_once_with(client._gathered_tasks)
                cancel.assert_called_once_with()
                close.assert_called_once_with()
                assert client._session is None
Esempio n. 2
0
async def test_disabled_error_handler():
    async def raise_runtime_error(*args, **kwargs):
        raise RuntimeError

    with patch.object(BasePeonyClient,
                      'request',
                      side_effect=raise_runtime_error):
        async with BasePeonyClient("", "") as client:
            with pytest.raises(RuntimeError):
                await client.api.test.get()
Esempio n. 3
0
def test_client_encoding_loads():
    text = bytes([194, 161])
    data = b"{\"hello\": \"%s\"}" % text

    client = BasePeonyClient("", "", encoding='utf-8')
    assert client._loads(data)['hello'] == text.decode('utf-8')

    client = BasePeonyClient("", "", encoding='ascii')
    with pytest.raises(UnicodeDecodeError):
        client._loads(data)
Esempio n. 4
0
async def test_bad_request():
    async def prepare_dummy(*args, **kwargs):
        return kwargs

    async with BasePeonyClient("", "") as dummy_client:
        dummy_client._session = MockSession(MockSessionRequest(status=404))
        with patch.object(dummy_client.headers,
                          'prepare_request',
                          side_effect=prepare_dummy):
            with pytest.raises(exceptions.HTTPNotFound):
                await dummy_client.request('get',
                                           "http://google.com/404",
                                           future=asyncio.Future())
Esempio n. 5
0
async def test_streaming_apis(dummy_client):
    with patch.object(dummy_client, 'request', side_effect=dummy) as request:
        await dummy_client.api.test.get()
        assert request.called

    with patch.object(dummy_client, 'stream_request') as request:
        dummy_client.stream.test.get()
        assert request.called

    client = BasePeonyClient("", "", streaming_apis={'api'})
    with patch.object(client, 'stream_request') as request:
        client.api.test.get()
        assert request.called

    with patch.object(client, 'request', side_effect=dummy) as request:
        await client.stream.test.get()
        assert request.called
Esempio n. 6
0
async def test_request_proxy():
    def raise_proxy(*args, proxy=None, **kwargs):
        raise RuntimeError(proxy)

    async with BasePeonyClient("", "",
                               proxy="http://some.proxy.com") as dummy_client:
        async with aiohttp.ClientSession() as session:
            with patch.object(dummy_client.headers,
                              'prepare_request',
                              side_effect=raise_proxy):
                try:
                    await dummy_client.request(method='get',
                                               url="http://hello.com",
                                               session=session,
                                               future=None)
                except RuntimeError as e:
                    assert str(e) == "http://some.proxy.com"
                else:
                    pytest.fail("Could not check proxy")
Esempio n. 7
0
async def test_request_encoding():
    class DummyCTX:
        def __init__(self, **kwargs):
            self.status = 200

        def __getattr__(self, item):
            return None

        def __aiter__(self):
            return self

        async def __aenter__(self):
            return self

        async def __aexit__(self, exc_type, exc_val, exc_tb):
            pass

    ENCODING = "best encoding"

    async def get_encoding(*args, encoding=None, **kwargs):
        return encoding

    def check_encoding(data, *args, **kwargs):
        assert data == ENCODING

    async with BasePeonyClient("", "") as client:
        async with aiohttp.ClientSession() as session:
            with patch.object(session, 'request', side_effect=DummyCTX):
                with patch.object(data_processing,
                                  'read',
                                  side_effect=get_encoding):
                    with patch.object(data_processing,
                                      'PeonyResponse',
                                      side_effect=check_encoding):
                        await client.request(method='get',
                                             url="http://hello.com",
                                             encoding=ENCODING,
                                             session=session,
                                             future=asyncio.Future())
Esempio n. 8
0
def test_run_exceptions_raise(event_loop):
    client = BasePeonyClient("", "", loop=event_loop)
    for exc in (Exception, RuntimeError, peony.exceptions.PeonyException):
        with patch.object(client, 'run_tasks', side_effect=exc):
            with pytest.raises(exc):
                client.run()
Esempio n. 9
0
def test_run_keyboard_interrupt(event_loop):
    client = BasePeonyClient("", "", loop=event_loop)
    with patch.object(client, 'run_tasks', side_effect=KeyboardInterrupt):
        client.run()
Esempio n. 10
0
def test_close_no_tasks():
    client = BasePeonyClient("", "")
    assert client._gathered_tasks is None
    client.close()
Esempio n. 11
0
def test_close_no_session():
    client = BasePeonyClient("", "")
    assert client._session is None
    client.close()
Esempio n. 12
0
def api_path():
    client = BasePeonyClient("", "", loop=asyncio.get_event_loop())
    with patch.object(client, 'request', side_effect=dummy):
        yield APIPath(['http://whatever.com', 'endpoint'], '.json', client)
Esempio n. 13
0
def test_client_base_url():
    base_url = "http://{api}.google.com/{version}"
    client = BasePeonyClient("", "", base_url=base_url, api_version="1")
    assert client.api.test.url() == "http://api.google.com/1/test.json"
Esempio n. 14
0
async def test_close_user_session():
    session = Mock()
    client = BasePeonyClient("", "", session=session)

    await client.close()
    assert not session.close.called
Esempio n. 15
0
def test_close_user_session():
    client = BasePeonyClient("", "", session=Mock())

    client.close()
    assert not client._session.close.called
Esempio n. 16
0
def test_client_error():
    with pytest.raises(TypeError):
        BasePeonyClient()
Esempio n. 17
0
# -*- coding: utf-8 -*-

import pytest

import peony.api
from peony import BasePeonyClient
from peony.general import twitter_base_api_url

# unique client

client = BasePeonyClient("", "")

# constants

url = twitter_base_api_url + "/test/endpoint.json"

# fixture functions


@pytest.fixture
def api():
    return peony.api.APIPath([twitter_base_api_url], '.json', client)


@pytest.fixture
def endpoint(api):
    return api.test.endpoint


# test functions
Esempio n. 18
0
def api_path():
    client = BasePeonyClient("", "")
    return APIPath(['http://whatever.com', 'endpoint'], '.json', client)