1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Collection of core tests improvements (#33757)

* Collection of core tests improvements

* Added some more

* Fix aiohttp client response release
This commit is contained in:
Franck Nijhof
2020-04-07 18:33:23 +02:00
committed by GitHub
parent 1f7803c541
commit 60bc517d01
12 changed files with 302 additions and 343 deletions

View File

@@ -1,5 +1,4 @@
"""Aiohttp test utils."""
import asyncio
from contextlib import contextmanager
import json as _json
import re
@@ -13,7 +12,7 @@ from yarl import URL
from homeassistant.const import EVENT_HOMEASSISTANT_CLOSE
retype = type(re.compile(""))
RETYPE = type(re.compile(""))
def mock_stream(data):
@@ -58,7 +57,7 @@ class AiohttpClientMocker:
if content is None:
content = b""
if not isinstance(url, retype):
if not isinstance(url, RETYPE):
url = URL(url)
if params:
url = url.with_query(params)
@@ -173,7 +172,7 @@ class AiohttpClientMockResponse:
return False
# regular expression matching
if isinstance(self._url, retype):
if isinstance(self._url, RETYPE):
return self._url.search(str(url)) is not None
if (
@@ -220,25 +219,20 @@ class AiohttpClientMockResponse:
"""Return content."""
return mock_stream(self.response)
@asyncio.coroutine
def read(self):
async def read(self):
"""Return mock response."""
return self.response
@asyncio.coroutine
def text(self, encoding="utf-8"):
async def text(self, encoding="utf-8"):
"""Return mock response as a string."""
return self.response.decode(encoding)
@asyncio.coroutine
def json(self, encoding="utf-8"):
async def json(self, encoding="utf-8"):
"""Return mock response as a json."""
return _json.loads(self.response.decode(encoding))
@asyncio.coroutine
def release(self):
"""Mock release."""
pass
def raise_for_status(self):
"""Raise error if status is 400 or higher."""
@@ -253,7 +247,6 @@ class AiohttpClientMockResponse:
def close(self):
"""Mock close."""
pass
@contextmanager