Coverage for tests/test_core.py: 100%

1 import contextlib

2

3 import pytest

4

5 from src.su6.core import (

6 ApplicationState,

7 Config,

8 ConfigError,

9 Verbosity,

10 _ensure_types,

11 _get_su6_config,

12 check_type,

13 get_su6_config,

14)

15

16 from ._shared import EXAMPLES_PATH

17

18

19 def test_verbosity_compare():

20 verbosity_1 = Verbosity.quiet

21 verbosity_2 = Verbosity.normal

22 verbosity_3 = Verbosity.verbose

23

24 assert verbosity_3 > verbosity_2 > verbosity_1

25 assert str(verbosity_3.value) > verbosity_2 > str(verbosity_1.value)

26 assert int(verbosity_3.value) > verbosity_2 > int(verbosity_1.value)

27 assert str(verbosity_3.value) > verbosity_2 > int(verbosity_1.value)

28 assert int(verbosity_3.value) > verbosity_2 > str(verbosity_1.value)

29

30 assert str(verbosity_3.value) >= verbosity_2 >= str(verbosity_1.value)

31 assert int(verbosity_3.value) >= verbosity_2 >= int(verbosity_1.value)

32 assert str(verbosity_3.value) >= verbosity_2 >= int(verbosity_1.value)

33 assert int(verbosity_3.value) >= verbosity_2 >= str(verbosity_1.value)

34

35 assert str(verbosity_1.value) < verbosity_2 < str(verbosity_3.value)

36 assert int(verbosity_1.value) < verbosity_2 < int(verbosity_3.value)

37 assert str(verbosity_1.value) < verbosity_2 < int(verbosity_3.value)

38 assert int(verbosity_1.value) < verbosity_2 < str(verbosity_3.value)

39

40 assert str(verbosity_1.value) <= verbosity_2 <= str(verbosity_3.value)

41 assert int(verbosity_1.value) <= verbosity_2 <= int(verbosity_3.value)

42 assert str(verbosity_1.value) <= verbosity_2 <= int(verbosity_3.value)

43 assert int(verbosity_1.value) <= verbosity_2 <= str(verbosity_3.value)

44

45 assert verbosity_3 == verbosity_3.value

46 assert verbosity_3 == str(verbosity_3.value)

47 assert verbosity_3 == int(verbosity_3.value)

48

49 with pytest.raises(TypeError):

50 assert verbosity_3 == []

51

52

53 def test_check_type():

54 assert check_type("str", str | int)

55 assert check_type([1, 2, 3], list[int])

56 assert check_type([1, 2, 3], list[int] | int)

57 assert not check_type([1, 2, 3], list[str])

58

59

60 def test_ensure_types():

61 assert _ensure_types({"float": 3.5}, {"float": float})

62 with pytest.raises(ConfigError):

63 try:

64 _ensure_types({"float": "not-a-float"}, {"float": float})

65 except ConfigError as e:

66 assert "float" in str(e) and "str" in str(e)

67 raise e

68

69

70 def test_get_su6_config():

71 # doesnt_exist - defaults

72 defaults = get_su6_config(toml_path="./doesnt-exist.toml")

73 empty = get_su6_config(toml_path=str(EXAMPLES_PATH / "empty.toml"))

74 none = get_su6_config(toml_path=None)

75 assert Config() == defaults

76 assert defaults.directory == empty.directory

77 assert none.pyproject is not None

78

79 with contextlib.chdir("/tmp"):

80 # no pyproject.toml in sight -> internal su6 config should return None and external should return default

81 assert _get_su6_config(overwrites={}) is None

82 assert get_su6_config() == defaults

83

84 # invalid toml should raise exception on debug verbosity but default on other verbosities

85

86 with pytest.raises(ConfigError):

87 get_su6_config(verbosity=Verbosity.debug, toml_path=str(EXAMPLES_PATH / "invalid.toml"))

88

89 assert get_su6_config(verbosity=Verbosity.verbose, toml_path=str(EXAMPLES_PATH / "invalid.toml")) == defaults

90

91

92 def test_loading_state_without_load_config():

93 state = ApplicationState()

94 # skip state.load_config

95 assert state.update_config() == get_su6_config()