Ejemplo n.º 1
0
        public async Task RemoveAsync_ExistingEntity_EntityDeleted()
        {
            var eventStore = _dbContext.Persons.Add(new PersonEventStore {
                Id = 12
            }).Entity;

            eventStore.Events.Add(
                new PersonEvent
            {
                SerializedEvent = "{\"NewName\": \"Silverback\"}",
                ClrType         =
                    "Silverback.Tests.EventSourcing.TestTypes.EntityEvents.NameChangedEvent, Silverback.EventSourcing.Tests"
            });
            _dbContext.SaveChanges();

            var repo = new PersonDbEventStoreRepository(new EfCoreDbContext <TestDbContext>(_dbContext));

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

            entity.Should().NotBeNull();

            await repo.RemoveAsync(entity !);

            await _dbContext.SaveChangesAsync();

            _dbContext.Persons.Should().BeEmpty();
            _dbContext.Persons.SelectMany(s => s.Events).Should().BeEmpty();
        }
Ejemplo n.º 2
0
        public async Task FindAsync_ExistingId_EventsAppliedInRightOrder()
        {
            var eventStore = _dbContext.Persons.Add(new PersonEventStore {
                Id = 12
            }).Entity;

            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)
            });

            _dbContext.SaveChanges();

            var repo = new PersonDbEventStoreRepository(new EfCoreDbContext <TestDbContext>(_dbContext));

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

            entity.Should().NotBeNull();
            entity !.Name.Should().Be("Silverback");
        }
Ejemplo n.º 3
0
        public async Task FindAsync_EventsWithLegacySerialization_EventsApplied()
        {
            var eventStore = _dbContext.Persons.Add(new PersonEventStore {
                Id = 12
            }).Entity;

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

            _dbContext.SaveChanges();

            var repo = new PersonDbEventStoreRepository(new EfCoreDbContext <TestDbContext>(_dbContext));

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

            entity.Should().NotBeNull();
            entity !.Name.Should().Be("Silverback");
            entity.Age.Should().Be(35);
        }
Ejemplo n.º 4
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 = "{\"NewName\": \"Silverback\"}",
                ClrType         =
                    "Silverback.Tests.EventSourcing.TestTypes.EntityEvents.NameChangedEvent, Silverback.EventSourcing.Tests"
            });
            _dbContext.SaveChanges();

            var repo = new PersonDbEventStoreRepository(new EfCoreDbContext <TestDbContext>(_dbContext));

            var person = await repo.FindAsync(p => p.Id == 12);

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

            await repo.StoreAsync(person);

            await _dbContext.SaveChangesAsync();

            _dbContext.Persons.Should().HaveCount(1);
            _dbContext.Persons.First().Events.Should().HaveCount(3);
        }
Ejemplo n.º 5
0
        public async Task FindAsync_NonExistingId_NullReturned()
        {
            var repo = new PersonDbEventStoreRepository(new EfCoreDbContext <TestDbContext>(_dbContext));

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

            entity.Should().BeNull();
        }