public async Task StoreAsync_ConcurrentlyModifyExistingEntity_ExceptionThrown() { var eventStore = new PersonEventStore { Id = 12, EntityVersion = 1 }; eventStore.Events.Add(new PersonEvent { SerializedEvent = "{" + "\"$type\": \"Silverback.Tests.EventSourcing.TestTypes.Person+NameChangedEvent, Silverback.EventSourcing.Tests\"," + "\"NewName\": \"Silverback\"" + "}" }); var repo = new PersonInMemoryEventStoreRepository(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>(); }
public async Task StoreAsync_ExistingEntity_NewEventsSaved() { var eventStore = new PersonEventStore { Id = 12, EntityVersion = 1 }; eventStore.Events.Add(new PersonEvent { SerializedEvent = "{" + "\"$type\": \"Silverback.Tests.EventSourcing.TestTypes.Person+NameChangedEvent, Silverback.EventSourcing.Tests\"," + "\"NewName\": \"Silverback\"" + "}" }); var repo = new PersonInMemoryEventStoreRepository(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); }
public async Task StoreAsync_ExistingEntity_VersionIncremented() { var eventStore = new PersonEventStore { Id = 12, EntityVersion = 1 }; eventStore.Events.Add( new PersonEvent { SerializedEvent = "{\"NewName\": \"Silverback\"}", ClrType = "Silverback.Tests.EventSourcing.TestTypes.EntityEvents.NameChangedEvent, Silverback.EventSourcing.Tests" }); var repo = new PersonInMemoryEventStoreRepository(eventStore); var person = repo.GetById(12); person.ChangeName("Sergio"); person.ChangeAge(35); await repo.StoreAsync(person); repo.EventStores.First().EntityVersion.Should().Be(3); }
public async Task StoreAsync_EntityWithSomeEvents_VersionCalculated() { var repo = new PersonInMemoryEventStoreRepository(); var person = new Person(); person.ChangeName("Sergio"); person.ChangeAge(35); await repo.StoreAsync(person); repo.EventStores.First().EntityVersion.Should().Be(2); }
public async Task StoreAsync_EntityWithSomeEvents_EventsSaved() { var repo = new PersonInMemoryEventStoreRepository(); var person = new Person(); person.ChangeName("Sergio"); person.ChangeAge(35); await repo.StoreAsync(person); repo.EventStores.Should().HaveCount(1); repo.EventStores.First().Events.Should().HaveCount(2); }