mirror of
https://github.com/home-assistant/core.git
synced 2025-08-11 16:45:19 +02:00
better mocking
This commit is contained in:
@@ -104,6 +104,11 @@ class PushCamera(Camera):
|
|||||||
self._timeout = timeout
|
self._timeout = timeout
|
||||||
self.queue = deque([], buffer_size)
|
self.queue = deque([], buffer_size)
|
||||||
|
|
||||||
|
self.queue.append(self._blank_image())
|
||||||
|
self._current_image = self.queue[0]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _blank_image(cls):
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import io
|
import io
|
||||||
|
|
||||||
@@ -112,8 +117,7 @@ class PushCamera(Camera):
|
|||||||
imgbuf = io.BytesIO()
|
imgbuf = io.BytesIO()
|
||||||
image.save(imgbuf, "JPEG")
|
image.save(imgbuf, "JPEG")
|
||||||
|
|
||||||
self.queue.append(imgbuf.getvalue())
|
return imgbuf.getvalue()
|
||||||
self._current_image = imgbuf.getvalue()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
@@ -144,6 +148,7 @@ class PushCamera(Camera):
|
|||||||
self._expired = async_track_point_in_utc_time(
|
self._expired = async_track_point_in_utc_time(
|
||||||
self.hass, reset_state, dt_util.utcnow() + self._timeout)
|
self.hass, reset_state, dt_util.utcnow() + self._timeout)
|
||||||
|
|
||||||
|
self._current_image = self.queue[-1]
|
||||||
self.async_schedule_update_ha_state()
|
self.async_schedule_update_ha_state()
|
||||||
|
|
||||||
async def async_camera_image(self):
|
async def async_camera_image(self):
|
||||||
|
@@ -2,7 +2,7 @@
|
|||||||
import io
|
import io
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from unittest import mock
|
from unittest.mock import patch
|
||||||
|
|
||||||
from homeassistant import core as ha
|
from homeassistant import core as ha
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
@@ -10,9 +10,10 @@ from homeassistant.util import dt as dt_util
|
|||||||
from tests.components.auth import async_setup_auth
|
from tests.components.auth import async_setup_auth
|
||||||
|
|
||||||
|
|
||||||
@mock.patch("PIL.Image.new")
|
|
||||||
async def test_bad_posting(aioclient_mock, hass, aiohttp_client):
|
async def test_bad_posting(aioclient_mock, hass, aiohttp_client):
|
||||||
"""Test that posting to wrong api endpoint fails."""
|
"""Test that posting to wrong api endpoint fails."""
|
||||||
|
with patch('homeassistant.components.camera.push.PushCamera._blank_image',
|
||||||
|
return_value=io.BytesIO(b'fakeinit')):
|
||||||
await async_setup_component(hass, 'camera', {
|
await async_setup_component(hass, 'camera', {
|
||||||
'camera': {
|
'camera': {
|
||||||
'platform': 'push',
|
'platform': 'push',
|
||||||
@@ -32,9 +33,10 @@ async def test_bad_posting(aioclient_mock, hass, aiohttp_client):
|
|||||||
assert resp.status == 400
|
assert resp.status == 400
|
||||||
|
|
||||||
|
|
||||||
@mock.patch("PIL.Image.new")
|
|
||||||
async def test_posting_url(aioclient_mock, hass, aiohttp_client):
|
async def test_posting_url(aioclient_mock, hass, aiohttp_client):
|
||||||
"""Test that posting to api endpoint works."""
|
"""Test that posting to api endpoint works."""
|
||||||
|
with patch('homeassistant.components.camera.push.PushCamera._blank_image',
|
||||||
|
return_value=io.BytesIO(b'fakeinit')):
|
||||||
await async_setup_component(hass, 'camera', {
|
await async_setup_component(hass, 'camera', {
|
||||||
'camera': {
|
'camera': {
|
||||||
'platform': 'push',
|
'platform': 'push',
|
||||||
|
Reference in New Issue
Block a user