From 17c26a4c27b51dca6e8450f7a7fee302a31ef406 Mon Sep 17 00:00:00 2001 From: Michael Schappacher Date: Sat, 1 Oct 2022 17:46:21 -0400 Subject: [PATCH] 100% Unit tests on scout.py --- firmware/lib/scout.py | 6 --- tests/test_scout.py | 97 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 92 insertions(+), 11 deletions(-) diff --git a/firmware/lib/scout.py b/firmware/lib/scout.py index 9413a51..da8d69e 100644 --- a/firmware/lib/scout.py +++ b/firmware/lib/scout.py @@ -2,8 +2,6 @@ import urequests as requests import ujson as json import uos as os -from lib.pin import onboard_led -import time class Scout: @@ -112,10 +110,6 @@ def connect_to_baby_buddy(base_url): baby_buddy_reachable = True except OSError: print("Failed to connect to BabyBuddy") - onboard_led() - time.sleep(1) - onboard_led() - onboard_led(0) print("Connected to BabyBuddy") return baby_buddy diff --git a/tests/test_scout.py b/tests/test_scout.py index 6efb16b..4c839ef 100644 --- a/tests/test_scout.py +++ b/tests/test_scout.py @@ -1,8 +1,7 @@ from unittest.mock import patch -from lib.scout import Scout +from lib.scout import Scout, connect_to_baby_buddy, send_api_request with patch('lib.scout.send_api_request') as mock_api: - scout = Scout("fake_url") @patch('lib.scout.send_api_request') @@ -12,13 +11,101 @@ def test_init_scout_children(mock_api_request): mock_api_request.assert_called_with("fake_url", "children") @patch('lib.scout.send_api_request') -def test_init_scout_children(mock_api_request): +def test_bottle_feed(mock_api_request): # Test Initializing Scout class scout.bottle_feed(0) mock_api_request.assert_called_with('fake_url', path='timers', data={'child': 0, 'name': 'feedings'}) @patch('lib.scout.send_api_request') -def test_init_scout_children(mock_api_request): +def test_right_breast(mock_api_request): # Test Initializing Scout class - scout.bottle_feed(0) + scout.right_breast(0) + mock_api_request.assert_called_with('fake_url', path='timers', data={'child': 0, 'name': 'feedings'}) + +@patch('lib.scout.send_api_request') +def test_left_breast(mock_api_request): + # Test Initializing Scout class + scout.left_breast(0) mock_api_request.assert_called_with('fake_url', path='timers', data={'child': 0, 'name': 'feedings'}) + +@patch('lib.scout.send_api_request') +def test_both_breasts(mock_api_request): + # Test Initializing Scout class + scout.breast_feed(0) + mock_api_request.assert_called_with('fake_url', path='timers', data={'child': 0, 'name': 'feedings'}) + +@patch('lib.scout.send_api_request') +def test_wet_solid_diaper(mock_api_request): + # Test Initializing Scout class + scout.wet_solid_diaper(0) + mock_api_request.assert_called_with('fake_url', 'changes', data={'wet': True, 'solid': True, 'child': 0}) + +@patch('lib.scout.send_api_request') +def test_solid_diaper(mock_api_request): + # Test Initializing Scout class + scout.solid_diaper(0) + mock_api_request.assert_called_with('fake_url', 'changes', data={'wet': False, 'solid': True, 'child': 0}) + +@patch('lib.scout.send_api_request') +def test_wet_diaper(mock_api_request): + # Test Initializing Scout class + scout.wet_diaper(0) + mock_api_request.assert_called_with('fake_url', 'changes', data={'wet': True, 'solid': False, 'child': 0}) + + +@patch('lib.scout.send_api_request') +def test_conect_to_baby_buddy(mock_api_request): + mock_api_request.side_effect = [OSError("test"), {"results": [{"id": 1}]}] + # Test Initializing Scout class + scout = connect_to_baby_buddy('fake_url') + mock_api_request.assert_called_with('fake_url', 'children') + +@patch('lib.scout.utils.retrieve_auth_variables') +@patch('lib.scout.requests') +def test_get_send_api_request(mock_request, mock_utils): + class FakeRequest: + def __init__(self): + self.content = '{"test": "test"}' + fake = FakeRequest() + mock_request.get.side_effect = [fake] + mock_utils.side_effect = [{"AUTHORIZATION": {"test": "test"}}, {"AUTHORIZATION": {"test": "test"}}] + request = send_api_request("test", "best", {"test": "test"}) + assert request == {'test': 'test'} + +@patch('lib.scout.utils.retrieve_auth_variables') +@patch('lib.scout.requests') +def test_post_send_api_request(mock_request, mock_utils): + class FakeRequest: + def __init__(self): + self.content = '{"test": "test"}' + fake = FakeRequest() + mock_request.post.side_effect = [fake] + mock_utils.side_effect = [{"AUTHORIZATION": {"test": "test"}}, {"AUTHORIZATION": {"test": "test"}}] + request = send_api_request("test", "best", {"headers": "headers"}, {"data": "data"}) + assert request == {'test': 'test'} + +@patch('lib.scout.send_api_request') +def test_sleep(mock_api_request): + # Test Initializing Scout class + scout.sleep(0) + mock_api_request.assert_called_with('fake_url', path='timers', data={'child': 0, 'name': 'sleep'}) + +@patch('lib.scout.send_api_request') +def test_tummy_time(mock_api_request): + # Test Initializing Scout class + scout.tummy_time(0) + mock_api_request.assert_called_with('fake_url', path='timers', data={'child': 0, 'name': 'tummy-times'}) + +@patch('lib.scout.send_api_request') +def test_get_timers(mock_api_request): + mock_api_request.side_effect = [{"results": [{"name": "sleep", "child": 0, "active": True}]}] + # Test Initializing Scout class + timer = scout.get_timer(0, "sleep") + assert timer == {'active': True, 'child': 0, 'name': 'sleep'} + +@patch('lib.scout.send_api_request') +def test_resolve_timers_current_timer(mock_api_request): + mock_api_request.side_effect = [{"results": [{"name": "sleep", "child": 0, "active": True, "id": 0}]}, None] + # Test Initializing Scout class + timer = scout.resolve_timers(0, "sleep", {"timer": 0}) + assert timer == None \ No newline at end of file