Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CloudEvents equality override #98

Merged
merged 4 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.1]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a bug in that CloudEvents could not be compared, or a feature in providing a nice way to compare CloudEvents?

Copy link
Contributor Author

@cumason123 cumason123 Aug 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cloudevent1 == cloudevent2

would only test the memory address instead of an actual equality test because we forgot to override the __eq__ operator. I'd say both of the above are applicable :)

### Added
- CloudEvent equality override ([#98])

## [1.0.0]
### Added
- Added a user friendly CloudEvent class with data validation ([#36])
Expand Down Expand Up @@ -77,3 +81,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#36]: https://github.com/cloudevents/sdk-python/pull/36
[#43]: https://github.com/cloudevents/sdk-python/pull/43
[#47]: https://github.com/cloudevents/sdk-python/pull/47
[#98]: https://github.com/cloudevents/sdk-python/pull/98
2 changes: 1 addition & 1 deletion cloudevents/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.0.0"
__version__ = "1.0.1"
3 changes: 3 additions & 0 deletions cloudevents/http/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def __init__(
f"Missing required keys: {required_set - attributes.keys()}"
)

def __eq__(self, other):
return self.data == other.data and self._attributes == other._attributes

# Data access is handled via `.data` member
# Attribute access is managed via Mapping type
def __getitem__(self, key):
Expand Down
71 changes: 71 additions & 0 deletions cloudevents/tests/test_http_cloudevent_overrides.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import pytest

from cloudevents.http import CloudEvent


@pytest.mark.parametrize("specversion", ["0.3", "1.0"])
def test_http_cloudevent_equality(specversion):
attributes = {
"source": "<source>",
"specversion": specversion,
"id": "my-id",
"time": "tomorrow",
"type": "tests.cloudevents.override",
"datacontenttype": "application/json",
"subject": "my-subject",
}
data = '{"name":"john"}'
event1 = CloudEvent(attributes, data)
event2 = CloudEvent(attributes, data)
assert event1 == event2
# Test different attributes
for key in attributes:
if key == "specversion":
continue
else:
attributes[key] = f"noise-{key}"
event3 = CloudEvent(attributes, data)
event2 = CloudEvent(attributes, data)
assert event2 == event3
assert event1 != event2 and event3 != event1

# Test different data
data = '{"name":"paul"}'
event3 = CloudEvent(attributes, data)
event2 = CloudEvent(attributes, data)
assert event2 == event3
assert event1 != event2 and event3 != event1


@pytest.mark.parametrize("specversion", ["0.3", "1.0"])
def test_http_cloudevent_mutates_equality(specversion):
attributes = {
"source": "<source>",
"specversion": specversion,
"id": "my-id",
"time": "tomorrow",
"type": "tests.cloudevents.override",
"datacontenttype": "application/json",
"subject": "my-subject",
}
data = '{"name":"john"}'
event1 = CloudEvent(attributes, data)
event2 = CloudEvent(attributes, data)
event3 = CloudEvent(attributes, data)

assert event1 == event2
# Test different attributes
for key in attributes:
if key == "specversion":
continue
else:
event2[key] = f"noise-{key}"
event3[key] = f"noise-{key}"
assert event2 == event3
assert event1 != event2 and event3 != event1

# Test different data
event2.data = '{"name":"paul"}'
event3.data = '{"name":"paul"}'
assert event2 == event3
assert event1 != event2 and event3 != event1