From 693501e5940aedc67b0b9537a14f357370999101 Mon Sep 17 00:00:00 2001 From: Curtis Mason Date: Thu, 13 Aug 2020 11:38:52 -0700 Subject: [PATCH 1/4] added tests to cloudevent eq Signed-off-by: Curtis Mason --- cloudevents/http/event.py | 6 ++ .../tests/test_http_cloudevent_overrides.py | 71 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 cloudevents/tests/test_http_cloudevent_overrides.py diff --git a/cloudevents/http/event.py b/cloudevents/http/event.py index a31b894b..42def3b3 100644 --- a/cloudevents/http/event.py +++ b/cloudevents/http/event.py @@ -68,6 +68,12 @@ 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): diff --git a/cloudevents/tests/test_http_cloudevent_overrides.py b/cloudevents/tests/test_http_cloudevent_overrides.py new file mode 100644 index 00000000..de6f8504 --- /dev/null +++ b/cloudevents/tests/test_http_cloudevent_overrides.py @@ -0,0 +1,71 @@ +from cloudevents.http import CloudEvent + +import pytest + + +@pytest.mark.parametrize("specversion", ["0.3", "1.0"]) +def test_http_cloudevent_equality(specversion): + attributes = { + "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": "", + "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 From a6714ed9d9ffd11ec9bbc508ceec160ecaac8d6b Mon Sep 17 00:00:00 2001 From: Curtis Mason Date: Thu, 13 Aug 2020 11:41:26 -0700 Subject: [PATCH 2/4] lint fix Signed-off-by: Curtis Mason --- cloudevents/http/event.py | 5 +---- .../tests/test_http_cloudevent_overrides.py | 18 +++++++++--------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/cloudevents/http/event.py b/cloudevents/http/event.py index 42def3b3..9cb4400e 100644 --- a/cloudevents/http/event.py +++ b/cloudevents/http/event.py @@ -69,10 +69,7 @@ def __init__( ) def __eq__(self, other): - return ( - self.data == other.data - and self._attributes == other._attributes - ) + return self.data == other.data and self._attributes == other._attributes # Data access is handled via `.data` member # Attribute access is managed via Mapping type diff --git a/cloudevents/tests/test_http_cloudevent_overrides.py b/cloudevents/tests/test_http_cloudevent_overrides.py index de6f8504..1babbe21 100644 --- a/cloudevents/tests/test_http_cloudevent_overrides.py +++ b/cloudevents/tests/test_http_cloudevent_overrides.py @@ -1,7 +1,7 @@ -from cloudevents.http import CloudEvent - import pytest +from cloudevents.http import CloudEvent + @pytest.mark.parametrize("specversion", ["0.3", "1.0"]) def test_http_cloudevent_equality(specversion): @@ -12,7 +12,7 @@ def test_http_cloudevent_equality(specversion): "time": "tomorrow", "type": "tests.cloudevents.override", "datacontenttype": "application/json", - "subject": "my-subject" + "subject": "my-subject", } data = '{"name":"john"}' event1 = CloudEvent(attributes, data) @@ -20,10 +20,10 @@ def test_http_cloudevent_equality(specversion): assert event1 == event2 # Test different attributes for key in attributes: - if key == 'specversion': + if key == "specversion": continue else: - attributes[key] = f'noise-{key}' + attributes[key] = f"noise-{key}" event3 = CloudEvent(attributes, data) event2 = CloudEvent(attributes, data) assert event2 == event3 @@ -46,7 +46,7 @@ def test_http_cloudevent_mutates_equality(specversion): "time": "tomorrow", "type": "tests.cloudevents.override", "datacontenttype": "application/json", - "subject": "my-subject" + "subject": "my-subject", } data = '{"name":"john"}' event1 = CloudEvent(attributes, data) @@ -56,11 +56,11 @@ def test_http_cloudevent_mutates_equality(specversion): assert event1 == event2 # Test different attributes for key in attributes: - if key == 'specversion': + if key == "specversion": continue else: - event2[key] = f'noise-{key}' - event3[key] = f'noise-{key}' + event2[key] = f"noise-{key}" + event3[key] = f"noise-{key}" assert event2 == event3 assert event1 != event2 and event3 != event1 From 5eabf741e0ff897b1f49fbb0ecf53272c8a0a917 Mon Sep 17 00:00:00 2001 From: Curtis Mason Date: Thu, 13 Aug 2020 11:42:58 -0700 Subject: [PATCH 3/4] modified changelog Signed-off-by: Curtis Mason --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa8f3a11..8a66cf3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] +### Added +- CloudEvent equality override ([#98]) + ## [1.0.0] ### Added - Added a user friendly CloudEvent class with data validation ([#36]) @@ -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 \ No newline at end of file From 27f7ea0b93998cc28b52c6568104bc176536c460 Mon Sep 17 00:00:00 2001 From: Curtis Mason Date: Thu, 13 Aug 2020 11:46:28 -0700 Subject: [PATCH 4/4] version bump Signed-off-by: Curtis Mason --- cloudevents/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudevents/__init__.py b/cloudevents/__init__.py index 5becc17c..5c4105cd 100644 --- a/cloudevents/__init__.py +++ b/cloudevents/__init__.py @@ -1 +1 @@ -__version__ = "1.0.0" +__version__ = "1.0.1"