Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
232 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[run] | ||
omit = | ||
aiobearychat/openapi/*_api.py | ||
aiobearychat/rtm/*_api.py | ||
|
||
[report] | ||
exclude_lines = | ||
pragma: no cover | ||
except NameError | ||
except ImportError | ||
pass | ||
def main | ||
if __name__ == .__main__.: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
import asyncio | ||
import json | ||
from typing import Optional, Dict | ||
from unittest.mock import MagicMock | ||
|
||
import attr | ||
import pytest | ||
|
||
from aiobearychat.abc import Requester, Response | ||
|
||
|
||
@attr.s | ||
class RequestParams: | ||
method = attr.ib() | ||
url = attr.ib() | ||
body = attr.ib() | ||
headers = attr.ib() | ||
requester_params = attr.ib() | ||
|
||
def body_deserialize(self): | ||
return json.loads(self.body) | ||
|
||
|
||
class MockRequester(Requester): | ||
def __init__(self, mock, response): | ||
self.mock = mock | ||
self.response = response | ||
self.request_params = None | ||
|
||
async def sleep(self, seconds: float) -> None: | ||
asyncio.sleep(0) | ||
self.mock.sleep(seconds) | ||
|
||
async def request(self, method: str, url: str, body: bytes = b'', | ||
headers: Optional[Dict] = None, | ||
**requester_params: Dict) -> Response: | ||
asyncio.sleep(0) | ||
self.request_params = RequestParams( | ||
method, url, body, headers, | ||
requester_params, | ||
) | ||
return self.response | ||
|
||
|
||
@pytest.yield_fixture() | ||
def requester(): | ||
mock = MagicMock() | ||
response = MagicMock() | ||
|
||
yield MockRequester(mock, response) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# -*- coding: utf-8 -*- | ||
from unittest.mock import MagicMock | ||
|
||
from aiobearychat.openapi.aiohttp import OpenAPI | ||
|
||
|
||
def test_init(): | ||
session = MagicMock() | ||
token = 'ttt' | ||
api = OpenAPI(session, token=token) | ||
assert api.meta._requester._session == session | ||
assert api._token == token | ||
assert api.meta._token == token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding: utf-8 -*- | ||
import pytest | ||
|
||
from aiobearychat.openapi.core import OpenAPI | ||
|
||
|
||
def test_init_openapi(requester): | ||
token = 'axxre' | ||
api = OpenAPI(requester, token=token) | ||
assert api._token == token | ||
assert api.meta._requester == requester | ||
|
||
|
||
def test_init_openapi_base_url(requester): | ||
token = 'axxre' | ||
base_url = 'http://a.com/c' | ||
api = OpenAPI(requester, token=token, base_url=base_url) | ||
assert api.base_url == base_url | ||
assert api.meta.base_url == base_url | ||
|
||
|
||
class TestOpenAPI: | ||
@pytest.yield_fixture(autouse=True) | ||
def setup_api(self, requester): | ||
self.token = 'tokenxxx' | ||
self.api = OpenAPI(requester, token=self.token) | ||
|
||
@pytest.mark.asyncio | ||
async def test_meta_get(self): | ||
await self.api.meta.get() | ||
requester = self.api.meta._requester | ||
request_params = requester.request_params | ||
assert request_params.method == 'get' | ||
assert request_params.url == self.api.base_url + '/meta' | ||
|
||
@pytest.mark.asyncio | ||
async def test_message_post(self): | ||
vchannel_id = 'atere_ere' | ||
text = '中文斥呵啥' | ||
attachments = [] | ||
await self.api.message.create(vchannel_id, text, attachments) | ||
requester = self.api.meta._requester | ||
request_params = requester.request_params | ||
payload = request_params.body_deserialize() | ||
assert request_params.method == 'post' | ||
assert request_params.url == self.api.base_url + '/message.create' | ||
assert payload['token'] == self.token | ||
assert payload['vchannel_id'] == vchannel_id | ||
assert payload['attachments'] == attachments |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from aiobearychat.requesters.aiohttp import Requester # noqa |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# -*- coding: utf-8 -*- | ||
from unittest.mock import MagicMock | ||
|
||
from aiobearychat.rtm.aiohttp import RtmAPI | ||
|
||
|
||
def test_init(): | ||
session = MagicMock() | ||
token = 'ttt' | ||
api = RtmAPI(session, token=token) | ||
assert api._requester._session == session | ||
assert api._token == token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
import pytest | ||
|
||
from aiobearychat.rtm.http import RtmAPI | ||
|
||
|
||
class TestRtmAPI: | ||
|
||
@pytest.yield_fixture(autouse=True) | ||
def setup_api(self, requester): | ||
self.token = token = 'token' | ||
self.api = RtmAPI(requester, token=token) | ||
|
||
@pytest.mark.asyncio | ||
async def test_start(self): | ||
api = self.api | ||
await api.start() | ||
requester = api._requester | ||
request_params = requester.request_params | ||
|
||
url = request_params.url | ||
payload = request_params.body_deserialize() | ||
assert url == api.base_url + '/start' | ||
assert payload['token'] == self.token | ||
|
||
@pytest.mark.asyncio | ||
async def test_message(self): | ||
api = self.api | ||
vchannel = '=xxxabc' | ||
text = 'hello' | ||
await api.message(vchannel, text) | ||
requester = api._requester | ||
request_params = requester.request_params | ||
|
||
url = request_params.url | ||
payload = request_params.body_deserialize() | ||
assert url == api.base_url + '/message' | ||
assert payload['token'] == self.token | ||
assert payload['vchannel'] == vchannel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
import json | ||
|
||
import attr | ||
|
||
from aiobearychat import sansio | ||
|
||
|
||
def test_format_url_params_none(): | ||
url = 'http://abc.com/efrf/' | ||
result = sansio.format_url(url, url_params=None) | ||
assert result == url | ||
|
||
|
||
def test_format_url_params_dict(): | ||
url = 'http://abc.com/efrf/' | ||
params = { | ||
'a': 'b', | ||
'c': 'd', | ||
} | ||
result = sansio.format_url(url, url_params=params) | ||
assert result == url + '?a=b&c=d' | ||
|
||
|
||
def test_clean_nothing_keys(): | ||
d = { | ||
'a': 'b', | ||
'c': attr.NOTHING, | ||
} | ||
result = sansio.clean_nothing_keys(d) | ||
assert 'c' not in result | ||
|
||
|
||
def test_clean_nothing_keys_no_change(): | ||
d = { | ||
1: 2, | ||
'a': 'b', | ||
'c': 'e', | ||
} | ||
result = sansio.clean_nothing_keys(d) | ||
assert result == d | ||
|
||
|
||
def test_response_json(): | ||
d = { | ||
'a': 2, | ||
'e': 3, | ||
'abc': '测试', | ||
} | ||
response = sansio.Response(200, {}, json.dumps(d)) | ||
assert response.json() == d |