Ejemplo n.º 1
0
        public async Task StoreAsync_ConcurrentlyModifyExistingEntity_ExceptionThrown()
        {
            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  = repo.Find(p => p.Id == 12);
            var person2 = repo.Find(p => p.Id == 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);

            await act.Should().ThrowAsync <EventStoreConcurrencyException>();
        }
Ejemplo n.º 2
0
        public void Store_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 = repo.Find(p => p.Id == 12);

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

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

            _dbContext.Persons.Should().HaveCount(1);
            _dbContext.Persons.First().Events.Should().HaveCount(3);
        }
Ejemplo n.º 3
0
        public void Remove_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 = repo.Find(p => p.Id == 12);

            entity.Should().NotBeNull();

            repo.Remove(entity !);
            _dbContext.SaveChanges();

            _dbContext.Persons.Should().BeEmpty();
            _dbContext.Persons.SelectMany(s => s.Events).Should().BeEmpty();
        }
Ejemplo n.º 4
0
        public void Find_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 = repo.Find(p => p.Id == 12);

            entity.Should().NotBeNull();
            entity !.Name.Should().Be("Silverback");
            entity.Age.Should().Be(35);
        }
Ejemplo n.º 5
0
        public void Find_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 = repo.Find(p => p.Id == 12);

            entity.Should().NotBeNull();
            entity !.Name.Should().Be("Silverback");
        }
Ejemplo n.º 6
0
        public void Find_NonExistingId_NullReturned()
        {
            var repo = new PersonDbEventStoreRepository(new EfCoreDbContext <TestDbContext>(_dbContext));

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

            entity.Should().BeNull();
        }
Ejemplo n.º 7
0
        public void Find_ExistingId_EntityRecreated()
        {
            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 = repo.Find(p => p.Id == 12);

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