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>(); }
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); }
public async Task StoreAsync_ExistingEntity_NewEventsSaved() { var eventStore = _dbContext.Persons.Add(new PersonEventStore { Id = 12, EntityVersion = 1 }).Entity; eventStore.Events.Add(new PersonEvent { SerializedEvent = "{" + "\"$type\": \"Silverback.Tests.EventSourcing.EntityFrameworkCore.TestTypes.Person+NameChangedEvent, Silverback.EventSourcing.EntityFrameworkCore.Tests\"," + "\"NewName\": \"Silverback\"" + "}" }); _dbContext.SaveChanges(); var repo = new PersonEventStoreRepository(_dbContext); var person = await repo.GetAsync(p => p.Id == 12); person.ChangeName("Sergio"); person.ChangeAge(35); await repo.StoreAsync(person); await _dbContext.SaveChangesAsync(); _dbContext.Persons.Count().Should().Be(1); _dbContext.Persons.First().Events.Count.Should().Be(3); }
public async Task StoreAsync_EntityWithSomeEvents_VersionCalculated() { var repo = new PersonEventStoreRepository(); 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 PersonEventStoreRepository(_dbContext); var person = new Person(); person.ChangeName("Sergio"); person.ChangeAge(35); await repo.StoreAsync(person); await _dbContext.SaveChangesAsync(); _dbContext.Persons.Count().Should().Be(1); _dbContext.Persons.First().Events.Count.Should().Be(2); }