Skip to content

Commit a067fcf

Browse files
committed
added test for piccolo asgi new
1 parent 9324292 commit a067fcf

File tree

5 files changed

+49
-14
lines changed

5 files changed

+49
-14
lines changed

piccolo/apps/asgi/commands/new.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from __future__ import annotations
2-
import colorama
3-
from jinja2 import Environment, FileSystemLoader
42
import os
53
import shutil
64
import typing as t
75

86
import black
7+
import colorama
8+
from jinja2 import Environment, FileSystemLoader
99

1010

1111
TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), "templates/starlette/")
@@ -23,29 +23,38 @@ def get_options_string(options: t.List[str]):
2323
)
2424

2525

26-
def new():
27-
"""
28-
Create a basic ASGI app, including Piccolo, routing, and an admin.
29-
"""
30-
tree = os.walk(TEMPLATE_DIR)
31-
26+
def get_routing_framework() -> str:
3227
print_instruction("Which routing framework?")
3328
router = input(f"{get_options_string(ROUTERS)}\n") or 0
29+
return ROUTERS[int(router)]
3430

31+
32+
def get_server() -> str:
3533
print_instruction("Which server?")
3634
server = input(f"{get_options_string(SERVERS)}\n") or 0
35+
return SERVERS[int(server)]
36+
37+
38+
def new(root: str = ""):
39+
"""
40+
Create a basic ASGI app, including Piccolo, routing, and an admin.
41+
42+
:param root:
43+
Where to create the app e.g. /my/folder. By default it creates the
44+
app in the current directory.
45+
46+
"""
47+
tree = os.walk(TEMPLATE_DIR)
3748

3849
template_context = {
39-
"server": SERVERS[int(server)],
40-
"router": ROUTERS[int(router)],
50+
"router": get_routing_framework(),
51+
"server": get_server(),
4152
}
4253

4354
for directory in tree:
4455
dir_path, sub_dir_names, file_names = directory # type: ignore
4556

46-
output_dir_path = os.path.join(
47-
os.getcwd(), dir_path.split(TEMPLATE_DIR)[-1]
48-
)
57+
output_dir_path = os.path.join(root, dir_path.split(TEMPLATE_DIR)[-1])
4958

5059
if not os.path.exists(output_dir_path):
5160
folder_name = output_dir_path.split("/")[-1]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Add migrations using `piccolo migrations new blog --auto`.
1+
Add migrations using `piccolo migrations new home --auto`.

tests/apps/asgi/__init__.py

Whitespace-only changes.

tests/apps/asgi/commands/__init__.py

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
import shutil
3+
from unittest import TestCase
4+
from unittest.mock import patch
5+
6+
from piccolo.apps.asgi.commands.new import new, SERVERS, ROUTERS
7+
8+
9+
class TestNewApp(TestCase):
10+
@patch(
11+
"piccolo.apps.asgi.commands.new.get_routing_framework",
12+
return_value=ROUTERS[0],
13+
)
14+
@patch(
15+
"piccolo.apps.asgi.commands.new.get_server", return_value=SERVERS[0],
16+
)
17+
def test_new(self, *args, **kwargs):
18+
root = "/tmp/asgi_app"
19+
20+
if os.path.exists(root):
21+
shutil.rmtree(root)
22+
23+
os.mkdir(root)
24+
new(root=root)
25+
26+
self.assertTrue(os.path.exists(os.path.join(root, "app.py")))

0 commit comments

Comments
 (0)