Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test(self):
# Setup the example application, use it as a context manager.
with ExampleApplicationWithSQLAlchemy(db_uri='sqlite:///:memory:') as app:
# Check there's a DB session.
self.assertIsInstance(app.db_session, ScopedSession)
# Check there's a stored event repo.
self.assertIsInstance(app.stored_event_repo, StoredEventRepository)
self.assertEqual(app.stored_event_repo.db_session, app.db_session)
# Check there's an event store.
self.assertIsInstance(app.event_store, EventStore)
self.assertEqual(app.event_store.stored_event_repo, app.stored_event_repo)
# Check there's a persistence subscriber.
self.assertIsInstance(app.persistence_subscriber, PersistenceSubscriber)
self.assertEqual(app.persistence_subscriber.event_store, app.event_store)
# Check there's an example repository.
self.assertIsInstance(app.example_repo, ExampleRepository)
assert isinstance(app, ExampleApplicationWithSQLAlchemy) # For PyCharm...
# Register a new example.
example1 = app.register_new_example(a=10, b=20)
self.assertIsInstance(example1, Example)
def test(self):
"""Checks self.app both is and work in the way an
instance of a subclass of ExampleApplication should do.
"""
# Check we're dealing with an example application.
assert isinstance(self.app, ExampleApplication)
# Check there's a stored event repo.
self.assertIsInstance(self.app.stored_event_repo, StoredEventRepository)
# Check there's an event store.
self.assertIsInstance(self.app.event_store, EventStore)
self.assertEqual(self.app.event_store.stored_event_repo, self.app.stored_event_repo)
# Check there's a persistence subscriber.
self.assertIsInstance(self.app.persistence_subscriber, PersistenceSubscriber)
self.assertEqual(self.app.persistence_subscriber.event_store, self.app.event_store)
# Check there's an example repository.
self.assertIsInstance(self.app.example_repo, ExampleRepo)
# Register a new example.
example1 = self.app.register_new_example(a=10, b=20)
self.assertIsInstance(example1, Example)
# Check the example is available in the repo.
entity1 = self.app.example_repo[example1.id]
self.assertEqual(10, entity1.a)
def register_new_edge(edge_id, label, first_char_index, last_char_index, source_node_id, dest_node_id):
"""Factory method, registers new edge.
"""
event = Edge.Created(
entity_id=edge_id,
label=label,
first_char_index=first_char_index,
last_char_index=last_char_index,
source_node_id=source_node_id,
dest_node_id=dest_node_id,
)
entity = Edge.mutate(event=event)
publish(event)
return entity
def register_new_suffix_tree(string, case_insensitive=False):
"""Factory method, returns new suffix tree object.
"""
suffix_tree_id = uuid4().hex
event = SuffixTree.Created(
entity_id=suffix_tree_id,
string=string,
case_insensitive=case_insensitive,
)
entity = SuffixTree.mutate(event=event)
publish(event)
return entity
self.assertRaises(ProgrammingError, mutableproperty, 123)
self.assertRaises(ProgrammingError, mutableproperty, None)
# Call the decorator with a function.
getter = lambda: None
p = mutableproperty(getter)
# Check we got a property object.
self.assertIsInstance(p, property)
# Check the property object has both setter and getter functions.
self.assertTrue(p.fset)
self.assertTrue(p.fget)
# Pretend we decorated an object.
o = EventSourcedEntity(entity_id='1', entity_version=1, domain_event_id=1)
o.__dict__['_'] = 'value1'
# Call the property's getter function.
value = p.fget(o)
self.assertEqual(value, 'value1')
# Call the property's setter function.
p.fset(o, 'value2')
# Check the attribute has changed.
value = p.fget(o)
self.assertEqual(value, 'value2')
# Check the property's getter function isn't the getter function we passed in.
self.assertNotEqual(p.fget, getter)
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.
"""
return self._suffix_node_id
def __repr__(self):
return "Node(suffix link: %d)" % self.suffix_node_id
class Edge(EventSourcedEntity):
"""An edge in the suffix tree.
"""
class Created(EventSourcedEntity.Created): pass
class AttributeChanged(EventSourcedEntity.AttributeChanged): pass
class Discarded(EventSourcedEntity.Discarded): pass
def __init__(self, label, first_char_index, last_char_index, source_node_id, dest_node_id, **kwargs):
super(Edge, self).__init__(**kwargs)
self._label = label
self._first_char_index = first_char_index
self._last_char_index = last_char_index
self._source_node_id = source_node_id
self._dest_node_id = dest_node_id
@mutableproperty
def label(self):
"""String part represented by this edge.
# 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'))