-
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.
Aggregates can now registes for fact events and have them generated automatically whenever they are changed. Also: - Allow Value Objects to hold all kinds of fields except associations
- Loading branch information
Showing
22 changed files
with
782 additions
and
13 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
File renamed without changes.
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,40 @@ | ||
import pytest | ||
|
||
from protean import BaseAggregate, BaseEntity | ||
from protean.fields import HasOne, String | ||
from protean.utils.mixins import Message | ||
|
||
|
||
class Account(BaseEntity): | ||
password_hash = String(max_length=512) | ||
|
||
|
||
class User(BaseAggregate): | ||
name = String(max_length=50, required=True) | ||
email = String(required=True) | ||
status = String(choices=["ACTIVE", "ARCHIVED"]) | ||
|
||
account = HasOne(Account) | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def register_elements(test_domain): | ||
test_domain.register(User, fact_events=True) | ||
test_domain.register(Account, part_of=User) | ||
test_domain.init(traverse=False) | ||
|
||
|
||
def test_generation_of_first_fact_event_on_persistence(test_domain): | ||
user = User(name="John Doe", email="[email protected]") | ||
test_domain.repository_for(User).add(user) | ||
|
||
# Read event from event store | ||
event_messages = test_domain.event_store.store.read(f"user-{user.id}") | ||
assert len(event_messages) == 1 | ||
|
||
# Deserialize event | ||
event = Message.to_object(event_messages[0]) | ||
assert event is not None | ||
assert event.__class__.__name__ == "UserFactEvent" | ||
assert event.name == "John Doe" | ||
assert event.email == "[email protected]" |
57 changes: 57 additions & 0 deletions
57
tests/aggregate/fact_event_class_generation/test_aggregate_with_has_many.py
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,57 @@ | ||
import pytest | ||
|
||
from protean import BaseAggregate, BaseEntity, BaseEvent | ||
from protean.core.aggregate import element_to_fact_event | ||
from protean.fields import Date, HasMany, List, String, ValueObject | ||
from protean.reflection import declared_fields | ||
|
||
|
||
class Customer(BaseAggregate): | ||
name = String(max_length=50) | ||
orders = HasMany("Order") | ||
|
||
|
||
class Order(BaseEntity): | ||
ordered_on = Date() | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def register_elements(test_domain): | ||
test_domain.register(Customer) | ||
test_domain.register(Order, part_of=Customer) | ||
test_domain.init(traverse=False) | ||
|
||
|
||
@pytest.fixture | ||
def event_cls(): | ||
return element_to_fact_event(Customer) | ||
|
||
|
||
def test_fact_event_class_generation(event_cls): | ||
assert event_cls.__name__ == "CustomerFactEvent" | ||
assert issubclass(event_cls, BaseEvent) | ||
assert len(declared_fields(event_cls)) == 3 | ||
|
||
assert all( | ||
field_name in declared_fields(event_cls) | ||
for field_name in ["name", "orders", "id"] | ||
) | ||
|
||
|
||
def test_orders_is_a_list_of_value_objects(event_cls): | ||
orders_field = declared_fields(event_cls)["orders"] | ||
|
||
assert isinstance(orders_field, List) | ||
assert isinstance(orders_field.content_type, ValueObject) | ||
assert orders_field.content_type._value_object_cls.__name__ == "OrderValueObject" | ||
|
||
|
||
def test_order_value_object_fields(event_cls): | ||
orders_field = declared_fields(event_cls)["orders"] | ||
order_vo_cls = orders_field.content_type._value_object_cls | ||
|
||
assert len(declared_fields(order_vo_cls)) == 2 | ||
assert all( | ||
field_name in declared_fields(order_vo_cls) | ||
for field_name in ["ordered_on", "id"] | ||
) |
92 changes: 92 additions & 0 deletions
92
tests/aggregate/fact_event_class_generation/test_aggregate_with_has_many_with_has_one.py
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,92 @@ | ||
import pytest | ||
|
||
from protean import BaseAggregate, BaseEntity, BaseEvent, BaseValueObject | ||
from protean.core.aggregate import element_to_fact_event | ||
from protean.fields import HasMany, HasOne, Integer, List, String, ValueObject | ||
from protean.reflection import declared_fields | ||
|
||
|
||
class University(BaseAggregate): | ||
name = String(max_length=50) | ||
departments = HasMany("Department") | ||
|
||
|
||
class Department(BaseEntity): | ||
name = String(max_length=50) | ||
dean = HasOne("Dean") | ||
|
||
|
||
class Dean(BaseEntity): | ||
name = String(max_length=50) | ||
age = Integer(min_value=21) | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def register_elements(test_domain): | ||
test_domain.register(University) | ||
test_domain.register(Department, part_of=University) | ||
test_domain.register(Dean, part_of=Department) | ||
test_domain.init(traverse=False) | ||
|
||
|
||
@pytest.fixture | ||
def event_cls(): | ||
return element_to_fact_event(University) | ||
|
||
|
||
def test_fact_event_class_generation(event_cls): | ||
assert event_cls.__name__ == "UniversityFactEvent" | ||
assert issubclass(event_cls, BaseEvent) | ||
assert len(declared_fields(event_cls)) == 3 | ||
|
||
assert all( | ||
field_name in declared_fields(event_cls) | ||
for field_name in ["name", "departments", "id"] | ||
) | ||
|
||
|
||
def test_departments_is_a_list_of_value_objects(event_cls): | ||
departments_field = declared_fields(event_cls)["departments"] | ||
|
||
assert isinstance(departments_field, List) | ||
assert isinstance(departments_field.content_type, ValueObject) | ||
assert ( | ||
departments_field.content_type._value_object_cls.__name__ | ||
== "DepartmentValueObject" | ||
) | ||
|
||
|
||
def test_dean_is_a_value_object(event_cls): | ||
departments_field = declared_fields(event_cls)["departments"] | ||
dean_field = declared_fields(departments_field.content_type._value_object_cls)[ | ||
"dean" | ||
] | ||
dean_vo_cls = dean_field._value_object_cls | ||
|
||
assert issubclass(dean_vo_cls, BaseValueObject) | ||
assert dean_vo_cls.__name__ == "DeanValueObject" | ||
|
||
|
||
def test_department_value_object_fields(event_cls): | ||
departments_field = declared_fields(event_cls)["departments"] | ||
department_vo_cls = departments_field.content_type._value_object_cls | ||
|
||
assert len(declared_fields(department_vo_cls)) == 3 | ||
assert all( | ||
field_name in declared_fields(department_vo_cls) | ||
for field_name in ["name", "dean", "id"] | ||
) | ||
|
||
|
||
def test_dean_value_object_fields(event_cls): | ||
departments_field = declared_fields(event_cls)["departments"] | ||
dean_field = declared_fields(departments_field.content_type._value_object_cls)[ | ||
"dean" | ||
] | ||
dean_vo_cls = dean_field._value_object_cls | ||
|
||
assert len(declared_fields(dean_vo_cls)) == 3 | ||
assert all( | ||
field_name in declared_fields(dean_vo_cls) | ||
for field_name in ["name", "age", "id"] | ||
) |
Oops, something went wrong.