Added check videos tests

- Moved motion detection setting to happen prior to return to prevent
motion detect sticking upon api failure
This commit is contained in:
Kevin Fronczak
2019-01-26 15:40:02 -05:00
parent 1922b2535c
commit 46c5d5eb3c
2 changed files with 45 additions and 40 deletions
+5 -4
View File
@@ -123,15 +123,16 @@ class BlinkSyncModule():
resp = api.request_videos(self.blink,
time=self.blink.last_refresh,
page=0)
for camera in self.cameras.keys():
self.motion[camera] = False
try:
info = resp['videos']
except (KeyError, TypeError):
_LOGGER.warning("Could not check for motion. Response: %s", resp)
return False
for camera in self.cameras.keys():
self.motion[camera] = False
for entry in info:
try:
name = entry['camera_name']
@@ -140,6 +141,6 @@ class BlinkSyncModule():
self.motion[name] = True
self.last_record[name] = {'clip': clip, 'time': timestamp}
except KeyError:
_LOGGER.info("Could not extract info from video entry.")
_LOGGER.debug("No new videos since last refresh.")
return True
+39 -35
View File
@@ -1,7 +1,6 @@
"""Tests camera and system functions."""
import unittest
from unittest import mock
import pytest
from blinkpy import blinkpy
from blinkpy.sync_module import BlinkSyncModule
@@ -43,42 +42,47 @@ class TestBlinkSyncModule(unittest.TestCase):
mock_resp.return_value = {'devicestatus': True}
self.assertEqual(self.blink.sync['test'].get_camera_info(), True)
@pytest.mark.skip(reason="Method removed.")
def test_get_videos_one_page(self, mock_resp):
"""Test video access."""
mock_resp.return_value = [
{
'camera_name': 'foobar',
'address': '/test/clip_1900_01_01_12_00_00AM.mp4',
'thumbnail': '/test/thumb'
def test_check_new_videos(self, mock_resp):
"""Test recent video response."""
mock_resp.return_value = {
'videos': [{
'camera_name': 'foo',
'address': '/foo/bar.mp4',
'created_at': '1970-01-01T00:00:00+0:00'
}]
}
]
expected_videos = {'foobar': [
{'clip': '/test/clip_1900_01_01_12_00_00AM.mp4',
'thumb': '/test/thumb'}]}
expected_recs = {'foobar': ['1900_01_01_12_00_00AM']}
expected_clips = {'foobar': {
'1900_01_01_12_00_00AM': '/test/clip_1900_01_01_12_00_00AM.mp4'}}
self.blink.sync['test'].get_videos(start_page=0, end_page=0)
self.assertEqual(self.blink.sync['test'].videos, expected_videos)
self.assertEqual(self.blink.sync['test'].record_dates, expected_recs)
self.assertEqual(self.blink.sync['test'].all_clips, expected_clips)
sync_module = self.blink.sync['test']
sync_module.cameras = {'foo': None}
self.assertEqual(sync_module.motion, {})
self.assertTrue(sync_module.check_new_videos())
self.assertEqual(sync_module.last_record['foo'],
{'clip': '/foo/bar.mp4',
'time': '1970-01-01T00:00:00+0:00'})
self.assertEqual(sync_module.motion, {'foo': True})
mock_resp.return_value = {'videos': []}
self.assertTrue(sync_module.check_new_videos())
self.assertEqual(sync_module.motion, {'foo': False})
self.assertEqual(sync_module.last_record['foo'],
{'clip': '/foo/bar.mp4',
'time': '1970-01-01T00:00:00+0:00'})
@pytest.mark.skip(reason="Method removed.")
def test_get_videos_multi_page(self, mock_resp):
"""Test video access with multiple pages."""
mock_resp.return_value = [
{
'camera_name': 'test',
'address': '/foo/bar_1900_01_01_12_00_00AM.mp4',
'thumbnail': '/foobar'
}
]
self.blink.sync['test'].get_videos()
self.assertEqual(mock_resp.call_count, 2)
mock_resp.reset_mock()
self.blink.sync['test'].get_videos(start_page=0, end_page=9)
self.assertEqual(mock_resp.call_count, 10)
def test_check_new_videos_failed(self, mock_resp):
"""Test method when response is unexpected."""
mock_resp.side_effect = [None, 'just a string', {}]
sync_module = self.blink.sync['test']
sync_module.cameras = {'foo': None}
sync_module.motion['foo'] = True
self.assertFalse(sync_module.check_new_videos())
self.assertFalse(sync_module.motion['foo'])
sync_module.motion['foo'] = True
self.assertFalse(sync_module.check_new_videos())
self.assertFalse(sync_module.motion['foo'])
sync_module.motion['foo'] = True
self.assertFalse(sync_module.check_new_videos())
self.assertFalse(sync_module.motion['foo'])
def test_sync_start(self, mock_resp):
"""Test sync start function."""