Ejemplo n.º 1
0
        public void Applying_an_event_with_no_handler_should_cause_an_exception()
        {
            var    theAggregate = new MyAggregateRoot();
            Action act          = theAggregate.MethodThatCausesAnEventThatDoesNotConaintAHandler;

            act.ShouldThrow <EventNotHandledException>();
        }
Ejemplo n.º 2
0
        public void Applying_an_event_when_there_is_no_unit_of_work_should_not_cause_an_exception()
        {
            var    theAggregate = new MyAggregateRoot();
            Action act          = () => theAggregate.MethodThatCausesAnEventThatHasAHandler();

            act.ShouldNotThrow();
        }
Ejemplo n.º 3
0
        public void Constructing_it_with_an_id_should_set_that_to_EventSourceId_property()
        {
            var theId        = Guid.NewGuid();
            var theAggregate = new MyAggregateRoot(theId);

            theAggregate.EventSourceId.Should().Be(theId);
        }
Ejemplo n.º 4
0
        public void Initializing_from_history_should_not_throw_an_exception_when_the_history_was_empty()
        {
            var theAggregate = new MyAggregateRoot();

            IEnumerable <SourcedEvent> history = new SourcedEvent[0];

            theAggregate.InitializeFromHistory(history);
        }
Ejemplo n.º 5
0
        public void Initializing_from_history_should_throw_an_exception_when_the_history_was_null()
        {
            var theAggregate = new MyAggregateRoot();

            Action act = () => theAggregate.InitializeFromHistory(null);

            act.ShouldThrow <ArgumentNullException>();
        }
Ejemplo n.º 6
0
        public void Initializing_from_history_should_not_throw_an_exception_when_the_history_was_empty()
        {
            var theAggregate = new MyAggregateRoot();

            var history = new CommittedEventStream(Guid.Empty);

            theAggregate.InitializeFromHistory(history);
        }
Ejemplo n.º 7
0
        public void Applying_an_event_should_call_the_event_handler_only_once()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            theAggregate.FooEventHandlerInvokeCount.Should().Be(1);
        }
Ejemplo n.º 8
0
        public void Loading_it_from_history_should_apply_all_events()
        {
            var aggId   = Guid.NewGuid();
            var history = new[] { new HandledEvent(Guid.NewGuid(), aggId, 1, DateTime.UtcNow), new HandledEvent(Guid.NewGuid(), aggId, 2, DateTime.UtcNow), new HandledEvent(Guid.NewGuid(), aggId, 3, DateTime.UtcNow) };

            var theAggregate = new MyAggregateRoot(history);

            theAggregate.FooEventHandlerInvokeCount.Should().Be(3);
        }
Ejemplo n.º 9
0
        public void It_should_initialize_with_a_new_id_given_by_the_generator_from_the_environment()
        {
            var generator = MockRepository.GenerateMock<IUniqueIdentifierGenerator>();
            NcqrsEnvironment.SetDefault<IUniqueIdentifierGenerator>(generator);

            var theAggregate = new MyAggregateRoot();

            generator.AssertWasCalled(g => g.GenerateNewId());
        }
Ejemplo n.º 10
0
        public void It_should_initialize_with_a_new_id_given_by_the_generator_from_the_environment()
        {
            var generator = MockRepository.GenerateMock <IUniqueIdentifierGenerator>();

            NcqrsEnvironment.SetDefault <IUniqueIdentifierGenerator>(generator);

            var theAggregate = new MyAggregateRoot();

            generator.AssertWasCalled(g => g.GenerateNewId());
        }
Ejemplo n.º 11
0
        public void Loading_it_from_history_should_apply_all_events()
        {
            var aggId        = Guid.NewGuid();
            var stream       = Prepare.Events(new HandledEvent(), new HandledEvent(), new HandledEvent()).ForSource(aggId);
            var theAggregate = new MyAggregateRoot(aggId);

            theAggregate.InitializeFromHistory(stream);

            theAggregate.FooEventHandlerInvokeCount.Should().Be(3);
        }
Ejemplo n.º 12
0
        public void Applying_an_event_that_is_already_owned_by_another_source_should_cause_an_exception()
        {
            var theEvent = new HandledEvent();

            theEvent.OverrideAggregateRootId(Guid.NewGuid());
            var theAggregate = new MyAggregateRoot();

            Action act = () => theAggregate.ApplyEvent(theEvent);

            act.ShouldThrow <InvalidOperationException>();
        }
Ejemplo n.º 13
0
        public void Applying_an_event_should_not_effect_the_initial_version()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.InitialVersion.Should().Be(0);
            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.InitialVersion.Should().Be(0);
            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.InitialVersion.Should().Be(0);
            theAggregate.MethodThatCausesAnEventThatHasAHandler();
        }
Ejemplo n.º 14
0
        public void Initiazling_from_wrong_history_with_wrong_sequence_should_throw_exception()
        {
            var        theAggregate  = new MyAggregateRoot();
            const long wrongSequence = 3;
            var        stream        = new CommittedEventStream(theAggregate.EventSourceId,
                                                                new CommittedEvent(Guid.NewGuid(), Guid.NewGuid(), theAggregate.EventSourceId, wrongSequence, DateTime.UtcNow, new HandledEvent(), new Version(1, 0)));

            Action act = () => theAggregate.InitializeFromHistory(stream);

            act.ShouldThrow <InvalidOperationException>().And.Message.Should().Contain("sequence");
        }
Ejemplo n.º 15
0
        public void Getting_the_uncommitted_via_the_IEventSource_interface_should_return_the_same_as_directly()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            var directResult       = theAggregate.GetUncommittedEvents();
            var viaInterfaceResult = ((IEventSource)theAggregate).GetUncommittedEvents();

            directResult.Should().BeEquivalentTo(viaInterfaceResult);
        }
Ejemplo n.º 16
0
        public void It_could_not_be_loaded_from_history_when_it_already_contains_uncommitted_events()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            var    history = new[] { new HandledEvent(), new HandledEvent() };
            Action act     = () => theAggregate.InitializeFromHistory(history);

            act.ShouldThrow <InvalidOperationException>();
        }
Ejemplo n.º 17
0
        public void Applying_an_event_should_at_it_to_the_uncommited_events()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            theAggregate.GetUncommittedEvents().Count().Should().Be(1);

            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            theAggregate.GetUncommittedEvents().Count().Should().Be(2);
        }
Ejemplo n.º 18
0
        public void Applying_an_event_should_at_it_to_the_uncommited_events()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            theAggregate.GetUncommittedEvents().Count().Should().Be(1);

            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            theAggregate.GetUncommittedEvents().Count().Should().Be(2);
        }
Ejemplo n.º 19
0
        public void Initiazling_from_history_with_correct_sequence_should_not_throw_exception()
        {
            var theAggregate = new MyAggregateRoot();

            var stream = new CommittedEventStream(theAggregate.EventSourceId,
                                                  new CommittedEvent(Guid.NewGuid(), Guid.NewGuid(), theAggregate.EventSourceId, 1, DateTime.UtcNow, new HandledEvent(), new Version(1, 0)),
                                                  new CommittedEvent(Guid.NewGuid(), Guid.NewGuid(), theAggregate.EventSourceId, 2, DateTime.UtcNow, new HandledEvent(), new Version(1, 0)),
                                                  new CommittedEvent(Guid.NewGuid(), Guid.NewGuid(), theAggregate.EventSourceId, 3, DateTime.UtcNow, new HandledEvent(), new Version(1, 0)),
                                                  new CommittedEvent(Guid.NewGuid(), Guid.NewGuid(), theAggregate.EventSourceId, 4, DateTime.UtcNow, new HandledEvent(), new Version(1, 0)),
                                                  new CommittedEvent(Guid.NewGuid(), Guid.NewGuid(), theAggregate.EventSourceId, 5, DateTime.UtcNow, new HandledEvent(), new Version(1, 0)));

            theAggregate.InitializeFromHistory(stream);
        }
Ejemplo n.º 20
0
        public void Initiazling_from_wrong_history_with_wrong_sequence_should_throw_exception()
        {
            var  theAggregate  = new MyAggregateRoot();
            long wrongSequence = 3;

            var event1 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, wrongSequence, DateTime.UtcNow);

            IEnumerable <SourcedEvent> history = new[] { event1 };

            Action act = () => theAggregate.InitializeFromHistory(history);

            act.ShouldThrow <InvalidOperationException>().And.Message.Should().Contain("sequence");
        }
Ejemplo n.º 21
0
        public void Accepting_the_changes_should_clear_the_uncommitted_events()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            theAggregate.AcceptChanges();

            theAggregate.GetUncommittedEvents().Should().BeEmpty();
        }
Ejemplo n.º 22
0
        public void Applying_an_event_should_not_effect_the_initial_version()
        {
            using (NcqrsEnvironment.Get<IUnitOfWorkFactory>().CreateUnitOfWork())
            {
                var theAggregate = new MyAggregateRoot();

                theAggregate.InitialVersion.Should().Be(0);
                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.InitialVersion.Should().Be(0);
                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.InitialVersion.Should().Be(0);
                theAggregate.MethodThatCausesAnEventThatHasAHandler();
            }
        }
Ejemplo n.º 23
0
        public void Accepting_the_changes_should_clear_the_uncommitted_events()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            theAggregate.AcceptChanges();

            theAggregate.GetUncommittedEvents().Should().BeEmpty();
        }
Ejemplo n.º 24
0
        public void Applying_an_event_should_not_effect_the_initial_version()
        {
            using (NcqrsEnvironment.Get <IUnitOfWorkFactory>().CreateUnitOfWork())
            {
                var theAggregate = new MyAggregateRoot();

                theAggregate.InitialVersion.Should().Be(0);
                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.InitialVersion.Should().Be(0);
                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.InitialVersion.Should().Be(0);
                theAggregate.MethodThatCausesAnEventThatHasAHandler();
            }
        }
Ejemplo n.º 25
0
        public void Initiazling_from_history_with_correct_sequence_should_not_throw_exception()
        {
            var theAggregate = new MyAggregateRoot();

            var event1 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 1, DateTime.UtcNow);
            var event2 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 2, DateTime.UtcNow);
            var event3 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 3, DateTime.UtcNow);
            var event4 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 4, DateTime.UtcNow);
            var event5 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 5, DateTime.UtcNow);

            IEnumerable <SourcedEvent> history = new[] { event1, event2, event3, event4, event5 };

            theAggregate.InitializeFromHistory(history);
        }
Ejemplo n.º 26
0
        public void Applying_an_event_should_at_it_to_the_uncommited_events()
        {
            using (NcqrsEnvironment.Get<IUnitOfWorkFactory>().CreateUnitOfWork())
            {
                var theAggregate = new MyAggregateRoot();

                theAggregate.MethodThatCausesAnEventThatHasAHandler();

                theAggregate.GetUncommittedEvents().Count().Should().Be(1);

                theAggregate.MethodThatCausesAnEventThatHasAHandler();

                theAggregate.GetUncommittedEvents().Count().Should().Be(2);
            }
        }
Ejemplo n.º 27
0
        public void Applying_an_event_should_affect_the_version()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.Version.Should().Be(0);

            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.Version.Should().Be(1);
            theAggregate.GetUncommittedEvents().Last().EventSequence.Should().Be(1);

            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.Version.Should().Be(2);
            theAggregate.GetUncommittedEvents().Last().EventSequence.Should().Be(2);

            theAggregate.MethodThatCausesAnEventThatHasAHandler();
        }
Ejemplo n.º 28
0
        public void Accepting_the_changes_should_set_the_initial_version_to_the_new_version()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            theAggregate.InitialVersion.Should().Be(0);

            theAggregate.AcceptChanges();

            theAggregate.InitialVersion.Should().Be(5);
        }
Ejemplo n.º 29
0
        public void Applying_an_event_should_affect_the_version()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.Version.Should().Be(0);

            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.Version.Should().Be(1);
            theAggregate.GetUncommittedEvents().Last().EventSequence.Should().Be(1);

            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.Version.Should().Be(2);
            theAggregate.GetUncommittedEvents().Last().EventSequence.Should().Be(2);

            theAggregate.MethodThatCausesAnEventThatHasAHandler();
        }
Ejemplo n.º 30
0
        public void Accepting_the_changes_should_set_the_initial_version_to_the_new_version()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            theAggregate.InitialVersion.Should().Be(0);

            theAggregate.AcceptChanges();

            theAggregate.InitialVersion.Should().Be(5);
        }
Ejemplo n.º 31
0
        public void Accepting_the_changes_should_clear_the_uncommitted_events()
        {
            using (NcqrsEnvironment.Get<IUnitOfWorkFactory>().CreateUnitOfWork())
            {
                var theAggregate = new MyAggregateRoot();

                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.MethodThatCausesAnEventThatHasAHandler();

                theAggregate.AcceptChanges();

                theAggregate.GetUncommittedEvents().Should().BeEmpty();
            }
        }
Ejemplo n.º 32
0
        public void Accepting_the_changes_should_set_the_initial_version_to_the_new_version()
        {
            using (NcqrsEnvironment.Get<IUnitOfWorkFactory>().CreateUnitOfWork())
            {
                var theAggregate = new MyAggregateRoot();

                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.MethodThatCausesAnEventThatHasAHandler();

                theAggregate.InitialVersion.Should().Be(0);

                theAggregate.AcceptChanges();

                theAggregate.InitialVersion.Should().Be(5);
            }
        }
Ejemplo n.º 33
0
        public void Applying_an_event_to_an_agg_root_with_history_should_call_the_event_handler_only_once()
        {
            var theAggregate = new MyAggregateRoot();

            var event1 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 1, DateTime.UtcNow);
            var event2 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 2, DateTime.UtcNow);
            var event3 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 3, DateTime.UtcNow);
            var event4 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 4, DateTime.UtcNow);
            var event5 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 5, DateTime.UtcNow);

            IEnumerable <SourcedEvent> history = new[] { event1, event2, event3, event4, event5 };

            theAggregate.InitializeFromHistory(history);

            var eventHandlerCountAfterInitialization = theAggregate.FooEventHandlerInvokeCount;

            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            theAggregate.FooEventHandlerInvokeCount.Should().Be(eventHandlerCountAfterInitialization + 1);
        }
Ejemplo n.º 34
0
        public void Save_test()
        {
            using (var work = NcqrsEnvironment.Get<IUnitOfWorkFactory>().CreateUnitOfWork())
            {
                var store = MockRepository.GenerateMock<IEventStore>();
                var bus = MockRepository.GenerateMock<IEventBus>();
                var aggregate = new MyAggregateRoot();

                aggregate.Foo();
                aggregate.Bar();

                store.Expect(s => s.Save(aggregate));
                bus.Expect(b => b.Publish((IEnumerable<IEvent>) null)).IgnoreArguments();

                var repository = new DomainRepository(store, bus);
                repository.Save(aggregate);

                bus.VerifyAllExpectations();
                store.VerifyAllExpectations();
            }
        }
Ejemplo n.º 35
0
        public void Save_test()
        {
            using (var work = NcqrsEnvironment.Get <IUnitOfWorkFactory>().CreateUnitOfWork())
            {
                var store     = MockRepository.GenerateMock <IEventStore>();
                var bus       = MockRepository.GenerateMock <IEventBus>();
                var aggregate = new MyAggregateRoot();

                aggregate.Foo();
                aggregate.Bar();

                store.Expect(s => s.Save(aggregate));
                bus.Expect(b => b.Publish((IEnumerable <IEvent>)null)).IgnoreArguments();

                var repository = new DomainRepository(store, bus);
                repository.Save(aggregate);

                bus.VerifyAllExpectations();
                store.VerifyAllExpectations();
            }
        }
Ejemplo n.º 36
0
        public void MissingEventRouteThrowsException()
        {
            // Setup the event router.
            var builder = new ApplyEventRouterBuilder <MyAggregateRoot, IEvent>();

            builder.Add <SomethingHappened>((agg, e) => agg.ApplyEvent(e));
            var router = builder.Build();

            // Set up the initial state.
            var initialState = new MyAggregateRoot();

            // Create some events to apply to the aggregate.
            var event1 = new SomethingHappened("Foo");
            var event2 = new SomethingElseHappened("Bar");

            // Route the events.
            var          exception       = Assert.Throws <InvalidOperationException>(() => router.Route(initialState, event1, event2));
            const string expectedMessage = "No route or default route found when attempting to apply " +
                                           "SomethingElseHappened to MyAggregateRoot";

            Assert.That(exception.Message, Is.EqualTo(expectedMessage));
        }
Ejemplo n.º 37
0
        public void EventIsRoutedAndApplied()
        {
            // Setup the event router.
            var builder = new ApplyEventRouterBuilder <MyAggregateRoot, IEvent>();

            builder.Add <SomethingHappened>((agg, e) => agg.ApplyEvent(e));
            builder.Add <SomethingElseHappened>((agg, e) => agg.ApplyEvent(e));
            var router = builder.Build();

            // Set up the initial state.
            var initialState = new MyAggregateRoot();

            // Create some events to apply to the aggregate.
            var event1 = new SomethingHappened("Foo");
            var event2 = new SomethingElseHappened("Bar");

            // Route the events.
            var newState = router.Route(initialState, event1, event2);

            Assert.That(newState.State, Is.EqualTo("Foo"));
            Assert.That(newState.OtherState, Is.EqualTo("Bar"));
        }
Ejemplo n.º 38
0
        public void DefaultEventRouteIsInvokedWhenSpecified()
        {
            // Setup the event router.
            var builder = new ApplyEventRouterBuilder <MyAggregateRoot, IEvent>();

            builder.Add <SomethingHappened>((agg, e) => agg.ApplyEvent(e));
            builder.Add <IEvent>((agg, e) => agg.DefaultApplyEvent(e)); // Default route
            var router = builder.Build();

            // Set up the initial state.
            var initialState = new MyAggregateRoot();

            // Create some events to apply to the aggregate.
            var event1 = new SomethingHappened("Foo");
            var event2 = new SomethingElseHappened("Bar");

            // Route the events.
            var newState = router.Route(initialState, event1, event2);

            Assert.That(newState.State, Is.EqualTo("Foo"));

            // We expect SomethingElseHappened to do nothing, since the default route just returns "this".
            Assert.That(newState.OtherState, Is.Null);
        }
Ejemplo n.º 39
0
        public void Initializing_from_history_should_throw_an_exception_when_the_history_was_null()
        {
            IEnumerable<SourcedEvent> nullHistory = null;
            var theAggregate = new MyAggregateRoot();

            Action act = () => theAggregate.InitializeFromHistory(nullHistory);

            act.ShouldThrow<ArgumentNullException>();
        }
Ejemplo n.º 40
0
        public void Initializing_from_history_should_not_throw_an_exception_when_the_history_was_empty()
        {
            var theAggregate = new MyAggregateRoot();

            IEnumerable<SourcedEvent> history = new SourcedEvent[0];

            theAggregate.InitializeFromHistory(history);
        }
Ejemplo n.º 41
0
        public void Initiazling_from_history_with_correct_sequence_should_not_throw_exception()
        {
            var theAggregate = new MyAggregateRoot();

            var stream = new CommittedEventStream(theAggregate.EventSourceId,
                new CommittedEvent(Guid.NewGuid(), Guid.NewGuid(), theAggregate.EventSourceId, 1, DateTime.UtcNow, new HandledEvent(), new Version(1, 0)),
                new CommittedEvent(Guid.NewGuid(), Guid.NewGuid(), theAggregate.EventSourceId, 2, DateTime.UtcNow, new HandledEvent(), new Version(1, 0)),
                new CommittedEvent(Guid.NewGuid(), Guid.NewGuid(), theAggregate.EventSourceId, 3, DateTime.UtcNow, new HandledEvent(), new Version(1, 0)),
                new CommittedEvent(Guid.NewGuid(), Guid.NewGuid(), theAggregate.EventSourceId, 4, DateTime.UtcNow, new HandledEvent(), new Version(1, 0)),
                new CommittedEvent(Guid.NewGuid(), Guid.NewGuid(), theAggregate.EventSourceId, 5, DateTime.UtcNow, new HandledEvent(), new Version(1, 0)));

            theAggregate.InitializeFromHistory(stream);
        }
Ejemplo n.º 42
0
        public void Applying_an_event_should_call_the_event_handler_only_once()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            theAggregate.FooEventHandlerInvokeCount.Should().Be(1);
        }
Ejemplo n.º 43
0
        public void Loading_it_from_history_should_apply_all_events()
        {
            var aggId = Guid.NewGuid();
            var history = new[] { new HandledEvent(Guid.NewGuid(), aggId, 1, DateTime.UtcNow), new HandledEvent(Guid.NewGuid(), aggId, 2, DateTime.UtcNow), new HandledEvent(Guid.NewGuid(), aggId, 3, DateTime.UtcNow) };

            var theAggregate = new MyAggregateRoot(history);

            theAggregate.FooEventHandlerInvokeCount.Should().Be(3);
        }
Ejemplo n.º 44
0
        public void It_should_initialize_with_no_uncommited_events()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.GetUncommittedEvents().Count().Should().Be(0);
        }
Ejemplo n.º 45
0
        public void Applying_an_event_when_there_is_no_unit_of_work_should_cause_an_exception()
        {
            var theAggregate = new MyAggregateRoot();
            Action act = ()=> theAggregate.MethodThatCausesAnEventThatHasAHandler();

            act.ShouldThrow<NoUnitOfWorkAvailableInThisContextException>();
        }
Ejemplo n.º 46
0
        public void Applying_an_event_should_the_the_version()
        {
            using (NcqrsEnvironment.Get<IUnitOfWorkFactory>().CreateUnitOfWork())
            {
                var theAggregate = new MyAggregateRoot();

                theAggregate.Version.Should().Be(0);

                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.Version.Should().Be(1);
                theAggregate.GetUncommittedEvents().Last().EventSequence.Should().Be(1);

                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.Version.Should().Be(2);
                theAggregate.GetUncommittedEvents().Last().EventSequence.Should().Be(2);

                theAggregate.MethodThatCausesAnEventThatHasAHandler();
            }
        }
Ejemplo n.º 47
0
        public void It_should_initialize_with_version_0()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.Version.Should().Be(0);
        }
Ejemplo n.º 48
0
        public void It_should_initialize_with_no_uncommited_events()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.GetUncommittedEvents().Count().Should().Be(0);
        }
Ejemplo n.º 49
0
        public void Applying_an_event_to_an_agg_root_with_history_should_call_the_event_handler_only_once()
        {
            var theAggregate = new MyAggregateRoot();

            var event1 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 1, DateTime.UtcNow);
            var event2 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 2, DateTime.UtcNow);
            var event3 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 3, DateTime.UtcNow);
            var event4 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 4, DateTime.UtcNow);
            var event5 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 5, DateTime.UtcNow);

            IEnumerable<SourcedEvent> history = new[] { event1, event2, event3, event4, event5 };

            theAggregate.InitializeFromHistory(history);

            var eventHandlerCountAfterInitialization = theAggregate.FooEventHandlerInvokeCount;

            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            theAggregate.FooEventHandlerInvokeCount.Should().Be(eventHandlerCountAfterInitialization + 1);
        }
Ejemplo n.º 50
0
        public void Initiazling_from_history_with_correct_sequence_should_not_throw_exception()
        {
            var theAggregate = new MyAggregateRoot();

            var event1 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 1, DateTime.UtcNow);
            var event2 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 2, DateTime.UtcNow);
            var event3 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 3, DateTime.UtcNow);
            var event4 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 4, DateTime.UtcNow);
            var event5 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 5, DateTime.UtcNow);

            IEnumerable<SourcedEvent> history = new[] { event1, event2, event3, event4, event5 };

            theAggregate.InitializeFromHistory(history);
        }
Ejemplo n.º 51
0
        public void Initiazling_from_wrong_history_with_wrong_sequence_should_throw_exception2()
        {
            var theAggregate = new MyAggregateRoot();
            long wrongSequence = 8;

            var event1 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 0, DateTime.UtcNow);
            var event2 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, 1, DateTime.UtcNow);
            var event3 = new HandledEvent(Guid.NewGuid(), theAggregate.EventSourceId, wrongSequence, DateTime.UtcNow);

            IEnumerable<SourcedEvent> history = new[] { event1, event2, event3 };

            Action act = () => theAggregate.InitializeFromHistory(history);
            act.ShouldThrow<InvalidOperationException>().And.Message.Should().Contain("sequence");
        }
Ejemplo n.º 52
0
        public void Applying_an_event_that_is_already_owned_by_another_source_should_cause_an_exception()
        {
            var theEvent = new HandledEvent();
            theEvent.OverrideAggregateRootId(Guid.NewGuid());
            var theAggregate = new MyAggregateRoot();

            Action act = () => theAggregate.ApplyEvent(theEvent);

            act.ShouldThrow<InvalidOperationException>();
        }
Ejemplo n.º 53
0
        public void It_could_not_be_loaded_from_history_when_it_already_contains_uncommitted_events()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            var history = new[] {new HandledEvent(), new HandledEvent()};
            Action act = () => theAggregate.InitializeFromHistory(history);

            act.ShouldThrow<InvalidOperationException>();
        }
Ejemplo n.º 54
0
 public void Applying_an_event_when_there_is_no_unit_of_work_should_not_cause_an_exception()
 {
     var theAggregate = new MyAggregateRoot();
     Action act = ()=>theAggregate.MethodThatCausesAnEventThatHasAHandler();
     act.ShouldNotThrow();
 }
Ejemplo n.º 55
0
        public void It_should_initialize_with_version_0()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.Version.Should().Be(0);
        }
Ejemplo n.º 56
0
        public void Applying_an_event_with_no_handler_should_cause_an_exception()
        {
            var theAggregate = new MyAggregateRoot();
            Action act = theAggregate.MethodThatCausesAnEventThatDoesNotConaintAHandler;

            act.ShouldThrow<EventNotHandledException>();
        }
Ejemplo n.º 57
0
        public void Applying_an_event_should_not_effect_the_initial_version()
        {
            var theAggregate = new MyAggregateRoot();

                theAggregate.InitialVersion.Should().Be(0);
                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.InitialVersion.Should().Be(0);
                theAggregate.MethodThatCausesAnEventThatHasAHandler();
                theAggregate.InitialVersion.Should().Be(0);
                theAggregate.MethodThatCausesAnEventThatHasAHandler();
        }
Ejemplo n.º 58
0
        public void Constructing_it_with_an_id_should_set_that_to_EventSourceId_property()
        {
            var theId = Guid.NewGuid();
            var theAggregate = new MyAggregateRoot(theId);

            theAggregate.EventSourceId.Should().Be(theId);
        }
Ejemplo n.º 59
0
        public void Getting_the_uncommitted_via_the_IEventSource_interface_should_return_the_same_as_directly()
        {
            var theAggregate = new MyAggregateRoot();

            theAggregate.MethodThatCausesAnEventThatHasAHandler();
            theAggregate.MethodThatCausesAnEventThatHasAHandler();

            var directResult = theAggregate.GetUncommittedEvents();
            var viaInterfaceResult = ((IEventSource) theAggregate).GetUncommittedEvents();
            directResult.Should().BeEquivalentTo(viaInterfaceResult);
        }
Ejemplo n.º 60
0
        public void Initiazling_from_wrong_history_with_wrong_sequence_should_throw_exception()
        {
            var theAggregate = new MyAggregateRoot();
            const long wrongSequence = 3;
            var stream = new CommittedEventStream(theAggregate.EventSourceId,
                new CommittedEvent(Guid.NewGuid(), Guid.NewGuid(), theAggregate.EventSourceId, wrongSequence, DateTime.UtcNow, new HandledEvent(), new Version(1, 0)));

            Action act = ()=> theAggregate.InitializeFromHistory(stream);
            act.ShouldThrow<InvalidOperationException>().And.Message.Should().Contain("sequence");
        }