Пример #1
0
 def test_bool(self):
     now = datetime.datetime.now(datetime.timezone.utc)
     year_from_now = now + datetime.timedelta(365)
     year_ago = now - datetime.timedelta(365)
     # Requests left.
     rate = sansio.RateLimit(
         remaining=1, limit=1, reset_epoch=year_from_now.timestamp()
     )
     assert rate
     # Reset passed.
     rate = sansio.RateLimit(remaining=0, limit=1, reset_epoch=year_ago.timestamp())
     assert rate
     # No requests and reset not passed.
     rate = sansio.RateLimit(
         remaining=0, limit=1, reset_epoch=year_from_now.timestamp()
     )
     assert not rate
Пример #2
0
 def test_init(self):
     left = 42
     rate = 64
     reset = datetime.datetime.now(datetime.timezone.utc)
     rate_limit = sansio.RateLimit(remaining=left,
                                   limit=rate,
                                   reset_epoch=reset.timestamp())
     assert rate_limit.remaining == left
     assert rate_limit.limit == rate
     assert rate_limit.reset_datetime == reset
Пример #3
0
 def test___str__(self):
     left = 4200
     rate = 65000
     reset = datetime.datetime.now(datetime.timezone.utc)
     message = str(
         sansio.RateLimit(limit=rate, remaining=left, reset_epoch=reset.timestamp())
     )
     assert format(left, ",") in message
     assert format(rate, ",") in message
     assert str(reset) in message
Пример #4
0
def test_RateLimitExceeded():
    rate = sansio.RateLimit(limit=1, remaining=0, reset_epoch=1)
    exc = RateLimitExceeded(rate)
    assert exc.status_code == http.HTTPStatus.FORBIDDEN
    exc = RateLimitExceeded(rate, "stuff happened")
    assert str(exc) == "stuff happened"