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

failed tests when using aioresponses #7853

Closed
1 task done
milonimrod opened this issue Nov 18, 2023 · 1 comment
Closed
1 task done

failed tests when using aioresponses #7853

milonimrod opened this issue Nov 18, 2023 · 1 comment
Labels

Comments

@milonimrod
Copy link

Describe the bug

When running a pytest that uses aioresponse to mock the calls using verion 3.9.0 I get an error:
object mock can't be used in await expression

when running the same code in version 3.8.5 everything is working

Traceback (most recent call last):
  File "<string>", line 19, in func_wrapper
  File "/Users/nimrodmilo/dev/web_consumer/web_consumer/data_extraction/web_crawling/instant_branding.py", line 142, in get_profile_image
    social_data = await get_social_link_data(
  File "/Users/nimrodmilo/dev/web_consumer/web_consumer/data_extraction/scrapping/social.py", line 40, in get_social_link_data
    return ans
  File "/Users/nimrodmilo/Library/Caches/pypoetry/virtualenvs/web-consumer-HhfrqN_r-py3.9/lib/python3.9/site-packages/aiohttp/client.py", line 1206, in __aexit__
    await self._resp.wait_for_close()
  File "/Users/nimrodmilo/Library/Caches/pypoetry/virtualenvs/web-consumer-HhfrqN_r-py3.9/lib/python3.9/site-packages/aiohttp/client_reqrep.py", line 1096, in wait_for_close
    await self._writer
TypeError: object Mock can't be used in 'await' expression

aioresponses version = 0.7.5

To Reproduce

  1. install aiohttp==3.9.0, and aioresponse==0.7.5
  2. run the following code:
import pytest
from aioresponses import aioresponses
from urllib.parse import quote_plus


@pytest.mark.asyncio
async def test_http_requests():
    # Setup for aioresponses to mock external API calls
    with aioresponses() as mock_aioresponse:
        mock_url = "http://mockapi.com/get_data"
        mock_response_data = {"key": "value"}

        # Mocking a GET request
        mock_aioresponse.get(
            mock_url,
            payload=mock_response_data
        )

        # Using aiohttp.ClientSession to make an actual request
        async with aiohttp.ClientSession() as session:
            async with session.get(mock_url) as response:
                result = await response.json()

                # Assert to check if the response matches the mocked data
                assert result == mock_response_data

Expected behavior

test should run

Logs/tracebacks

test_instant_branding.py::test_http_requests FAILED                      [100%]
test_instant_branding.py:178 (test_http_requests)
@pytest.mark.asyncio
    async def test_http_requests():
        # Setup for aioresponses to mock external API calls
        with aioresponses() as mock_aioresponse:
            mock_url = "http://mockapi.com/get_data"
            mock_response_data = {"key": "value"}
    
            # Mocking a GET request
            mock_aioresponse.get(
                mock_url,
                payload=mock_response_data
            )
    
            # Using aiohttp.ClientSession to make an actual request
            async with aiohttp.ClientSession() as session:
                async with session.get(mock_url) as response:
>                   result = await response.json()

test_instant_branding.py:195: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/Users/nimrodmilo/Library/Caches/pypoetry/virtualenvs/web-consumer-HhfrqN_r-py3.9/lib/python3.9/site-packages/aiohttp/client_reqrep.py:1161: in json
    await self.read()
/Users/nimrodmilo/Library/Caches/pypoetry/virtualenvs/web-consumer-HhfrqN_r-py3.9/lib/python3.9/site-packages/aiohttp/client_reqrep.py:1114: in read
    await self._wait_released()  # Underlying connection released
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <ClientResponse(http://mockapi.com/get_data) [200 OK]>
<CIMultiDict('Content-Type': 'application/json')>


    async def _wait_released(self) -> None:
        if self._writer is not None:
>           await self._writer
E           TypeError: object Mock can't be used in 'await' expression

/Users/nimrodmilo/Library/Caches/pypoetry/virtualenvs/web-consumer-HhfrqN_r-py3.9/lib/python3.9/site-packages/aiohttp/client_reqrep.py:1080: TypeError

During handling of the above exception, another exception occurred:

    @pytest.mark.asyncio
    async def test_http_requests():
        # Setup for aioresponses to mock external API calls
        with aioresponses() as mock_aioresponse:
            mock_url = "http://mockapi.com/get_data"
            mock_response_data = {"key": "value"}
    
            # Mocking a GET request
            mock_aioresponse.get(
                mock_url,
                payload=mock_response_data
            )
    
            # Using aiohttp.ClientSession to make an actual request
            async with aiohttp.ClientSession() as session:
                async with session.get(mock_url) as response:
                    result = await response.json()
    
                    # Assert to check if the response matches the mocked data
>                   assert result == mock_response_data

test_instant_branding.py:198: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/Users/nimrodmilo/Library/Caches/pypoetry/virtualenvs/web-consumer-HhfrqN_r-py3.9/lib/python3.9/site-packages/aiohttp/client.py:1206: in __aexit__
    await self._resp.wait_for_close()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <ClientResponse(http://mockapi.com/get_data) [200 OK]>
<CIMultiDict('Content-Type': 'application/json')>


    async def wait_for_close(self) -> None:
        if self._writer is not None:
>           await self._writer
E           TypeError: object Mock can't be used in 'await' expression

/Users/nimrodmilo/Library/Caches/pypoetry/virtualenvs/web-consumer-HhfrqN_r-py3.9/lib/python3.9/site-packages/aiohttp/client_reqrep.py:1096: TypeError

Python Version

3.9.16

aiohttp Version

3.9.0

multidict Version

Name: multidict
Version: 6.0.4
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache 2
Location: /Users/nimrodmilo/Library/Caches/pypoetry/virtualenvs/web-consumer-HhfrqN_r-py3.9/lib/python3.9/site-packages
Requires:
Required-by: aiohttp, yarl

yarl Version

Name: yarl
Version: 1.9.2
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl/
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache-2.0
Location: /Users/nimrodmilo/Library/Caches/pypoetry/virtualenvs/web-consumer-HhfrqN_r-py3.9/lib/python3.9/site-packages
Requires: idna, multidict
Required-by: aiohttp

OS

mac OS

Related component

Client

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@milonimrod milonimrod added the bug label Nov 18, 2023
@Dreamsorcerer
Copy link
Member

pnuckowski/aioresponses#248

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants