Coverage for tests/test_tutorial/test_subcommands/test_tutorial003.py: 100%
120 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 18:26 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-09-09 18:26 +0000
1import os 1habcdefg
2import subprocess 1habcdefg
3import sys 1habcdefg
5import pytest 1habcdefg
6from typer.testing import CliRunner 1habcdefg
8from docs_src.subcommands import tutorial003 1habcdefg
9from docs_src.subcommands.tutorial003 import items, users 1habcdefg
11runner = CliRunner() 1habcdefg
14@pytest.fixture() 1habcdefg
15def mod(monkeypatch): 1abcdefg
16 with monkeypatch.context() as m: 1habcdefg
17 m.syspath_prepend(list(tutorial003.__path__)[0]) 1habcdefg
18 from docs_src.subcommands.tutorial003 import main 1habcdefg
20 return main 1habcdefg
23@pytest.fixture() 1habcdefg
24def app(mod): 1abcdefg
25 return mod.app 1habcdefg
28def test_help(app): 1habcdefg
29 result = runner.invoke(app, ["--help"]) 1habcdefg
30 assert result.exit_code == 0 1habcdefg
31 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1habcdefg
32 assert "Commands" in result.output 1habcdefg
33 assert "items" in result.output 1habcdefg
34 assert "users" in result.output 1habcdefg
35 assert "lands" in result.output 1habcdefg
38def test_help_items(app): 1habcdefg
39 result = runner.invoke(app, ["items", "--help"]) 1habcdefg
40 assert result.exit_code == 0 1habcdefg
41 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1habcdefg
42 assert "Commands" in result.output 1habcdefg
43 assert "create" in result.output 1habcdefg
44 assert "delete" in result.output 1habcdefg
45 assert "sell" in result.output 1habcdefg
48def test_items_create(app): 1habcdefg
49 result = runner.invoke(app, ["items", "create", "Wand"]) 1habcdefg
50 assert result.exit_code == 0 1habcdefg
51 assert "Creating item: Wand" in result.output 1habcdefg
52 # For coverage, becauses the monkeypatch above sometimes confuses coverage
53 result = runner.invoke(items.app, ["create", "Wand"]) 1habcdefg
54 assert result.exit_code == 0 1habcdefg
55 assert "Creating item: Wand" in result.output 1habcdefg
58def test_items_sell(app): 1habcdefg
59 result = runner.invoke(app, ["items", "sell", "Vase"]) 1habcdefg
60 assert result.exit_code == 0 1habcdefg
61 assert "Selling item: Vase" in result.output 1habcdefg
62 # For coverage, becauses the monkeypatch above sometimes confuses coverage
63 result = runner.invoke(items.app, ["sell", "Vase"]) 1habcdefg
64 assert result.exit_code == 0 1habcdefg
65 assert "Selling item: Vase" in result.output 1habcdefg
68def test_items_delete(app): 1habcdefg
69 result = runner.invoke(app, ["items", "delete", "Vase"]) 1habcdefg
70 assert result.exit_code == 0 1habcdefg
71 assert "Deleting item: Vase" in result.output 1habcdefg
72 # For coverage, becauses the monkeypatch above sometimes confuses coverage
73 result = runner.invoke(items.app, ["delete", "Vase"]) 1habcdefg
74 assert result.exit_code == 0 1habcdefg
75 assert "Deleting item: Vase" in result.output 1habcdefg
78def test_help_users(app): 1habcdefg
79 result = runner.invoke(app, ["users", "--help"]) 1habcdefg
80 assert result.exit_code == 0 1habcdefg
81 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1habcdefg
82 assert "Commands" in result.output 1habcdefg
83 assert "create" in result.output 1habcdefg
84 assert "delete" in result.output 1habcdefg
85 assert "sell" not in result.output 1habcdefg
88def test_users_create(app): 1habcdefg
89 result = runner.invoke(app, ["users", "create", "Camila"]) 1habcdefg
90 assert result.exit_code == 0 1habcdefg
91 assert "Creating user: Camila" in result.output 1habcdefg
92 # For coverage, becauses the monkeypatch above sometimes confuses coverage
93 result = runner.invoke(users.app, ["create", "Camila"]) 1habcdefg
94 assert result.exit_code == 0 1habcdefg
95 assert "Creating user: Camila" in result.output 1habcdefg
98def test_users_delete(app): 1habcdefg
99 result = runner.invoke(app, ["users", "delete", "Camila"]) 1habcdefg
100 assert result.exit_code == 0 1habcdefg
101 assert "Deleting user: Camila" in result.output 1habcdefg
102 # For coverage, becauses the monkeypatch above sometimes confuses coverage
103 result = runner.invoke(users.app, ["delete", "Camila"]) 1habcdefg
104 assert result.exit_code == 0 1habcdefg
105 assert "Deleting user: Camila" in result.output 1habcdefg
108def test_help_lands(app): 1habcdefg
109 result = runner.invoke(app, ["lands", "--help"]) 1habcdefg
110 assert result.exit_code == 0 1habcdefg
111 assert "lands [OPTIONS] COMMAND [ARGS]..." in result.output 1habcdefg
112 assert "Commands" in result.output 1habcdefg
113 assert "reigns" in result.output 1habcdefg
114 assert "towns" in result.output 1habcdefg
117def test_help_lands_reigns(app): 1habcdefg
118 result = runner.invoke(app, ["lands", "reigns", "--help"]) 1habcdefg
119 assert result.exit_code == 0 1habcdefg
120 assert "lands reigns [OPTIONS] COMMAND [ARGS]..." in result.output 1habcdefg
121 assert "Commands" in result.output 1habcdefg
122 assert "conquer" in result.output 1habcdefg
123 assert "destroy" in result.output 1habcdefg
126def test_lands_reigns_conquer(app): 1habcdefg
127 result = runner.invoke(app, ["lands", "reigns", "conquer", "Gondor"]) 1habcdefg
128 assert result.exit_code == 0 1habcdefg
129 assert "Conquering reign: Gondor" in result.output 1habcdefg
132def test_lands_reigns_destroy(app): 1habcdefg
133 result = runner.invoke(app, ["lands", "reigns", "destroy", "Mordor"]) 1habcdefg
134 assert result.exit_code == 0 1habcdefg
135 assert "Destroying reign: Mordor" in result.output 1habcdefg
138def test_help_lands_towns(app): 1habcdefg
139 result = runner.invoke(app, ["lands", "towns", "--help"]) 1habcdefg
140 assert result.exit_code == 0 1habcdefg
141 assert "lands towns [OPTIONS] COMMAND [ARGS]..." in result.output 1habcdefg
142 assert "Commands" in result.output 1habcdefg
143 assert "burn" in result.output 1habcdefg
144 assert "found" in result.output 1habcdefg
147def test_lands_towns_found(app): 1habcdefg
148 result = runner.invoke(app, ["lands", "towns", "found", "Cartagena"]) 1habcdefg
149 assert result.exit_code == 0 1habcdefg
150 assert "Founding town: Cartagena" in result.output 1habcdefg
153def test_lands_towns_burn(app): 1habcdefg
154 result = runner.invoke(app, ["lands", "towns", "burn", "New Asgard"]) 1habcdefg
155 assert result.exit_code == 0 1habcdefg
156 assert "Burning town: New Asgard" in result.output 1habcdefg
159def test_scripts(mod): 1habcdefg
160 from docs_src.subcommands.tutorial003 import items, lands, reigns, towns, users 1habcdefg
162 env = os.environ.copy() 1habcdefg
163 env["PYTHONPATH"] = ":".join(list(tutorial003.__path__)) 1habcdefg
165 for module in [mod, items, lands, reigns, towns, users]: 1habcdefg
166 result = subprocess.run( 1habcdefg
167 [sys.executable, "-m", "coverage", "run", module.__file__, "--help"],
168 capture_output=True,
169 encoding="utf-8",
170 env=env,
171 )
172 assert "Usage" in result.stdout 1habcdefg