|
1 | 1 | import datetime |
2 | 2 |
|
| 3 | +import pytest |
| 4 | + |
3 | 5 | from scim2_models import Address |
4 | 6 | from scim2_models import Email |
5 | 7 | from scim2_models import Im |
6 | 8 | from scim2_models import PhoneNumber |
7 | 9 | from scim2_models import Photo |
8 | 10 | from scim2_models import Reference |
9 | 11 | from scim2_models import User |
| 12 | +from scim2_models.attributes import ExtensibleStringEnum |
10 | 13 |
|
11 | 14 |
|
12 | 15 | def test_minimal_user(load_sample): |
@@ -124,3 +127,64 @@ def test_full_user(load_sample): |
124 | 127 | obj.meta.location |
125 | 128 | == "https://example.com/v2/Users/2819c223-7f76-453a-919d-413861904646" |
126 | 129 | ) |
| 130 | + |
| 131 | + |
| 132 | +def test_extensible_enum_canonical_values(): |
| 133 | + """Test that canonical enum values work as expected.""" |
| 134 | + |
| 135 | + class TestEnum(ExtensibleStringEnum): |
| 136 | + foo = "foo" |
| 137 | + bar = "bar" |
| 138 | + |
| 139 | + assert TestEnum.foo == "foo" |
| 140 | + assert TestEnum.bar == "bar" |
| 141 | + assert str(TestEnum.foo) == "foo" |
| 142 | + |
| 143 | + |
| 144 | +def test_extensible_enum_arbitrary_values(): |
| 145 | + """Test that arbitrary string values are accepted.""" |
| 146 | + |
| 147 | + class TestEnum(ExtensibleStringEnum): |
| 148 | + foo = "foo" |
| 149 | + bar = "bar" |
| 150 | + |
| 151 | + # Create instances with arbitrary values |
| 152 | + custom = TestEnum("custom_value") |
| 153 | + another = TestEnum("another_value") |
| 154 | + |
| 155 | + assert str(custom) == "custom_value" |
| 156 | + assert str(another) == "another_value" |
| 157 | + assert custom == "custom_value" |
| 158 | + assert another == "another_value" |
| 159 | + |
| 160 | + |
| 161 | +def test_extensible_enum_non_string_rejected(): |
| 162 | + """Test that non-string values are rejected.""" |
| 163 | + |
| 164 | + class TestEnum(ExtensibleStringEnum): |
| 165 | + foo = "foo" |
| 166 | + |
| 167 | + with pytest.raises(ValueError, match="is not a valid string value"): |
| 168 | + TestEnum(123) |
| 169 | + |
| 170 | + with pytest.raises(ValueError, match="is not a valid string value"): |
| 171 | + TestEnum(None) |
| 172 | + |
| 173 | + |
| 174 | +def test_complex_attribute_extensible_types(): |
| 175 | + """Test that complex attribute types support RFC 7643 extensibility.""" |
| 176 | + # Test with Email - canonical and arbitrary types |
| 177 | + email_canonical = Email(value="test@example.com", type=Email.Type.work) |
| 178 | + assert str(email_canonical.type) == "work" |
| 179 | + |
| 180 | + email_custom = Email(value="john.doe@example.com", type="company") # Issue #34 case |
| 181 | + assert str(email_custom.type) == "company" |
| 182 | + |
| 183 | + # Test serialization works correctly |
| 184 | + data = email_custom.model_dump() |
| 185 | + assert data["type"] == "company" |
| 186 | + |
| 187 | + # Test round-trip serialization |
| 188 | + restored = Email.model_validate(data) |
| 189 | + assert str(restored.type) == "company" |
| 190 | + assert restored.value == "john.doe@example.com" |
0 commit comments