def test_get_or_default(self): # TODO: TestCaseType must be a generic type like a TestCaseType[T] = Tuple[..., T, T] TestCaseType = Tuple[Callable[[], Optional[str]], # value_getter func str, # default str, # expected ] class Bar: def __init__(self, value: Optional[int]) -> None: self.value: Optional[int] = value class Foo: def __init__(self, bar: Optional[Bar]) -> None: self.bar: Optional[Bar] = bar bar1 = Bar(42) foo1 = Foo(bar1) foo2 = Foo(None) bar3 = Bar(None) foo3 = Foo(bar3) test_cases: List[TestCaseType] = [ (lambda: None, 'default str', 'default str'), (lambda: None, 1, 1), (lambda: foo1.bar.value, 1, 42), (lambda: foo2.bar.value, 2, 2), (lambda: foo3.bar.value, 3, 3), ] for (value_getter, default, expected) in test_cases: self.assertEqual(expected, Value.get_or_default(value_getter, default))
def __need_to_show_break_notification(self) -> bool: if not self.is_work_time: return False if self.is_lock_screen_activated: return False if not self.is_break_notification_allowed_to_show: return False now = datetime.now() start_work_time = self.is_work_time_update_time last_break_time = self.last_lock_screen_time if self.last_lock_screen_time else start_work_time if (now - last_break_time ).total_seconds() < self.user_breaks_interval_hours * 60 * 60: return False last_break_reminder_time = Value.get_or_default( lambda: self.last_break_notification. last_shown, # type: ignore[union-attr] start_work_time) if (now - last_break_reminder_time).total_seconds() < 15 * 60: return False return True