Coverage for tests / test_tutorial / test_commands / test_options / test_tutorial001.py: 100%

56 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-11-18 11:12 +0000

1import importlib 1abcdefghij

2import subprocess 1abcdefghij

3import sys 1abcdefghij

4from types import ModuleType 1abcdefghij

5 

6import pytest 1abcdefghij

7from typer.testing import CliRunner 1abcdefghij

8 

9runner = CliRunner() 1abcdefghij

10 

11 

12@pytest.fixture( 1abcdefghij

13 name="mod", 

14 params=[ 

15 pytest.param("tutorial001_non_an"), 

16 pytest.param("tutorial001"), 

17 ], 

18) 

19def get_mod(request: pytest.FixtureRequest) -> ModuleType: 1abcdefghij

20 module_name = f"docs_src.commands.options.{request.param}" 1abcdefghij

21 mod = importlib.import_module(module_name) 1abcdefghij

22 return mod 1abcdefghij

23 

24 

25def test_help(mod: ModuleType): 1abcdefghij

26 result = runner.invoke(mod.app, ["--help"]) 1abcdefghij

27 assert result.exit_code == 0 1abcdefghij

28 assert "Commands" in result.output 1abcdefghij

29 assert "create" in result.output 1abcdefghij

30 assert "delete" in result.output 1abcdefghij

31 assert "delete-all" in result.output 1abcdefghij

32 assert "init" in result.output 1abcdefghij

33 

34 

35def test_create(mod: ModuleType): 1abcdefghij

36 result = runner.invoke(mod.app, ["create", "Camila"]) 1abcdefghij

37 assert result.exit_code == 0 1abcdefghij

38 assert "Creating user: Camila" in result.output 1abcdefghij

39 

40 

41def test_delete(mod: ModuleType): 1abcdefghij

42 result = runner.invoke(mod.app, ["delete", "Camila"], input="y\n") 1abcdefghij

43 assert result.exit_code == 0 1abcdefghij

44 assert "Are you sure you want to delete the user? [y/n]:" in result.output 1abcdefghij

45 assert "Deleting user: Camila" in result.output 1abcdefghij

46 

47 

48def test_no_delete(mod: ModuleType): 1abcdefghij

49 result = runner.invoke(mod.app, ["delete", "Camila"], input="n\n") 1abcdefghij

50 assert result.exit_code == 0 1abcdefghij

51 assert "Are you sure you want to delete the user? [y/n]:" in result.output 1abcdefghij

52 assert "Operation cancelled" in result.output 1abcdefghij

53 

54 

55def test_delete_all(mod: ModuleType): 1abcdefghij

56 result = runner.invoke(mod.app, ["delete-all"], input="y\n") 1abcdefghij

57 assert result.exit_code == 0 1abcdefghij

58 assert "Are you sure you want to delete ALL users? [y/n]:" in result.output 1abcdefghij

59 assert "Deleting all users" in result.output 1abcdefghij

60 

61 

62def test_no_delete_all(mod: ModuleType): 1abcdefghij

63 result = runner.invoke(mod.app, ["delete-all"], input="n\n") 1abcdefghij

64 assert result.exit_code == 0 1abcdefghij

65 assert "Are you sure you want to delete ALL users? [y/n]:" in result.output 1abcdefghij

66 assert "Operation cancelled" in result.output 1abcdefghij

67 

68 

69def test_delete_all_force(mod: ModuleType): 1abcdefghij

70 result = runner.invoke(mod.app, ["delete-all", "--force"]) 1abcdefghij

71 assert result.exit_code == 0 1abcdefghij

72 assert "Are you sure you want to delete ALL users? [y/n]:" not in result.output 1abcdefghij

73 assert "Deleting all users" in result.output 1abcdefghij

74 

75 

76def test_init(mod: ModuleType): 1abcdefghij

77 result = runner.invoke(mod.app, ["init"]) 1abcdefghij

78 assert result.exit_code == 0 1abcdefghij

79 assert "Initializing user database" in result.output 1abcdefghij

80 

81 

82def test_script(mod: ModuleType): 1abcdefghij

83 result = subprocess.run( 1abcdefghij

84 [sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], 

85 capture_output=True, 

86 encoding="utf-8", 

87 ) 

88 assert "Usage" in result.stdout 1abcdefghij