|  | 
|  | 1 | +# SPDX-FileCopyrightText: 2024-present Datadog, Inc. <dev@datadoghq.com> | 
|  | 2 | +# | 
|  | 3 | +# SPDX-License-Identifier: MIT | 
|  | 4 | +from __future__ import annotations | 
|  | 5 | + | 
|  | 6 | +import pytest | 
|  | 7 | + | 
|  | 8 | +from dda.utils.enums import AliasedStrEnum | 
|  | 9 | + | 
|  | 10 | + | 
|  | 11 | +class ExampleEnum(AliasedStrEnum): | 
|  | 12 | +    """Test enum for testing AliasedStrEnum functionality.""" | 
|  | 13 | + | 
|  | 14 | +    LINUX = "linux" | 
|  | 15 | +    WINDOWS = ("windows", "nt", "win") | 
|  | 16 | +    MACOS = ("macos", "darwin", "osx") | 
|  | 17 | + | 
|  | 18 | + | 
|  | 19 | +class TestAliasedStrEnum: | 
|  | 20 | +    def test_canonical_value_access(self): | 
|  | 21 | +        """Test that canonical values work correctly.""" | 
|  | 22 | +        assert ExampleEnum.LINUX == "linux" | 
|  | 23 | +        assert ExampleEnum.WINDOWS == "windows" | 
|  | 24 | +        assert ExampleEnum.MACOS == "macos" | 
|  | 25 | + | 
|  | 26 | +    def test_alias_equality(self): | 
|  | 27 | +        """Test that alias values are equal to the enum member.""" | 
|  | 28 | +        assert ExampleEnum.WINDOWS == "windows" | 
|  | 29 | +        assert ExampleEnum.WINDOWS == "nt" | 
|  | 30 | +        assert ExampleEnum.WINDOWS == "win" | 
|  | 31 | + | 
|  | 32 | +        assert ExampleEnum.MACOS == "macos" | 
|  | 33 | +        assert ExampleEnum.MACOS == "darwin" | 
|  | 34 | +        assert ExampleEnum.MACOS == "osx" | 
|  | 35 | + | 
|  | 36 | +    def test_alias_inequality(self): | 
|  | 37 | +        """Test that non-alias values are not equal.""" | 
|  | 38 | +        assert ExampleEnum.WINDOWS != "linux" | 
|  | 39 | +        assert ExampleEnum.LINUX != "windows" | 
|  | 40 | +        assert ExampleEnum.MACOS != "linux" | 
|  | 41 | + | 
|  | 42 | +    def test_instantiation_by_canonical_value(self): | 
|  | 43 | +        """Test instantiation using canonical values.""" | 
|  | 44 | +        assert ExampleEnum("linux") is ExampleEnum.LINUX | 
|  | 45 | +        assert ExampleEnum("windows") is ExampleEnum.WINDOWS | 
|  | 46 | +        assert ExampleEnum("macos") is ExampleEnum.MACOS | 
|  | 47 | + | 
|  | 48 | +    def test_instantiation_by_alias(self): | 
|  | 49 | +        """Test instantiation using alias values.""" | 
|  | 50 | +        assert ExampleEnum("nt") is ExampleEnum.WINDOWS | 
|  | 51 | +        assert ExampleEnum("win") is ExampleEnum.WINDOWS | 
|  | 52 | +        assert ExampleEnum("darwin") is ExampleEnum.MACOS | 
|  | 53 | +        assert ExampleEnum("osx") is ExampleEnum.MACOS | 
|  | 54 | + | 
|  | 55 | +    def test_instantiation_invalid_value(self): | 
|  | 56 | +        """Test that invalid values raise ValueError.""" | 
|  | 57 | +        with pytest.raises(match="'bsd' is not a valid TestOS"): | 
|  | 58 | +            ExampleEnum("bsd") | 
|  | 59 | +        with pytest.raises(match="'invalid' is not a valid TestOS"): | 
|  | 60 | +            ExampleEnum("invalid") | 
|  | 61 | + | 
|  | 62 | +    def test_name_access(self): | 
|  | 63 | +        """Test that name property returns the enum member name.""" | 
|  | 64 | +        assert ExampleEnum.LINUX.name == "LINUX" | 
|  | 65 | +        assert ExampleEnum.WINDOWS.name == "WINDOWS" | 
|  | 66 | +        assert ExampleEnum.MACOS.name == "MACOS" | 
|  | 67 | + | 
|  | 68 | +    def test_value_property(self): | 
|  | 69 | +        """Test that value property returns the canonical value.""" | 
|  | 70 | +        assert ExampleEnum.LINUX.value == "linux" | 
|  | 71 | +        assert ExampleEnum.WINDOWS.value == "windows" | 
|  | 72 | +        assert ExampleEnum.MACOS.value == "macos" | 
|  | 73 | + | 
|  | 74 | +    def test_aliases_property(self): | 
|  | 75 | +        """Test that aliases property returns the set of non-canonical values.""" | 
|  | 76 | +        assert ExampleEnum.LINUX.aliases == set() | 
|  | 77 | +        assert ExampleEnum.WINDOWS.aliases == {"nt", "win"} | 
|  | 78 | +        assert ExampleEnum.MACOS.aliases == {"darwin", "osx"} | 
|  | 79 | + | 
|  | 80 | +    def test_values_property(self): | 
|  | 81 | +        """Test that values property returns all values (canonical + aliases).""" | 
|  | 82 | +        assert ExampleEnum.LINUX.values == {"linux"} | 
|  | 83 | +        assert ExampleEnum.WINDOWS.values == {"windows", "nt", "win"} | 
|  | 84 | +        assert ExampleEnum.MACOS.values == {"macos", "darwin", "osx"} | 
|  | 85 | + | 
|  | 86 | +    def test_str_representation(self): | 
|  | 87 | +        """Test string representation returns canonical value.""" | 
|  | 88 | +        assert str(ExampleEnum.LINUX) == "linux" | 
|  | 89 | +        assert str(ExampleEnum.WINDOWS) == "windows" | 
|  | 90 | +        assert str(ExampleEnum.MACOS) == "macos" | 
|  | 91 | + | 
|  | 92 | +    def test_repr_representation(self): | 
|  | 93 | +        """Test repr representation includes aliases.""" | 
|  | 94 | +        assert repr(ExampleEnum.LINUX) == "<TestOS.LINUX: 'linux'>" | 
|  | 95 | + | 
|  | 96 | +        # For members with aliases, check that aliases are included | 
|  | 97 | +        assert repr(ExampleEnum.WINDOWS) == "<TestOS.WINDOWS: 'windows' ('nt', 'win')>" | 
|  | 98 | +        assert repr(ExampleEnum.MACOS) == "<TestOS.MACOS: 'macos' ('darwin', 'osx')>" | 
|  | 99 | + | 
|  | 100 | +    def test_containment(self): | 
|  | 101 | +        """Test containment operator with alias values.""" | 
|  | 102 | +        assert "linux" in ExampleEnum.LINUX | 
|  | 103 | +        assert "windows" in ExampleEnum.WINDOWS | 
|  | 104 | +        assert "nt" in ExampleEnum.WINDOWS | 
|  | 105 | +        assert "win" in ExampleEnum.WINDOWS | 
|  | 106 | +        assert "darwin" in ExampleEnum.MACOS | 
|  | 107 | +        assert "osx" in ExampleEnum.MACOS | 
|  | 108 | + | 
|  | 109 | +        assert "bsd" not in ExampleEnum.LINUX | 
|  | 110 | +        assert "linux" not in ExampleEnum.WINDOWS | 
|  | 111 | + | 
|  | 112 | +    def test_hash_consistency(self): | 
|  | 113 | +        """Test that hash is consistent for the same enum member.""" | 
|  | 114 | +        assert hash(ExampleEnum.LINUX) == hash(ExampleEnum.LINUX) | 
|  | 115 | +        assert hash(ExampleEnum.WINDOWS) == hash(ExampleEnum.WINDOWS) | 
|  | 116 | + | 
|  | 117 | +        # Hash should be based on canonical value | 
|  | 118 | +        assert hash(ExampleEnum.WINDOWS) == hash("windows") | 
|  | 119 | + | 
|  | 120 | +    def test_member_access_by_name(self): | 
|  | 121 | +        """Test accessing members by name using bracket notation.""" | 
|  | 122 | +        assert ExampleEnum["LINUX"] is ExampleEnum.LINUX | 
|  | 123 | +        assert ExampleEnum["WINDOWS"] is ExampleEnum.WINDOWS | 
|  | 124 | +        assert ExampleEnum["MACOS"] is ExampleEnum.MACOS | 
|  | 125 | + | 
|  | 126 | +    def test_member_access_by_alias_fails(self): | 
|  | 127 | +        """Test that accessing by alias value using bracket notation fails.""" | 
|  | 128 | +        with pytest.raises(KeyError): | 
|  | 129 | +            ExampleEnum["nt"] | 
|  | 130 | +        with pytest.raises(KeyError): | 
|  | 131 | +            ExampleEnum["darwin"] | 
|  | 132 | + | 
|  | 133 | +    def test_iteration(self): | 
|  | 134 | +        """Test that enum can be iterated over.""" | 
|  | 135 | +        members = list(ExampleEnum) | 
|  | 136 | +        assert len(members) == 3 | 
|  | 137 | +        assert ExampleEnum.LINUX in members | 
|  | 138 | +        assert ExampleEnum.WINDOWS in members | 
|  | 139 | +        assert ExampleEnum.MACOS in members | 
|  | 140 | + | 
|  | 141 | +        members2 = [] | 
|  | 142 | +        for member in ExampleEnum: | 
|  | 143 | +            members2.append(member)  # noqa: PERF402 | 
|  | 144 | +        assert members == members2 | 
0 commit comments