Skip to content

Commit

Permalink
100% Unit tests on scout.py
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSchapp committed Oct 1, 2022
1 parent bdeacef commit 17c26a4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 11 deletions.
6 changes: 0 additions & 6 deletions firmware/lib/scout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down
97 changes: 92 additions & 5 deletions tests/test_scout.py
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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

0 comments on commit 17c26a4

Please sign in to comment.