From 882c14a49501b379e465d9d76e04afbf94eb8f2e Mon Sep 17 00:00:00 2001 From: Mikhail Shagov Date: Tue, 12 Sep 2023 13:19:39 +0100 Subject: [PATCH] feat: get_or_none method (#185) --- .gitignore | 2 ++ README.md | 31 +++++++++++++++++-------------- peewee_async.py | 7 +++++++ tests/test_common.py | 20 ++++++++++++++++++++ 4 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 tests/test_common.py diff --git a/.gitignore b/.gitignore index a49443e..a6f90a5 100644 --- a/.gitignore +++ b/.gitignore @@ -61,3 +61,5 @@ target/ # poetry poetry.lock +# pytest +.pytest_cache/ diff --git a/README.md b/README.md index c63fb22..295e3ef 100644 --- a/README.md +++ b/README.md @@ -27,13 +27,13 @@ Install Install with `pip` for PostgreSQL: -``` +```bash pip install --pre peewee-async; pip install aiopg ``` or for MySQL: -``` +```bash pip install --pre peewee-async; pip install aiomysql ``` @@ -57,7 +57,7 @@ database = peewee_async.PostgresqlDatabase( user='user', host='127.0.0.1', port='5432', - password='password' + password='password', ) class TestModel(peewee.Model): @@ -106,22 +106,25 @@ http://peewee-async.readthedocs.io Developing ---------- -Install dependencies using pip: - -```pip install -e .[develop]``` - -Or using [poetry](https://python-poetry.org/docs/): - -```poetry install -E develop``` +Install dependencies using pip: +```bash +pip install -e .[develop] +``` +Or using [poetry](https://python-poetry.org/docs/): +```bash +poetry install -E develop +``` Run databases: - -```docker-compose up -d``` +```bash +docker-compose up -d +``` Run tests: - -```pytest tests -v -s``` +```bash +pytest tests -v -s +``` Discuss ------- diff --git a/peewee_async.py b/peewee_async.py index 3870c0d..f6e2cd9 100644 --- a/peewee_async.py +++ b/peewee_async.py @@ -201,6 +201,13 @@ async def get_or_create(self, model_, defaults=None, **kwargs): data.update({k: v for k, v in kwargs.items() if '__' not in k}) return (await self.create(model_, **data)), True + async def get_or_none(self, model_, *args, **kwargs): + """Try to get an object and return None if it doesn't exist.""" + try: + return (await self.get(model_, *args, **kwargs)) + except model_.DoesNotExist: + pass + async def update(self, obj, only=None): """Update the object in the database. Optionally, update only the specified fields. For creating a new object use :meth:`.create()` diff --git a/tests/test_common.py b/tests/test_common.py new file mode 100644 index 0000000..d9b417b --- /dev/null +++ b/tests/test_common.py @@ -0,0 +1,20 @@ +import uuid + +from tests.conftest import all_dbs +from tests.models import TestModel + + +@all_dbs +async def test_get_or_none(manager): + """Test get_or_none manager function.""" + text1 = "Test %s" % uuid.uuid4() + text2 = "Test %s" % uuid.uuid4() + + obj1 = await manager.create(TestModel, text=text1) + obj2 = await manager.get_or_none(TestModel, text=text1) + obj3 = await manager.get_or_none(TestModel, text=text2) + + assert obj1 == obj2 + assert obj1 is not None + assert obj2 is not None + assert obj3 is None