|
| 1 | +"""Tests for automatic enum type conversion in Config descriptors. |
| 2 | +
|
| 3 | +Tests that StrEnum, IntEnum, IntFlag, and Enum defaults are automatically |
| 4 | +wrapped in their corresponding data type converters when used with Config. |
| 5 | +""" |
| 6 | + |
| 7 | +import enum |
| 8 | +from enum import IntEnum, IntFlag, StrEnum |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +from confkit import Config as ConfigBase |
| 12 | +from confkit.data_types import Enum as ConfigEnum |
| 13 | +from confkit.data_types import IntEnum as ConfigIntEnum |
| 14 | +from confkit.data_types import IntFlag as ConfigIntFlag |
| 15 | +from confkit.data_types import StrEnum as ConfigStrEnum |
| 16 | +from confkit.parsers import IniParser |
| 17 | + |
| 18 | + |
| 19 | +class LogLevel(StrEnum): |
| 20 | + """String-based enum for log levels.""" |
| 21 | + |
| 22 | + DEBUG = "debug" |
| 23 | + INFO = "info" |
| 24 | + WARNING = "warning" |
| 25 | + ERROR = "error" |
| 26 | + |
| 27 | + |
| 28 | +class Priority(IntEnum): |
| 29 | + """Integer-based enum for task priorities.""" |
| 30 | + |
| 31 | + LOW = 0 |
| 32 | + MEDIUM = 5 |
| 33 | + HIGH = 10 |
| 34 | + |
| 35 | + |
| 36 | +class Permission(IntFlag): |
| 37 | + """Integer flag enum for permission bits.""" |
| 38 | + |
| 39 | + NONE = 0 |
| 40 | + READ = 1 |
| 41 | + WRITE = 2 |
| 42 | + EXECUTE = 4 |
| 43 | + ALL = READ | WRITE | EXECUTE |
| 44 | + |
| 45 | + |
| 46 | +class StandardEnum(enum.Enum): |
| 47 | + """Standard enum for testing.""" |
| 48 | + |
| 49 | + OPTION_A = 1 |
| 50 | + OPTION_B = 2 |
| 51 | + |
| 52 | + |
| 53 | +class TestStrEnumAutoWrapping: |
| 54 | + """Test that StrEnum instances are automatically wrapped.""" |
| 55 | + |
| 56 | + def test_strenum_auto_wraps_to_config_strenum(self, tmp_path: Path) -> None: |
| 57 | + """Test that Config(StrEnum.value) auto-wraps to Config(ConfigStrEnum(StrEnum.value)).""" |
| 58 | + config_file = tmp_path / "config.ini" |
| 59 | + config_file.write_text("") |
| 60 | + |
| 61 | + class StrEnumConfig(ConfigBase): |
| 62 | + pass |
| 63 | + |
| 64 | + StrEnumConfig.set_parser(IniParser()) |
| 65 | + StrEnumConfig.set_file(config_file) |
| 66 | + StrEnumConfig._has_read_config = False |
| 67 | + |
| 68 | + class AppConfig: |
| 69 | + log_level = StrEnumConfig(LogLevel.INFO) |
| 70 | + |
| 71 | + descriptor = AppConfig.__dict__["log_level"] |
| 72 | + assert isinstance(descriptor._data_type, ConfigStrEnum) |
| 73 | + assert descriptor._data_type.value == LogLevel.INFO |
| 74 | + |
| 75 | + |
| 76 | +class TestIntEnumAutoWrapping: |
| 77 | + """Test that IntEnum instances are automatically wrapped.""" |
| 78 | + |
| 79 | + def test_intenum_auto_wraps_to_config_intenum(self, tmp_path: Path) -> None: |
| 80 | + """Test that Config(IntEnum.value) auto-wraps to Config(ConfigIntEnum(IntEnum.value)).""" |
| 81 | + config_file = tmp_path / "config.ini" |
| 82 | + config_file.write_text("") |
| 83 | + |
| 84 | + class IntEnumConfig(ConfigBase): |
| 85 | + pass |
| 86 | + |
| 87 | + IntEnumConfig.set_parser(IniParser()) |
| 88 | + IntEnumConfig.set_file(config_file) |
| 89 | + IntEnumConfig._has_read_config = False |
| 90 | + |
| 91 | + class AppConfig: |
| 92 | + priority = IntEnumConfig(Priority.MEDIUM) |
| 93 | + |
| 94 | + descriptor = AppConfig.__dict__["priority"] |
| 95 | + assert isinstance(descriptor._data_type, ConfigIntEnum) |
| 96 | + assert descriptor._data_type.value == Priority.MEDIUM |
| 97 | + |
| 98 | + |
| 99 | +class TestIntFlagAutoWrapping: |
| 100 | + """Test that IntFlag instances are automatically wrapped.""" |
| 101 | + |
| 102 | + def test_intflag_auto_wraps_to_config_intflag(self, tmp_path: Path) -> None: |
| 103 | + """Test that Config(IntFlag.value) auto-wraps to Config(ConfigIntFlag(IntFlag.value)).""" |
| 104 | + config_file = tmp_path / "config.ini" |
| 105 | + config_file.write_text("") |
| 106 | + |
| 107 | + class IntFlagConfig(ConfigBase): |
| 108 | + pass |
| 109 | + |
| 110 | + IntFlagConfig.set_parser(IniParser()) |
| 111 | + IntFlagConfig.set_file(config_file) |
| 112 | + IntFlagConfig._has_read_config = False |
| 113 | + |
| 114 | + class AppConfig: |
| 115 | + perms = IntFlagConfig(Permission.READ) |
| 116 | + |
| 117 | + descriptor = AppConfig.__dict__["perms"] |
| 118 | + assert isinstance(descriptor._data_type, ConfigIntFlag) |
| 119 | + assert descriptor._data_type.value == Permission.READ |
| 120 | + |
| 121 | + |
| 122 | +class TestStandardEnumAutoWrapping: |
| 123 | + """Test that standard Enum instances are automatically wrapped.""" |
| 124 | + |
| 125 | + def test_standard_enum_auto_wraps_to_config_enum(self, tmp_path: Path) -> None: |
| 126 | + """Test that Config(Enum.value) auto-wraps to Config(ConfigEnum(Enum.value)).""" |
| 127 | + config_file = tmp_path / "config.ini" |
| 128 | + config_file.write_text("") |
| 129 | + |
| 130 | + class EnumConfig(ConfigBase): |
| 131 | + pass |
| 132 | + |
| 133 | + EnumConfig.set_parser(IniParser()) |
| 134 | + EnumConfig.set_file(config_file) |
| 135 | + EnumConfig._has_read_config = False |
| 136 | + |
| 137 | + class AppConfig: |
| 138 | + option = EnumConfig(StandardEnum.OPTION_A) |
| 139 | + |
| 140 | + descriptor = AppConfig.__dict__["option"] |
| 141 | + assert isinstance(descriptor._data_type, ConfigEnum) |
| 142 | + assert descriptor._data_type.value == StandardEnum.OPTION_A |
0 commit comments