-
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.
Run all invariants on init and attribute changes
When an aggregate or entity object is initialized, or its attribute value is changed, this commit ensures that all `clean()` methods (which in turn run all invariant validations) are run across the aggregate. This commit also adds `atomic_change` context manager for scenarios where multiple aggregates in an aggregate need to be changed before it becomes valid again.
- Loading branch information
Showing
16 changed files
with
459 additions
and
47 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
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,45 @@ | ||
"""Test `atomic_change` context manager""" | ||
|
||
import pytest | ||
|
||
from protean import BaseAggregate, atomic_change, invariant | ||
from protean.fields import Integer | ||
from protean.exceptions import ValidationError | ||
|
||
|
||
class TestAtomicChange: | ||
def test_atomic_change_context_manager(self): | ||
class TestAggregate(BaseAggregate): | ||
pass | ||
|
||
aggregate = TestAggregate() | ||
|
||
with atomic_change(aggregate): | ||
assert aggregate._disable_invariant_checks is True | ||
|
||
assert aggregate._disable_invariant_checks is False | ||
|
||
def test_clean_is_not_triggered_within_context_manager(self, test_domain): | ||
class TestAggregate(BaseAggregate): | ||
value1 = Integer() | ||
value2 = Integer() | ||
|
||
@invariant | ||
def raise_error(self): | ||
if self.value2 != self.value1 + 1: | ||
raise ValidationError({"_entity": ["Invariant error"]}) | ||
|
||
test_domain.register(TestAggregate) | ||
test_domain.init(traverse=False) | ||
|
||
aggregate = TestAggregate(value1=1, value2=2) | ||
|
||
# This raises an error because of the invariant | ||
with pytest.raises(ValidationError): | ||
aggregate.value1 = 2 | ||
aggregate.value2 = 3 | ||
|
||
# This should not raise an error because of the context manager | ||
with atomic_change(aggregate): | ||
aggregate.value1 = 2 | ||
aggregate.value2 = 3 |
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
Oops, something went wrong.