Skip to content

fix __eq__ operator raises attribute error on non-cloudevent values #172

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

Merged
merged 13 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 5 additions & 2 deletions cloudevents/http/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ def __init__(self, attributes: typing.Dict[str, str], data: typing.Any = None):
f"Missing required keys: {required_set - self._attributes.keys()}"
)

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

# Data access is handled via `.data` member
# Attribute access is managed via Mapping type
Expand Down
46 changes: 34 additions & 12 deletions cloudevents/tests/test_http_cloudevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ 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


@pytest.fixture(
params=(
1,
None,
object(),
"Hello World",
)
)
def non_cloudevent_value(request):
return request.param


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


def test_http_cloudevent_equality_must_not_throw(dummy_event, non_cloudevent_value):
assert isinstance(dummy_event == non_cloudevent_value, bool)


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 @@ -131,18 +165,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