public void GivenEmptyEventStore_WhenLoadingEvents_ThenNothingShouldBeReturned()
        {
            // Arrange
            IEventStore store = new InMemoryEventStore();

            // Act
            IEnumerable<object> events = store.Load(Guid.NewGuid());

            // Assert
            CollectionAssert.IsEmpty(events);
        }
        public void GivenEmptyEventStore_WhenAddingEventsOneAtATime_ThenStoreShouldContainThoseEvents()
        {
            // Arrange
            IEventStore store = new InMemoryEventStore();
            var userId = Guid.NewGuid();
            var userRegistered = new UserRegistered(userId);

            // Act
            store.Store(userId, userRegistered);

            // Assert
            CollectionAssert.IsNotEmpty(((InMemoryEventStore)store).Events);
            Assert.AreSame(userRegistered, ((InMemoryEventStore)store).Events.First());
        }
Beispiel #3
0
        public void GivenAggregateWithNoUncommittedEvents_WhenSaved_ThenEventStoreShouldContainNoAdditionalEvents()
        {
            // Arrange
            var eventStore = new InMemoryEventStore();
            var repository = new EventSourcingRepository<User>(eventStore, new Mock<IConcurrencyMonitor>().Object);
            var user = new User();

            var expectedEvents = Enumerable.Empty<object>();

            // Act
            repository.Save(user);

            // Assert
            CollectionAssertAreJsonEqual(expectedEvents, eventStore.Events);
        }
        public void GivenEmptyEventStore_WhenAddingEventsInBulk_ThenStoreShouldContainThoseEvents()
        {
            // Arrange
            IEventStore store = new InMemoryEventStore();
            var user = new User();
            user.Register();
            user.ChangePassword("newpassword");

            // Act
            store.Store(user.Id, user.GetUncommittedEvents());

            // Assert
            CollectionAssert.IsNotEmpty(((InMemoryEventStore)store).Events);
            CollectionAssert.AreEqual(user.GetUncommittedEvents(), ((InMemoryEventStore)store).Events);
        }
Beispiel #5
0
        public void GivenAggregateWithUncommittedEvents_WhenSaved_ThenEventStoreShouldContainThoseEvents()
        {
            // Arrange
            var eventStore = new InMemoryEventStore();
            IRepository<User> repository = new EventSourcingRepository<User>(eventStore, new Mock<IConcurrencyMonitor>().Object);
            var user = new User();
            user.Register();

            var expectedEvents = new object[] { new UserRegistered(user.Id) };

            // Act
            repository.Save(user);

            // Assert
            CollectionAssertAreJsonEqual(expectedEvents, eventStore.Events);
        }
Beispiel #6
0
        public void GivenAggregateWithUncommittedEvents_WhenSaved_ThenBaseVersionShouldMatchCurrentVersion()
        {
            // Arrange
            var userId = Guid.NewGuid();
            var eventStore = new InMemoryEventStore();
            var userRegistered = new UserRegistered(userId);
            eventStore.Update(userId, userRegistered);

            var repository = new EventSourcingRepository<User>(eventStore, new Mock<IConcurrencyMonitor>().Object);
            var user = repository.Get(userId);
            user.ChangePassword("newpassword");

            var currentVersion = user.GetUncommittedEvents().Cast<Event>().Last().Version;

            // Act
            repository.Save(user);

            // Assert
            Assert.AreEqual(currentVersion, user.BaseVersion, "User's base version has not been updated to match current version on successful save.");
        }
        public void GivenEventStorePopulatedWithEventsForMultipleAggregates_WhenLoadingEventsForAnAggregate_ThenShouldReturnEventsForThatAggregate()
        {
            // Arrange
            IEventStore store = new InMemoryEventStore();

            var user1 = new User();
            user1.Register();
            user1.ChangePassword("user1pwd1");
            user1.ChangePassword("user1pwd2");

            var user2 = new User();
            user2.Register();
            user2.ChangePassword("user2pwd1");
            user2.ChangePassword("user2pwd2");

            store.Store(user1.Id, user1.GetUncommittedEvents());
            store.Store(user2.Id, user2.GetUncommittedEvents());

            // Act
            IEnumerable<object> eventsForUser1 = store.Load(user1.Id);

            // Assert
            CollectionAssert.AreEqual(user1.GetUncommittedEvents(), eventsForUser1, "Store didn't return user1's events properly.");
        }
Beispiel #8
0
        public void GivenPopulatedEventStore_WhenLoadingSpecificVersionOfAggregate_ThenRepositoryShouldRebuildThatAggregateToThatVersion()
        {
            // Arrange
            var eventStore = new InMemoryEventStore();
            var userId = Guid.NewGuid();
            var events = new object[]
            {
                new UserRegistered(userId),
                new UserChangedPassword("newpassword"),
                new UserChangedPassword("newnewpassword")
            };
            eventStore.Update(userId, events);
            var repository = new EventSourcingRepository<User>(eventStore, new Mock<IConcurrencyMonitor>().Object);

            // Act
            User user = repository.Get(userId, ((Event)events[1]).Version);

            // Assert
            Assert.AreEqual(((Event)events[1]).Version, user.BaseVersion);
        }
Beispiel #9
0
        public void GivenPopulatedEventStore_WhenLoadingAggregate_ThenRepositoryShouldRebuildThatAggregate()
        {
            // Arrange
            var eventStore = new InMemoryEventStore();
            var userId = Guid.NewGuid();
            eventStore.Update(
                userId,
                new object[]
                {
                    new UserRegistered(userId),
                    new UserChangedPassword("newpassword"),
                    new UserChangedPassword("newnewpassword")
                });
            var repository = new EventSourcingRepository<User>(eventStore, new Mock<IConcurrencyMonitor>().Object);

            // Act
            User user = repository.Get(userId);

            // Assert
            Assert.Throws<InvalidOperationException>(() => user.ChangePassword("newnewpassword")); // Should fail if the user's events were correctly applied
        }
Beispiel #10
0
        public void GivenPopulatedEventStore_WhenLoadingAggregate_ThenAggregateVersionShouldReflectStoredEvents()
        {
            // Arrange
            var eventStore = new InMemoryEventStore();
            var userId = Guid.NewGuid();
            var latestVersion = Guid.NewGuid();
            eventStore.Update(
                userId,
                new object[]
                {
                    new UserRegistered(userId),
                    new UserChangedPassword("newpassword"),
                    new UserChangedPassword("newnewpassword") { Version = latestVersion }
                });
            var repository = new EventSourcingRepository<User>(eventStore, new Mock<IConcurrencyMonitor>().Object);

            // Act
            User user = repository.Get(userId);

            // Assert
            Assert.AreEqual(latestVersion, user.BaseVersion);
        }
Beispiel #11
0
        public void GivenExistingAggregateWithUnseenChanges_WhenSaving_ThenShouldCheckConcurrencyWithCorrectEvents()
        {
            // Arrange
            var userId = Guid.NewGuid();
            var eventStore = new InMemoryEventStore();
            eventStore.Update(userId, new UserRegistered(userId));

            var concurrencyMonitor = new Mock<IConcurrencyMonitor>();
            var repository = new EventSourcingRepository<User>(eventStore, concurrencyMonitor.Object);
            var user = repository.Get(userId);

            // Now another user changes the password before we get chance to save our changes:
            eventStore.Update(userId, new UserChangedPassword("adifferentpassword"));

            user.ChangePassword("newpassword");

            // Act
            repository.Save(user);

            // Assert
            concurrencyMonitor.Verify(
                monitor =>
                monitor.CheckForConflicts(
                    It.Is<IEnumerable<object>>(
                        unseenEvents => (unseenEvents.Single() as UserChangedPassword).NewPassword == "adifferentpassword"),
                    It.Is<IEnumerable<object>>(
                        uncommittedEvents => (uncommittedEvents.Single() as UserChangedPassword).NewPassword == "newpassword")));
        }
        public void GivenEventStorePopulatedWithManyEventsForAnAggregate_WhenLoadingForSpecificVersion_ThenShouldOnlyLoadEventsUpToAndIncludingThatVersion()
        {
            // Arrange
            IEventStore store = new InMemoryEventStore();
            var id = Guid.NewGuid();
            var allEvents = new object[]
                                {
                                    new UserRegistered(id),          // v1
                                    new UserChangedPassword("pwd1"), // v2
                                    new UserChangedPassword("pwd2"), // v3
                                    new UserChangedPassword("pwd3"), // v4
                                    new UserChangedPassword("pwd4"), // v5
                                };
            store.Store(id, allEvents);

            // Act
            IEnumerable<object> version3 = store.Load(id, ((Event)allEvents[2]).Version);

            // Assert
            CollectionAssert.AreEqual(allEvents.Take(3), version3);
        }