diff --git a/README.rst b/README.rst index f50cbaa..0944880 100644 --- a/README.rst +++ b/README.rst @@ -74,6 +74,9 @@ Client session # If you are not using context manager, you need to close session manually s.close() + # Again, don't forget to await in the AsyncIO mode + await s.close() + # Fetching documents documents = s.get('resource_type') # Or if you want only 1, then diff --git a/src/jsonapi_client/session.py b/src/jsonapi_client/session.py index 63e7ab3..4924b04 100644 --- a/src/jsonapi_client/session.py +++ b/src/jsonapi_client/session.py @@ -278,15 +278,15 @@ async def __aexit__(self, exc_type, exc_val, exc_tb): logger.info('Exiting session') if not exc_type: await self.commit() - self.close() + await self.close() def close(self): """ Close session and invalidate resources. """ - if self.enable_async: - self._aiohttp_session.close() self.invalidate() + if self.enable_async: + return self._aiohttp_session.close() def invalidate(self): """ diff --git a/tests/test_client.py b/tests/test_client.py index 349d435..b96fbbc 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -490,7 +490,7 @@ def test_error_404(mocked_fetch, api_schema): with pytest.raises(DocumentError) as e: s.get('error') - assert 'Error document was fetched' in str(e) + assert 'Error document was fetched' in str(e.value) @pytest.mark.asyncio @@ -510,8 +510,8 @@ async def test_error_404_async(mocked_fetch, api_schema): assert e.value.errors['status_code'] == 404 with pytest.raises(DocumentError) as e: await s.get('error') - assert 'Error document was fetched' in str(e) - s.close() + assert 'Error document was fetched' in str(e.value) + await s.close() def test_relationships_with_context_manager(mocked_fetch, api_schema): @@ -946,7 +946,7 @@ def test_schema_validation(mocked_fetch): with pytest.raises(ValidationError) as e: article = s.get('articles') #article.title.startswith('JSON API paints') - assert 'is not of type \'number\'' in str(e) + assert 'is not of type \'number\'' in str(e.value) def make_patch_json(ids, type_, field_name=None):