Last active
October 8, 2019 20:53
-
-
Save Unit03/89e8ca5f5e47582139078b0bce10524d to your computer and use it in GitHub Desktop.
Event Sourcing - event code sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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