def test_it_cannot_set_tuples_as_metadata_value(self): domain_event = EventA("0A919B3E-5BCB-41DC-B157-8A9E2A7198B", "foo") obj = object() with self.assertRaises(DomainEventException) as ex: domain_event.add_metadata("foo", obj) self.assertEqual(str(ex.exception), "Can only set metadata with simple data types (bool, dict, int, list, string)")
def test_it_can_serialize(self): domain_event = EventA("0A919B3E-5BCB-41DC-B157-8A9E2A7198BE", "foo") expected_result = { 'aggregate_root_id': '0A919B3E-5BCB-41DC-B157-8A9E2A7198BE', 'an_event_property': 'foo', } self.assertEqual(domain_event.serialize(), expected_result)
def test_it_has_a_loaded_version(self): """ test if events are versioned when aggregate is loaded """ aggregate_root_id = '897878D0-1230-408B-A980-7A9C24EBDEFA' events = [ EventA(aggregate_root_id, 'my_prop'), EventA(aggregate_root_id, 'my_prop1'), EventA(aggregate_root_id, 'my_prop2'), ] aggregate = MyTestAggregate() aggregate.initialize_state(events) self.assertEqual(aggregate.get_aggregate_root_version(), 3)
def test_it_can_emit_a_stream_of_events(self): event_listener = SimpleEventListener() event_bus = BasicBus() event_bus.subscribe(event_listener) stream_of_events = [ EventA("792E4DDA-5AE2-4BF3-A834-62D09892DC62", "foo"), EventA("EC407041-8454-44E2-873F-951B227B3BFB", "bar") ] event_bus.emit(stream_of_events) self.assertEqual(2, len(event_listener.get_received_messages()))
def test_it_can_store_and_load_multiple_events(self): """ test if InMemoryStore can store and load multiple events """ store = InMemoryStore() aggregate_root_id = '897878D0-1230-408B-A980-7A9C24EBDEFA' eventstream = [ EventA(aggregate_root_id, 'my_prop'), EventA(aggregate_root_id, 'my_prop1'), EventA(aggregate_root_id, 'my_prop2'), ] store.save(eventstream, aggregate_root_id) self.assertEqual(3, len(store.load(aggregate_root_id)))
def test_it_can_store_and_load_an_event(self): """ test if InMemoryStore can actually store an event """ store = SQLStore(self.__session) aggregate_root_id = '897878D0-1230-408B-A980-7A9C24EBDEFA' event_a = EventA(aggregate_root_id, 'my_prop') event_a.set_aggregate_root_version(1) eventstream = [ event_a, ] store.save(eventstream, aggregate_root_id) self.assertEqual(1, len(store.load(aggregate_root_id)))
def test_it_throws_when_id_doesnt_exists(self): """ test if InMemoryStore throws AggregateRootIdNotFoundError """ store = InMemoryStore() aggregate_root_id = '897878D0-1230-408B-A980-7A9C24EBDEFA' eventstream = [ EventA(aggregate_root_id, 'my_prop'), EventA(aggregate_root_id, 'my_prop1'), EventA(aggregate_root_id, 'my_prop2'), ] store.save(eventstream, aggregate_root_id) with self.assertRaises(AggregateRootIdNotFoundError) as ex: store.load('108AEB44-7842-4F36-A113-60A3786670C2') self.assertEqual( "Aggregate root id does not exist: 108AEB44-7842-4F36-A113-60A3786670C2", str(ex.exception))
def test_it_can_restore_state_with_three_events(self): """ Creates an aggregate and try to restore state with adding trhee events """ aggregate_root_id = '897878D0-1230-408B-A980-7A9C24EBDEFA' events = [ EventA(aggregate_root_id, 'my_prop'), EventA(aggregate_root_id, 'my_prop1'), EventA(aggregate_root_id, 'my_prop2'), ] aggregate = MyTestAggregate() aggregate.initialize_state(events) self.assertEqual(len(aggregate.get_uncommitted_events()), 0) self.assertEqual(aggregate.get_aggregate_root_id(), aggregate_root_id) self.assertEqual( aggregate.__dict__.get('_MyTestAggregate__an_event_property'), 'my_prop2')
def test_it_can_load_an_eventstream(self): aggregate_id = 'AB9850E7-B590-4A65-B513-91ABD6DC6F40' eventStream = [ EventA(aggregate_root_id=aggregate_id, an_event_property='prop1'), EventA(aggregate_root_id=aggregate_id, an_event_property='prop2'), EventA(aggregate_root_id=aggregate_id, an_event_property='prop3') ] store = InMemoryStore() store.save(eventStream, aggregate_id) repository = DefaultRepository( 'esframework.tests.repository.repository_test.MyTestAggregate', store, BasicBus()) aggregate = repository.load(aggregate_root_id=aggregate_id) self.assertEqual(aggregate.get_aggregate_root_id(), aggregate_id) self.assertEqual( aggregate.__dict__.get('_MyTestAggregate__an_event_property'), 'prop3')
def test_it_can_store_and_load_an_event(self): """ test if InMemoryStore can actually store an event """ store = InMemoryStore() aggregate_root_id = '897878D0-1230-408B-A980-7A9C24EBDEFA' eventstream = [ EventA(aggregate_root_id, 'my_prop'), ] store.save(eventstream, aggregate_root_id) self.assertEqual(1, len(store.load(aggregate_root_id)))
def test_it_can_set_an_eventstream(self): """ test if the aggregate root can be set at a default state """ eventstream = [ EventA(aggregate_root_id='D60A2BD0-AE8D-4FF1-A5AE-B4F706356389', an_event_property='a') ] testcase = AggregateRootTestCase() testcase.withAggregate(MyTestAggregate()) testcase.has_eventstream(eventstream) self.assertEqual( testcase.__dict__.get( '_AggregateRootTestCase__eventstream_at_initialisation'), eventstream)
def test_it_can_store_and_load_multiple_events_multiple_aggregates(self): """ test if InMemoryStore can deal with multiple aggregates """ store = InMemoryStore() aggr_root_id_1 = '897878D0-1230-408B-A980-7A9C24EBDEFA' eventstream = [ EventA(aggr_root_id_1, 'my_prop'), EventA(aggr_root_id_1, 'my_prop1'), EventA(aggr_root_id_1, 'my_prop2'), ] store.save(eventstream, aggr_root_id_1) aggr_root_id_2 = '108AEB44-7842-4F36-A113-60A3786670C2' eventstream = [ EventA(aggr_root_id_2, 'my_prop'), EventA(aggr_root_id_2, 'my_prop1'), ] store.save(eventstream, aggr_root_id_2) self.assertEqual(3, len(store.load(aggr_root_id_1))) self.assertEqual(2, len(store.load(aggr_root_id_2)))
def test_it_can_deserialize(self): serialized_data = { 'aggregate_root_id': '0A919B3E-5BCB-41DC-B157-8A9E2A7198BE', 'an_event_property': 'foo', } # domain_event: EventA domain_event = EventA.deserialize(serialized_data) self.assertEqual( domain_event.get_aggregate_root_id(), '0A919B3E-5BCB-41DC-B157-8A9E2A7198BE' ) self.assertEqual( domain_event.get_an_event_property(), 'foo' )
def test_it_can_detect_outdated_aggregates(self): aggregate_id = 'AB9850E7-B590-4A65-B513-91ABD6DC6F40' eventStream = [ EventA(aggregate_root_id=aggregate_id, an_event_property='prop1'), ] store = InMemoryStore() store.save(eventStream, aggregate_id) repository = DefaultRepository( 'esframework.tests.repository.repository_test.MyTestAggregate', store, BasicBus()) aggregate_one = repository.load(aggregate_root_id=aggregate_id) aggregate_two = repository.load(aggregate_root_id=aggregate_id) aggregate_one.event_b(aggregate_id, 'my_prop_value') repository.save(aggregate_one) with self.assertRaises(AggregateRootOutOfSyncError) as ex: aggregate_two.event_b(aggregate_id, 'my_prop_value') repository.save(aggregate_two) self.assertEqual( str(ex.exception), "Aggregate root in store is newer then current aggregate")
def test_it_cannot_have_duplicate_metadata(self): domain_event = EventA("0A919B3E-5BCB-41DC-B157-8A9E2A7198B", "foo") domain_event.add_metadata("foo", "bar") with self.assertRaises(DomainEventException) as ex: domain_event.add_metadata("foo", "bar") self.assertEqual(str(ex.exception), 'Metadata is already set!')
def test_it_can_add_metadata_with_a_string_as_value(self): domain_event = EventA("0A919B3E-5BCB-41DC-B157-8A9E2A7198BE", "foo") domain_event.add_metadata("foo", "bar") self.assertEqual(domain_event.get_metadata(), [{"foo": "bar"}])
def test_it_can_add_metadata_with_a_list_as_value(self): domain_event = EventA("0A919B3E-5BCB-41DC-B157-8A9E2A7198BE", "foo") domain_event.add_metadata("foo", ['bar', 'baz']) self.assertEqual(domain_event.get_metadata(), [{"foo": ['bar', 'baz']}])
def test_it_cannot_set_causation_id_twice(self): domain_event = EventA("0A919B3E-5BCB-41DC-B157-8A9E2A7198BE", "foo") domain_event.set_causation_id("6C28E7B7-61A1-432D-8778-DA94BE334969") with self.assertRaises(DomainEventException) as ex: domain_event.set_causation_id("4AFA7DC7-5268-4BE9-B065-27D19CE4DD5F") self.assertEqual(str(ex.exception), "Causation id can only be set once!")
def test_it_cannot_set_version_twice(self): domain_event = EventA("0A919B3E-5BCB-41DC-B157-8A9E2A7198BE", "foo") domain_event.set_aggregate_root_version(1) with self.assertRaises(DomainEventException) as ex: domain_event.set_aggregate_root_version(1) self.assertEqual(str(ex.exception), "Version can only be set once!")
def test_it_cannot_remove_non_existent_data(self): domain_event = EventA("0A919B3E-5BCB-41DC-B157-8A9E2A7198B", "foo") with self.assertRaises(DomainEventException) as ex: domain_event.remove_metadata("foo", "bar") self.assertEqual(str(ex.exception), "Can't remove non existent metadata!")
def test_it_can_remove_metadata(self): domain_event = EventA("0A919B3E-5BCB-41DC-B157-8A9E2A7198BE", "foo") domain_event.add_metadata("foo", "bar") self.assertEqual(domain_event.get_metadata(), [{"foo": "bar"}]) domain_event.remove_metadata("foo", "bar") self.assertEqual(domain_event.get_metadata(), [])
def test_it_can_store_and_load_multiple_events(self): """ test if SQLStore can store and load multiple events """ store = SQLStore(self.__session) aggregate_root_id = '52B6A306-540C-4796-92E4-A10520EA3ED7' event1 = EventA(aggregate_root_id, 'my_prop') event1.set_aggregate_root_version(1) event2 = EventA(aggregate_root_id, 'my_prop1') event2.set_aggregate_root_version(2) event3 = EventA(aggregate_root_id, 'my_prop2') event3.set_aggregate_root_version(3) eventstream = [ event1, event2, event3 ] store.save(eventstream, aggregate_root_id) self.assertEqual(3, len(store.load(aggregate_root_id)))
def test_it_can_store_and_load_multiple_events_multiple_aggregates(self): """ test if SQLStore can deal with multiple aggregates """ store = SQLStore(self.__session) aggr_root_id_1 = '897878D0-1230-408B-A980-7A9C24EBDEFA' event1 = EventA(aggr_root_id_1, 'my_prop') event1.set_aggregate_root_version(1) event2 = EventA(aggr_root_id_1, 'my_prop1') event2.set_aggregate_root_version(2) event3 = EventA(aggr_root_id_1, 'my_prop2') event3.set_aggregate_root_version(3) eventstream = [ event1, event2, event3 ] store.save(eventstream, aggr_root_id_1) aggr_root_id_2 = '108AEB44-7842-4F36-A113-60A3786670C2' event4 = EventA(aggr_root_id_2, 'my_prop') event4.set_aggregate_root_version(1) event5 = EventA(aggr_root_id_2, 'my_prop1') event5.set_aggregate_root_version(2) eventstream = [ event4, event5 ] store.save(eventstream, aggr_root_id_2) self.assertEqual(3, len(store.load(aggr_root_id_1))) self.assertEqual(2, len(store.load(aggr_root_id_2)))
def test_it_can_set_correct_causation_ids(self): """ test if SQLStore can deal properly with causation ids """ store = SQLStore(self.__session) aggr_root = '897878D0-1230-408B-A980-7A9C24EBDEFA' event1 = EventA(aggr_root, 'my_prop') event1.set_aggregate_root_version(1) event2 = EventA(aggr_root, 'my_prop1') event2.set_aggregate_root_version(2) event3 = EventA(aggr_root, 'my_prop2') event3.set_aggregate_root_version(3) eventstream = [ event1, event2, event3 ] store.save(eventstream, aggr_root) # stored_eventstream: List[DomainEvent] stored_eventstream = store.load(aggr_root) event4 = EventA(aggr_root, 'my_prop') event4.set_aggregate_root_version(4) event4.set_causation_id(stored_eventstream[1].get_event_id()) event5 = EventA(aggr_root, 'my_prop1') event5.set_aggregate_root_version(5) event5.set_causation_id(stored_eventstream[1].get_event_id()) eventstream = [ event4, event5 ] store.save(eventstream, aggr_root) stored_eventstream = store.load(aggr_root) self.assertEqual(5, len(stored_eventstream)) self.assertEqual( stored_eventstream[1].get_event_id(), stored_eventstream[2].get_causation_id() ) self.assertEqual( aggr_root, stored_eventstream[2].get_correlation_id() ) self.assertEqual( stored_eventstream[1].get_event_id(), stored_eventstream[3].get_causation_id() ) self.assertEqual( aggr_root, stored_eventstream[3].get_correlation_id() ) self.assertEqual( stored_eventstream[1].get_event_id(), stored_eventstream[4].get_causation_id() ) self.assertEqual( aggr_root, stored_eventstream[4].get_correlation_id() )