public void When_a_command_reserves_a_unique_value_then_a_subsequent_request_by_a_different_owner_fails()
        {
            // arrange
            var username = Any.CamelCaseName(5);

            // act
            var account1 = new CustomerAccount();

            account1.Apply(new RequestUserName
            {
                UserName  = username,
                Principal = new Customer
                {
                    Name = Any.CamelCaseName()
                }
            });
            account1.ConfirmSave();

            var account2        = new CustomerAccount();
            var secondPrincipal = new Customer
            {
                Name = Any.CamelCaseName()
            };
            var secondAttempt = account2.Validate(new RequestUserName
            {
                UserName  = username,
                Principal = secondPrincipal
            });

            // assert
            secondAttempt.ShouldBeInvalid($"The user name {username} is taken. Please choose another.");
        }
Exemple #2
0
        public void Reserving_a_unique_value_can_happen_during_command_validation()
        {
            // arrange
            var name      = Any.CamelCaseName();
            var firstCall = true;

            Configuration.Current.ReservationService = new FakeReservationService((value, scope, actor) =>
            {
                if (firstCall)
                {
                    firstCall = false;
                    return(true);
                }
                return(false);
            });

            // act
            var account1 = new CustomerAccount();

            account1.Apply(new RequestUserName
            {
                UserName = name
            });
            account1.ConfirmSave();

            var account2      = new CustomerAccount();
            var secondAttempt = account2.Validate(new RequestUserName
            {
                UserName = name
            });

            // assert
            secondAttempt.ShouldBeInvalid(string.Format("The user name {0} is taken. Please choose another.", name));
        }
        public void When_a_command_reserves_a_unique_value_then_a_subsequent_request_by_the_same_owner_succeeds()
        {
            // arrange
            var username = Any.CamelCaseName(5);

            // act
            var account1  = new CustomerAccount();
            var principal = new Customer
            {
                Name = Any.CamelCaseName()
            };

            account1.Apply(new RequestUserName
            {
                UserName  = username,
                Principal = principal
            });
            account1.ConfirmSave();

            var account2      = new CustomerAccount();
            var secondAttempt = account2.Validate(new RequestUserName
            {
                UserName  = username,
                Principal = principal
            });

            // assert
            secondAttempt.ShouldBeValid();
        }
        public void Reserving_a_unique_value_can_happen_during_command_validation()
        {
            // arrange
            var name = Any.CamelCaseName();
            var firstCall = true;
            Configuration.Current.ReservationService = new FakeReservationService((value, scope, actor) =>
            {
                if (firstCall)
                {
                    firstCall = false;
                    return true;
                }
                return false;
            });

            // act
            var account1 = new CustomerAccount();
            account1.Apply(new RequestUserName
            {
                UserName = name
            });
            account1.ConfirmSave();

            var account2 = new CustomerAccount();
            var secondAttempt = account2.Validate(new RequestUserName
            {
                UserName = name
            });

            // assert
            secondAttempt.ShouldBeInvalid(string.Format("The user name {0} is taken. Please choose another.", name));
        }
        public void When_instantiated_from_a_snapshot_the_aggregate_version_is_correct()
        {
            // arrange
            var customerAccount = new CustomerAccount()
                                  .Apply(new ChangeEmailAddress(Any.Email()));

            customerAccount.ConfirmSave();
            var snapshot = customerAccount.CreateSnapshot();

            snapshot.Version = 12;

            // act
            var accountFromSnapshot = new CustomerAccount(snapshot);

            // assert
            accountFromSnapshot.Version.Should().Be(12);
        }
        public async Task ETags_are_saved_in_the_snapshot()
        {
            var etag = Any.Guid().ToString();
            var account = new CustomerAccount().Apply(new RequestSpam
                                                      {
                                                          ETag = etag
                                                      });
            account.ConfirmSave();

            var repository = new InMemorySnapshotRepository();
            await repository.SaveSnapshot(account);

            var snapshot = await repository.GetSnapshot(account.Id);

            snapshot.ETags
                    .Should()
                    .Contain(etag);
        }
Exemple #7
0
        public async Task ETags_are_saved_in_the_snapshot()
        {
            var etag    = Any.Guid().ToString();
            var account = new CustomerAccount().Apply(new RequestSpam
            {
                ETag = etag
            });

            account.ConfirmSave();

            var repository = new InMemorySnapshotRepository();
            await repository.SaveSnapshot(account);

            var snapshot = await repository.GetSnapshot(account.Id);

            snapshot.ETags
            .Should()
            .Contain(etag);
        }
        public void EventSourcedAggregate_can_be_rehydrated_from_an_empty_event_sequence_when_using_a_snapshot()
        {
            // arrange
            var emailAddress = Any.Email();

            var customerAccount = new CustomerAccount()
                                  .Apply(new ChangeEmailAddress(emailAddress));

            customerAccount.ConfirmSave();
            var snapshot = customerAccount.CreateSnapshot();

            //act
            var fromSnapshot = new CustomerAccount(snapshot);

            // assert
            fromSnapshot.EmailAddress
            .Should()
            .Be(emailAddress);
        }
        public void An_aggregate_instantiated_from_a_snapshot_adds_new_events_at_the_correct_sequence_number()
        {
            // arrange
            var account = new CustomerAccount()
                          .Apply(new ChangeEmailAddress(Any.Email()));

            account.ConfirmSave();
            var snapshot = account.CreateSnapshot();

            snapshot.Version = 12;

            // act
            var accountFromSnapshot = new CustomerAccount(snapshot);

            accountFromSnapshot.Apply(new RequestNoSpam());

            // assert
            accountFromSnapshot.Version.Should().Be(13);
            accountFromSnapshot.PendingEvents.Last().SequenceNumber.Should().Be(13);
        }
Exemple #10
0
        public async Task Repeated_ETags_are_not_repeated_in_the_snapshot()
        {
            var account = new CustomerAccount()
                          .Apply(new RequestSpam
            {
                ETag = "a"
            })
                          .Apply(new RequestSpam
            {
                ETag = "a"
            });

            account.ConfirmSave();

            var repository = new InMemorySnapshotRepository();
            await repository.SaveSnapshot(account);

            var snapshot = await repository.GetSnapshot(account.Id);

            snapshot.ETags
            .Should()
            .ContainSingle(etag => etag == "a");
        }
        public void Accessing_event_history_from_an_aggregate_instantiated_using_a_snapshot_throws()
        {
            // arrange
            var account = new CustomerAccount()
                          .Apply(new ChangeEmailAddress(Any.Email()));

            account.ConfirmSave();
            var snapshot = account
                           .CreateSnapshot();

            snapshot.Version = 10;
            account          = new CustomerAccount(snapshot);

            // act
            Action getEvents = () => account.Events();

            // assert
            getEvents.ShouldThrow <InvalidOperationException>()
            .And
            .Message
            .Should()
            .Contain("Aggregate was sourced from a snapshot, so event history is unavailable.");
        }
        public void Attempting_to_re_source_an_aggregate_that_was_instantiated_using_a_snapshot_succeeds_if_the_specified_version_is_at_or_after_the_snapshot()
        {
            // arrange
            var originalEmail = Any.Email();

            var account = new CustomerAccount()
                          .Apply(new ChangeEmailAddress(originalEmail));

            account.ConfirmSave();

            var snapshot = account.CreateSnapshot();

            snapshot.Version = 10;
            account          = new CustomerAccount(snapshot);

            account.Apply(new ChangeEmailAddress(Any.Email()));

            // act
            var accountAtVersion10 = account.AsOfVersion(10);

            // arrange
            accountAtVersion10.Version.Should().Be(10);
            accountAtVersion10.EmailAddress.Should().Be(originalEmail);
        }
        public void Attempting_to_re_source_an_aggregate_that_was_instantiated_using_a_snapshot_throws_if_the_specified_version_is_prior_to_the_snapshot()
        {
            // arrange
            var account = new CustomerAccount()
                          .Apply(new ChangeEmailAddress(Any.Email()));

            account.ConfirmSave();
            var snapshot = account
                           .CreateSnapshot();

            snapshot.Version = 10;

            account = new CustomerAccount(snapshot);

            // act
            Action rollBack = () => account.AsOfVersion(9);

            // assert
            rollBack.ShouldThrow <InvalidOperationException>()
            .And
            .Message
            .Should()
            .Contain("Snapshot version is later than specified version.");
        }
        public async Task Repeated_ETags_are_not_repeated_in_the_snapshot()
        {
            var account = new CustomerAccount()
                .Apply(new RequestSpam
                       {
                           ETag = "a"
                       })
                .Apply(new RequestSpam
                       {
                           ETag = "a"
                       });
            account.ConfirmSave();

            var repository = new InMemorySnapshotRepository();
            await repository.SaveSnapshot(account);

            var snapshot = await repository.GetSnapshot(account.Id);

            snapshot.ETags
                    .Should()
                    .ContainSingle(etag => etag == "a");
        }