-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add extra test * Add exists
- Loading branch information
Showing
6 changed files
with
224 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -772,7 +772,6 @@ The `values_list()` can also be combined with `filter`, `only` as per usual. | |
* **exclude_none** - Boolean flag indicating if the fields with `None` should be excluded. | ||
* **flat** - Boolean flag indicating the results should be flattened. | ||
|
||
|
||
### Only | ||
|
||
Returns the results containing **only** the fields in the query and nothing else. | ||
|
@@ -893,6 +892,17 @@ Get a document by the `_id`. This functionality accepts the parameter `id` as st | |
user = await User.query().get_document_by_id(user.id) | ||
``` | ||
|
||
### Exists | ||
|
||
The `exists()` is used when you want to check if a record exists in the DB or not. | ||
|
||
=== "Manager" | ||
|
||
```python | ||
await User.objects.exists(email="[email protected]", is_active=False) | ||
await User.objects.filter(email="[email protected]", is_active=False).exists() | ||
``` | ||
|
||
## Useful methods | ||
|
||
### Get or create | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -872,6 +872,17 @@ Obter um documento pelo `_id`. Esta funcionalidade aceita o parâmetro `id` como | |
user = await User.query().get_document_by_id(user.id) | ||
``` | ||
|
||
### Exists | ||
|
||
O `exists()` é utilizado quando deseja verificar se um registro existe na base de dados ou não. | ||
|
||
=== "Manager" | ||
|
||
```python | ||
await User.objects.exists(email="[email protected]", is_active=False) | ||
await User.objects.filter(email="[email protected]", is_active=False).exists() | ||
``` | ||
|
||
## Métodos úteis | ||
|
||
### Get or create | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from decimal import Decimal | ||
from typing import AsyncGenerator, List | ||
|
||
import pydantic | ||
import pytest | ||
|
||
import mongoz | ||
from tests.conftest import client | ||
|
||
pytestmark = pytest.mark.anyio | ||
pydantic_version = pydantic.__version__[:3] | ||
|
||
|
||
class Badges(mongoz.EmbeddedDocument): | ||
score: Decimal = mongoz.Decimal(max_digits=5, decimal_places=2, null=True) | ||
name: str = mongoz.String() | ||
|
||
|
||
class Achievement(mongoz.Document): | ||
name: str = mongoz.String() | ||
total_score: Decimal = mongoz.Decimal(max_digits=5, decimal_places=2, null=True) # noqa | ||
badges: List[Badges] = mongoz.Array(Badges, null=True) | ||
|
||
class Meta: | ||
registry = client | ||
database = "test_db" | ||
|
||
|
||
@pytest.fixture(scope="function", autouse=True) | ||
async def prepare_database() -> AsyncGenerator: | ||
await Achievement.objects.delete() | ||
yield | ||
await Achievement.objects.delete() | ||
|
||
|
||
async def test_decimal_on_update() -> None: | ||
badges = [{"name": "badge1", "score": "32083.33"}] | ||
await Achievement.objects.create(name="Batman", total_score="22.246") | ||
|
||
arch = await Achievement.objects.last() | ||
|
||
arch.total_score = Decimal("28") | ||
await arch.save() | ||
|
||
arch = await Achievement.objects.last() | ||
|
||
await arch.update(total_score=Decimal("30")) | ||
|
||
arch = await Achievement.objects.last() | ||
|
||
await Achievement.objects.filter().update(total_score=Decimal("40"), badges=badges) | ||
|
||
arch = await Achievement.objects.last() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from typing import AsyncGenerator, List, Optional | ||
|
||
import pydantic | ||
import pytest | ||
|
||
import mongoz | ||
from mongoz import Document, Index, IndexType, ObjectId, Order | ||
from tests.conftest import client | ||
|
||
pytestmark = pytest.mark.anyio | ||
pydantic_version = pydantic.__version__[:3] | ||
|
||
indexes = [ | ||
Index("name", unique=True), | ||
Index(keys=[("year", Order.DESCENDING), ("genre", IndexType.HASHED)]), | ||
] | ||
|
||
|
||
class Movie(Document): | ||
name: str = mongoz.String() | ||
year: int = mongoz.Integer() | ||
tags: Optional[List[str]] = mongoz.Array(str, null=True) | ||
uuid: Optional[ObjectId] = mongoz.ObjectId(null=True) | ||
is_published: bool = mongoz.Boolean(default=False) | ||
|
||
class Meta: | ||
registry = client | ||
database = "test_db" | ||
indexes = indexes | ||
|
||
|
||
@pytest.fixture(scope="function", autouse=True) | ||
async def prepare_database() -> AsyncGenerator: | ||
await Movie.drop_indexes(force=True) | ||
await Movie.objects.delete() | ||
await Movie.create_indexes() | ||
yield | ||
await Movie.drop_indexes(force=True) | ||
await Movie.objects.delete() | ||
await Movie.create_indexes() | ||
|
||
|
||
async def test_exists_true() -> None: | ||
movies = await Movie.objects.all() | ||
assert len(movies) == 0 | ||
|
||
await Movie.objects.create(name="Forrest Gump", year=2003, is_published=True) | ||
|
||
assert await Movie.objects.exists(name="Forrest Gump") is True | ||
assert await Movie.objects.exists(name="Forrest Gump", year=2003) is True | ||
assert await Movie.objects.exists(name="Forrest Gump", year=2004) is False | ||
|
||
assert await Movie.objects.filter(name="Forrest Gump").exists() is True | ||
assert await Movie.objects.filter(name="Forrest Gump", year=2003).exists() is True | ||
assert await Movie.objects.filter(name="Forrest Gump", year=2004).exists() is False |