Skip to content

Commit

Permalink
feat: get_or_none method (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
mishaga authored Sep 12, 2023
1 parent 6c9a012 commit 882c14a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ target/
# poetry
poetry.lock

# pytest
.pytest_cache/
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand All @@ -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):
Expand Down Expand Up @@ -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
-------
Expand Down
7 changes: 7 additions & 0 deletions peewee_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
Expand Down
20 changes: 20 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 882c14a

Please sign in to comment.