-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Introduce `Metadata` Valueobject within events to store event metadata, instead of relying on metadata in Messages - Introduce `payload` property in Event to retrieve event payload / aggregate data. This is necessary because there wiil be three major parts to each event eventually - data (payload), metadata of event, and headers - Introduce a `from_events` factory method to encapsulate the aspect of initializing an aggregate from the first event, applying the event on it, and subsequently applying the rest of the events. This keeps the logic of aggregate initialization in a single place. - Pass `Event` object to `_apply` method in Event Sourced Aggregates, instead of a `Dict`. We were passing a Message payload, contents of which is not necessary for aggregate data attributes. - Clean up snapshot fetching and aggregate initialization because of the above changes
- Loading branch information
Showing
20 changed files
with
321 additions
and
75 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,5 +212,3 @@ nav: | |
- Community: | ||
- community/index.md | ||
- community/contributing.md | ||
extra_css: | ||
- stylesheets/extra.css |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from datetime import datetime | ||
from uuid import uuid4 | ||
|
||
import pytest | ||
|
||
from protean import BaseEvent, BaseEventSourcedAggregate | ||
from protean.fields import String | ||
from protean.fields.basic import Identifier | ||
|
||
|
||
class User(BaseEventSourcedAggregate): | ||
id = Identifier(identifier=True) | ||
email = String() | ||
name = String() | ||
|
||
def login(self): | ||
self.raise_(UserLoggedIn(user_id=self.id)) | ||
|
||
|
||
class UserLoggedIn(BaseEvent): | ||
user_id = Identifier(identifier=True) | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def register_elements(test_domain): | ||
test_domain.register(User) | ||
test_domain.register(UserLoggedIn, part_of=User) | ||
test_domain.init(traverse=False) | ||
|
||
|
||
def test_event_metadata(test_domain): | ||
user_id = str(uuid4()) | ||
user = User(id=user_id, email="<EMAIL>", name="<NAME>") | ||
|
||
user.login() | ||
|
||
assert len(user._events) == 1 | ||
|
||
event = user._events[0] | ||
assert event._metadata is not None | ||
|
||
assert event._metadata.kind == "EVENT" | ||
assert isinstance(event._metadata.timestamp, datetime) | ||
# assert event._metadata.id == f"test.user.v1.{user.user_id}.1" | ||
|
||
assert event.to_dict() == { | ||
"_metadata": { | ||
"kind": "EVENT", | ||
"timestamp": str(event._metadata.timestamp), | ||
}, | ||
"user_id": event.user_id, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from uuid import uuid4 | ||
|
||
import pytest | ||
|
||
from protean import BaseEvent, BaseEventSourcedAggregate | ||
from protean.fields import String | ||
from protean.fields.basic import Identifier | ||
|
||
|
||
class User(BaseEventSourcedAggregate): | ||
id = Identifier(identifier=True) | ||
email = String() | ||
name = String() | ||
|
||
def login(self): | ||
self.raise_(UserLoggedIn(user_id=self.id)) | ||
|
||
|
||
class UserLoggedIn(BaseEvent): | ||
user_id = Identifier(identifier=True) | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def register_elements(test_domain): | ||
test_domain.register(User) | ||
test_domain.register(UserLoggedIn, part_of=User) | ||
test_domain.init(traverse=False) | ||
|
||
|
||
def test_event_payload(): | ||
user_id = str(uuid4()) | ||
user = User(id=user_id, email="<EMAIL>", name="<NAME>") | ||
|
||
user.login() | ||
event = user._events[0] | ||
|
||
assert event.to_dict() == { | ||
"_metadata": { | ||
"kind": "EVENT", | ||
"timestamp": str(event._metadata.timestamp), | ||
}, | ||
"user_id": event.user_id, | ||
} | ||
|
||
assert event.payload == { | ||
"user_id": event.user_id, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,10 @@ class UserAdded(BaseEvent): | |
assert event.email_address == "[email protected]" | ||
|
||
assert event.to_dict() == { | ||
"_metadata": { | ||
"kind": "EVENT", | ||
"timestamp": str(event._metadata.timestamp), | ||
}, | ||
"email": { | ||
"address": "[email protected]", | ||
}, | ||
|
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
Oops, something went wrong.