Skip to content

Commit d110366

Browse files
committed
PA-655 Added table as the output for Website.get for particular domain, by: Giles, Piotr
1 parent 52582db commit d110366

File tree

2 files changed

+62
-17
lines changed

2 files changed

+62
-17
lines changed

cli/website.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
#!/usr/bin/python3
2-
import typer
2+
33
from pprint import pformat
44
from typing_extensions import Annotated
5-
from pythonanywhere_core.website import Website
5+
6+
import typer
67
from snakesay import snakesay
8+
from tabulate import tabulate
9+
10+
from pythonanywhere_core.website import Website
11+
712

813
app = typer.Typer(no_args_is_help=True)
914

15+
1016
@app.command()
1117
def create(
1218
domain_name: Annotated[
@@ -46,17 +52,31 @@ def get(
4652
)
4753
):
4854
"""If no domain name is specified, list all domains. Otherwise get details for specified domain"""
49-
# TODO: implement get if domain_name is provided
50-
51-
websites = Website().list()
52-
typer.echo(
53-
snakesay(
54-
f"You have {len(websites)} website(s). "
55+
website = Website()
56+
if domain_name is not None:
57+
website_info = website.get(domain_name=domain_name)
58+
table = tabulate(
59+
[
60+
["domain name", website_info["domain_name"]],
61+
["enabled", website_info["enabled"]],
62+
["command", website_info["webapp"]["command"]],
63+
["access log", website_info["logfiles"]["access"]],
64+
["error log", website_info["logfiles"]["error"]],
65+
["server log", website_info["logfiles"]["server"]],
66+
],
67+
tablefmt="simple",
68+
)
69+
typer.echo(table)
70+
else:
71+
websites = website.list()
72+
typer.echo(
73+
snakesay(
74+
f"You have {len(websites)} website(s). "
75+
)
76+
)
77+
typer.echo(
78+
pformat(websites)
5579
)
56-
)
57-
typer.echo(
58-
pformat(websites)
59-
)
6080

6181

6282
@app.command()

tests/test_cli_website.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import getpass
2+
from unittest.mock import call
3+
24
import pytest
35
from typer.testing import CliRunner
46

57
from cli.website import app
68

9+
710
runner = CliRunner()
811

12+
913
@pytest.fixture
1014
def domain_name():
1115
return "foo.bar.com"
@@ -22,6 +26,11 @@ def website_info(domain_name, command):
2226
"domain_name": domain_name,
2327
"enabled": True,
2428
"id": 42,
29+
"logfiles": {
30+
"access": f"/var/log/{domain_name}.access.log",
31+
"error": f"/var/log/{domain_name}.error.log",
32+
"server": f"/var/log/{domain_name}.server.log",
33+
},
2534
"user": getpass.getuser(),
2635
"webapp": {
2736
"command": command,
@@ -107,18 +116,35 @@ def test_get_with_no_domain_lists_websites(mocker, website_info):
107116
assert "foo.bar.com" in result.stdout
108117

109118

110-
def test_get_with_domain_gives_details_for_domain():
119+
def test_get_with_domain_gives_details_for_domain(mocker, website_info, domain_name):
120+
mock_website = mocker.patch("cli.website.Website")
121+
mock_website.return_value.get.return_value = website_info
122+
mock_tabulate = mocker.patch("cli.website.tabulate")
123+
mock_echo = mocker.patch("cli.website.typer.echo")
124+
111125
result = runner.invoke(
112126
app,
113127
[
114128
"get",
115129
"-d",
116-
"www.domain.com",
130+
domain_name
117131
],
118132
)
119-
print(result.stdout)
133+
120134
assert result.exit_code == 0
121-
assert False, "TODO"
135+
mock_website.return_value.get.assert_called_once_with(domain_name=domain_name)
136+
assert mock_tabulate.call_args == call(
137+
[
138+
["domain name", website_info["domain_name"]],
139+
["enabled", website_info["enabled"]],
140+
["command", website_info["webapp"]["command"]],
141+
["access log", website_info["logfiles"]["access"]],
142+
["error log", website_info["logfiles"]["error"]],
143+
["server log", website_info["logfiles"]["server"]],
144+
],
145+
tablefmt="simple",
146+
)
147+
mock_echo.assert_called_once_with(mock_tabulate.return_value)
122148

123149

124150
def test_reload_with_no_domain_barfs():
@@ -167,4 +193,3 @@ def test_delete_with_domain_deletes_it():
167193
)
168194
assert result.exit_code == 0
169195
assert False, "TODO"
170-

0 commit comments

Comments
 (0)