示例#1
0
    def test_process_with_example_policy(self):
        # Construct example process.
        process_class = ProcessApplication.mixin(self.infrastructure_class)
        with process_class(
                name="test",
                policy=example_policy,
                persist_event_type=ExampleAggregate.Event,
                setup_table=True,
        ) as process:
            # Make the process follow itself.
            process.follow("test", process.notification_log)

            # Create an aggregate.
            aggregate = ExampleAggregate.__create__()
            aggregate.__save__()

            # Run the process.
            process.run()

            # Check the aggregate has been "moved on".
            self.assertTrue(process.repository[aggregate.id].is_moved_on)

            # Check the __contains__ method of the repo wrapper.
            self.assertIn(aggregate.id, WrappedRepository(process.repository))
            self.assertNotIn(uuid4(), WrappedRepository(process.repository))

            # Check the repository wrapper tracks causal dependencies.
            repository = WrappedRepository(process.repository)
            aggregate = repository[aggregate.id]
            causal_dependencies = repository.causal_dependencies
            self.assertEqual(len(causal_dependencies), 1)
            self.assertEqual((aggregate.id, 1), causal_dependencies[0])

            # Check events from more than one aggregate are stored.
            self.assertIn(aggregate.second_id, process.repository)
示例#2
0
 def projection_policy(repository: WrappedRepository, event):
     if isinstance(event, ExampleAggregate.Created):
         projection = self.projection_record_class(
             projection_id=projection_id, state=str(event.timestamp))
         repository.save_orm_obj(projection)
     elif isinstance(event, ExampleAggregate.MovedOn):
         projection = self.get_projection_record(
             projection_record_class=self.projection_record_class,
             projection_id=projection_id,
             record_manager=repository.repository.event_store.
             record_manager,
         )
         assert projection is not None
         repository.delete_orm_obj(projection)