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

Refactor assert_called_with to compare selected fields #257

Open
wants to merge 1 commit 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
40 changes: 17 additions & 23 deletions aioresponses/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,35 +373,29 @@

def assert_called_with(self, url: 'Union[URL, str, Pattern]',
method: str = hdrs.METH_GET,
args_to_match: Optional[Tuple] = None,
*args: Any,
**kwargs: Any):
"""assert that the last call was made with the specified arguments.
"""Assert that the last call was made with the specified arguments."""

Raises an AssertionError if the args and keyword args passed in are
different to the last call to the mock."""
url = normalize_url(merge_params(url, kwargs.get('params')))
method = method.upper()
key = (method, url)
try:
expected = self.requests[key][-1]
except KeyError:
expected_string = self._format_call_signature(
url, method=method, *args, **kwargs
)
raise AssertionError(
'%s call not found' % expected_string
)
actual = self._build_request_call(method, *args, **kwargs)
if not expected == actual:
expected_string = self._format_call_signature(
expected,
)
actual_string = self._format_call_signature(
actual
)
raise AssertionError(
'%s != %s' % (expected_string, actual_string)
)

if not self.requests.get(key):
raise AssertionError(f'{self._format_call_signature(url, method=method, *args, **kwargs)} call not found')

Check failure on line 386 in aioresponses/core.py

View workflow job for this annotation

GitHub Actions / run flake8

[flake8] reported by reviewdog 🐶 line too long (118 > 79 characters) Raw Output: ./aioresponses/core.py:386:80: E501 line too long (118 > 79 characters)

actual = self.requests[key][-1]
expected = self._build_request_call(method, *args, **kwargs)

if args_to_match is not None:
raise_error = any(
arg not in actual.kwargs or actual.kwargs[arg] != expected.kwargs[arg] for arg in args_to_match)

Check failure on line 393 in aioresponses/core.py

View workflow job for this annotation

GitHub Actions / run flake8

[flake8] reported by reviewdog 🐶 line too long (112 > 79 characters) Raw Output: ./aioresponses/core.py:393:80: E501 line too long (112 > 79 characters)
else:
raise_error = actual != expected

if raise_error:
raise AssertionError(f'{self._format_call_signature(actual)} != {self._format_call_signature(expected)}')

Check failure on line 398 in aioresponses/core.py

View workflow job for this annotation

GitHub Actions / run flake8

[flake8] reported by reviewdog 🐶 line too long (117 > 79 characters) Raw Output: ./aioresponses/core.py:398:80: E501 line too long (117 > 79 characters)

def assert_any_call(self, url: 'Union[URL, str, Pattern]',
method: str = hdrs.METH_GET,
Expand Down
56 changes: 56 additions & 0 deletions tests/test_aioresponses.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,62 @@ def test_post_with_data(self, m: aioresponses):
headers={'User-Agent': 'aiorequest'}
)

@aioresponses()
def test_post_with_data_selected_field_to_compare(self, m: aioresponses):
body = {'foo': 'bar'}
payload = {'spam': 'eggs'}
user_agent = {'User-Agent': 'aioresponses'}
m.post(
self.url,
payload=payload,
headers=dict(connection='keep-alive'),
body=body,
)
response = self.run_async(
self.session.post(
self.url,
data=payload,
headers=user_agent
)
)
self.assertIsInstance(response, ClientResponse)
self.assertEqual(response.status, 200)
response_data = self.run_async(response.json())
self.assertEqual(response_data, payload)
m.assert_called_once_with(
self.url,
method='POST',
args_to_match=['data', 'headers'],
data=payload,
headers={'User-Agent': 'aioresponses'}
)
m.assert_called_once_with(
self.url,
method='POST',
args_to_match=['data'],
data=payload,
)
m.assert_called_once_with(
self.url,
method='POST',
args_to_match=['headers'],
headers={'User-Agent': 'aioresponses'}
)
m.assert_called_once_with(
self.url,
method='POST',
args_to_match=[],
)
# Wrong data
with self.assertRaises(AssertionError):
m.assert_called_once_with(
self.url,
method='POST',
args_to_match=['data', 'headers', 'body'],
data=body,
headers={'User-Agent': 'aioresponses'}
)

@aioresponses()
async def test_streaming(self, m):
m.get(self.url, body='Test')
Expand Down
Loading