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

35 statements  

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

1import importlib 1abcdefghij

2import subprocess 1abcdefghij

3import sys 1abcdefghij

4from datetime import datetime 1abcdefghij

5from types import ModuleType 1abcdefghij

6 

7import pytest 1abcdefghij

8from typer.testing import CliRunner 1abcdefghij

9 

10runner = CliRunner() 1abcdefghij

11 

12 

13@pytest.fixture( 1abcdefghij

14 name="mod", 

15 params=[ 

16 pytest.param("tutorial002_non_an"), 

17 pytest.param("tutorial002"), 

18 ], 

19) 

20def get_mod(request: pytest.FixtureRequest) -> ModuleType: 1abcdefghij

21 module_name = f"docs_src.parameter_types.datetime.{request.param}" 1abcdefghij

22 mod = importlib.import_module(module_name) 1abcdefghij

23 return mod 1abcdefghij

24 

25 

26def test_main(mod: ModuleType): 1abcdefghij

27 result = runner.invoke(mod.app, ["1969-10-29"]) 1abcdefghij

28 assert result.exit_code == 0 1abcdefghij

29 assert "Launch will be at: 1969-10-29 00:00:00" in result.output 1abcdefghij

30 

31 

32def test_usa_weird_date_format(mod: ModuleType): 1abcdefghij

33 result = runner.invoke(mod.app, ["10/29/1969"]) 1abcdefghij

34 assert result.exit_code == 0 1abcdefghij

35 assert "Launch will be at: 1969-10-29 00:00:00" in result.output 1abcdefghij

36 

37 

38def test_main_datetime_object(mod: ModuleType): 1abcdefghij

39 my_map = {"launch_date": datetime(1969, 10, 29, 3, 40, 2)} 1abcdefghij

40 result = runner.invoke(mod.app, [], default_map=my_map) 1abcdefghij

41 assert result.exit_code == 0 1abcdefghij

42 assert "Launch will be at: 1969-10-29 03:40:02" in result.output 1abcdefghij

43 

44 

45def test_invalid(mod: ModuleType): 1abcdefghij

46 result = runner.invoke(mod.app, ["not-a-date"]) 1abcdefghij

47 assert result.exit_code != 0 1abcdefghij

48 assert "Invalid value for 'launch_date'" in result.output 1abcdefghij

49 assert "does not match" in result.output 1abcdefghij

50 assert "'%Y-%m-%d', '%m/%d/%Y'" in result.output 1abcdefghij

51 

52 

53def test_script(mod: ModuleType): 1abcdefghij

54 result = subprocess.run( 1abcdefghij

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

56 capture_output=True, 

57 encoding="utf-8", 

58 ) 

59 assert "Usage" in result.stdout 1abcdefghij