Skip to content

Commit

Permalink
feat: implement Schema[attribute_name] to access schema attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Dec 11, 2024
1 parent 7755737 commit 8e30112
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Changelog
[0.3.0] - Unreleased
--------------------

Added
^^^^^
- :meth:`Attribute.get_attribute <scim2_models.Attribute.get_attribute>` can be called with brackets.

Changed
^^^^^^^
- Add a :paramref:`~scim2_models.BaseModel.model_validate.original`
Expand Down
12 changes: 12 additions & 0 deletions scim2_models/rfc7643/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,12 @@ def get_attribute(self, attribute_name: str) -> Optional["Attribute"]:
return sub_attribute
return None

def __getitem__(self, name):
"""Find an attribute by its name."""
if attribute := self.get_attribute(name):
return attribute
raise KeyError(f"This attribute has no '{name}' sub-attribute")


class Schema(Resource):
schemas: Annotated[list[str], Required.true] = [
Expand Down Expand Up @@ -280,3 +286,9 @@ def get_attribute(self, attribute_name: str) -> Optional[Attribute]:
if attribute.name == attribute_name:
return attribute
return None

def __getitem__(self, name):
"""Find an attribute by its name."""
if attribute := self.get_attribute(name):
return attribute
raise KeyError(f"This schema has no '{name}' attribute")
14 changes: 12 additions & 2 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,18 @@ def test_get_schema_attribute(load_sample):
payload = load_sample("rfc7643-8.7.1-schema-user.json")
schema = Schema.model_validate(payload)
assert schema.get_attribute("invalid") is None
with pytest.raises(KeyError):
schema["invalid"]

assert schema.attributes[0].name == "userName"
assert schema.attributes[0].mutability == Mutability.read_write
schema.get_attribute("userName").mutability = Mutability.read_only

schema.get_attribute("userName").mutability = Mutability.read_only
assert schema.attributes[0].mutability == Mutability.read_only

schema["userName"].mutability = Mutability.read_write
assert schema.attributes[0].mutability == Mutability.read_write


def test_get_attribute_attribute(load_sample):
"""Test the Schema.get_attribute method."""
Expand All @@ -107,9 +112,14 @@ def test_get_attribute_attribute(load_sample):
attribute = schema.get_attribute("members")

assert attribute.get_attribute("invalid") is None
with pytest.raises(KeyError):
attribute["invalid"]

assert attribute.sub_attributes[0].name == "value"
assert attribute.sub_attributes[0].mutability == Mutability.immutable
attribute.get_attribute("value").mutability = Mutability.read_only

attribute.get_attribute("value").mutability = Mutability.read_only
assert attribute.sub_attributes[0].mutability == Mutability.read_only

attribute["value"].mutability = Mutability.read_write
assert attribute.sub_attributes[0].mutability == Mutability.read_write

0 comments on commit 8e30112

Please sign in to comment.