Coverage for tests/test_tutorial/test_parameter_types/test_number/test_tutorial001_an.py: 100%

53 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-09 18:26 +0000

1import subprocess 1abcdefgh

2import sys 1abcdefgh

3 

4import typer 1abcdefgh

5import typer.core 1abcdefgh

6from typer.testing import CliRunner 1abcdefgh

7 

8from docs_src.parameter_types.number import tutorial001_an as mod 1abcdefgh

9 

10runner = CliRunner() 1abcdefgh

11 

12app = typer.Typer() 1abcdefgh

13app.command()(mod.main) 1abcdefgh

14 

15 

16def test_help(): 1abcdefgh

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

18 assert result.exit_code == 0 1abcdefgh

19 assert "--age" in result.output 1abcdefgh

20 assert "INTEGER RANGE" in result.output 1abcdefgh

21 assert "--score" in result.output 1abcdefgh

22 assert "FLOAT RANGE" in result.output 1abcdefgh

23 

24 

25def test_help_no_rich(): 1abcdefgh

26 rich = typer.core.rich 1abcdefgh

27 typer.core.rich = None 1abcdefgh

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

29 assert result.exit_code == 0 1abcdefgh

30 assert "--age" in result.output 1abcdefgh

31 assert "INTEGER RANGE" in result.output 1abcdefgh

32 assert "--score" in result.output 1abcdefgh

33 assert "FLOAT RANGE" in result.output 1abcdefgh

34 typer.core.rich = rich 1abcdefgh

35 

36 

37def test_params(): 1abcdefgh

38 result = runner.invoke(app, ["5", "--age", "20", "--score", "90"]) 1abcdefgh

39 assert result.exit_code == 0 1abcdefgh

40 assert "ID is 5" in result.output 1abcdefgh

41 assert "--age is 20" in result.output 1abcdefgh

42 assert "--score is 90.0" in result.output 1abcdefgh

43 

44 

45def test_invalid_id(): 1abcdefgh

46 result = runner.invoke(app, ["1002"]) 1abcdefgh

47 assert result.exit_code != 0 1abcdefgh

48 assert ( 1abcdefgh

49 "Invalid value for 'ID': 1002 is not in the range 0<=x<=1000." in result.output 

50 ) 

51 

52 

53def test_invalid_age(): 1abcdefgh

54 result = runner.invoke(app, ["5", "--age", "15"]) 1abcdefgh

55 assert result.exit_code != 0 1abcdefgh

56 assert "Invalid value for '--age': 15 is not in the range x>=18" in result.output 1abcdefgh

57 

58 

59def test_invalid_score(): 1abcdefgh

60 result = runner.invoke(app, ["5", "--age", "20", "--score", "100.5"]) 1abcdefgh

61 assert result.exit_code != 0 1abcdefgh

62 assert ( 1abcdefgh

63 "Invalid value for '--score': 100.5 is not in the range x<=100." 

64 in result.output 

65 ) 

66 

67 

68def test_negative_score(): 1abcdefgh

69 result = runner.invoke(app, ["5", "--age", "20", "--score", "-5"]) 1abcdefgh

70 assert result.exit_code == 0 1abcdefgh

71 assert "ID is 5" in result.output 1abcdefgh

72 assert "--age is 20" in result.output 1abcdefgh

73 assert "--score is -5.0" in result.output 1abcdefgh

74 

75 

76def test_script(): 1abcdefgh

77 result = subprocess.run( 1abcdefgh

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

79 capture_output=True, 

80 encoding="utf-8", 

81 ) 

82 assert "Usage" in result.stdout 1abcdefgh