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); }
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 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"); }
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 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); }
public void Store_ExistingEntity_VersionIncremented() { 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); repo.Store(person); repo.EventStores.First().EntityVersion.Should().Be(3); }
public async Task StoreAsync_ExistingEntity_NewEventsSaved() { 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.Should().HaveCount(1); repo.EventStores.First().Events.Should().HaveCount(3); }
public async Task RemoveAsync_ExistingEntity_EntityDeleted() { var eventStore = new PersonEventStore { Id = 12 }; eventStore.Events.Add( new PersonEvent { SerializedEvent = "{\"NewName\": \"Silverback\"}", ClrType = "Silverback.Tests.EventSourcing.TestTypes.EntityEvents.NameChangedEvent, Silverback.EventSourcing.Tests" }); var repo = new PersonInMemoryEventStoreRepository(eventStore); var entity = repo.GetById(12); entity.Should().NotBeNull(); await repo.RemoveAsync(entity); repo.EventStores.Should().BeEmpty(); repo.EventStores.SelectMany(s => s.Events).Should().BeEmpty(); }
public void GetAggregateEntity_ExistingId_EventsAppliedInRightOrder() { var eventStore = new PersonEventStore { Id = 12 }; eventStore.Events.Add( new PersonEvent { SerializedEvent = "{\"NewName\": \"Silverback\"}", ClrType = "Silverback.Tests.EventSourcing.TestTypes.EntityEvents.NameChangedEvent, Silverback.EventSourcing.Tests", Timestamp = DateTime.Parse("2000-05-05", CultureInfo.InvariantCulture) }); eventStore.Events.Add( new PersonEvent { SerializedEvent = "{\"NewName\": \"Sergio\"}", ClrType = "Silverback.Tests.EventSourcing.TestTypes.EntityEvents.NameChangedEvent, Silverback.EventSourcing.Tests", Timestamp = DateTime.Parse("2000-03-01", CultureInfo.InvariantCulture) }); var repo = new PersonInMemoryEventStoreRepository(eventStore); var entity = repo.GetById(12); entity.Name.Should().Be("Silverback"); }
public void Store_ConcurrentlyModifyExistingEntity_ExceptionThrown() { 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); var person2 = repo.GetById(12); person.ChangeName("Sergio"); person.ChangeAge(35); person2.ChangeName("Sergio"); person2.ChangeAge(35); repo.Store(person); Action act = () => repo.Store(person2); act.Should().Throw <EventStoreConcurrencyException>(); }
public void GetAggregateEntity_ExistingIdWithPastSnapshot_OnlyRelevantEventsApplied() { 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") }); eventStore.Events.Add(new PersonEventStore.PersonEvent { SerializedEvent = "{" + "\"$type\": \"Silverback.Tests.EventSourcing.TestTypes.Person+AgeChangedEvent, Silverback.EventSourcing.Tests\"," + "\"NewAge\": 16" + "}", Timestamp = DateTime.Parse("2000-02-01") }); eventStore.Events.Add(new PersonEventStore.PersonEvent { SerializedEvent = "{" + "\"$type\": \"Silverback.Tests.EventSourcing.TestTypes.Person+AgeChangedEvent, Silverback.EventSourcing.Tests\"," + "\"NewAge\": 35" + "}", Timestamp = DateTime.Parse("2019-07-06") }); var repo = new PersonEventStoreRepository(eventStore); var entity = repo.GetSnapshotById(12, DateTime.Parse("2000-03-01")); entity.Name.Should().Be("Sergio"); entity.Age.Should().Be(16); }
public void GetAggregateEntity_ExistingIdWithPastSnapshot_OnlyRelevantEventsApplied() { var eventStore = new PersonEventStore { Id = 12 }; eventStore.Events.Add( new PersonEvent { SerializedEvent = "{\"NewName\": \"Silverback\"}", ClrType = "Silverback.Tests.EventSourcing.TestTypes.EntityEvents.NameChangedEvent, Silverback.EventSourcing.Tests", Timestamp = DateTime.Parse("2000-05-05", CultureInfo.InvariantCulture) }); eventStore.Events.Add( new PersonEvent { SerializedEvent = "{\"NewName\": \"Sergio\"}", ClrType = "Silverback.Tests.EventSourcing.TestTypes.EntityEvents.NameChangedEvent, Silverback.EventSourcing.Tests", Timestamp = DateTime.Parse("2000-03-01", CultureInfo.InvariantCulture) }); eventStore.Events.Add( new PersonEvent { SerializedEvent = "{\"NewAge\": 16}", ClrType = "Silverback.Tests.EventSourcing.TestTypes.EntityEvents.AgeChangedEvent, Silverback.EventSourcing.Tests", Timestamp = DateTime.Parse("2000-02-01", CultureInfo.InvariantCulture) }); eventStore.Events.Add( new PersonEvent { SerializedEvent = "{\"NewAge\": 35}", ClrType = "Silverback.Tests.EventSourcing.TestTypes.EntityEvents.AgeChangedEvent, Silverback.EventSourcing.Tests", Timestamp = DateTime.Parse("2019-07-06", CultureInfo.InvariantCulture) }); var repo = new PersonInMemoryEventStoreRepository(eventStore); var entity = repo.GetSnapshotById(12, DateTime.Parse("2000-03-01", CultureInfo.InvariantCulture)); entity.Name.Should().Be("Sergio"); entity.Age.Should().Be(16); }
public void GetAggregateEntity_ExistingId_EntityRecreated() { var eventStore = new PersonEventStore { Id = 12 }; eventStore.Events.Add(new PersonEvent { SerializedEvent = "{" + "\"$type\": \"Silverback.Tests.EventSourcing.TestTypes.Person+NameChangedEvent, Silverback.EventSourcing.Tests\"," + "\"NewName\": \"Silverback\"" + "}" }); var repo = new PersonInMemoryEventStoreRepository(eventStore); var entity = repo.GetById(12); entity.Should().NotBe(null); }