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

66 statements  

« 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

4 

5import pytest 1habcdefg

6from typer.testing import CliRunner 1habcdefg

7 

8from docs_src.subcommands import tutorial001 1habcdefg

9 

10runner = CliRunner() 1habcdefg

11 

12 

13@pytest.fixture() 1habcdefg

14def mod(monkeypatch): 1abcdefg

15 with monkeypatch.context(): 1habcdefg

16 monkeypatch.syspath_prepend(list(tutorial001.__path__)[0]) 1habcdefg

17 from docs_src.subcommands.tutorial001 import main 1habcdefg

18 

19 return main 1habcdefg

20 

21 

22@pytest.fixture() 1habcdefg

23def app(mod): 1abcdefg

24 return mod.app 1habcdefg

25 

26 

27def test_help(app): 1habcdefg

28 result = runner.invoke(app, ["--help"]) 1habcdefg

29 assert result.exit_code == 0 1habcdefg

30 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1habcdefg

31 assert "Commands" in result.output 1habcdefg

32 assert "items" in result.output 1habcdefg

33 assert "users" in result.output 1habcdefg

34 

35 

36def test_help_items(app): 1habcdefg

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

38 assert result.exit_code == 0 1habcdefg

39 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1habcdefg

40 assert "Commands" in result.output 1habcdefg

41 assert "create" in result.output 1habcdefg

42 assert "delete" in result.output 1habcdefg

43 assert "sell" in result.output 1habcdefg

44 

45 

46def test_items_create(app): 1habcdefg

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

48 assert result.exit_code == 0 1habcdefg

49 assert "Creating item: Wand" in result.output 1habcdefg

50 

51 

52def test_items_sell(app): 1habcdefg

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

54 assert result.exit_code == 0 1habcdefg

55 assert "Selling item: Vase" in result.output 1habcdefg

56 

57 

58def test_items_delete(app): 1habcdefg

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

60 assert result.exit_code == 0 1habcdefg

61 assert "Deleting item: Vase" in result.output 1habcdefg

62 

63 

64def test_help_users(app): 1habcdefg

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

66 assert result.exit_code == 0 1habcdefg

67 assert "[OPTIONS] COMMAND [ARGS]..." in result.output 1habcdefg

68 assert "Commands" in result.output 1habcdefg

69 assert "create" in result.output 1habcdefg

70 assert "delete" in result.output 1habcdefg

71 assert "sell" not in result.output 1habcdefg

72 

73 

74def test_users_create(app): 1habcdefg

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

76 assert result.exit_code == 0 1habcdefg

77 assert "Creating user: Camila" in result.output 1habcdefg

78 

79 

80def test_users_delete(app): 1habcdefg

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

82 assert result.exit_code == 0 1habcdefg

83 assert "Deleting user: Camila" in result.output 1habcdefg

84 

85 

86def test_scripts(mod): 1habcdefg

87 from docs_src.subcommands.tutorial001 import items, users 1habcdefg

88 

89 env = os.environ.copy() 1habcdefg

90 env["PYTHONPATH"] = ":".join(list(tutorial001.__path__)) 1habcdefg

91 

92 for module in [mod, items, users]: 1habcdefg

93 result = subprocess.run( 1habcdefg

94 [sys.executable, "-m", "coverage", "run", module.__file__, "--help"], 

95 capture_output=True, 

96 encoding="utf-8", 

97 env=env, 

98 ) 

99 assert "Usage" in result.stdout 1habcdefg