Skip to content

Commit

Permalink
fix generated tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tsung-Ju Lii <[email protected]>
  • Loading branch information
Tsung-Ju Lii committed Aug 24, 2023
1 parent 5da8c72 commit aeb2d17
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ $ tree "{{cookiecutter.project_name}}"
├── conftest.py # Fixtures for all tests.
{%- if cookiecutter.db_info.name != "none" %}
├── db # module contains db configurations
{%- if cookiecutter.db_info.name != "mongodb" %}
│   ├── dao # Data Access Objects. Contains different classes to interact with database.
{%- endif %}
│   └── models # Package contains different models for ORMs.
{%- endif %}
├── __main__.py # Startup script. Starts uvicorn.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
from piccolo.conf.apps import Finder
from piccolo.table import create_tables, drop_tables

{%- elif cookiecutter.orm == "beanie" %}
import beanie
from motor.motor_asyncio import AsyncIOMotorClient

{%- endif %}


Expand Down Expand Up @@ -332,6 +336,23 @@ async def setup_db() -> AsyncGenerator[None, None]:
await drop_database(engine)
{%- endif %}

{%- elif cookiecutter.orm == "beanie" %}
@pytest.fixture(autouse=True)
async def setup_db() -> AsyncGenerator[None, None]:
"""
Fixture to create database connection.
:yield: nothing.
"""
client = AsyncIOMotorClient(settings.db_url.human_repr())
from {{cookiecutter.project_name}}.db.models import load_all_models # noqa: WPS433
await beanie.init_beanie(
database=client[settings.db_base],
document_models=load_all_models(),
)
yield


{%- endif %}

{%- if cookiecutter.enable_rmq == 'True' %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
"""{{cookiecutter.project_name}} models."""
"""{{cookiecutter.project_name}} models."""

from {{cookiecutter.project_name}}.db.models.dummy_model import DummyModel


def load_all_models(): # type: ignore
"""Load all models from this folder.""" # noqa: DAR201
return [
DummyModel, # type: ignore
]
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ async def get_dummy_models(
:param limit: limit of dummy objects, defaults to 10.
:param offset: offset of dummy objects, defaults to 0.
{%- if cookiecutter.db_info.name != "mongodb" %}
:param dummy_dao: DAO for dummy models.
{%- endif %}
:return: list of dummy objects from database.
"""
return await dummy_dao.get_all_dummies(limit=limit, offset=offset)
Expand All @@ -38,8 +36,6 @@ async def create_dummy_model(
Creates dummy model in the database.
:param new_dummy_object: new dummy model item.
{%- if cookiecutter.db_info.name != "mongodb" %}
:param dummy_dao: DAO for dummy models.
{%- endif %}
"""
await dummy_dao.create_dummy_model(name=new_dummy_object.name)
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,13 @@ def _setup_db(app: FastAPI) -> None: # pragma: no cover
{%- if cookiecutter.orm == "beanie" %}
import beanie
from motor.motor_asyncio import AsyncIOMotorClient
from {{cookiecutter.project_name}}.db.models.dummy_model import DummyModel
from {{cookiecutter.project_name}}.db.models import load_all_models
async def _setup_db(app: FastAPI) -> None:
client = AsyncIOMotorClient(settings.db_url.human_repr())
app.state.db_client = client
await beanie.init_beanie(
database=client[settings.db_base],
document_models=[
DummyModel, # type: ignore
]
document_models=load_all_models(),
)
{%- endif %}

Expand Down

0 comments on commit aeb2d17

Please sign in to comment.