Ejemplo n.º 1
0
 def test_wait_returns_none_if_there_are_no_available_requests(self):
     throttle = SimpleRateThrottle()
     throttle.num_requests = 1
     throttle.duration = 60
     throttle.now = throttle.timer()
     throttle.history = [throttle.timer() for _ in range(3)]
     assert throttle.wait() is None
Ejemplo n.º 2
0
 def test_wait_returns_correct_waiting_time_without_history(self):
     throttle = SimpleRateThrottle()
     throttle.num_requests = 1
     throttle.duration = 60
     throttle.history = []
     waiting_time = throttle.wait()
     assert isinstance(waiting_time, float)
     assert waiting_time == 30.0
Ejemplo n.º 3
0
    def test_allow_request_returns_true_if_key_is_none(self, loop,
                                                       monkeypatch):
        throttle = SimpleRateThrottle()
        throttle.rate = "some rate"

        async def gck(*args):
            return None

        monkeypatch.setattr(throttle, "get_cache_key", gck)

        assert (loop.run_until_complete(
            throttle.allow_request(request={}, view={})) is True)
Ejemplo n.º 4
0
 def test_get_cache_key_raises_not_implemented_error(self, loop):
     with pytest.raises(NotImplementedError):
         loop.run_until_complete(SimpleRateThrottle().get_cache_key({}, {}))
Ejemplo n.º 5
0
 def test_allow_request_returns_true_if_rate_is_none(self, loop):
     assert (loop.run_until_complete(SimpleRateThrottle().allow_request(
         request={}, view={})) is True)
Ejemplo n.º 6
0
 def test_parse_rate_returns_tuple_with_none_if_rate_not_provided(self):
     rate = SimpleRateThrottle().parse_rate(None)
     assert rate == (None, None)
Ejemplo n.º 7
0
 def test_throttle_raises_error_if_rate_is_missing(self):
     SimpleRateThrottle.scope = "invalid scope"
     with pytest.raises(ImproperlyConfigured):
         SimpleRateThrottle()
Ejemplo n.º 8
0
 def test_get_rate_raises_error_if_scope_is_missing(self):
     throttle = SimpleRateThrottle()
     with pytest.raises(ImproperlyConfigured):
         throttle.scope = None
         throttle.get_rate()