-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests and Travis CI integration.
- Loading branch information
1 parent
5d3753b
commit 7c2c36d
Showing
7 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,17 @@ | ||
*.pyx | ||
*.pyc | ||
|
||
*.swp | ||
*.orig | ||
|
||
.idea/ | ||
|
||
# virtualenv | ||
venv/ | ||
env/ | ||
|
||
build/ | ||
dist/ | ||
|
||
*.egg** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
language: python | ||
python: | ||
- "2.7" | ||
- "3.5" | ||
script: "make test" | ||
branches: | ||
only: | ||
- master | ||
services: | ||
- redis-server | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
init: | ||
python setup.py install | ||
test: | ||
python tests/rate_limit_test.py | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[bdist_wheel] | ||
universal=1 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
import unittest | ||
import time | ||
from redis_rate_limit import RateLimit, TooManyRequests | ||
|
||
|
||
class TestRedisRateLimit(unittest.TestCase): | ||
def setUp(self): | ||
""" | ||
Initialises Rate Limit class and delete all keys from Redis. | ||
""" | ||
self.rate_limit = RateLimit(resource='test', client='localhost', | ||
max_requests=10) | ||
self.rate_limit._reset() | ||
|
||
def _make_10_requests(self): | ||
""" | ||
Increments usage ten times. | ||
""" | ||
for x in range(0, 10): | ||
with self.rate_limit: | ||
pass | ||
|
||
def test_limit_10_max_request(self): | ||
""" | ||
Should raise TooManyRequests Exception when trying to increment for the | ||
eleventh time. | ||
""" | ||
self.assertEquals(self.rate_limit.get_usage(), 0) | ||
self.assertEquals(self.rate_limit.has_been_exceeded(), False) | ||
|
||
self._make_10_requests() | ||
self.assertEquals(self.rate_limit.get_usage(), 10) | ||
self.assertEquals(self.rate_limit.has_been_exceeded(), True) | ||
|
||
with self.assertRaises(TooManyRequests): | ||
with self.rate_limit: | ||
pass | ||
|
||
self.assertEquals(self.rate_limit.get_usage(), 11) | ||
self.assertEquals(self.rate_limit.has_been_exceeded(), True) | ||
|
||
def test_expire(self): | ||
""" | ||
Should not raise TooManyRequests Exception when trying to increment for | ||
the eleventh time after the expire time. | ||
""" | ||
self._make_10_requests() | ||
time.sleep(1) | ||
with self.rate_limit: | ||
pass | ||
|
||
def test_not_expired(self): | ||
""" | ||
Should raise TooManyRequests Exception when the expire time has not | ||
been reached yet. | ||
""" | ||
self.rate_limit = RateLimit(resource='test', client='localhost', | ||
max_requests=10, expire=2) | ||
self._make_10_requests() | ||
time.sleep(1) | ||
with self.assertRaises(TooManyRequests): | ||
with self.rate_limit: | ||
pass | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
|