def test_get_expiration_with_no_ttl(): cache = Cache() assert not cache._get_expiration_from_ttl(ttl=None) cache = Cache(ttl=60) planned = datetime.now() expiration = cache._get_expiration_from_ttl(ttl=None) assert isinstance(expiration, datetime) assert expiration.second < planned.second + 1 assert expiration.second > planned.second - 1
def test_get_expiration_with_datetime_ttl(): cache = Cache() planned = datetime.now() + timedelta(seconds=10) expiration = cache._get_expiration_from_ttl(ttl=planned) assert isinstance(expiration, datetime) assert expiration.second < planned.second + 1 assert expiration.second > planned.second - 1 cache = Cache(ttl=60) planned = datetime.now() + timedelta(seconds=10) expiration = cache._get_expiration_from_ttl(ttl=planned) assert isinstance(expiration, datetime) assert expiration.second < planned.second + 1 assert expiration.second > planned.second - 1
def test_get_expiration_with_ttl_typeerror(): cache = Cache() with pytest.raises(TypeError): assert cache._get_expiration_from_ttl(ttl="test") with pytest.raises(TypeError): assert cache._get_expiration_from_ttl(ttl=True)