Skip to content

Commit

Permalink
fix: check for CloudEvents returned from @event fn (#18)
Browse files Browse the repository at this point in the history
Check to see if a function decorated with @event returns a CloudEvent.
If so, just return it instead of creating a new event.

Signed-off-by: Lance Ball <lball@redhat.com>
  • Loading branch information
lance authored Apr 12, 2021
1 parent e4067a0 commit 02e8513
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions parliament/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def event_decorator(func):
@functools.wraps(func)
def event_wrapper(*args, **kwargs):
data = func(*args, **kwargs)
if isinstance(data, CloudEvent):
return data
attributes = {
"type": event_type,
"source": event_source
Expand Down
18 changes: 18 additions & 0 deletions tests/decorator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,21 @@ def f(data: str) -> str:
return data
ce = f("test value")
assert ce.data == "test value"


def test_handles_returned_events():
"""
Test that the @event decorator can detect when a function
has returned a CloudEvent, and simply pass it on.
"""
@event
def f() -> CloudEvent:
attributes = {
"type": "user.type",
"source": "/user/source"
}
return CloudEvent(attributes, "Hola!")
ce = f()
assert ce.data == "Hola!"
assert ce["type"] == "user.type"
assert ce["source"] == "/user/source"

0 comments on commit 02e8513

Please sign in to comment.