Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mozillazg committed Aug 27, 2017
1 parent 35bcad1 commit 38de3d7
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 3 deletions.
13 changes: 13 additions & 0 deletions .coveragerc
@@ -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__.:
3 changes: 0 additions & 3 deletions aiobearychat/abc.py
Expand Up @@ -8,9 +8,6 @@

class Requester(_abc.ABC):

def __init__(self, *args, **kwargs):
pass

@_abc.abstractmethod
async def request(self, method: str, url: str,
body: bytes = b'', headers: Optional[Dict] = None,
Expand Down
1 change: 1 addition & 0 deletions requirements_dev.txt
Expand Up @@ -10,6 +10,7 @@ Jinja2>=2.9.6
marshmallow>=2.13.6
pre-commit>=0.7.6
pypandoc>=1.4
pytest-asyncio>=0.6.0
pytest-cov>=2.2.0
pytest>=2.8.5
Sphinx>=1.3.1
Expand Down
51 changes: 51 additions & 0 deletions tests/conftest.py
@@ -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 added tests/openapi/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions tests/openapi/test_aiohttp.py
@@ -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
49 changes: 49 additions & 0 deletions tests/openapi/test_core.py
@@ -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 added tests/requesters/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions tests/requesters/test_aiohttp.py
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-

from aiobearychat.requesters.aiohttp import Requester # noqa
Empty file added tests/rtm/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions tests/rtm/test_aiohttp.py
@@ -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
39 changes: 39 additions & 0 deletions tests/rtm/test_http.py
@@ -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
51 changes: 51 additions & 0 deletions tests/test_sansio.py
@@ -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

0 comments on commit 38de3d7

Please sign in to comment.