/
test_tutorial003.py
167 lines (126 loc) · 5.38 KB
/
test_tutorial003.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import subprocess
import pytest
from typer.testing import CliRunner
from docs_src.subcommands import tutorial003
from docs_src.subcommands.tutorial003 import items, users
runner = CliRunner()
@pytest.fixture()
def mod(monkeypatch):
with monkeypatch.context() as m:
m.syspath_prepend(list(tutorial003.__path__)[0])
from docs_src.subcommands.tutorial003 import main
return main
@pytest.fixture()
def app(mod):
return mod.app
def test_help(app):
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0
assert "[OPTIONS] COMMAND [ARGS]..." in result.output
assert "Commands" in result.output
assert "items" in result.output
assert "users" in result.output
assert "lands" in result.output
def test_help_items(app):
result = runner.invoke(app, ["items", "--help"])
assert result.exit_code == 0
assert "[OPTIONS] COMMAND [ARGS]..." in result.output
assert "Commands" in result.output
assert "create" in result.output
assert "delete" in result.output
assert "sell" in result.output
def test_items_create(app):
result = runner.invoke(app, ["items", "create", "Wand"])
assert result.exit_code == 0
assert "Creating item: Wand" in result.output
# For coverage, becauses the monkeypatch above sometimes confuses coverage
result = runner.invoke(items.app, ["create", "Wand"])
assert result.exit_code == 0
assert "Creating item: Wand" in result.output
def test_items_sell(app):
result = runner.invoke(app, ["items", "sell", "Vase"])
assert result.exit_code == 0
assert "Selling item: Vase" in result.output
# For coverage, becauses the monkeypatch above sometimes confuses coverage
result = runner.invoke(items.app, ["sell", "Vase"])
assert result.exit_code == 0
assert "Selling item: Vase" in result.output
def test_items_delete(app):
result = runner.invoke(app, ["items", "delete", "Vase"])
assert result.exit_code == 0
assert "Deleting item: Vase" in result.output
# For coverage, becauses the monkeypatch above sometimes confuses coverage
result = runner.invoke(items.app, ["delete", "Vase"])
assert result.exit_code == 0
assert "Deleting item: Vase" in result.output
def test_help_users(app):
result = runner.invoke(app, ["users", "--help"])
assert result.exit_code == 0
assert "[OPTIONS] COMMAND [ARGS]..." in result.output
assert "Commands" in result.output
assert "create" in result.output
assert "delete" in result.output
assert "sell" not in result.output
def test_users_create(app):
result = runner.invoke(app, ["users", "create", "Camila"])
assert result.exit_code == 0
assert "Creating user: Camila" in result.output
# For coverage, becauses the monkeypatch above sometimes confuses coverage
result = runner.invoke(users.app, ["create", "Camila"])
assert result.exit_code == 0
assert "Creating user: Camila" in result.output
def test_users_delete(app):
result = runner.invoke(app, ["users", "delete", "Camila"])
assert result.exit_code == 0
assert "Deleting user: Camila" in result.output
# For coverage, becauses the monkeypatch above sometimes confuses coverage
result = runner.invoke(users.app, ["delete", "Camila"])
assert result.exit_code == 0
assert "Deleting user: Camila" in result.output
def test_help_lands(app):
result = runner.invoke(app, ["lands", "--help"])
assert result.exit_code == 0
assert "lands [OPTIONS] COMMAND [ARGS]..." in result.output
assert "Commands" in result.output
assert "reigns" in result.output
assert "towns" in result.output
def test_help_lands_reigns(app):
result = runner.invoke(app, ["lands", "reigns", "--help"])
assert result.exit_code == 0
assert "lands reigns [OPTIONS] COMMAND [ARGS]..." in result.output
assert "Commands" in result.output
assert "conquer" in result.output
assert "destroy" in result.output
def test_lands_reigns_conquer(app):
result = runner.invoke(app, ["lands", "reigns", "conquer", "Gondor"])
assert result.exit_code == 0
assert "Conquering reign: Gondor" in result.output
def test_lands_reigns_destroy(app):
result = runner.invoke(app, ["lands", "reigns", "destroy", "Mordor"])
assert result.exit_code == 0
assert "Destroying reign: Mordor" in result.output
def test_help_lands_towns(app):
result = runner.invoke(app, ["lands", "towns", "--help"])
assert result.exit_code == 0
assert "lands towns [OPTIONS] COMMAND [ARGS]..." in result.output
assert "Commands" in result.output
assert "burn" in result.output
assert "found" in result.output
def test_lands_towns_found(app):
result = runner.invoke(app, ["lands", "towns", "found", "Cartagena"])
assert result.exit_code == 0
assert "Founding town: Cartagena" in result.output
def test_lands_towns_burn(app):
result = runner.invoke(app, ["lands", "towns", "burn", "New Asgard"])
assert result.exit_code == 0
assert "Burning town: New Asgard" in result.output
def test_scripts(mod):
from docs_src.subcommands.tutorial003 import items, lands, reigns, towns, users
for module in [mod, items, lands, reigns, towns, users]:
result = subprocess.run(
["coverage", "run", module.__file__, "--help"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
assert "Usage" in result.stdout