Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hashable & equalable #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ async def websocket_endpoint(websocket: WebSocket):


if __name__ == "__main__":
uvicorn.run("main:app", debug=True, reload=True)
uvicorn.run("main:app", reload=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uvicorn.run("main:app", debug=True, reload=True)
TypeError: run() got an unexpected keyword argument 'debug'

8 changes: 8 additions & 0 deletions fastapi_limiter/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ async def __call__(self, request: Request, response: Response):
if pexpire != 0:
return await callback(request, response, pexpire)

def __hash__(self) -> int:
return hash(f"limiter-{(self.times, self.milliseconds)}")

def __eq__(self, other: object) -> bool:
if isinstance(other, RateLimiter):
return (self.times, self.milliseconds) == (other.times, other.milliseconds)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verify equivalence in a simple way.

return False


class WebSocketRateLimiter(RateLimiter):
async def __call__(self, ws: WebSocket, context_key=""):
Expand Down
48 changes: 48 additions & 0 deletions tests/test_depends.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from time import sleep

import pytest
from starlette.testclient import TestClient

from examples.main import app
from fastapi_limiter.depends import RateLimiter


def test_limiter():
Expand Down Expand Up @@ -65,3 +67,49 @@ def test_limiter_websockets():
data = ws.receive_text()
assert data == "Hello, world"
ws.close()


@pytest.mark.parametrize(
("eq_left", "eq_right", "neq_left1", "neq_right1", "neq_left2", "neq_right2"),
[
(
RateLimiter(times=1, milliseconds=5),
RateLimiter(times=1, milliseconds=5),
RateLimiter(times=1, milliseconds=5),
RateLimiter(times=2, milliseconds=5),
RateLimiter(times=1, milliseconds=5),
RateLimiter(times=1, milliseconds=10),
),
(
RateLimiter(times=1, seconds=5),
RateLimiter(times=1, seconds=5),
RateLimiter(times=1, seconds=5),
RateLimiter(times=2, seconds=5),
RateLimiter(times=1, seconds=5),
RateLimiter(times=1, seconds=10),
),
(
RateLimiter(times=1, minutes=5),
RateLimiter(times=1, minutes=5),
RateLimiter(times=1, minutes=5),
RateLimiter(times=2, minutes=5),
RateLimiter(times=1, minutes=5),
RateLimiter(times=1, minutes=10),
),
(
RateLimiter(times=1, hours=5),
RateLimiter(times=1, hours=5),
RateLimiter(times=1, hours=5),
RateLimiter(times=2, hours=5),
RateLimiter(times=1, hours=5),
RateLimiter(times=1, hours=10),
),
],
)
def test_limiter_equality(eq_left, eq_right, neq_left1, neq_right1, neq_left2, neq_right2):
assert hash(eq_left) == hash(eq_right)
assert eq_left == eq_right
assert hash(neq_left1) != hash(neq_right1)
assert neq_left1 != neq_right1
assert hash(neq_left2) != hash(neq_right2)
assert neq_left2 != neq_right2
Loading