-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add app context template * added tests for creating a context template
- Loading branch information
Showing
13 changed files
with
245 additions
and
6 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
Empty file.
57 changes: 57 additions & 0 deletions
57
esmerald/conf/directives/app_template_context/controllers.py-tpl
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,57 @@ | ||
""" | ||
Generated by 'esmerald createapp' using Esmerald {{ esmerald_version }}. | ||
""" | ||
{% if with_basic_controller == True %} | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, TypeVar | ||
|
||
from esmerald import APIView, Inject, delete, get, post, put, status | ||
|
||
from .service import {{ name|capitalize }}Service | ||
from .repository import {{ name|capitalize }}Repository | ||
|
||
if TYPE_CHECKING: | ||
from esmerald.types import Dependencies | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
class {{ name|capitalize }}APIController(APIView): | ||
""" | ||
A {{ name|lower }} controller that defines the basic | ||
operations of the CRUD. | ||
|
||
This serves as an example how to simply initialise the | ||
controller out of the box. | ||
|
||
!!! Note | ||
Make sure you update the return signatures to your | ||
defined ones. | ||
""" | ||
|
||
path: str = "/{{ name|lower }}" | ||
tags: list["str"] = ["{{ name|capitalize }}"] | ||
dependencies: "Dependencies" = { | ||
"repository": Inject({{ name|capitalize }}Repository), | ||
"service": Inject({{ name|capitalize }}Service) | ||
} | ||
|
||
@post("/") | ||
async def create(self, service: "{{ name|capitalize }}Service") -> T: ... | ||
|
||
@get("/{pk:int}") | ||
async def get_by_id(self, pk: int, service: "{{ name|capitalize }}Service") -> T: ... | ||
|
||
@get("/") | ||
async def get_all(self, service: "{{ name|capitalize }}Service") -> list[T]: ... | ||
|
||
@put("/{pk:int}") | ||
async def update(self, pk: int, service: "{{ name|capitalize }}Service") -> T: ... | ||
|
||
@delete("/{pk:int}") | ||
async def delete_by_id(self, pk: int, service: "{{ name|capitalize }}Service") -> T: ... | ||
{% else %} | ||
# Create your controllers here | ||
{% endif %} |
Empty file.
Empty file.
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,6 @@ | ||
""" | ||
Generated by 'esmerald createapp' using Esmerald {{ esmerald_version }}. | ||
""" | ||
from pydantic import BaseModel | ||
|
||
# create your pydantic models here |
20 changes: 20 additions & 0 deletions
20
esmerald/conf/directives/app_template_context/repository.py-tpl
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,20 @@ | ||
from typing import TypeVar | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class {{ name|capitalize }}Repository: | ||
""" | ||
A {{ name|lower }} repository that defines the basic operations of the CRUD. | ||
|
||
This serves as an example how to simply initialise the repository out of the box. | ||
""" | ||
async def create(self) -> T: ... | ||
|
||
async def get_by_id(self, pk: int) -> T: ... | ||
|
||
async def get_all(self) -> list[T]: ... | ||
|
||
async def update(self, pk: int) -> T: ... | ||
|
||
async def delete_by_id(self, pk: int) -> T: ... |
28 changes: 28 additions & 0 deletions
28
esmerald/conf/directives/app_template_context/service.py-tpl
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,28 @@ | ||
from typing import TYPE_CHECKING, TypeVar | ||
|
||
|
||
if TYPE_CHECKING: | ||
from .repository import {{ name|capitalize }}Repository | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class {{ name|capitalize }}Service: | ||
""" | ||
A {{ name|lower }} service that defines the basic operations of the CRUD. | ||
|
||
This serves as an example how to simply initialise the service out of the box. | ||
""" | ||
|
||
def __init__(self, repository: "{{ name|capitalize }}Repository"): | ||
self.repository = repository | ||
|
||
async def create(self) -> T: ... | ||
|
||
async def get_by_id(self, pk: int) -> T: ... | ||
|
||
async def get_all(self) -> list[T]: ... | ||
|
||
async def update(self, pk: int) -> T: ... | ||
|
||
async def delete_by_id(self, pk: int) -> T: ... |
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,6 @@ | ||
""" | ||
Generated by 'esmerald createapp' using Esmerald {{ esmerald_version }}. | ||
""" | ||
from esmerald.testclient import EsmeraldTestClient | ||
|
||
# Create your tests here. |
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 @@ | ||
""" | ||
Generated by 'esmerald createapp' using Esmerald {{ esmerald_version }}. | ||
""" | ||
from esmerald import Include, Gateway | ||
|
||
# Create your routes here. | ||
{% if not with_basic_controller == True %} | ||
route_patterns = [ | ||
] | ||
{% else %} | ||
from .controllers import {{ name|capitalize }}APIController | ||
|
||
|
||
route_patterns = [ | ||
Gateway(handler={{ name|capitalize }}APIController) | ||
] | ||
{% endif %} |
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,88 @@ | ||
import os | ||
import shutil | ||
|
||
import pytest | ||
|
||
from esmerald import Esmerald | ||
from tests.cli.utils import run_cmd | ||
|
||
app = Esmerald(routes=[]) | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def create_folders(): | ||
os.chdir(os.path.split(os.path.abspath(__file__))[0]) | ||
try: | ||
os.remove("app.db") | ||
except OSError: | ||
pass | ||
try: | ||
shutil.rmtree("myproject") | ||
except OSError: | ||
pass | ||
try: | ||
shutil.rmtree("temp_folder") | ||
except OSError: | ||
pass | ||
|
||
yield | ||
|
||
try: | ||
os.remove("app.db") | ||
except OSError: | ||
pass | ||
try: | ||
shutil.rmtree("myproject") | ||
except OSError: | ||
pass | ||
try: | ||
shutil.rmtree("temp_folder") | ||
except OSError: | ||
pass | ||
|
||
|
||
def _run_asserts(): | ||
assert os.path.isfile("myapp/__init__.py") is True | ||
assert os.path.isfile("myapp/tests.py") is True | ||
assert os.path.isfile("myapp/dtos.py") is True | ||
assert os.path.isfile("myapp/urls.py") is True | ||
assert os.path.isfile("myapp/controllers.py") is True | ||
assert os.path.isfile("myapp/repository.py") is True | ||
assert os.path.isfile("myapp/service.py") is True | ||
assert os.path.isfile("myapp/directives/__init__.py") is True | ||
assert os.path.isfile("myapp/directives/operations/__init__.py") is True | ||
|
||
|
||
def test_create_app_with_env_var(create_folders): | ||
(o, e, ss) = run_cmd("tests.cli.main:app", "esmerald createproject myproject") | ||
assert ss == 0 | ||
|
||
os.chdir("myproject/myproject/apps") | ||
|
||
(o, e, ss) = run_cmd("tests.cli.main:app", "esmerald createapp myapp --context") | ||
|
||
_run_asserts() | ||
|
||
|
||
def test_create_app_without_env_var(create_folders): | ||
(o, e, ss) = run_cmd("tests.cli.main:app", "esmerald createproject myproject", is_app=False) | ||
assert ss == 0 | ||
|
||
os.chdir("myproject/myproject/apps") | ||
|
||
(o, e, ss) = run_cmd("tests.cli.main:app", "esmerald createapp myapp --context", is_app=False) | ||
|
||
_run_asserts() | ||
|
||
|
||
def test_create_app_without_env_var_with_app_flag(create_folders): | ||
(o, e, ss) = run_cmd("tests.cli.main:app", "esmerald createproject myproject", is_app=False) | ||
assert ss == 0 | ||
|
||
os.chdir("myproject/myproject/apps") | ||
|
||
(o, e, ss) = run_cmd( | ||
"tests.cli.main:app", "esmerald --app tests.cli.main:app createapp myapp --context", is_app=False | ||
) | ||
|
||
_run_asserts() |