Skip to content

Commit

Permalink
feat: implement Attribute.get_attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
azmeuk committed Dec 9, 2024
1 parent ca2f011 commit d6795a2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
6 changes: 6 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

[0.2.12] - Unreleased
---------------------
Added
^^^^^
- Implement :meth:`Attribute.get_attribute <scim2_models.Attribute.get_attribute>`.

[0.2.11] - 2024-12-08
---------------------
Added
Expand Down
7 changes: 7 additions & 0 deletions scim2_models/rfc7643/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ def to_python(self) -> Optional[tuple[Any, Field]]:

return annotation, field

def get_attribute(self, attribute_name: str) -> Optional["Attribute"]:
"""Find an attribute by its name."""
for sub_attribute in self.sub_attributes or []:
if sub_attribute.name == attribute_name:
return sub_attribute
return None


class Schema(Resource):
schemas: Annotated[list[str], Required.true] = [
Expand Down
17 changes: 16 additions & 1 deletion tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_uri_ids():
Schema(id="invalid\nuri")


def test_get_attribute(load_sample):
def test_get_schema_attribute(load_sample):
"""Test the Schema.get_attribute method."""
payload = load_sample("rfc7643-8.7.1-schema-user.json")
schema = Schema.model_validate(payload)
Expand All @@ -98,3 +98,18 @@ def test_get_attribute(load_sample):
schema.get_attribute("userName").mutability = Mutability.read_only

assert schema.attributes[0].mutability == Mutability.read_only


def test_get_attribute_attribute(load_sample):
"""Test the Schema.get_attribute method."""
payload = load_sample("rfc7643-8.7.1-schema-group.json")
schema = Schema.model_validate(payload)
attribute = schema.get_attribute("members")

assert attribute.get_attribute("invalid") is None

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

assert attribute.sub_attributes[0].mutability == Mutability.read_only

0 comments on commit d6795a2

Please sign in to comment.