Example #1
0
        public void Store_ConcurrentlyModifyExistingEntity_ExceptionThrown()
        {
            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  = repo.Get(p => p.Id == 12);
            var person2 = repo.Get(p => p.Id == 12);

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

            repo.Store(person);
            Action act = () => repo.Store(person2);

            act.Should().Throw <SilverbackConcurrencyException>();
        }
Example #2
0
        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);
        }
Example #3
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);
        }
Example #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");
        }
Example #5
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>();
        }
Example #6
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);
        }
Example #7
0
        public async Task RemoveAsync_ExistingEntity_EntityDeleted()
        {
            var eventStore = _dbContext.Persons.Add(new PersonEventStore {
                Id = 12
            }).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 entity = await repo.GetAsync(p => p.Id == 12);

            entity.Should().NotBeNull();

            await repo.RemoveAsync(entity);

            await _dbContext.SaveChangesAsync();

            _dbContext.Persons.Count().Should().Be(0);
            _dbContext.Persons.SelectMany(s => s.Events).Count().Should().Be(0);
        }
Example #8
0
        public void Store_ExistingEntity_VersionIncremented()
        {
            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 = repo.Get(p => p.Id == 12);

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

            repo.Store(person);

            _dbContext.Persons.First().EntityVersion.Should().Be(3);
        }
Example #9
0
        public async Task GetAsync_ExistingId_EventsAppliedInRightOrder()
        {
            var eventStore = _dbContext.Persons.Add(new PersonEventStore {
                Id = 12
            }).Entity;

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

            _dbContext.SaveChanges();

            var repo = new PersonEventStoreRepository(_dbContext);

            var entity = await repo.GetAsync(p => p.Id == 12);

            entity.Name.Should().Be("Silverback");
        }
Example #10
0
        public async Task GetAsync_ExistingId_EventsApplied()
        {
            var eventStore = _dbContext.Persons.Add(new PersonEventStore {
                Id = 12
            }).Entity;

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

            _dbContext.SaveChanges();

            var repo = new PersonEventStoreRepository(_dbContext);

            var entity = await repo.GetAsync(p => p.Id == 12);

            entity.Name.Should().Be("Silverback");
            entity.Age.Should().Be(35);
        }
Example #11
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);
        }
Example #12
0
        public void GetAggregateEntity_NonExistingId_NullReturned()
        {
            var repo = new PersonEventStoreRepository();

            var entity = repo.GetById(11);

            entity.Should().BeNull();
        }
Example #13
0
        public void Get_NonExistingId_NullReturned()
        {
            var repo = new PersonEventStoreRepository(_dbContext);

            var entity = repo.Get(p => p.Id == 12);

            entity.Should().BeNull();
        }
Example #14
0
        public async Task GetAsync_NonExistingId_NullReturned()
        {
            var repo = new PersonEventStoreRepository(_dbContext);

            var entity = await repo.GetAsync(p => p.Id == 12);

            entity.Should().BeNull();
        }
Example #15
0
        public void Remove_NonExistingEntity_ReturnsNull()
        {
            var repo = new PersonEventStoreRepository();

            var entity = new Person(123);

            var result = repo.Remove(entity);

            result.Should().BeNull();
        }
Example #16
0
        public async Task RemoveAsync_NonExistingEntity_ReturnsNull()
        {
            var repo = new PersonEventStoreRepository();

            var entity = new Person(123);

            var result = await repo.RemoveAsync(entity);

            result.Should().BeNull();
        }
Example #17
0
        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);
        }
Example #18
0
        public void Store_EntityWithSomeEvents_EventsSaved()
        {
            var repo   = new PersonEventStoreRepository();
            var person = new Person();

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

            repo.Store(person);

            repo.EventStores.Count.Should().Be(1);
            repo.EventStores.First().Events.Count.Should().Be(2);
        }
Example #19
0
        public void Store_EntityWithSomeEvents_VersionCalculated()
        {
            var repo   = new PersonEventStoreRepository(_dbContext);
            var person = new Person();

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

            repo.Store(person);
            _dbContext.SaveChanges();

            _dbContext.Persons.First().EntityVersion.Should().Be(2);
        }
Example #20
0
        public async Task GetSnapshotAsync_ExistingIdWithPastSnapshot_OnlyRelevantEventsApplied()
        {
            var eventStore = _dbContext.Persons.Add(new PersonEventStore {
                Id = 12
            }).Entity;

            eventStore.Events.Add(new PersonEvent
            {
                SerializedEvent = "{" +
                                  "\"$type\": \"Silverback.Tests.EventSourcing.EntityFrameworkCore.TestTypes.Person+NameChangedEvent, Silverback.EventSourcing.EntityFrameworkCore.Tests\"," +
                                  "\"NewName\": \"Silverback\"" +
                                  "}",
                Timestamp = DateTime.Parse("2000-05-05")
            });
            eventStore.Events.Add(new PersonEvent
            {
                SerializedEvent = "{" +
                                  "\"$type\": \"Silverback.Tests.EventSourcing.EntityFrameworkCore.TestTypes.Person+NameChangedEvent, Silverback.EventSourcing.EntityFrameworkCore.Tests\"," +
                                  "\"NewName\": \"Sergio\"" +
                                  "}",
                Timestamp = DateTime.Parse("2000-03-01")
            });
            eventStore.Events.Add(new PersonEvent
            {
                SerializedEvent = "{" +
                                  "\"$type\": \"Silverback.Tests.EventSourcing.EntityFrameworkCore.TestTypes.Person+AgeChangedEvent, Silverback.EventSourcing.EntityFrameworkCore.Tests\"," +
                                  "\"NewAge\": 16" +
                                  "}",
                Timestamp = DateTime.Parse("2000-02-01")
            });
            eventStore.Events.Add(new PersonEvent
            {
                SerializedEvent = "{" +
                                  "\"$type\": \"Silverback.Tests.EventSourcing.EntityFrameworkCore.TestTypes.Person+AgeChangedEvent, Silverback.EventSourcing.EntityFrameworkCore.Tests\"," +
                                  "\"NewAge\": 35" +
                                  "}",
                Timestamp = DateTime.Parse("2019-07-06")
            });

            _dbContext.SaveChanges();

            var repo = new PersonEventStoreRepository(_dbContext);

            var entity = await repo.GetSnapshotAsync(p => p.Id == 12, DateTime.Parse("2000-03-01"));

            entity.Name.Should().Be("Sergio");
            entity.Age.Should().Be(16);
        }
Example #21
0
        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);
        }
Example #22
0
        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);
        }
Example #23
0
        public void Get_ExistingId_EntityRecreated()
        {
            var eventStore = _dbContext.Persons.Add(new PersonEventStore {
                Id = 12
            }).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 entity = repo.Get(p => p.Id == 12);

            entity.Should().NotBe(null);
        }