-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "using" method in model queryset manager, which will be used to c…
…hange database at runtime while querying. (#28) * Add "using" method in model queryset manager, which will be used to change database at runtime while querying. And this method will work for create, get, filter querymanager's methods * update the doc and add the test case for manager's "using" method * Update the changes to resolve the linter issues. * update the requested changes * Resolve the lint issues.
- Loading branch information
1 parent
d4aa72b
commit 9393bd0
Showing
4 changed files
with
94 additions
and
4 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
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
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,44 @@ | ||
from typing import List, Optional | ||
|
||
import pydantic | ||
import pytest | ||
|
||
import mongoz | ||
from mongoz import Document, ObjectId | ||
from mongoz.exceptions import DocumentNotFound | ||
from tests.conftest import client | ||
|
||
pytestmark = pytest.mark.anyio | ||
pydantic_version = pydantic.__version__[:3] | ||
|
||
|
||
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) | ||
|
||
class Meta: | ||
registry = client | ||
database = "test_db" | ||
|
||
|
||
async def test_model_using() -> None: | ||
await Movie.objects.create(name="Harshali", year=2024) | ||
await Movie.objects.using("test_my_db").create(name="Harshali Zode", year=2024) | ||
|
||
movie = await Movie.objects.get() | ||
assert movie.name == "Harshali" | ||
|
||
movie = await Movie.objects.using("test_my_db").get() | ||
assert movie.name == "Harshali Zode" | ||
|
||
movie = await Movie.objects.using("test_my_db").filter(name="Harshali Zode").get() | ||
assert movie.name == "Harshali Zode" | ||
|
||
movie = await Movie.objects.using("test_my_db").filter(_id=movie.id).get() | ||
assert movie.name == "Harshali Zode" | ||
|
||
with pytest.raises(DocumentNotFound): | ||
await Movie.objects.filter(name="Harshali Zode").get() | ||
await Movie.objects.using("test_my_db").filter(name="Harshali").get() |