Skip to content

Commit

Permalink
Implement DoesNotExist exception under Model #23
Browse files Browse the repository at this point in the history
  • Loading branch information
hakancelikdev committed Dec 22, 2022
1 parent 022784a commit 22e51eb
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ All notable changes to this project will be documented in this file.
- model.objects.filter
- model.save
- model.update
- Implement DoesNotExist exception under Model [#23](https://github.com/hakancelikdev/pydbm/issues/23)

### Fixed
- Fix find object annotations from the model.
Expand Down
6 changes: 2 additions & 4 deletions docs/tutorial/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,13 @@ That's it, now we have saved our user to the database.
Get method is used to get the data from the database and return it as a model instance.

```python
from pydbm import DoesNotExist

try:
user = UserModel.objects.get(user.id)
except DoesNotExist:
except UserModel.DoesNotExist:
print("User does not exist")
```

If the user exists in the database, it will return the user, otherwise it raises `DoesNotExists` exception.
If the user exists in the database, it will return the user, otherwise it raises `UserModel.DoesNotExists` exception.


### Update
Expand Down
3 changes: 1 addition & 2 deletions src/pydbm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pydbm.exceptions import DoesNotExists, PydbmBaseException, PydbmTypeError, ValidationError
from pydbm.exceptions import PydbmBaseException, PydbmTypeError, ValidationError
from pydbm.models import (
BaseModel,
Field,
Expand All @@ -17,7 +17,6 @@
from pydbm.typing_extra import NormalizationT, ValidatorT

__all__ = (
"DoesNotExists",
"PydbmBaseException",
"PydbmTypeError",
"ValidationError",
Expand Down
3 changes: 1 addition & 2 deletions src/pydbm/database/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from pathlib import Path

from pydbm.database.data_types import BaseDataType
from pydbm.exceptions import DoesNotExists
from pydbm.inspect_extra import get_obj_annotations

if typing.TYPE_CHECKING:
Expand Down Expand Up @@ -152,7 +151,7 @@ def get(self, *, pk: str) -> BaseModel:

return self.model(**fields)

raise DoesNotExists(f"{self.model.__name__} with pk {pk} does not exists")
raise self.model.DoesNotExists(f"{self.model.__name__} with pk {pk} does not exists")

def update(self, *, pk: str, **updated_fields) -> None:
model = self.get(pk=pk)
Expand Down
7 changes: 0 additions & 7 deletions src/pydbm/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

__all__ = (
"PydbmBaseException",
"DoesNotExists",
"PydbmTypeError",
"ValidationError",
)
Expand All @@ -14,12 +13,6 @@ class PydbmBaseException(Exception):
pass


class DoesNotExists(PydbmBaseException):
"""Exception for not found id in the models."""

pass


class PydbmTypeError(PydbmBaseException, TypeError):
"""Exception for not valid type of value."""

Expand Down
2 changes: 2 additions & 0 deletions src/pydbm/models/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pydbm import typing_extra
from pydbm.database import DatabaseManager
from pydbm.exceptions import PydbmBaseException
from pydbm.inspect_extra import get_obj_annotations
from pydbm.models.fields import AutoField, Field, Undefined

Expand Down Expand Up @@ -48,6 +49,7 @@ def __init__(cls, cls_name: str, bases: tuple[Meta, ...], namespace: dict[str, t

cls.required_fields, cls.not_required_fields = mcs.split_fields(list(fields.values()))
cls.objects = DatabaseManager(model=cls, table_name=config.table_name) # type: ignore
cls.DoesNotExists = type("DoesNotExists", (PydbmBaseException,), {"__doc__": "Exception for not found id in the models."}) # noqa: E501

for key, value in fields.items():
setattr(cls, key, value)
Expand Down
4 changes: 2 additions & 2 deletions tests/models/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from pydbm import BaseModel, DoesNotExists
from pydbm import BaseModel


class Model(BaseModel):
Expand Down Expand Up @@ -168,7 +168,7 @@ class Example(BaseModel):
model = Example.objects.create(str="str")

model.delete()
with pytest.raises(DoesNotExists) as cm:
with pytest.raises(model.DoesNotExists) as cm:
Example.objects.get(pk=model.id)
assert str(cm.value) == "Example with pk 341be97d9aff90c9978347f66f945b77 does not exists"

Expand Down

0 comments on commit 22e51eb

Please sign in to comment.