From: Scott Gasch Date: Sun, 6 Feb 2022 21:45:21 +0000 (-0800) Subject: Add a simple test for waitable presence. X-Git-Url: https://wannabe.guru.org/gitweb/?a=commitdiff_plain;h=b22b39493c5b6c747b16e9430f3833bb8869cef6;p=python_utils.git Add a simple test for waitable presence. --- diff --git a/dateparse/dateparse_utils.py b/dateparse/dateparse_utils.py index 54a4736..79ce8a3 100755 --- a/dateparse/dateparse_utils.py +++ b/dateparse/dateparse_utils.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# type: ignore """ Parse dates in a variety of formats. diff --git a/string_utils.py b/string_utils.py index 3ae9eea..ec662d2 100644 --- a/string_utils.py +++ b/string_utils.py @@ -142,16 +142,16 @@ MARGIN_RE = re.compile(r"^[^\S\r\n]+") ESCAPE_SEQUENCE_RE = re.compile(r"\[[^A-Za-z]*[A-Za-z]") NUM_SUFFIXES = { - "Pb": (1024 ** 5), - "P": (1024 ** 5), - "Tb": (1024 ** 4), - "T": (1024 ** 4), - "Gb": (1024 ** 3), - "G": (1024 ** 3), - "Mb": (1024 ** 2), - "M": (1024 ** 2), - "Kb": (1024 ** 1), - "K": (1024 ** 1), + "Pb": (1024**5), + "P": (1024**5), + "Tb": (1024**4), + "T": (1024**4), + "Gb": (1024**3), + "G": (1024**3), + "Mb": (1024**2), + "M": (1024**2), + "Kb": (1024**1), + "K": (1024**1), } @@ -1075,7 +1075,7 @@ def to_date(in_str: str) -> Optional[datetime.date]: """ Parses a date string. See DateParser docs for details. """ - import dateparse.dateparse_utils as dp # type: ignore + import dateparse.dateparse_utils as dp try: d = dp.DateParser() diff --git a/tests/waitable_presence_test.py b/tests/waitable_presence_test.py new file mode 100755 index 0000000..ff4225c --- /dev/null +++ b/tests/waitable_presence_test.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +import re +import unittest +from unittest.mock import MagicMock + +import base_presence +import unittest_utils # Needed for --unittests_ignore_perf flag +import waitable_presence +from type.locations import Location + + +class TestWaitablePresence(unittest.TestCase): + def test_basic_functionality(self): + mock_detector = base_presence.PresenceDetection() + mock_detector.update = MagicMock() + mock_detector.is_anyone_in_location_now = MagicMock(return_value=True) + wp = waitable_presence.WaitablePresenceDetectorWithMemory( + 1.0, Location.HOUSE, mock_detector + ) + changed = wp.wait() + (someone_is_home, since) = wp.is_someone_home() + mock_detector.update.assert_called_with() + mock_detector.is_anyone_in_location_now.assert_called_with(Location.HOUSE) + self.assertTrue(changed) + self.assertTrue(someone_is_home) + self.assertNotEqual(None, since) + wp.reset() + wp.shutdown() + + +if __name__ == '__main__': + unittest.main() diff --git a/waitable_presence.py b/waitable_presence.py index e1da431..46d7cbe 100644 --- a/waitable_presence.py +++ b/waitable_presence.py @@ -41,13 +41,17 @@ class WaitablePresenceDetectorWithMemory(state_tracker.WaitableAutomaticStateTra self, override_update_interval_sec: float = 60.0, override_location: Location = site_config.get_location(), + injected_presence_detector: Optional[base_presence.PresenceDetection] = None, ) -> None: self.last_someone_is_home: Optional[bool] = None self.someone_is_home: Optional[bool] = None self.everyone_gone_since: Optional[datetime.datetime] = None self.someone_home_since: Optional[datetime.datetime] = None self.location = override_location - self.detector: base_presence.PresenceDetection = base_presence.PresenceDetection() + if injected_presence_detector is not None: + self.detector: base_presence.PresenceDetection = injected_presence_detector + else: + self.detector = base_presence.PresenceDetection() super().__init__( { 'poll_presence': override_update_interval_sec, @@ -87,14 +91,14 @@ class WaitablePresenceDetectorWithMemory(state_tracker.WaitableAutomaticStateTra logger.debug('PresenceDetector is incomplete; trying to reinitialize...') self.detector = base_presence.PresenceDetection() - def is_someone_home(self) -> Tuple[bool, datetime.datetime]: + def is_someone_home(self) -> Optional[Tuple[bool, datetime.datetime]]: """Returns a tuple of a bool that indicates whether someone is home and a datetime that indicates how long either someone has been home or no one has been home. """ if self.someone_is_home is None: - raise Exception("Too Soon!") + return None # checked too soon, wait a bit. if self.someone_is_home: assert self.someone_home_since is not None return (True, self.someone_home_since)