Exemple #1
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);
        }
Exemple #2
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);
        }
Exemple #3
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");
        }
Exemple #4
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);
        }
Exemple #5
0
        public async Task GetAsync_NonExistingId_NullReturned()
        {
            var repo = new PersonEventStoreRepository(_dbContext);

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

            entity.Should().BeNull();
        }