-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update docs regarding connections and update related tests (#258)
* update connection docs Changes: - improve connection docs - test harder that the not connected warnings are appropiate. * modernize integration tests * fix py < 3.11 compatibility
- Loading branch information
Showing
11 changed files
with
190 additions
and
66 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,15 @@ | ||
from edgy import Registry, Instance, monkay | ||
|
||
models = Registry(database="sqlite:///db.sqlite", echo=True) | ||
|
||
|
||
async def main(): | ||
# check if settings are loaded | ||
monkay.evaluate_settings_once(ignore_import_errors=False) | ||
# monkey-patch app so you can use edgy shell | ||
monkay.set_instance(Instance(app=app, registry=registry)) | ||
await models.__aenter__() | ||
try: | ||
... | ||
finally: | ||
await models.__aexit__() |
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,17 @@ | ||
from contextlib import asynccontextmanager | ||
from esmerald import Esmerald | ||
|
||
from edgy import Registry, Instance, monkay | ||
|
||
models = Registry(database="sqlite:///db.sqlite", echo=True) | ||
|
||
|
||
app = Esmerald( | ||
routes=[...], | ||
on_startup=[models.__aenter__], | ||
on_shutdown=[models.__aexit__], | ||
) | ||
# check if settings are loaded | ||
monkay.evaluate_settings_once(ignore_import_errors=False) | ||
# monkey-patch app so you can use edgy shell | ||
monkay.set_instance(Instance(app=app, registry=registry)) |
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 |
---|---|---|
@@ -1,35 +1,31 @@ | ||
from collections.abc import AsyncGenerator | ||
import warnings | ||
from collections.abc import AsyncGenerator, Generator | ||
|
||
import pytest | ||
from anyio import from_thread, sleep, to_thread | ||
from esmerald import Esmerald, Gateway, post | ||
from esmerald.testclient import EsmeraldTestClient | ||
from httpx import ASGITransport, AsyncClient | ||
from pydantic import __version__, field_validator | ||
|
||
import edgy | ||
from edgy.exceptions import DatabaseNotConnectedWarning | ||
from edgy.testclient import DatabaseTestClient | ||
from tests.settings import DATABASE_URL | ||
|
||
database = DatabaseTestClient(DATABASE_URL) | ||
models = edgy.Registry(database=edgy.Database(database, force_rollback=True)) | ||
models = edgy.Registry(database=edgy.Database(database, force_rollback=False)) | ||
|
||
pytestmark = pytest.mark.anyio | ||
pydantic_version = ".".join(__version__.split(".")[:2]) | ||
|
||
|
||
@pytest.fixture(autouse=True, scope="module") | ||
async def create_test_database(): | ||
async with database: | ||
await models.create_all() | ||
yield | ||
if not database.drop: | ||
await models.drop_all() | ||
|
||
|
||
@pytest.fixture(autouse=True, scope="function") | ||
async def rollback_transactions(): | ||
async with models.database: | ||
yield | ||
await models.create_all() | ||
yield | ||
if not database.drop: | ||
await models.drop_all() | ||
|
||
|
||
def blocking_function(): | ||
|
@@ -80,8 +76,8 @@ async def create_user(data: User) -> User: | |
def app(): | ||
app = Esmerald( | ||
routes=[Gateway(handler=create_user)], | ||
on_startup=[database.connect], | ||
on_shutdown=[database.disconnect], | ||
on_startup=[models.__aenter__], | ||
on_shutdown=[models.__aexit__], | ||
) | ||
return app | ||
|
||
|
@@ -93,43 +89,76 @@ async def async_client(app) -> AsyncGenerator: | |
yield ac | ||
|
||
|
||
@pytest.fixture() | ||
def esmerald_client(app) -> Generator: | ||
with EsmeraldTestClient(app, base_url="http://test") as ac: | ||
yield ac | ||
|
||
|
||
async def test_creates_a_user_raises_value_error(async_client): | ||
data = { | ||
"name": "Edgy", | ||
"email": "[email protected]", | ||
"language": "EN", | ||
"description": "A description", | ||
} | ||
response = await async_client.post("/create", json=data) | ||
assert response.status_code == 400 # default from Esmerald POST | ||
assert response.json() == { | ||
"detail": "Validation failed for http://test/create with method POST.", | ||
"errors": [ | ||
{ | ||
"type": "missing", | ||
"loc": ["posts"], | ||
"msg": "Field required", | ||
"input": { | ||
"name": "Edgy", | ||
"email": "[email protected]", | ||
"language": "EN", | ||
"description": "A description", | ||
}, | ||
"url": f"https://errors.pydantic.dev/{pydantic_version}/v/missing", | ||
} | ||
], | ||
} | ||
with warnings.catch_warnings(): | ||
warnings.simplefilter("error") | ||
data = { | ||
"name": "Edgy", | ||
"email": "[email protected]", | ||
"language": "EN", | ||
"description": "A description", | ||
} | ||
async with models: | ||
response = await async_client.post("/create", json=data) | ||
assert response.status_code == 400 # default from Esmerald POST | ||
assert response.json() == { | ||
"detail": "Validation failed for http://test/create with method POST.", | ||
"errors": [ | ||
{ | ||
"type": "missing", | ||
"loc": ["posts"], | ||
"msg": "Field required", | ||
"input": { | ||
"name": "Edgy", | ||
"email": "[email protected]", | ||
"language": "EN", | ||
"description": "A description", | ||
}, | ||
"url": f"https://errors.pydantic.dev/{pydantic_version}/v/missing", | ||
} | ||
], | ||
} | ||
|
||
|
||
async def test_creates_a_user(async_client): | ||
async with models: | ||
data = { | ||
"name": "Edgy", | ||
"email": "[email protected]", | ||
"language": "EN", | ||
"description": "A description", | ||
"posts": [{"comment": "A comment"}], | ||
} | ||
response = await async_client.post("/create", json=data) | ||
assert response.status_code == 201 # default from Esmerald POST | ||
reponse_json = response.json() | ||
reponse_json.pop("id") | ||
assert reponse_json == { | ||
"name": "Edgy", | ||
"email": "[email protected]", | ||
"language": "EN", | ||
"description": "A description", | ||
"comment": "A COMMENT", | ||
"total_posts": 1, | ||
} | ||
|
||
|
||
async def test_creates_a_user_warnings(async_client): | ||
data = { | ||
"name": "Edgy", | ||
"email": "[email protected]", | ||
"language": "EN", | ||
"description": "A description", | ||
"posts": [{"comment": "A comment"}], | ||
} | ||
response = await async_client.post("/create", json=data) | ||
with pytest.warns(DatabaseNotConnectedWarning): | ||
response = await async_client.post("/create", json=data) | ||
assert response.status_code == 201 # default from Esmerald POST | ||
reponse_json = response.json() | ||
reponse_json.pop("id") | ||
|
@@ -141,3 +170,27 @@ async def test_creates_a_user(async_client): | |
"comment": "A COMMENT", | ||
"total_posts": 1, | ||
} | ||
|
||
|
||
def test_creates_a_user_sync(esmerald_client): | ||
with warnings.catch_warnings(): | ||
warnings.simplefilter("error") | ||
data = { | ||
"name": "Edgy", | ||
"email": "[email protected]", | ||
"language": "EN", | ||
"description": "A description", | ||
"posts": [{"comment": "A comment"}], | ||
} | ||
response = esmerald_client.post("/create", json=data) | ||
assert response.status_code == 201 # default from Esmerald POST | ||
reponse_json = response.json() | ||
reponse_json.pop("id") | ||
assert reponse_json == { | ||
"name": "Edgy", | ||
"email": "[email protected]", | ||
"language": "EN", | ||
"description": "A description", | ||
"comment": "A COMMENT", | ||
"total_posts": 1, | ||
} |
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
Oops, something went wrong.