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-02-09 12:36 +0000

1import importlib 1abcdefgh

2import subprocess 1abcdefgh

3import sys 1abcdefgh

4from types import ModuleType 1abcdefgh

5 

6import pytest 1abcdefgh

7from typer.testing import CliRunner 1abcdefgh

8 

9runner = CliRunner() 1abcdefgh

10 

11 

12@pytest.fixture( 1abcdefgh

13 name="mod", 

14 params=[ 

15 pytest.param("tutorial001_py39"), 

16 pytest.param("tutorial001_an_py39"), 

17 ], 

18) 

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

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

21 mod = importlib.import_module(module_name) 1abcdefgh

22 return mod 1abcdefgh

23 

24 

25def test_help(mod: ModuleType): 1abcdefgh

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

27 assert result.exit_code == 0 1abcdefgh

28 assert "Commands" in result.output 1abcdefgh

29 assert "create" in result.output 1abcdefgh

30 assert "delete" in result.output 1abcdefgh

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

32 assert "init" in result.output 1abcdefgh

33 

34 

35def test_create(mod: ModuleType): 1abcdefgh

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

37 assert result.exit_code == 0 1abcdefgh

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

39 

40 

41def test_delete(mod: ModuleType): 1abcdefgh

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

43 assert result.exit_code == 0 1abcdefgh

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

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

46 

47 

48def test_no_delete(mod: ModuleType): 1abcdefgh

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

50 assert result.exit_code == 0 1abcdefgh

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

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

53 

54 

55def test_delete_all(mod: ModuleType): 1abcdefgh

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

57 assert result.exit_code == 0 1abcdefgh

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

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

60 

61 

62def test_no_delete_all(mod: ModuleType): 1abcdefgh

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

64 assert result.exit_code == 0 1abcdefgh

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

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

67 

68 

69def test_delete_all_force(mod: ModuleType): 1abcdefgh

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

71 assert result.exit_code == 0 1abcdefgh

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

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

74 

75 

76def test_init(mod: ModuleType): 1abcdefgh

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

78 assert result.exit_code == 0 1abcdefgh

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

80 

81 

82def test_script(mod: ModuleType): 1abcdefgh

83 result = subprocess.run( 1abcdefgh

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

85 capture_output=True, 

86 encoding="utf-8", 

87 ) 

88 assert "Usage" in result.stdout 1abcdefgh