-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
254 additions
and
45 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,11 @@ | ||
from typing import Optional | ||
|
||
from pydastic import ESAsyncModel | ||
|
||
|
||
class Car(ESAsyncModel): | ||
model: str | ||
year: Optional[int] | ||
|
||
class Meta: | ||
index = "car" |
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,43 @@ | ||
import pytest | ||
from car import Car | ||
from elasticsearch import AsyncElasticsearch | ||
|
||
from pydastic import ESAsyncModel | ||
|
||
pytestmark = pytest.mark.asyncio | ||
|
||
|
||
def test_model_definition_yields_error_without_meta_class(): | ||
with pytest.raises(NotImplementedError): | ||
|
||
class AsyncUser(ESAsyncModel): | ||
pass | ||
|
||
|
||
def test_model_definition_yields_error_without_index(): | ||
with pytest.raises(NotImplementedError): | ||
|
||
class AsyncUser(ESAsyncModel): | ||
class Meta: | ||
pass | ||
|
||
|
||
@pytest.mark.asyncio | ||
def test_model_async_save_without_connection_raises_attribute_error(): | ||
with pytest.raises(AttributeError): | ||
user = Car(model="Fiat") | ||
user.save(wait_for=True) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_model_async_save(async_es: AsyncElasticsearch): | ||
user = Car(model="Fiat") | ||
await user.save(wait_for=True) | ||
assert user.id is not None | ||
|
||
res = await async_es.get(index=user.Meta.index, id=user.id) | ||
assert res["found"] | ||
|
||
# Check that fields match exactly | ||
model = user.to_es() | ||
assert res["_source"] == model |