Coverage for tests / test_tutorial / test_parameter_types / test_datetime / test_tutorial001.py: 100%

37 statements  

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

1import subprocess 1abcdefg

2import sys 1abcdefg

3from datetime import datetime 1abcdefg

4 

5import typer 1abcdefg

6from typer.testing import CliRunner 1abcdefg

7 

8from docs_src.parameter_types.datetime import tutorial001_py310 as mod 1abcdefg

9 

10runner = CliRunner() 1abcdefg

11app = mod.app 1abcdefg

12 

13 

14def test_type_repr(): 1abcdefg

15 command = typer.main.get_command(app) 1abcdefg

16 birth_param = next(param for param in command.params if param.name == "birth") 1abcdefg

17 assert repr(birth_param.type) == "DateTime" 1abcdefg

18 

19 

20def test_help(): 1abcdefg

21 result = runner.invoke(app, ["--help"]) 1abcdefg

22 assert result.exit_code == 0 1abcdefg

23 assert "<%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d %H:%M:%S>" in result.output 1abcdefg

24 

25 

26def test_main(): 1abcdefg

27 result = runner.invoke(app, ["1956-01-31T10:00:00"]) 1abcdefg

28 assert result.exit_code == 0 1abcdefg

29 assert "Interesting day to be born: 1956-01-31 10:00:00" in result.output 1abcdefg

30 assert "Birth hour: 10" in result.output 1abcdefg

31 

32 

33def test_main_datetime_object(): 1abcdefg

34 result = runner.invoke( 1abcdefg

35 app, [], default_map={"birth": datetime(1956, 1, 31, 10, 0, 0)} 

36 ) 

37 assert result.exit_code == 0 1abcdefg

38 assert "Interesting day to be born: 1956-01-31 10:00:00" in result.output 1abcdefg

39 assert "Birth hour: 10" in result.output 1abcdefg

40 

41 

42def test_invalid(): 1abcdefg

43 result = runner.invoke(app, ["july-19-1989"]) 1abcdefg

44 assert result.exit_code != 0 1abcdefg

45 assert "Invalid value for 'birth'" in result.output 1abcdefg

46 assert "'july-19-1989' does not match the formats" in result.output 1abcdefg

47 assert "%Y-%m-%d" in result.output 1abcdefg

48 assert "%Y-%m-%dT%H:%M:%S" in result.output 1abcdefg

49 assert "%Y-%m-%d %H:%M:%S" in result.output 1abcdefg

50 

51 

52def test_script(): 1abcdefg

53 result = subprocess.run( 1abcdefg

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

55 capture_output=True, 

56 encoding="utf-8", 

57 ) 

58 assert "Usage" in result.stdout 1abcdefg