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.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
|
||||
import io
|
||||
|
||||
@@ -112,8 +117,7 @@ class PushCamera(Camera):
|
||||
imgbuf = io.BytesIO()
|
||||
image.save(imgbuf, "JPEG")
|
||||
|
||||
self.queue.append(imgbuf.getvalue())
|
||||
self._current_image = imgbuf.getvalue()
|
||||
return imgbuf.getvalue()
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
@@ -144,6 +148,7 @@ class PushCamera(Camera):
|
||||
self._expired = async_track_point_in_utc_time(
|
||||
self.hass, reset_state, dt_util.utcnow() + self._timeout)
|
||||
|
||||
self._current_image = self.queue[-1]
|
||||
self.async_schedule_update_ha_state()
|
||||
|
||||
async def async_camera_image(self):
|
||||
|
@@ -2,7 +2,7 @@
|
||||
import io
|
||||
|
||||
from datetime import timedelta
|
||||
from unittest import mock
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant import core as ha
|
||||
from homeassistant.setup import async_setup_component
|
||||
@@ -10,14 +10,15 @@ from homeassistant.util import dt as dt_util
|
||||
from tests.components.auth import async_setup_auth
|
||||
|
||||
|
||||
@mock.patch("PIL.Image.new")
|
||||
async def test_bad_posting(aioclient_mock, hass, aiohttp_client):
|
||||
"""Test that posting to wrong api endpoint fails."""
|
||||
await async_setup_component(hass, 'camera', {
|
||||
'camera': {
|
||||
'platform': 'push',
|
||||
'name': 'config_test',
|
||||
}})
|
||||
with patch('homeassistant.components.camera.push.PushCamera._blank_image',
|
||||
return_value=io.BytesIO(b'fakeinit')):
|
||||
await async_setup_component(hass, 'camera', {
|
||||
'camera': {
|
||||
'platform': 'push',
|
||||
'name': 'config_test',
|
||||
}})
|
||||
|
||||
client = await async_setup_auth(hass, aiohttp_client)
|
||||
|
||||
@@ -32,14 +33,15 @@ async def test_bad_posting(aioclient_mock, hass, aiohttp_client):
|
||||
assert resp.status == 400
|
||||
|
||||
|
||||
@mock.patch("PIL.Image.new")
|
||||
async def test_posting_url(aioclient_mock, hass, aiohttp_client):
|
||||
"""Test that posting to api endpoint works."""
|
||||
await async_setup_component(hass, 'camera', {
|
||||
'camera': {
|
||||
'platform': 'push',
|
||||
'name': 'config_test',
|
||||
}})
|
||||
with patch('homeassistant.components.camera.push.PushCamera._blank_image',
|
||||
return_value=io.BytesIO(b'fakeinit')):
|
||||
await async_setup_component(hass, 'camera', {
|
||||
'camera': {
|
||||
'platform': 'push',
|
||||
'name': 'config_test',
|
||||
}})
|
||||
|
||||
client = await async_setup_auth(hass, aiohttp_client)
|
||||
files = {'image': io.BytesIO(b'fake')}
|
||||
|
Reference in New Issue
Block a user