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

31 statements  

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

1import subprocess 1abcdefg

2import sys 1abcdefg

3import uuid 1abcdefg

4 

5import typer 1abcdefg

6from typer.testing import CliRunner 1abcdefg

7 

8from docs_src.parameter_types.uuid 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 user_id_param = next(param for param in command.params if param.name == "user_id") 1abcdefg

17 assert repr(user_id_param.type) == "UUID" 1abcdefg

18 

19 

20def test_main(): 1abcdefg

21 result = runner.invoke(app, ["d48edaa6-871a-4082-a196-4daab372d4a1"]) 1abcdefg

22 assert result.exit_code == 0 1abcdefg

23 assert "User ID is d48edaa6-871a-4082-a196-4daab372d4a1" in result.output 1abcdefg

24 assert "UUID version is: 4" in result.output 1abcdefg

25 

26 

27def test_main_with_uuid_object(): 1abcdefg

28 user_id = uuid.UUID("d48edaa6-871a-4082-a196-4daab372d4a1") 1abcdefg

29 result = runner.invoke(app, [], default_map={"user_id": user_id}) 1abcdefg

30 assert result.exit_code == 0 1abcdefg

31 assert "User ID is d48edaa6-871a-4082-a196-4daab372d4a1" in result.output 1abcdefg

32 assert "UUID version is: 4" in result.output 1abcdefg

33 

34 

35def test_invalid_uuid(): 1abcdefg

36 result = runner.invoke(app, ["7479706572-72756c6573"]) 1abcdefg

37 assert result.exit_code != 0 1abcdefg

38 assert "Invalid value for 'user_id'" in result.output 1abcdefg

39 assert "not a valid UUID" in result.output 1abcdefg

40 

41 

42def test_script(): 1abcdefg

43 result = subprocess.run( 1abcdefg

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

45 capture_output=True, 

46 encoding="utf-8", 

47 ) 

48 assert "Usage" in result.stdout 1abcdefg