Skip to content

Instantly share code, notes, and snippets.

@Unit03 Unit03/event.py
Last active Oct 8, 2019

Embed
What would you like to do?
Event Sourcing - event code sample
class MyEvent:
def __init__(self, foo: int):
"""For issuing new events in the application."""
self.foo = foo
@classmethod
def unserialize(cls, payload: dict) -> "MyEvent":
"""To use when fetching an existing event from the event log."""
return cls(foo=payload["foo"])
def serialize(self) -> dict:
"""To use when storing a new event in the event log."""
return {"foo": self.foo}
def test_my_event_serialize():
event = MyEvent(1)
assert event.serialize() == {"foo": 1}
def test_my_event_unserialize():
assert MyEvent.unserialize({"foo": 1}) == MyEvent(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.