示例#1
0
    def test_snapshot_state_after_versioning_events(self):
        """
        Mimick evolution of an event class, from original to version 2.

        Could/should just test reconstruct_object() calls __upcast_state__,
        but this test is decoupled from implementation, and simply checks
        upcasting is active when storing and retrieving aggregate state.
        """
        app = SnapshottingApplication.mixin(SQLAlchemyApplication)(
            persist_event_type=BaseAggregateRoot.Event, )
        with app:
            my_aggregate = MultiVersionAggregateFixture.__create__()
            # Trigger without any kwargs (original behaviour).
            my_aggregate.trigger()
            my_aggregate.__save__()

            # Take a snapshot.
            app.repository.take_snapshot(my_aggregate.id)

            # Check the default values are used for value and units.
            copy = app.repository[my_aggregate.id]
            assert isinstance(copy, MultiVersionAggregateFixture)
            self.assertEqual(copy.value, None)
            self.assertEqual(copy.units, None)

            # Increase the version of the aggregate and event class.
            MultiVersionAggregateFixture.__class_version__ = 1
            MultiVersionAggregateFixture.Triggered.__class_version__ = 1
            MultiVersionAggregateFixture.Triggered.__upcast__ = (
                MultiVersionAggregateFixture.Triggered.upcast_v1)
            MultiVersionAggregateFixture.Triggered.mutate = (
                MultiVersionAggregateFixture.Triggered.mutate_v1)

            # Check v1 code upcasts snapshot (which doesnt have 'value').
            copy = app.repository[my_aggregate.id]
            assert isinstance(copy, MultiVersionAggregateFixture)
            self.assertEqual(copy.value, 0)  # gets default
            self.assertEqual(copy.units, None)

            # Trigger with value kwarg (first version behaviour).
            my_aggregate.trigger(value=10)
            my_aggregate.__save__()

            # Check v1 code can read original and first event versions.
            copy = app.repository[my_aggregate.id]
            assert isinstance(copy, MultiVersionAggregateFixture)
            self.assertEqual(copy.value, 10)
            self.assertEqual(copy.units, None)

            # Take a snapshot.
            app.repository.take_snapshot(my_aggregate.id)

            # Check the default values are used for value and units.
            copy = app.repository[my_aggregate.id]
            assert isinstance(copy, MultiVersionAggregateFixture)
            self.assertEqual(copy.value, 10)
            self.assertEqual(copy.units, None)

            # Increase the version of the aggregate and event class.
            MultiVersionAggregateFixture.__class_version__ = 2
            MultiVersionAggregateFixture.Triggered.__class_version__ = 2
            MultiVersionAggregateFixture.Triggered.__upcast__ = (
                MultiVersionAggregateFixture.Triggered.upcast_v2)
            MultiVersionAggregateFixture.Triggered.mutate = (
                MultiVersionAggregateFixture.Triggered.mutate_v2)

            # Check v2 code upcasts snapshot (which doesnt have 'units').
            copy = app.repository[my_aggregate.id]
            assert isinstance(copy, MultiVersionAggregateFixture)
            self.assertEqual(copy.value, 10)
            self.assertEqual(copy.units, '')  # gets default
class TestSnapshottingApplication(TestSimpleApplication):
    infrastructure_class = SnapshottingApplication.mixin(SQLAlchemyApplication)
class TestSnapshottingAxonApplication(TestSimpleApplication):
    infrastructure_class = SnapshottingApplication.mixin(AxonApplication)
class TestSnapshottingApplication(TestSimpleApplication):
    application_class = SnapshottingApplication.mixin(SQLAlchemyApplication)