Coverage for unit_tests/test_002_nested_models.py: 100%

1 import subprocess

2 import sys

3

4 from typer.testing import CliRunner

5

6 import pydantic_typer

7 from examples import example_002_nested_models as mod

8

9 runner = CliRunner()

10

11 app = pydantic_typer.Typer()

12 app.command()(mod.main)

13

14

15 def test_help():

16 result = runner.invoke(app, ["--help"])

17 assert result.exit_code == 0

18

19

20 def test_parse_pydantic_model():

21 result = runner.invoke(

22 app,

23 [

24 "--person.name",

25 "Jeff",

26 "--person.pet.name",

27 "Lassie",

28 "--person.pet.species",

29 "dog",

30 ],

31 )

32 assert (

33 "name='Jeff' age=None pet=Pet(name='Lassie', species='dog') <class 'examples.example_002_nested_models.Person'>"

34 in result.output

35 )

36

37

38 def test_script():

39 result = subprocess.run(

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

41 capture_output=True,

42 encoding="utf-8",

43 check=False,

44 )

45 assert "Usage" in result.stdout