Coverage for tests/test_tutorial/test_subcommands/test_tutorial003.py: 100%

120 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-04-14 00:18 +0000

1import os 1iabcdefgh

2import subprocess 1iabcdefgh

3import sys 1iabcdefgh

4 

5import pytest 1iabcdefgh

6from typer.testing import CliRunner 1iabcdefgh

7 

8from docs_src.subcommands import tutorial003 1iabcdefgh

9from docs_src.subcommands.tutorial003 import items, users 1iabcdefgh

10 

11runner = CliRunner() 1iabcdefgh

12 

13 

14@pytest.fixture() 1iabcdefgh

15def mod(monkeypatch): 1abcdefgh

16 with monkeypatch.context() as m: 1iabcdefgh

17 m.syspath_prepend(list(tutorial003.__path__)[0]) 1iabcdefgh

18 from docs_src.subcommands.tutorial003 import main 1iabcdefgh

19 

20 return main 1iabcdefgh

21 

22 

23@pytest.fixture() 1iabcdefgh

24def app(mod): 1abcdefgh

25 return mod.app 1iabcdefgh

26 

27 

28def test_help(app): 1iabcdefgh

29 result = runner.invoke(app, ["--help"]) 1iabcdefgh

30 assert result.exit_code == 0 1iabcdefgh

31 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1iabcdefgh

32 assert "Commands" in result.output 1iabcdefgh

33 assert "items" in result.output 1iabcdefgh

34 assert "users" in result.output 1iabcdefgh

35 assert "lands" in result.output 1iabcdefgh

36 

37 

38def test_help_items(app): 1iabcdefgh

39 result = runner.invoke(app, ["items", "--help"]) 1iabcdefgh

40 assert result.exit_code == 0 1iabcdefgh

41 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1iabcdefgh

42 assert "Commands" in result.output 1iabcdefgh

43 assert "create" in result.output 1iabcdefgh

44 assert "delete" in result.output 1iabcdefgh

45 assert "sell" in result.output 1iabcdefgh

46 

47 

48def test_items_create(app): 1iabcdefgh

49 result = runner.invoke(app, ["items", "create", "Wand"]) 1iabcdefgh

50 assert result.exit_code == 0 1iabcdefgh

51 assert "Creating item: Wand" in result.output 1iabcdefgh

52 # For coverage, because the monkeypatch above sometimes confuses coverage 

53 result = runner.invoke(items.app, ["create", "Wand"]) 1iabcdefgh

54 assert result.exit_code == 0 1iabcdefgh

55 assert "Creating item: Wand" in result.output 1iabcdefgh

56 

57 

58def test_items_sell(app): 1iabcdefgh

59 result = runner.invoke(app, ["items", "sell", "Vase"]) 1iabcdefgh

60 assert result.exit_code == 0 1iabcdefgh

61 assert "Selling item: Vase" in result.output 1iabcdefgh

62 # For coverage, because the monkeypatch above sometimes confuses coverage 

63 result = runner.invoke(items.app, ["sell", "Vase"]) 1iabcdefgh

64 assert result.exit_code == 0 1iabcdefgh

65 assert "Selling item: Vase" in result.output 1iabcdefgh

66 

67 

68def test_items_delete(app): 1iabcdefgh

69 result = runner.invoke(app, ["items", "delete", "Vase"]) 1iabcdefgh

70 assert result.exit_code == 0 1iabcdefgh

71 assert "Deleting item: Vase" in result.output 1iabcdefgh

72 # For coverage, because the monkeypatch above sometimes confuses coverage 

73 result = runner.invoke(items.app, ["delete", "Vase"]) 1iabcdefgh

74 assert result.exit_code == 0 1iabcdefgh

75 assert "Deleting item: Vase" in result.output 1iabcdefgh

76 

77 

78def test_help_users(app): 1iabcdefgh

79 result = runner.invoke(app, ["users", "--help"]) 1iabcdefgh

80 assert result.exit_code == 0 1iabcdefgh

81 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1iabcdefgh

82 assert "Commands" in result.output 1iabcdefgh

83 assert "create" in result.output 1iabcdefgh

84 assert "delete" in result.output 1iabcdefgh

85 assert "sell" not in result.output 1iabcdefgh

86 

87 

88def test_users_create(app): 1iabcdefgh

89 result = runner.invoke(app, ["users", "create", "Camila"]) 1iabcdefgh

90 assert result.exit_code == 0 1iabcdefgh

91 assert "Creating user: Camila" in result.output 1iabcdefgh

92 # For coverage, because the monkeypatch above sometimes confuses coverage 

93 result = runner.invoke(users.app, ["create", "Camila"]) 1iabcdefgh

94 assert result.exit_code == 0 1iabcdefgh

95 assert "Creating user: Camila" in result.output 1iabcdefgh

96 

97 

98def test_users_delete(app): 1iabcdefgh

99 result = runner.invoke(app, ["users", "delete", "Camila"]) 1iabcdefgh

100 assert result.exit_code == 0 1iabcdefgh

101 assert "Deleting user: Camila" in result.output 1iabcdefgh

102 # For coverage, because the monkeypatch above sometimes confuses coverage 

103 result = runner.invoke(users.app, ["delete", "Camila"]) 1iabcdefgh

104 assert result.exit_code == 0 1iabcdefgh

105 assert "Deleting user: Camila" in result.output 1iabcdefgh

106 

107 

108def test_help_lands(app): 1iabcdefgh

109 result = runner.invoke(app, ["lands", "--help"]) 1iabcdefgh

110 assert result.exit_code == 0 1iabcdefgh

111 assert "lands [OPTIONS] COMMAND [ARGS]..." in result.output 1iabcdefgh

112 assert "Commands" in result.output 1iabcdefgh

113 assert "reigns" in result.output 1iabcdefgh

114 assert "towns" in result.output 1iabcdefgh

115 

116 

117def test_help_lands_reigns(app): 1iabcdefgh

118 result = runner.invoke(app, ["lands", "reigns", "--help"]) 1iabcdefgh

119 assert result.exit_code == 0 1iabcdefgh

120 assert "lands reigns [OPTIONS] COMMAND [ARGS]..." in result.output 1iabcdefgh

121 assert "Commands" in result.output 1iabcdefgh

122 assert "conquer" in result.output 1iabcdefgh

123 assert "destroy" in result.output 1iabcdefgh

124 

125 

126def test_lands_reigns_conquer(app): 1iabcdefgh

127 result = runner.invoke(app, ["lands", "reigns", "conquer", "Gondor"]) 1iabcdefgh

128 assert result.exit_code == 0 1iabcdefgh

129 assert "Conquering reign: Gondor" in result.output 1iabcdefgh

130 

131 

132def test_lands_reigns_destroy(app): 1iabcdefgh

133 result = runner.invoke(app, ["lands", "reigns", "destroy", "Mordor"]) 1iabcdefgh

134 assert result.exit_code == 0 1iabcdefgh

135 assert "Destroying reign: Mordor" in result.output 1iabcdefgh

136 

137 

138def test_help_lands_towns(app): 1iabcdefgh

139 result = runner.invoke(app, ["lands", "towns", "--help"]) 1iabcdefgh

140 assert result.exit_code == 0 1iabcdefgh

141 assert "lands towns [OPTIONS] COMMAND [ARGS]..." in result.output 1iabcdefgh

142 assert "Commands" in result.output 1iabcdefgh

143 assert "burn" in result.output 1iabcdefgh

144 assert "found" in result.output 1iabcdefgh

145 

146 

147def test_lands_towns_found(app): 1iabcdefgh

148 result = runner.invoke(app, ["lands", "towns", "found", "Cartagena"]) 1iabcdefgh

149 assert result.exit_code == 0 1iabcdefgh

150 assert "Founding town: Cartagena" in result.output 1iabcdefgh

151 

152 

153def test_lands_towns_burn(app): 1iabcdefgh

154 result = runner.invoke(app, ["lands", "towns", "burn", "New Asgard"]) 1iabcdefgh

155 assert result.exit_code == 0 1iabcdefgh

156 assert "Burning town: New Asgard" in result.output 1iabcdefgh

157 

158 

159def test_scripts(mod): 1iabcdefgh

160 from docs_src.subcommands.tutorial003 import items, lands, reigns, towns, users 1iabcdefgh

161 

162 env = os.environ.copy() 1iabcdefgh

163 env["PYTHONPATH"] = ":".join(list(tutorial003.__path__)) 1iabcdefgh

164 

165 for module in [mod, items, lands, reigns, towns, users]: 1iabcdefgh

166 result = subprocess.run( 1iabcdefgh

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 1iabcdefgh