Exemple #1
0
            public User(Guid id, string name)
            {
                var @event = new UserCreated(id, name);

                Enqueue(@event);
                Apply(@event);
            }
        public void AggregateStream_ShouldReturnSpecifiedVersionOfTheStream()
        {
            var streamId             = Guid.NewGuid();
            var userCreated          = new UserCreated(streamId, "John Doe");
            var userNameUpdated      = new UserNameUpdated(streamId, "Adam Smith");
            var userNameUpdatedAgain = new UserNameUpdated(streamId, "Michael Newman");

            eventStore.AppendEvent <User>(streamId, userCreated);
            eventStore.AppendEvent <User>(streamId, userNameUpdated);
            eventStore.AppendEvent <User>(streamId, userNameUpdatedAgain);

            var aggregateAtVersion1 = eventStore.AggregateStream <User>(streamId, 1);

            aggregateAtVersion1.Id.Should().Be(streamId);
            aggregateAtVersion1.Name.Should().Be(userCreated.UserName);
            aggregateAtVersion1.Version.Should().Be(1);

            var aggregateAtVersion2 = eventStore.AggregateStream <User>(streamId, 2);

            aggregateAtVersion2.Id.Should().Be(streamId);
            aggregateAtVersion2.Name.Should().Be(userNameUpdated.UserName);
            aggregateAtVersion2.Version.Should().Be(2);

            var aggregateAtVersion3 = eventStore.AggregateStream <User>(streamId, 3);

            aggregateAtVersion3.Id.Should().Be(streamId);
            aggregateAtVersion3.Name.Should().Be(userNameUpdatedAgain.UserName);
            aggregateAtVersion3.Version.Should().Be(3);
        }
Exemple #3
0
 private void Apply(UserDashboard item, UserCreated @event)
 {
     item.Id          = @event.UserId;
     item.UserName    = @event.UserName;
     item.OrdersCount = 0;
     item.TotalAmount = 0;
 }
Exemple #4
0
        public void AppendEventFunction_WhenStreamDoesNotExist_CreateNewStream_And_AppendNewEvent()
        {
            var streamId = Guid.NewGuid();
            var @event   = new UserCreated("John Doe");

            var result = eventStore.AppendEvent <User>(streamId, @event);

            result.Should().BeTrue();

            var wasStreamCreated = databaseConnection.QuerySingle <bool>(
                "select exists (select 1 from streams where id = @streamId)", new { streamId }
                );

            wasStreamCreated.Should().BeTrue();

            wasEventAppended(streamId);

            var stream = databaseConnection.QueryFirst <Stream>(
                "select version from streams where id = @streamId", new { streamId }
                );

            stream.Version.Should().Be(1);

            streamId = Guid.NewGuid();

            eventStore.AppendEvent <UserCreated>(streamId, @event, 0);

            stream = databaseConnection.QuerySingle <Stream>(
                "select version from streams where id = @streamId", new { streamId }
                );

            stream.Version.Should().Be(0);
        }
 void Apply(UserCreated @event)
 {
     databaseConnection.Execute(
         @"INSERT INTO UserDashboards (Id, UserName, OrdersCount, TotalAmount)
         VALUES (@UserId, @UserName, 0, 0)",
         @event
         );
 }
        public void GetStreamState_ShouldReturnProperStreamInfo()
        {
            var streamId = Guid.NewGuid();
            var @event   = new UserCreated(streamId, "John Doe");

            eventStore.AppendEvent <User>(streamId, @event);

            var streamState = eventStore.GetStreamState(streamId);

            streamState.Id.Should().Be(streamId);
            streamState.Type.Should().Be(typeof(User));
            streamState.Version.Should().Be(1);
        }
Exemple #7
0
        public void AggregateStream_ShouldReturnObjectWithStateBasedOnEvents()
        {
            var streamId        = Guid.NewGuid();
            var userCreated     = new UserCreated(streamId, "John Doe");
            var userNameUpdated = new UserNameUpdated(streamId, "Adam Smith");

            eventStore.AppendEvent <User>(streamId, userCreated);
            eventStore.AppendEvent <User>(streamId, userNameUpdated);

            var aggregate = eventStore.AggregateStream <User>(streamId);

            aggregate.Id.Should().Be(streamId);
            aggregate.Name.Should().Be(userNameUpdated.UserName);
            aggregate.Version.Should().Be(2);
        }
        public void GetEvents_ShouldReturnAppendedEvents()
        {
            var streamId        = Guid.NewGuid();
            var userCreated     = new UserCreated(streamId, "John Doe");
            var userNameUpdated = new UserNameUpdated(streamId, "Adam Smith");

            eventStore.AppendEvent <User>(streamId, userCreated);
            eventStore.AppendEvent <User>(streamId, userNameUpdated);

            var events = eventStore.GetEvents(streamId);

            events.Should().HaveCount(2);

            events.OfType <UserCreated>().Should().Contain(
                e => e.UserId == userCreated.UserId && e.UserName == userCreated.UserName);

            events.OfType <UserNameUpdated>().Should().Contain(
                e => e.UserId == userNameUpdated.UserId && e.UserName == userNameUpdated.UserName);
        }
        public void AppendEventFunction_WhenStreamDoesNotExist_CreateNewStream_And_AppendNewEvent()
        {
            var streamId = Guid.NewGuid();
            var @event   = new UserCreated("John Doe");

            var result = eventStore.AppendEvent <User>(streamId, @event);

            result.Should().BeTrue();

            var wasStreamCreated = databaseConnection.QuerySingle <bool>(
                "select exists (select 1 from streams where id = @streamId)", new { streamId }
                );

            wasStreamCreated.Should().BeTrue();

            var wasEventAppended = databaseConnection.QuerySingle <bool>(
                "select exists (select 1 from events where stream_id = @streamId)", new { streamId }
                );

            wasEventAppended.Should().BeTrue();

            // todo update this test to check versions are correct
        }
Exemple #10
0
 private void Apply(UserCreated @event)
 {
     Id   = @event.UserId;
     Name = @event.UserName;
 }
Exemple #11
0
 private void Apply(UserCreated @event)
 {
     throw new NotImplementedException("TODO");
 }