| 1 |
"""Test events namespace.""" |
| 2 |
# pylint: disable=missing-docstring |
| 3 |
import asyncio |
| 4 |
from unittest.mock import AsyncMock, patch |
| 5 |
|
| 6 |
import pytest |
| 7 |
import pytest_asyncio |
| 8 |
import aiohttp |
| 9 |
|
| 10 |
from aiogithubapi import GitHubAPI, GitHubEventModel |
| 11 |
|
| 12 |
from tests.common import TEST_REPOSITORY_NAME, TOKEN, MockedRequests, MockResponse |
| 13 |
|
| 14 |
|
| 15 |
@pytest_asyncio.fixture |
| 16 |
async def wait_mock(): |
| 17 |
async def _mocker(_, __): |
| 18 |
await asyncio.sleep(0) |
| 19 |
|
| 20 |
with patch("aiogithubapi.namespaces.events._GitHubEventsBaseNamespace._wait", _mocker): |
| 21 |
yield |
| 22 |
|
| 23 |
|
| 24 |
@pytest.mark.asyncio |
| 25 |
async def test_subscription(github_api: GitHubAPI, mock_requests: MockedRequests): |
| 26 |
tasks_before = asyncio.all_tasks( |
| 27 |
github_api._client._loop) # This includes the test task |
| 28 |
assert len(tasks_before) == 1 |
| 29 |
|
| 30 |
event_callback_mock = AsyncMock() |
| 31 |
|
| 32 |
subscription_id = await github_api.repos.events.subscribe( |
| 33 |
TEST_REPOSITORY_NAME, event_callback=event_callback_mock |
| 34 |
) |
| 35 |
|
| 36 |
assert subscription_id in github_api.repos.events._subscriptions |
| 37 |
while not event_callback_mock.called: |
| 38 |
await asyncio.sleep(0) |
| 39 |
|
| 40 |
event: GitHubEventModel = event_callback_mock.call_args[0][0] |
| 41 |
assert event.type == "PushEvent" |
| 42 |
|
| 43 |
assert len(asyncio.all_tasks(github_api._client._loop) - tasks_before) == 1 |
| 44 |
|
| 45 |
tasks = github_api.repos.events.unsubscribe( |
| 46 |
subscription_id=subscription_id) |
| 47 |
await asyncio.wait(tasks) |
| 48 |
assert not github_api.repos.events._subscriptions |
| 49 |
assert len(asyncio.all_tasks(github_api._client._loop) - tasks_before) == 0 |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.asyncio |
| 53 |
async def test_stopping_all_subscriptions(github_api: GitHubAPI, mock_requests: MockedRequests): |
| 54 |
tasks_before = asyncio.all_tasks( |
| 55 |
github_api._client._loop) # This includes the test task |
| 56 |
assert len(tasks_before) == 1 |
| 57 |
|
| 58 |
event_callback_mock = AsyncMock() |
| 59 |
|
| 60 |
subscription_id_1 = await github_api.repos.events.subscribe( |
| 61 |
TEST_REPOSITORY_NAME, event_callback=event_callback_mock |
| 62 |
) |
| 63 |
subscription_id_2 = await github_api.repos.events.subscribe( |
| 64 |
TEST_REPOSITORY_NAME, event_callback=event_callback_mock |
| 65 |
) |
| 66 |
|
| 67 |
assert subscription_id_1 in github_api.repos.events._subscriptions |
| 68 |
assert subscription_id_2 in github_api.repos.events._subscriptions |
| 69 |
while not event_callback_mock.called: |
| 70 |
await asyncio.sleep(0) |
| 71 |
|
| 72 |
event: GitHubEventModel = event_callback_mock.call_args[0][0] |
| 73 |
assert event.type == "PushEvent" |
| 74 |
|
| 75 |
assert len(asyncio.all_tasks(github_api._client._loop) - tasks_before) == 2 |
| 76 |
|
| 77 |
await github_api.repos.events.unsubscribe_all() |
| 78 |
assert len(asyncio.all_tasks(github_api._client._loop) - tasks_before) == 0 |
| 79 |
|
| 80 |
|
| 81 |
@pytest.mark.asyncio |
| 82 |
async def test_stopping_all_subscriptions_async_with( |
| 83 |
client_session: aiohttp.ClientSession, |
| 84 |
mock_requests: MockedRequests, |
| 85 |
): |
| 86 |
event_loop = asyncio.get_event_loop() |
| 87 |
tasks_before = asyncio.all_tasks(event_loop) # This includes the test task |
| 88 |
assert len(tasks_before) == 1 |
| 89 |
|
| 90 |
event_callback_mock = AsyncMock() |
| 91 |
|
| 92 |
async with GitHubAPI(token=TOKEN, session=client_session) as github_api: |
| 93 |
subscription_id = await github_api.repos.events.subscribe( |
| 94 |
TEST_REPOSITORY_NAME, event_callback=event_callback_mock |
| 95 |
) |
| 96 |
|
| 97 |
assert subscription_id in github_api.repos.events._subscriptions |
| 98 |
while not event_callback_mock.called: |
| 99 |
await asyncio.sleep(0) |
| 100 |
|
| 101 |
event: GitHubEventModel = event_callback_mock.call_args[0][0] |
| 102 |
assert event.type == "PushEvent" |
| 103 |
|
| 104 |
assert len(asyncio.all_tasks(event_loop) - tasks_before) == 1 |
| 105 |
|
| 106 |
assert len(asyncio.all_tasks(event_loop) - tasks_before) == 0 |
| 107 |
|
| 108 |
|
| 109 |
@pytest.mark.asyncio |
| 110 |
async def test_subscription_exceptions_not_modified( |
| 111 |
github_api: GitHubAPI, |
| 112 |
mock_response: MockResponse, |
| 113 |
mock_requests: MockedRequests, |
| 114 |
wait_mock: None, |
| 115 |
): |
| 116 |
mock_requests.clear() |
| 117 |
event_callback_mock = AsyncMock() |
| 118 |
error_callback_mock = AsyncMock() |
| 119 |
|
| 120 |
mock_response.mock_status = 304 |
| 121 |
|
| 122 |
subscription_id = await github_api.repos.events.subscribe( |
| 123 |
TEST_REPOSITORY_NAME, |
| 124 |
event_callback=event_callback_mock, |
| 125 |
error_callback=error_callback_mock, |
| 126 |
) |
| 127 |
|
| 128 |
while mock_requests.called < 10: |
| 129 |
await asyncio.sleep(0) |
| 130 |
|
| 131 |
assert subscription_id in github_api.repos.events._subscriptions |
| 132 |
assert not event_callback_mock.called |
| 133 |
assert not error_callback_mock.called |
| 134 |
|
| 135 |
github_api.repos.events.unsubscribe() |
| 136 |
assert not github_api.repos.events._subscriptions |
| 137 |
|
| 138 |
|
| 139 |
@pytest.mark.asyncio |
| 140 |
async def test_subscription_exceptions_not_found( |
| 141 |
github_api: GitHubAPI, |
| 142 |
mock_response: MockResponse, |
| 143 |
wait_mock: None, |
| 144 |
): |
| 145 |
event_callback_mock = AsyncMock() |
| 146 |
error_callback_mock = AsyncMock() |
| 147 |
|
| 148 |
mock_response.mock_status = 404 |
| 149 |
|
| 150 |
await github_api.repos.events.subscribe( |
| 151 |
TEST_REPOSITORY_NAME, |
| 152 |
event_callback=event_callback_mock, |
| 153 |
error_callback=error_callback_mock, |
| 154 |
) |
| 155 |
|
| 156 |
while not error_callback_mock.called: |
| 157 |
await asyncio.sleep(0) |
| 158 |
|
| 159 |
assert not event_callback_mock.called |
| 160 |
assert error_callback_mock.called |
| 161 |
|
| 162 |
github_api.repos.events.unsubscribe() |
| 163 |
assert not github_api.repos.events._subscriptions |
| 164 |
|
| 165 |
|
| 166 |
@pytest.mark.asyncio |
| 167 |
async def test_subscription_exception( |
| 168 |
github_api: GitHubAPI, |
| 169 |
mock_response: MockResponse, |
| 170 |
mock_requests: MockedRequests, |
| 171 |
wait_mock: None, |
| 172 |
): |
| 173 |
event_callback_mock = AsyncMock() |
| 174 |
error_callback_mock = AsyncMock() |
| 175 |
|
| 176 |
mock_response.mock_status = 400 |
| 177 |
|
| 178 |
await github_api.repos.events.subscribe( |
| 179 |
TEST_REPOSITORY_NAME, |
| 180 |
event_callback=event_callback_mock, |
| 181 |
error_callback=error_callback_mock, |
| 182 |
) |
| 183 |
|
| 184 |
while not error_callback_mock.called: |
| 185 |
await asyncio.sleep(0) |
| 186 |
|
| 187 |
while mock_requests.called < 10: |
| 188 |
await asyncio.sleep(0) |
| 189 |
|
| 190 |
assert not event_callback_mock.called |
| 191 |
assert error_callback_mock.called |
| 192 |
|
| 193 |
github_api.repos.events.unsubscribe() |
| 194 |
assert not github_api.repos.events._subscriptions |
| 195 |
|
| 196 |
|
| 197 |
@pytest.mark.asyncio |
| 198 |
async def test_subscription_exception_in_handler( |
| 199 |
github_api: GitHubAPI, |
| 200 |
mock_response: MockResponse, |
| 201 |
wait_mock: None, |
| 202 |
): |
| 203 |
event_callback_mock = AsyncMock(side_effect=TypeError) |
| 204 |
error_callback_mock = AsyncMock() |
| 205 |
|
| 206 |
await github_api.repos.events.subscribe( |
| 207 |
TEST_REPOSITORY_NAME, |
| 208 |
event_callback=event_callback_mock, |
| 209 |
error_callback=error_callback_mock, |
| 210 |
) |
| 211 |
|
| 212 |
while not event_callback_mock.called and not error_callback_mock.called: |
| 213 |
await asyncio.sleep(0) |
| 214 |
|
| 215 |
assert event_callback_mock.called |
| 216 |
assert error_callback_mock.called |
| 217 |
|
| 218 |
github_api.repos.events.unsubscribe() |
| 219 |
assert not github_api.repos.events._subscriptions |