Skip to content

Commit

Permalink
address ruff errors
Browse files Browse the repository at this point in the history
  • Loading branch information
adbar committed Jan 10, 2025
1 parent 1d2c0fe commit 0af98c3
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions test_bot/test_timer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""Test functions dedicated to time measurement and conversion."""

from datetime import datetime, timedelta

from lib import timer


def test_time_conversion():
def test_time_conversion() -> None:
"""Test conversion of time units."""
assert timer.msec(1000) == timedelta(milliseconds=1000)
assert timer.to_msec(timedelta(milliseconds=1000)) == 1000.0

Expand All @@ -26,70 +29,67 @@ def test_time_conversion():
assert timer.to_seconds(timer.years(1)) == 31536000.0


def test_init_default():
def test_init() -> None:
"""Test Timer class init."""
t = timer.Timer()
assert t.duration == timedelta(0)
assert t.starting_time is not None

def test_init_custom_duration():
duration = timedelta(seconds=10)
t = timer.Timer(duration)
assert t.duration == duration
assert t.starting_time is not None

def test_init_backdated_timestamp():
backdated_timestamp = datetime.now() - timedelta(seconds=10)
t = timer.Timer(backdated_timestamp=backdated_timestamp)
assert t.starting_time is not None
assert t.time_since_reset() >= timedelta(seconds=10)

def test_is_expired_not_expired():
def test_is_expired() -> None:
"""Test timer expiration."""
t = timer.Timer(timedelta(seconds=10))
assert not t.is_expired()

def test_is_expired_expired():
t = timer.Timer(timedelta(seconds=0))
assert t.is_expired()

def test_is_expired_expired_after_reset():
t = timer.Timer(timedelta(seconds=10))
t.reset()
t.starting_time -= 10
assert t.is_expired()

def test_reset():
def test_reset() -> None:
"""Test timer reset."""
t = timer.Timer(timedelta(seconds=10))
t.reset()
assert t.starting_time is not None
assert timer.sec_str(t.time_since_reset()) == timer.sec_str(timedelta(0))

def test_time_since_reset():
def test_time() -> None:
"""Test time measurement, expiration, and time until expiration."""
t = timer.Timer(timedelta(seconds=10))
t.starting_time -= 5
assert timer.sec_str(t.time_since_reset()) == timer.sec_str(timedelta(seconds=5))

def test_time_until_expiration():
t = timer.Timer(timedelta(seconds=10))
t.starting_time -= 5
assert timer.sec_str(t.time_until_expiration()) == timer.sec_str(timedelta(seconds=5))

def test_time_until_expiration_expired():
t = timer.Timer(timedelta(seconds=10))
t.starting_time -= 15 # Simulate time passing
assert t.time_until_expiration() == timedelta(0)

def test_starting_timestamp():
t = timer.Timer(timedelta(seconds=10))
timestamp_format = "%Y-%m-%d %H:%M:%S"
expected_timestamp = (datetime.now() - t.time_since_reset()).strftime(timestamp_format)
assert t.starting_timestamp(timestamp_format) == expected_timestamp

def test_time_until_expiration_max_zero():
t = timer.Timer(timedelta(seconds=10))
t.starting_time -= 15
assert t.time_until_expiration() == timedelta(0)

def test_time_until_expiration_max_positive():
t = timer.Timer(timedelta(seconds=10))
t.starting_time -= 5
assert timer.sec_str(t.time_until_expiration()) == timer.sec_str(timedelta(seconds=5))

def test_starting_timestamp() -> None:
"""Test timestamp conversion and integration."""
t = timer.Timer(timedelta(seconds=10))
timestamp_format = "%Y-%m-%d %H:%M:%S"
expected_timestamp = (datetime.now() - t.time_since_reset()).strftime(timestamp_format)
assert t.starting_timestamp(timestamp_format) == expected_timestamp

0 comments on commit 0af98c3

Please sign in to comment.