Skip to content
Closed
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ dist
/.cache/
*.swp
/build/
.pytest_cache
.venv
.vscode
.pytest_cache
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Client session
s.close()

# Again, don't forget to await in the AsyncIO mode
await s.close()
await s.close_async()

# Fetching documents
documents = s.get('resource_type')
Expand Down
11 changes: 9 additions & 2 deletions src/jsonapi_client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,22 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
logger.info('Exiting session')
if not exc_type:
await self.commit()
await self.close()
await self.close_async()

def close(self):
"""
Close session and invalidate resources.
"""
self.invalidate()
if self.enable_async:
return self._aiohttp_session.close()
logger.warning('Please, use close_async instead')

async def close_async(self):
"""
Close async session and invalidate resources.
"""
self.invalidate()
return await self._aiohttp_session.close()

def invalidate(self):
"""
Expand Down
22 changes: 13 additions & 9 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ async def test_initialization_async(mocked_fetch, article_schema):
s.resources_by_resource_identifier[('comments', '5')]
assert s.resources_by_link['http://example.com/people/9'] is \
s.resources_by_resource_identifier[('people', '9')]
s.close()
await s.close_async()


def test_basic_attributes(mocked_fetch, article_schema):
Expand Down Expand Up @@ -267,7 +267,7 @@ async def test_basic_attributes_async(mocked_fetch, article_schema):
my_attrs = {i for i in dir(article.fields) if not i.startswith('_')}

assert my_attrs == attr_set
s.close()
await s.close_async()


def test_relationships_single(mocked_fetch, article_schema):
Expand Down Expand Up @@ -353,7 +353,8 @@ async def test_relationships_single_async(mocked_fetch, article_schema):
await article3.comment_or_author.fetch()
assert article3.author.resource is None
assert article3.comment_or_author.resource is None
s.close()
await s.close_async()


def test_relationships_multi(mocked_fetch, article_schema):
s = Session('http://localhost:8080', schema=article_schema)
Expand Down Expand Up @@ -511,7 +512,7 @@ async def test_error_404_async(mocked_fetch, api_schema):
with pytest.raises(DocumentError) as e:
await s.get('error')
assert 'Error document was fetched' in str(e.value)
await s.close()
await s.close_async()


def test_relationships_with_context_manager(mocked_fetch, api_schema):
Expand Down Expand Up @@ -712,7 +713,8 @@ async def test_more_relationships_async_fetch(mocked_fetch, api_schema):
await parent_lease.fetch()
assert parent_lease.resource.active_status == 'active'
# ^ now parent lease is fetched, but attribute access goes through Relationship
s.close()
await s.close_async()


class SuccessfullResponse:
status_code = 200
Expand Down Expand Up @@ -858,7 +860,7 @@ async def test_result_pagination_iteration_async(mocked_fetch, api_schema):
assert len(leases) == 6
for l in range(len(leases)):
assert leases[l].id == str(l+1)
s.close()
await s.close_async()


def test_result_filtering(mocked_fetch, api_schema):
Expand Down Expand Up @@ -1016,7 +1018,7 @@ async def test_posting_successfull_async(mock_req_async, mock_update_resource):

mock_req_async.assert_called_once_with('post', 'http://localhost:80801/api/leases',
agr_data)
s.close()
await s.close_async()

@pytest.mark.parametrize('commit', [0, 1])
@pytest.mark.parametrize('kw_format', [0, 1])
Expand Down Expand Up @@ -1193,7 +1195,8 @@ async def test_posting_successfull_without_schema(mock_req_async, mock_update_re

mock_req_async.assert_called_once_with('post', 'http://localhost:80801/api/leases',
agr_data)
s.close()
await s.close_async()


def test_posting_post_validation_error():
s = Session('http://localhost:80801/api', schema=api_schema_all)
Expand Down Expand Up @@ -1406,7 +1409,8 @@ async def test_relationship_manipulation_async(mock_req_async, mocked_fetch, art
make_patch_json([6, 7, 8, 9, 10, 11],
'comments'))
mock_req_async.reset_mock()
s.close()
await s.close_async()


def test_relationship_manipulation_alternative_api(mock_req, mocked_fetch, article_schema, mock_update_resource):
s = Session('http://localhost:80801/', schema=article_schema)
Expand Down