Skip to content

Commit

Permalink
fix __eq__ operator raises attribute error on non-cloudevent values (#…
Browse files Browse the repository at this point in the history
…172)

* fix: non-cloudevents values must not equal to cloudevents values (#171)

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* test: refactor move fixtures to beginning

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* test: cloudevent equality bug regression (#171)

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* style: remove redundent else

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* test: remove redundent test

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* test: refactor non_cloudevent_value into a parameterization

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* docs: update changelog

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* docs: fix bad merge

Signed-off-by: Alexander Tkachev <sasha64sasha@gmail.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
sasha-tkachev and pre-commit-ci[bot] authored Jul 13, 2022
1 parent f39b964 commit ad111ae
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `.get` accessor for even properties ([#165])
- Added type information for all event member functions ([#173])

### Fixed
- Fixed event `__eq__` operator raising `AttributeError` on non-CloudEvent values ([#172])

### Changed
- Code quality and styling tooling is unified and configs compatibility is ensured ([#167])
- CI configurations updated and added macOS and Windows tests ([#169])
Expand All @@ -18,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed
- `docs` folder and related unused tooling ([#168])


## [1.3.0] — 2022-09-07
### Added
- Python 3.9 support ([#144])
Expand Down Expand Up @@ -156,4 +160,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#168]: https://github.com/cloudevents/sdk-python/pull/168
[#169]: https://github.com/cloudevents/sdk-python/pull/169
[#170]: https://github.com/cloudevents/sdk-python/pull/170
[#172]: https://github.com/cloudevents/sdk-python/pull/172
[#173]: https://github.com/cloudevents/sdk-python/pull/173
4 changes: 3 additions & 1 deletion cloudevents/http/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def __init__(self, attributes: typing.Dict[str, str], data: typing.Any = None):
)

def __eq__(self, other: typing.Any) -> bool:
return self.data == other.data and self._attributes == other._attributes
if isinstance(other, CloudEvent):
return self.data == other.data and self._attributes == other._attributes
return False

# Data access is handled via `.data` member
# Attribute access is managed via Mapping type
Expand Down
39 changes: 27 additions & 12 deletions cloudevents/tests/test_http_cloudevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ def your_dummy_data():
return '{"name":"paul"}'


@pytest.fixture()
def dummy_event(dummy_attributes, my_dummy_data):
return CloudEvent(attributes=dummy_attributes, data=my_dummy_data)


@pytest.fixture()
def non_exiting_attribute_name(dummy_event):
result = "nonexisting"
assert result not in dummy_event
return result


def test_http_cloudevent_equality(dummy_attributes, my_dummy_data, your_dummy_data):
data = my_dummy_data
event1 = CloudEvent(dummy_attributes, data)
Expand All @@ -71,6 +83,21 @@ def test_http_cloudevent_equality(dummy_attributes, my_dummy_data, your_dummy_da
assert event1 != event2 and event3 != event1


@pytest.mark.parametrize(
"non_cloudevent_value",
(
1,
None,
object(),
"Hello World",
),
)
def test_http_cloudevent_must_not_equal_to_non_cloudevent_value(
dummy_event, non_cloudevent_value
):
assert not dummy_event == non_cloudevent_value


def test_http_cloudevent_mutates_equality(
dummy_attributes, my_dummy_data, your_dummy_data
):
Expand Down Expand Up @@ -145,18 +172,6 @@ def test_none_json_or_string():
assert _json_or_string(None) is None


@pytest.fixture()
def dummy_event(dummy_attributes, my_dummy_data):
return CloudEvent(attributes=dummy_attributes, data=my_dummy_data)


@pytest.fixture()
def non_exiting_attribute_name(dummy_event):
result = "nonexisting"
assert result not in dummy_event
return result


def test_get_operation_on_non_existing_attribute_must_not_raise_exception(
dummy_event, non_exiting_attribute_name
):
Expand Down

0 comments on commit ad111ae

Please sign in to comment.