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

feat: integer repeat #237

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Development Lead
Contributors
------------

None yet. Why not be the first?
* Felix Claessen <[email protected]>
6 changes: 4 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,16 @@ E.g. for cases you want to test retrying mechanisms
def test_multiple_responses(m):
loop = asyncio.get_event_loop()
session = aiohttp.ClientSession()
m.get('http://example.com', status=500, repeat=True)
m.get('http://example.com', status=200) # will not take effect
m.get('http://example.com', status=500, repeat=2)
m.get('http://example.com', status=200) # will take effect after two preceding calls

resp1 = loop.run_until_complete(session.get('http://example.com'))
resp2 = loop.run_until_complete(session.get('http://example.com'))
resp3 = loop.run_until_complete(session.get('http://example.com'))

assert resp1.status == 500
assert resp2.status == 500
assert resp3.status == 200


**match URLs with regular expressions**
Expand Down
8 changes: 6 additions & 2 deletions aioresponses/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self, url: Union[URL, str, Pattern],
content_type: str = 'application/json',
response_class: Optional[Type[ClientResponse]] = None,
timeout: bool = False,
repeat: bool = False,
repeat: Union[bool, int] = False,
reason: Optional[str] = None,
callback: Optional[Callable] = None):
if isinstance(url, Pattern):
Expand Down Expand Up @@ -313,7 +313,7 @@ def add(self, url: 'Union[URL, str, Pattern]', method: str = hdrs.METH_GET,
payload: Optional[Dict] = None,
headers: Optional[Dict] = None,
response_class: Optional[Type[ClientResponse]] = None,
repeat: bool = False,
repeat: Union[bool, int] = False,
timeout: bool = False,
reason: Optional[str] = None,
callback: Optional[Callable] = None) -> None:
Expand Down Expand Up @@ -471,6 +471,10 @@ async def match(

if matcher.repeat is False:
del self._matches[key]
elif matcher.repeat == 2:
Copy link
Owner

Choose a reason for hiding this comment

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

why equal to 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because I meant repeat=1 to have the same effect as repeat=False. That is, I interpret the number of repetitions to include the first call. I guess an alternative would be to define the number of repetitions as the number after the first call.

That is, for repeat=5, either:

  • respond 5 times with foo
  • respond with foo, and also do that the next 5 times (so 6 in total)

The former seems more intuitive to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The repeat=1 case was actually broken. I fixed it and added a test. I also elaborated on this matter in the main README.

matcher.repeat = False
elif isinstance(matcher.repeat, int):
matcher.repeat -= 1

if self.is_exception(response_or_exc):
raise response_or_exc
Expand Down
9 changes: 9 additions & 0 deletions tests/test_aioresponses.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,15 @@ async def test_assert_called_twice(self, m: aioresponses):
with self.assertRaises(AssertionError):
m.assert_called_once()

@aioresponses()
async def test_integer_repeat(self, m: aioresponses):
m.get(self.url, repeat=2)
m.assert_not_called()
await self.session.get(self.url)
await self.session.get(self.url)
with self.assertRaises(ClientConnectionError):
await self.session.get(self.url)

@aioresponses()
async def test_assert_any_call(self, m: aioresponses):
http_bin_url = "http://httpbin.org"
Expand Down