Skip to content

Commit

Permalink
feat(client): add is_registered() method (#801)
Browse files Browse the repository at this point in the history
## Change Summary

Asked for here:
https://discord.com/channels/933860922039099444/933860923117043718/1135458731006632027

## Checklist

- [ ] Unit tests for the changes exist
- [ ] Tests pass without significant drop in coverage
- [ ] Documentation reflects changes where applicable
- [ ] Test snapshots have been
[updated](https://prisma-client-py.readthedocs.io/en/latest/contributing/contributing/#snapshot-tests)
if applicable

## Agreement

By submitting this pull request, I confirm that you can use, modify,
copy and redistribute this contribution, under the terms of your choice.
  • Loading branch information
RobertCraigie authored Jul 31, 2023
1 parent e1bfe86 commit 0bc9f95
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/prisma/generator/templates/client.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ class Prisma:
self.disconnect()
{% endif %}

def is_registered(self) -> bool:
"""Returns True if this client instance is registered"""
try:
return get_client() is self
except errors.ClientNotRegisteredError:
return False

def is_connected(self) -> bool:
"""Returns True if the client is connected to the query engine, False otherwise."""
return self.__engine is not None
Expand Down
14 changes: 14 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,17 @@ async def test_copied_client_does_not_close_engine(client: Prisma) -> None:

assert client.is_connected()
await client.user.count() # ensure queries can still be executed


def test_is_registered(client: Prisma) -> None:
"""The Prisma.is_registered() method can be used both when the client is registered
and when there is no client registered at all.
"""
assert client.is_registered()

other_client = Prisma()
assert not other_client.is_registered()

with reset_client():
assert not client.is_registered()
assert not other_client.is_registered()
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ class Prisma:
if self.is_connected():
await self.disconnect()

def is_registered(self) -> bool:
"""Returns True if this client instance is registered"""
try:
return get_client() is self
except errors.ClientNotRegisteredError:
return False

def is_connected(self) -> bool:
"""Returns True if the client is connected to the query engine, False otherwise."""
return self.__engine is not None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ class Prisma:
if self.is_connected():
self.disconnect()

def is_registered(self) -> bool:
"""Returns True if this client instance is registered"""
try:
return get_client() is self
except errors.ClientNotRegisteredError:
return False

def is_connected(self) -> bool:
"""Returns True if the client is connected to the query engine, False otherwise."""
return self.__engine is not None
Expand Down

0 comments on commit 0bc9f95

Please sign in to comment.