def test_clock_datetime_timezone(fake_qtile, monkeypatch): """ test clock with datetime timezone """ class FakeDateutilTZ: class TZ: @classmethod def gettz(cls, val): None tz = TZ # Set up references to pytz and dateutil so we know these aren't being used # If they're called, the widget would try to run None(self.timezone) which # would raise an exception clock.pytz = None clock.dateutil = FakeDateutilTZ # Fake datetime module just adds the timezone value to the time clk3 = clock.Clock(timezone=1) fakebar = Bar([clk3], 24) fakebar.window = FakeWindow() fakebar.width = 10 fakebar.height = 10 fakebar.draw = no_op clk3._configure(fake_qtile, fakebar) text = clk3.poll() # Default time is 10:20 and we add 1 hour for the timezone assert text == "11:20"
def test_clock_invalid_timezone(fake_qtile, monkeypatch): """ test clock widget with invalid timezone (and no pytz or dateutil modules) """ class FakeDateutilTZ: @classmethod def tz(cls): return cls @classmethod def gettz(cls, val): return None # pytz and dateutil must not be in the sys.modules dict... monkeypatch.delitem(sys.modules, "pytz") monkeypatch.delitem(sys.modules, "dateutil") # Set up references to pytz and dateutil so we know these aren't being used # If they're called, the widget would try to run None(self.timezone) which # would raise an exception clock.pytz = None clock.dateutil = FakeDateutilTZ # Fake datetime module just adds the timezone value to the time clk2 = clock.Clock(timezone="1") fakebar = Bar([clk2], 24) fakebar.window = FakeWindow() fakebar.width = 10 fakebar.height = 10 fakebar.draw = no_op clk2._configure(fake_qtile, fakebar) # An invalid timezone current causes a TypeError with pytest.raises(TypeError): clk2.poll()
def test_clock_dateutil_timezone(fake_qtile, monkeypatch, fake_window): """test clock with dateutil timezone""" class FakeDateutilTZ: class TZ: @classmethod def gettz(cls, val): return int(val) + 2 tz = TZ # pytz must not be in the sys.modules dict... monkeypatch.delitem(sys.modules, "pytz") # ...but dateutil must be monkeypatch.setitem(sys.modules, "dateutil", True) # Set up references to pytz and dateutil so we know these aren't being used # If they're called, the widget would try to run None(self.timezone) which # would raise an exception clock.pytz = None clock.dateutil = FakeDateutilTZ # Pytz timezone must be a string clk5 = clock.Clock(timezone="1") fakebar = FakeBar([clk5], window=fake_window) clk5._configure(fake_qtile, fakebar) text = clk5.poll() # Default time is 10:20 and we add 1 hour for the timezone plus and extra # 1 for the pytz function assert text == "13:20"
def test_clock(fake_qtile, monkeypatch, fake_window): """test clock output with default settings""" monkeypatch.setattr("libqtile.widget.clock.datetime", MockDatetime) clk1 = clock.Clock() fakebar = FakeBar([clk1], window=fake_window) clk1._configure(fake_qtile, fakebar) text = clk1.poll() assert text == "10:20"
def test_clock_tick(manager_nospawn, minimal_conf_noscreen, monkeypatch): ''' Test clock ticks ''' class FakeDateutilTZ: class TZ: @classmethod def gettz(cls, val): return int(val) + 2 tz = TZ class TickingDateTime(datetime.datetime): offset = 0 @classmethod def now(cls, *args, **kwargs): return cls(2021, 1, 1, 10, 20, 30) # This will return 10:20 on first call and 10:21 on all # subsequent calls def astimezone(self, tzone=None): extra = datetime.timedelta(minutes=TickingDateTime.offset) if TickingDateTime.offset < 1: TickingDateTime.offset += 1 if tzone is None: return self + extra return self + datetime.timedelta(hours=tzone) + extra # pytz must not be in the sys.modules dict... monkeypatch.delitem(sys.modules, "pytz") # ...but dateutil must be monkeypatch.setitem(sys.modules, "dateutil", True) # Override datetime monkeypatch.setattr("libqtile.widget.clock.datetime", TickingDateTime) # Set up references to pytz and dateutil so we know these aren't being used # If they're called, the widget would try to run None(self.timezone) which # would raise an exception clock.pytz = None clock.dateutil = FakeDateutilTZ # set a long update interval as we'll tick manually clk6 = clock.Clock(update_interval=100) config = minimal_conf_noscreen config.screens = [libqtile.config.Screen(top=libqtile.bar.Bar([clk6], 10))] manager_nospawn.start(config) topbar = manager_nospawn.c.bar["top"] manager_nospawn.c.widget["clock"].eval("self.tick()") assert topbar.info()["widgets"][0]["text"] == "10:21"
def test_clock(fake_qtile, monkeypatch): """ test clock output with default settings """ monkeypatch.setattr("libqtile.widget.clock.datetime", MockDatetime) clk1 = clock.Clock() fakebar = Bar([clk1], 24) fakebar.window = FakeWindow() fakebar.width = 10 fakebar.height = 10 fakebar.draw = no_op clk1._configure(fake_qtile, fakebar) text = clk1.poll() assert text == "10:20"
def test_clock_pytz_timezone(fake_qtile, monkeypatch): """ test clock with pytz timezone """ class FakeDateutilTZ: class TZ: @classmethod def gettz(cls, val): None tz = TZ class FakePytz: # pytz timezone is a string so convert it to an int and add 1 # to show that this code is being run @classmethod def timezone(cls, value): return int(value) + 1 # We need pytz in the sys.modules dict monkeypatch.setitem(sys.modules, "pytz", True) # Set up references to pytz and dateutil so we know these aren't being used # If they're called, the widget would try to run None(self.timezone) which # would raise an exception clock.pytz = FakePytz clock.dateutil = FakeDateutilTZ # Pytz timezone must be a string clk4 = clock.Clock(timezone="1") fakebar = Bar([clk4], 24) fakebar.window = FakeWindow() fakebar.width = 10 fakebar.height = 10 fakebar.draw = no_op clk4._configure(fake_qtile, fakebar) text = clk4.poll() # Default time is 10:20 and we add 1 hour for the timezone plus and extra # 1 for the pytz function assert text == "12:20"