Ejemplo n.º 1
0
        public async Task StoreAsync_ConcurrentlyModifyExistingEntity_ExceptionThrown()
        {
            var eventStore = new PersonEventStore {
                PersonId = 12, EntityVersion = 1
            };

            eventStore.Events.Add(new PersonEventStore.PersonEvent
            {
                SerializedEvent = "{" +
                                  "\"$type\": \"Silverback.Tests.EventSourcing.TestTypes.Person+NameChangedEvent, Silverback.EventSourcing.Tests\"," +
                                  "\"NewName\": \"Silverback\"" +
                                  "}"
            });

            var repo = new PersonEventStoreRepository(eventStore);

            var person  = repo.GetById(12);
            var person2 = repo.GetById(12);

            person.ChangeName("Sergio");
            person.ChangeAge(35);
            person2.ChangeName("Sergio");
            person2.ChangeAge(35);

            await repo.StoreAsync(person);

            Func <Task> act = async() => await repo.StoreAsync(person2);

            act.Should().Throw <SilverbackConcurrencyException>();
        }
Ejemplo n.º 2
0
        public async Task StoreAsync_ExistingEntity_NewEventsSaved()
        {
            var eventStore = new PersonEventStore {
                PersonId = 12, EntityVersion = 1
            };

            eventStore.Events.Add(new PersonEventStore.PersonEvent
            {
                SerializedEvent = "{" +
                                  "\"$type\": \"Silverback.Tests.EventSourcing.TestTypes.Person+NameChangedEvent, Silverback.EventSourcing.Tests\"," +
                                  "\"NewName\": \"Silverback\"" +
                                  "}"
            });

            var repo = new PersonEventStoreRepository(eventStore);

            var person = repo.GetById(12);

            person.ChangeName("Sergio");
            person.ChangeAge(35);

            await repo.StoreAsync(person);

            repo.EventStores.Count.Should().Be(1);
            repo.EventStores.First().Events.Count.Should().Be(3);
        }
Ejemplo n.º 3
0
        public async Task RemoveAsync_ExistingEntity_EntityDeleted()
        {
            var eventStore = new PersonEventStore {
                PersonId = 12
            };

            eventStore.Events.Add(new PersonEventStore.PersonEvent
            {
                SerializedEvent = "{" +
                                  "\"$type\": \"Silverback.Tests.EventSourcing.TestTypes.Person+NameChangedEvent, Silverback.EventSourcing.Tests\"," +
                                  "\"NewName\": \"Silverback\"" +
                                  "}"
            });

            var repo = new PersonEventStoreRepository(eventStore);

            var entity = repo.GetById(12);

            entity.Should().NotBeNull();

            await repo.RemoveAsync(entity);

            repo.EventStores.Count.Should().Be(0);
            repo.EventStores.SelectMany(s => s.Events).Count().Should().Be(0);
        }
Ejemplo n.º 4
0
        public void GetAggregateEntity_ExistingId_EventsAppliedInRightOrder()
        {
            var eventStore = new PersonEventStore {
                PersonId = 12
            };

            eventStore.Events.Add(new PersonEventStore.PersonEvent
            {
                SerializedEvent = "{" +
                                  "\"$type\": \"Silverback.Tests.EventSourcing.TestTypes.Person+NameChangedEvent, Silverback.EventSourcing.Tests\"," +
                                  "\"NewName\": \"Silverback\"" +
                                  "}",
                Timestamp = DateTime.Parse("2000-05-05")
            });
            eventStore.Events.Add(new PersonEventStore.PersonEvent
            {
                SerializedEvent = "{" +
                                  "\"$type\": \"Silverback.Tests.EventSourcing.TestTypes.Person+NameChangedEvent, Silverback.EventSourcing.Tests\"," +
                                  "\"NewName\": \"Sergio\"" +
                                  "}",
                Timestamp = DateTime.Parse("2000-03-01")
            });
            var repo = new PersonEventStoreRepository(eventStore);

            var entity = repo.GetById(12);

            entity.Name.Should().Be("Silverback");
        }
Ejemplo n.º 5
0
        public void GetAggregateEntity_ExistingId_EventsApplied()
        {
            var eventStore = new PersonEventStore {
                PersonId = 12
            };

            eventStore.Events.Add(new PersonEventStore.PersonEvent
            {
                SerializedEvent = "{" +
                                  "\"$type\": \"Silverback.Tests.EventSourcing.TestTypes.Person+NameChangedEvent, Silverback.EventSourcing.Tests\"," +
                                  "\"NewName\": \"Silverback\"" +
                                  "}"
            });
            eventStore.Events.Add(new PersonEventStore.PersonEvent
            {
                SerializedEvent = "{" +
                                  "\"$type\": \"Silverback.Tests.EventSourcing.TestTypes.Person+AgeChangedEvent, Silverback.EventSourcing.Tests\"," +
                                  "\"NewAge\": 35" +
                                  "}"
            });

            var repo = new PersonEventStoreRepository(eventStore);

            var entity = repo.GetById(12);

            entity.Name.Should().Be("Silverback");
            entity.Age.Should().Be(35);
        }
Ejemplo n.º 6
0
        public void GetAggregateEntity_NonExistingId_NullReturned()
        {
            var repo = new PersonEventStoreRepository();

            var entity = repo.GetById(11);

            entity.Should().BeNull();
        }