-
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.
`id` serves as the primary identifier of an event. It's format is <domain-name>.<event-class-name>.<event-version>.<aggregate-id>.<aggregate-version> This is applicable for Event Sourced Aggregates, and may undergo slight modifications for regular Aggregates. The changes in tests are largely because events were being added to the event store directly. We cannot add plain events anymore because their metadata is being enriched in the Aggregate's `raise_` method.
- Loading branch information
Showing
26 changed files
with
469 additions
and
328 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
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
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,37 @@ | ||
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_is_generated_with_unique_id(): | ||
identifier = str(uuid4()) | ||
user = User(id=identifier, email="[email protected]", name="Foo Bar") | ||
user.login() | ||
|
||
event = user._events[0] | ||
assert event._metadata.id == f"Test.User.v1.{identifier}.0" | ||
assert event._metadata.sequence_id == 0 |
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 |
---|---|---|
|
@@ -26,17 +26,21 @@ class UserAdded(BaseEvent): | |
assert event.email == Email(address="[email protected]") | ||
assert event.email_address == "[email protected]" | ||
|
||
assert event.to_dict() == { | ||
"_metadata": { | ||
"kind": "EVENT", | ||
"timestamp": str(event._metadata.timestamp), | ||
"version": "v1", | ||
}, | ||
"email": { | ||
"address": "[email protected]", | ||
}, | ||
"name": "John Doe", | ||
} | ||
assert ( | ||
event.to_dict() | ||
== { | ||
"_metadata": { | ||
"id": None, # ID is none because the event is not being raised in the proper way (with `_raise`) | ||
"timestamp": str(event._metadata.timestamp), | ||
"version": "v1", | ||
"sequence_id": None, # Sequence is unknown because event is not being raised as part of an aggregate | ||
}, | ||
"email": { | ||
"address": "[email protected]", | ||
}, | ||
"name": "John Doe", | ||
} | ||
) | ||
|
||
def test_that_domain_event_can_be_reconstructed_from_dict_enclosing_vo(self): | ||
class Email(BaseValueObject): | ||
|
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,26 @@ | ||
import pytest | ||
|
||
from protean import BaseEventSourcedAggregate | ||
from protean.exceptions import IncorrectUsageError | ||
from protean.fields import Identifier, String | ||
|
||
|
||
class User(BaseEventSourcedAggregate): | ||
id = Identifier(identifier=True) | ||
email = String() | ||
name = String() | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def register_elements(test_domain): | ||
test_domain.register(User) | ||
test_domain.init(traverse=False) | ||
|
||
|
||
def test_exception_on_empty_aggregate_object(test_domain): | ||
with pytest.raises(IncorrectUsageError) as exception: | ||
test_domain.repository_for(User).add(None) | ||
|
||
assert exception.value.messages == { | ||
"_entity": ["Aggregate object to persist is invalid"] | ||
} |
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 |
---|---|---|
|
@@ -30,6 +30,20 @@ class User(BaseEventSourcedAggregate): | |
name = String() | ||
status = String(default="INACTIVE") | ||
|
||
@classmethod | ||
def register(cls, id, email, name): | ||
user = User(id=id, email=email, name=name) | ||
user.raise_(Registered(id=id, email=email, name=name)) | ||
|
||
return user | ||
|
||
def activate(self): | ||
self.raise_(Activated(id=self.id)) | ||
|
||
def rename(self, name): | ||
self.name = name | ||
self.raise_(Renamed(id=self.id, name=name)) | ||
|
||
@apply | ||
def registered(self, _: Registered) -> None: | ||
self.status = "INACTIVE" | ||
|
@@ -55,9 +69,8 @@ def register_elements(test_domain): | |
@pytest.mark.eventstore | ||
def test_appending_messages_to_aggregate(test_domain): | ||
identifier = str(uuid4()) | ||
event = Registered(id=identifier, email="[email protected]") | ||
user = User(id=identifier, email="[email protected]") | ||
test_domain.event_store.store.append_aggregate_event(user, event) | ||
user = User.register(id=identifier, email="[email protected]", name="John Doe") | ||
test_domain.event_store.store.append_aggregate_event(user, user._events[0]) | ||
|
||
messages = test_domain.event_store.store._read("user") | ||
|
||
|
@@ -67,22 +80,20 @@ def test_appending_messages_to_aggregate(test_domain): | |
@pytest.mark.eventstore | ||
def test_version_increment_on_new_event(test_domain): | ||
identifier = str(uuid4()) | ||
event1 = Registered(id=identifier, email="[email protected]") | ||
|
||
user = User(**event1.payload) | ||
test_domain.event_store.store.append_aggregate_event(user, event1) | ||
user = User.register(id=identifier, email="[email protected]", name="John Doe") | ||
test_domain.event_store.store.append_aggregate_event(user, user._events[0]) | ||
|
||
events = test_domain.event_store.store._read(f"user-{identifier}") | ||
assert events[0]["position"] == 0 | ||
|
||
event2 = Activated(id=identifier) | ||
test_domain.event_store.store.append_aggregate_event(user, event2) | ||
user.activate() | ||
test_domain.event_store.store.append_aggregate_event(user, user._events[1]) | ||
|
||
events = test_domain.event_store.store._read(f"user-{identifier}") | ||
assert events[-1]["position"] == 1 | ||
|
||
event3 = Renamed(id=identifier, name="Jane Doe") | ||
test_domain.event_store.store.append_aggregate_event(user, event3) | ||
user.rename(name="John Doe 2") | ||
test_domain.event_store.store.append_aggregate_event(user, user._events[2]) | ||
|
||
events = test_domain.event_store.store._read(f"user-{identifier}") | ||
assert events[-1]["position"] == 2 |
Oops, something went wrong.