Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Check the repo now raises a KeyError.
self.assertRaises(KeyError, repo.__getitem__, entity1.id)
# Check the entity can't be discarded twice.
self.assertRaises(AssertionError, entity1.discard)
# Should fail to validate event with wrong entity ID.
self.assertRaises(EntityIDConsistencyError,
entity2._validate_originator,
DomainEvent(entity_id=entity2.id+'wrong', entity_version=0)
)
# Should fail to validate event with wrong entity version.
self.assertRaises(EntityVersionConsistencyError,
entity2._validate_originator,
DomainEvent(entity_id=entity2.id, entity_version=0)
)
# Should validate event with correct entity ID and version.
entity2._validate_originator(
DomainEvent(entity_id=entity2.id, entity_version=entity2.version)
)
# Check an entity can be reregistered with the same ID.
replacement_event = Example.Created(entity_id=entity1.id, a=11, b=12)
# replacement = Example.mutate(event=replacement_event)
publish(event=replacement_event)
# Check the replacement entity can be retrieved from the example repository.
replacement = repo[entity1.id]
assert isinstance(replacement, Example)
self.assertEqual(replacement.a, 11)
self.assertEqual(replacement.b, 12)
self.assertEqual(3, entity1.count_heartbeats())
self.assertEqual(3, repo[entity1.id].count_heartbeats())
# Check the entity can be discarded.
entity1.discard()
# Check the repo now raises a KeyError.
self.assertRaises(KeyError, repo.__getitem__, entity1.id)
# Check the entity can't be discarded twice.
self.assertRaises(AssertionError, entity1.discard)
# Should fail to validate event with wrong entity ID.
self.assertRaises(EntityIDConsistencyError,
entity2._validate_originator,
DomainEvent(entity_id=entity2.id+'wrong', entity_version=0)
)
# Should fail to validate event with wrong entity version.
self.assertRaises(EntityVersionConsistencyError,
entity2._validate_originator,
DomainEvent(entity_id=entity2.id, entity_version=0)
)
# Should validate event with correct entity ID and version.
entity2._validate_originator(
DomainEvent(entity_id=entity2.id, entity_version=entity2.version)
)
# Check an entity can be reregistered with the same ID.
replacement_event = Example.Created(entity_id=entity1.id, a=11, b=12)
# replacement = Example.mutate(event=replacement_event)
publish(event=replacement_event)
# Check the entity can't be discarded twice.
self.assertRaises(AssertionError, entity1.discard)
# Should fail to validate event with wrong entity ID.
self.assertRaises(EntityIDConsistencyError,
entity2._validate_originator,
DomainEvent(entity_id=entity2.id+'wrong', entity_version=0)
)
# Should fail to validate event with wrong entity version.
self.assertRaises(EntityVersionConsistencyError,
entity2._validate_originator,
DomainEvent(entity_id=entity2.id, entity_version=0)
)
# Should validate event with correct entity ID and version.
entity2._validate_originator(
DomainEvent(entity_id=entity2.id, entity_version=entity2.version)
)
# Check an entity can be reregistered with the same ID.
replacement_event = Example.Created(entity_id=entity1.id, a=11, b=12)
# replacement = Example.mutate(event=replacement_event)
publish(event=replacement_event)
# Check the replacement entity can be retrieved from the example repository.
replacement = repo[entity1.id]
assert isinstance(replacement, Example)
self.assertEqual(replacement.a, 11)
self.assertEqual(replacement.b, 12)
def test_not_implemented_error(self):
# Define an event class.
class UnsupportedEvent(DomainEvent): pass
# Check we get an error when attempting to mutate on the event.
self.assertRaises(NotImplementedError, Example.mutate, Example, UnsupportedEvent('1', '0'))
self._canonize_suffix(suffix, string)
class SuffixTreeNode(EventSourcedEntity):
"""A node in the suffix tree.
"""
class Created(EventSourcedEntity.Created): pass
class AttributeChanged(EventSourcedEntity.AttributeChanged): pass
class Discarded(EventSourcedEntity.Discarded): pass
class ChildNodeAdded(DomainEvent): pass
class ChildNodeRemoved(DomainEvent): pass
def __init__(self, suffix_node_id=None, string_id=None, *args, **kwargs):
super(SuffixTreeNode, self).__init__(*args, **kwargs)
self._suffix_node_id = suffix_node_id
self._string_id = string_id
self._child_node_ids = OrderedDict()
@mutableproperty
def suffix_node_id(self):
"""The id of a node with a matching suffix, representing a suffix link.
None indicates this node has no suffix link.
"""
@mutableproperty
def string_id(self):
from eventsourcing.exceptions import (
EntityIsDiscarded,
HeadHashError,
OriginatorIDError,
OriginatorVersionError,
)
from eventsourcing.utils.times import decimaltimestamp_from_uuid
from eventsourcing.utils.topic import get_topic, resolve_topic
class DomainEntity(object):
"""
Supertype for domain model entity.
"""
class Event(EventWithOriginatorID, DomainEvent):
"""
Supertype for events of domain model entities.
"""
# def __mutate__(self, obj):
# # Call super method.
# return super(DomainEntity.Event, self).__mutate__(obj)
def __check_obj__(self, obj: "DomainEntity"):
"""
Checks state of obj before mutating.
:param obj: Domain entity to be checked.
:raises OriginatorIDError: if the originator_id is mismatched
"""
def __eq__(self, other: object) -> bool:
"""
Tests for equality of two event objects.
:rtype: bool
"""
return isinstance(other, DomainEvent) and self.__hash__() == other.__hash__()
suffix.first_char_index += e.length + 1
suffix.source_node_id = e.dest_node_id
self._canonize_suffix(suffix, string)
class SuffixTreeNode(EventSourcedEntity):
"""A node in the suffix tree.
"""
class Created(EventSourcedEntity.Created): pass
class AttributeChanged(EventSourcedEntity.AttributeChanged): pass
class Discarded(EventSourcedEntity.Discarded): pass
class ChildNodeAdded(DomainEvent): pass
class ChildNodeRemoved(DomainEvent): pass
def __init__(self, suffix_node_id=None, string_id=None, *args, **kwargs):
super(SuffixTreeNode, self).__init__(*args, **kwargs)
self._suffix_node_id = suffix_node_id
self._string_id = string_id
self._child_node_ids = OrderedDict()
@mutableproperty
def suffix_node_id(self):
"""The id of a node with a matching suffix, representing a suffix link.
None indicates this node has no suffix link.
"""
# Make sure events that are applied to the entity have originated
# from the entity at the version the instance it is current at.
# - this assumes _validate_originator() is called in mutators.
__always_validate_originator_version__ = True
class Created(EventSourcedEntity.Created):
pass
class AttributeChanged(EventSourcedEntity.AttributeChanged):
pass
class Discarded(EventSourcedEntity.Discarded):
pass
class Heartbeat(DomainEvent):
pass
def __init__(self, a, b, **kwargs):
super(Example, self).__init__(**kwargs)
self._a = a
self._b = b
self._count_heartbeats = 0
@mutableproperty
def a(self):
return self._a
@mutableproperty
def b(self):
return self._b