Skip to content

Commit

Permalink
add Entity.__getitem__
Browse files Browse the repository at this point in the history
  • Loading branch information
leoschwarz committed Aug 12, 2024
1 parent 317ae7e commit 07c6668
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 4 additions & 0 deletions bfabric/entities/core/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def find_all(cls, ids: list[int], client: Bfabric) -> dict[int, Self]:
logger.warning(f"Only found {len(results)} out of {len(ids)}.")
return results

def __getitem__(self, key: str) -> Any:
"""Returns the value of a key in the data dictionary."""
return self.__data_dict[key]

def __repr__(self) -> str:
"""Returns the string representation of the workunit."""
return f"{self.__class__.__name__}({repr(self.__data_dict)}, client={repr(self.__client)})"
Expand Down
8 changes: 6 additions & 2 deletions bfabric/tests/unit/entities/core/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

@pytest.fixture
def mock_client(mocker):
client = mocker.Mock(spec=Bfabric)
return client
return mocker.Mock(spec=Bfabric)


@pytest.fixture
Expand Down Expand Up @@ -70,6 +69,11 @@ def test_find_all_when_not_all_found(mocker, mock_client) -> None:
mock_client.read.assert_called_once_with("test_endpoint", obj={"id": [1, 5]})


def test_get_item(mock_entity) -> None:
assert mock_entity["id"] == 1
assert mock_entity["name"] == "Test Entity"


def test_repr(mock_entity, mock_data_dict) -> None:
entity = Entity(mock_data_dict, None)
assert repr(entity) == "Entity({'id': 1, 'name': 'Test Entity'}, client=None)"
Expand Down
3 changes: 2 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ Versioning currently follows `X.Y.Z` where

- The `Bfabric` instance is now pickleable.
- Entities mapping:
- Add `Entity.id` and `Entity.web_url` properties.
- Add `Entity.__getitem__` to access fields from the data dictionary directly.
- More types and relationships
- Relationships defer imports to descriptor call, i.e. circular relationships are possible now.
- Add `Entity.id` and `Entity.web_url` properties.
- `HasOne` and `HasMany` allow defining `optional=True` to indicate fields which can be missing under some circumstances.

## \[1.13.4\] - 2024-08-05
Expand Down

0 comments on commit 07c6668

Please sign in to comment.