Skip to content

Commit

Permalink
#50 Implement exists() method
Browse files Browse the repository at this point in the history
  • Loading branch information
hakancelikdev committed Jul 14, 2023
1 parent 68c831d commit 48477b6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/tutorial/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ You can filter data by using the fields of the model.
users = list(UserModel.objects.filter(username="hakancelik"))
```

### Exists

Exists method is used to check if the data exists in the database, returns True if the data exists, otherwise returns False.
If you pass unique together fields of the model, it will be faster.

```python
is_exists: bool = UserModel.objects.exists(username="hakancelik")
``````

## Model properties

Expand Down
18 changes: 17 additions & 1 deletion src/pydbm/database/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,27 @@ def all(self) -> typing.Iterable[DbmModel]:
for key in self:
yield self.get(id=key)

def filter(self, **kwargs) -> typing.Iterable[DbmModel]:
def filter(self, **kwargs) -> typing.Iterator[DbmModel]:
def check(model: DbmModel) -> bool:
return all(model.fields[key] == value for key, value in kwargs.items())

yield from filter(check, self.all())

def exists(self, **kwargs) -> bool:
if (id := kwargs.pop("id", None)) is None and self.model._config.unique_together == tuple(kwargs.keys()):
auto_field = AutoField(
field_name=PRIMARY_KEY,
field_type=str,
unique_together=self.model._config.unique_together
)
id = auto_field(fields=kwargs).get_default_value()

if id is not None:
with self as db:
data_from_dbm: bytes = db.get(id, None)
return data_from_dbm is not None
else:
return not (next(self.filter(**kwargs), False) is False)

def count(self):
return len(self)
43 changes: 43 additions & 0 deletions tests/models/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,46 @@ class Model(DbmModel):

Model(username="celik").save()
assert Model.objects.count() == 2


def test_base_exists_true():
class Model(DbmModel):
username: str

class Config:
unique_together = ("username",)

Model(username="hakan").save()
assert Model.objects.exists(username="hakan") is True


def test_base_exists_false():
class Model(DbmModel):
username: str

assert Model.objects.exists(username="hakan") is False


def test_base_exists_true_more_fields():
class Model(DbmModel):
username: str
name: str
surname: str

class Config:
unique_together = ("username",)

Model(username="hakan", name="hakan", surname="celik").save()
assert Model.objects.exists(name="hakan", surname="celik") is True


def test_base_exists_false_more_fields():
class Model(DbmModel):
username: str
name: str
surname: str

class Config:
unique_together = ("username",)

assert Model.objects.exists(name="hakan", surname="celik") is False

0 comments on commit 48477b6

Please sign in to comment.