better mocking

This commit is contained in:
Diogo Gomes
2018-06-27 01:28:20 +01:00
parent b3383c70b3
commit 2b2f4065f3
2 changed files with 22 additions and 15 deletions

View File

@@ -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):

View File

@@ -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,14 +10,15 @@ 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."""
await async_setup_component(hass, 'camera', { with patch('homeassistant.components.camera.push.PushCamera._blank_image',
'camera': { return_value=io.BytesIO(b'fakeinit')):
'platform': 'push', await async_setup_component(hass, 'camera', {
'name': 'config_test', 'camera': {
}}) 'platform': 'push',
'name': 'config_test',
}})
client = await async_setup_auth(hass, aiohttp_client) 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 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."""
await async_setup_component(hass, 'camera', { with patch('homeassistant.components.camera.push.PushCamera._blank_image',
'camera': { return_value=io.BytesIO(b'fakeinit')):
'platform': 'push', await async_setup_component(hass, 'camera', {
'name': 'config_test', 'camera': {
}}) 'platform': 'push',
'name': 'config_test',
}})
client = await async_setup_auth(hass, aiohttp_client) client = await async_setup_auth(hass, aiohttp_client)
files = {'image': io.BytesIO(b'fake')} files = {'image': io.BytesIO(b'fake')}