From 2143fecb4be9014248c8ad291864ddec8214ff93 Mon Sep 17 00:00:00 2001 From: Austin Tyler Conn Date: Thu, 8 Aug 2024 20:24:04 +0000 Subject: [PATCH] Add tests --- tests/test_api.py | 22 +++++++++++++++++++ tests/test_camera_functions.py | 25 +++++++++++++++++++++ tests/test_sync_module.py | 40 ++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) diff --git a/tests/test_api.py b/tests/test_api.py index 8077fdde..449bf4fe 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -203,3 +203,25 @@ async def test_wait_for_command(self, mock_resp): response = await api.wait_for_command(self.blink, None) self.assertFalse(response) + + async def test_request_camera_snooze(self, mock_resp): + """Test request_camera_snooze.""" + mock_resp.return_value = mresp.MockResponse({}, 200) + response = await api.request_camera_snooze( + self.blink, "network", "camera_id", "owl", {} + ) + self.assertEqual(response.status, 200) + response = await api.request_camera_snooze( + self.blink, "network", "camera_id", "catalina", {} + ) + self.assertEqual(response.status, 200) + response = await api.request_camera_snooze( + self.blink, "network", "camera_id", "doorbell", {} + ) + self.assertEqual(response.status, 200) + + async def test_request_sync_snooze(self, mock_resp): + """Test sync snooze update.""" + mock_resp.return_value = mresp.MockResponse({}, 200) + response = await api.request_sync_snooze(self.blink, "network", {}) + self.assertEqual(response.status, 200) diff --git a/tests/test_camera_functions.py b/tests/test_camera_functions.py index 0ca75699..d066e364 100644 --- a/tests/test_camera_functions.py +++ b/tests/test_camera_functions.py @@ -9,6 +9,7 @@ import datetime from unittest import mock from unittest import IsolatedAsyncioTestCase +from blinkpy import api from blinkpy.blinkpy import Blink from blinkpy.helpers.util import BlinkURLHandler from blinkpy.sync_module import BlinkSyncModule @@ -222,6 +223,30 @@ async def test_night_vision(self, mock_resp): mock_resp.return_value = mresp.MockResponse({"code": 400}, 400) self.assertIsNone(await self.camera.async_set_night_vision("on")) + async def test_snooze_till(self, mock_resp): + """Test snooze_till property.""" + mock_resp = {"camera": [{"snooze_till": 1234567890}]} + with mock.patch.object( + api, + "request_get_config", + return_value=mock_resp, + ): + result = await self.camera.snooze_till + self.assertEqual(result, {"camera": [{"snooze_till": 1234567890}]}) + + async def test_async_snooze(self, mock_resp): + """Test async_snooze function.""" + mock_resp = mresp.MockResponse({}, 200) + with mock.patch("blinkpy.api.request_camera_snooze", return_value=mock_resp): + response = await self.camera.async_snooze() + self.assertEqual(response, {}) + mock_resp = mresp.MockResponse({}, 200) + with mock.patch("blinkpy.api.request_camera_snooze", return_value=mock_resp): + response = await self.camera.async_snooze() + self.assertEqual(response, {}) + response = await self.camera.async_snooze("invalid_value") + self.assertIsNone(response) + async def test_record(self, mock_resp): """Test camera record function.""" with mock.patch( diff --git a/tests/test_sync_module.py b/tests/test_sync_module.py index a1c81cbf..eff1c529 100644 --- a/tests/test_sync_module.py +++ b/tests/test_sync_module.py @@ -1,6 +1,7 @@ """Tests camera and system functions.""" import datetime +from json import dumps import logging from unittest import IsolatedAsyncioTestCase from unittest import mock @@ -653,3 +654,42 @@ async def test_download_delete(self, mock_prepdl, mock_del, mock_dl, mock_resp): mock_del.return_value = mock.AsyncMock() mock_dl.return_value = False self.assertFalse(await item.download_video_delete(self.blink, "filename.mp4")) + + async def test_async_snooze(self, mock_resp): + """Test successful snooze.""" + with mock.patch( + "blinkpy.api.request_sync_snooze", new_callable=mock.AsyncMock + ) as mock_resp_local: + mock_resp_local.return_value.status = 200 + mock_resp_local.return_value.json.return_value = {"status": 200} + snooze_time = 240 + expected_data = dumps({"snooze_time": snooze_time}) + expected_response = {"status": 200} + + self.assertEqual( + await self.blink.sync["test"].async_snooze(snooze_time), + expected_response, + ) + mock_resp_local.assert_called_once_with( + self.blink, + self.blink.sync["test"].network_id, + data=expected_data, + ) + + mock_resp_local.return_value.status = 400 + mock_resp_local.return_value.json.return_value = None + expected_response = None + + self.assertEqual( + await self.blink.sync["test"].async_snooze(snooze_time), + expected_response, + ) + + async def test_snooze_till(self, mock_resp) -> None: + """Test snooze_till method.""" + mock_resp.return_value = {"snooze_till": "2022-01-01T00:00:00Z"} + self.assertEqual( + await self.blink.sync["test"].snooze_till, "2022-01-01T00:00:00Z" + ) + mock_resp.return_value = None + self.assertIsNone(await self.blink.sync["test"].snooze_till)