Exemplo n.º 1
0
        public async Task SaveAggregate_AlreadyInStoreWithoutEvents_AddingOne()
        {
            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                rawContext.Database.EnsureCreated();

                var bankje = new Raw.Bank {
                    Id = 17, Version = 0,
                };
                rawContext.Banks.Add(bankje);
                rawContext.SaveChanges();
            }

            Bank bank = new Bank(17);

            bank.OpenAccount(new OpenAccount("Mats Nevenstam"));
            var target = new BankRepository(_options);

            await target.SaveAsync(bank);

            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                var bankFromDb = rawContext.Banks.Include(b => b.Events).FirstOrDefault();

                Assert.IsNotNull(bankFromDb);
                Assert.AreEqual(17, bankFromDb.Id);
                Assert.AreEqual(1, bankFromDb.Version);

                var onlyEvent = rawContext.BankEvents.Single();
                Assert.AreEqual(1, onlyEvent.Version);
                Assert.AreEqual("DDD.Core.Application.Test.AccountOpened", onlyEvent.EventType);
                Assert.IsTrue(onlyEvent.EventData.Contains("Mats Nevenstam"));
            }
        }
Exemplo n.º 2
0
        public async Task SaveAggregate_AlreadyInStoreWithoutEvents()
        {
            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                rawContext.Database.EnsureCreated();

                var bankje = new Raw.Bank {
                    Id = 7, Version = 0,
                };
                rawContext.Banks.Add(bankje);
                rawContext.SaveChanges();
            }

            Bank bank   = new Bank(7);
            var  target = new BankRepository(_options);

            await target.SaveAsync(bank);

            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                var bankFromDb = rawContext.Banks.Include(b => b.Events).FirstOrDefault();

                Assert.IsNotNull(bankFromDb);
                Assert.AreEqual(7, bankFromDb.Id);
                Assert.AreEqual(0, bankFromDb.Version);
                Assert.IsFalse(bankFromDb.Events.Any());
            }
        }
        public async Task LoadAggregateRootWithOneEvent()
        {
            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                rawContext.Database.EnsureCreated();

                var events = new List <Raw.BankEvent> {
                    new Raw.BankEvent
                    {
                        Id        = 1,
                        BankId    = 7,
                        Version   = 1,
                        EventType = "DDD.Core.Application.Test.AccountOpened",
                        EventData = "{ 'AccountNumber':5, 'Owner':'Karina van Irak' }",
                    }
                };
                var bankje = new Raw.Bank {
                    Id = 7, Version = 12, Events = events
                };

                rawContext.Banks.Add(bankje);
                rawContext.SaveChanges();
            }

            var target = new BankRepository(_options);

            Bank bank = await target.FindAsync(7);

            Assert.AreEqual(7, bank.Id);
            Assert.AreEqual(1, bank.Version);
            Assert.AreEqual(1, bank.HandleAccountOpenedCallCount);
        }
Exemplo n.º 4
0
        public async Task SaveAggregate_AlreadyInStoreWithEvents_AddingThree()
        {
            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                rawContext.Database.EnsureCreated();

                var events = new List <Raw.BankEvent> {
                    new Raw.BankEvent
                    {
                        BankId    = 7, Version = 2,
                        EventType = "DDD.Core.Application.Test.AccountOpened",
                        EventData = "{\r\n  \"AccountNumber\": 5,\r\n  \"Owner\": \"Karina van Irak\"\r\n}",
                    },
                    new Raw.BankEvent
                    {
                        BankId    = 7, Version = 2,
                        EventType = "DDD.Core.Application.Test.AccountOpened",
                        EventData = "{\r\n  \"AccountNumber\": 6,\r\n  \"Owner\": \"Evert 't Reve\"\r\n}",
                    },
                };
                var bankje = new Raw.Bank {
                    Id = 7, Version = 2, Events = events
                };
                rawContext.Banks.Add(bankje);
                rawContext.SaveChanges();
            }

            var  target = new BankRepository(_options);
            Bank bank   = await target.FindAsync(7);

            bank.OpenAccount(new OpenAccount("Karel Lillerak"));
            bank.OpenAccount(new OpenAccount("Sara van Avaras"));
            bank.OpenAccount(new OpenAccount("Mats Nevenstam"));

            await target.SaveAsync(bank);

            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                var bankFromDb = rawContext.Banks.Include(b => b.Events).FirstOrDefault();

                Assert.IsNotNull(bankFromDb);
                Assert.AreEqual(7, bankFromDb.Id);
                Assert.AreEqual(5, bankFromDb.Version);
                List <Raw.BankEvent> eventList = bankFromDb.Events.ToList();
                Assert.AreEqual(5, eventList.Count);
                Assert.IsTrue(eventList[0].EventData.Contains("Karina van Irak"));
                Assert.IsTrue(eventList[1].EventData.Contains("Evert 't Reve"));
                Assert.IsTrue(eventList[2].EventData.Contains("Karel Lillerak"));
                Assert.IsTrue(eventList[3].EventData.Contains("Sara van Avaras"));
                Assert.IsTrue(eventList[4].EventData.Contains("Mats Nevenstam"));
            }
        }
Exemplo n.º 5
0
        public async Task SaveAggregate_WhenPersistentStoreIsModifiedSinceRead()
        {
            // setup database: bank with 1 event
            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                rawContext.Database.EnsureCreated();

                var events = new List <Raw.BankEvent> {
                    new Raw.BankEvent
                    {
                        BankId    = 7, Version = 1,
                        EventType = "DDD.Core.Application.Test.AccountOpened",
                        EventData = "{\r\n  \"AccountNumber\": 5,\r\n  \"Owner\": \"Karina van Irak\"\r\n}",
                    },
                };
                var bankje = new Raw.Bank {
                    Id = 7, Version = 1, Events = events
                };
                rawContext.Banks.Add(bankje);
                rawContext.SaveChanges();
            }

            // load bank from database
            var  target = new BankRepository(_options);
            Bank bank   = await target.FindAsync(7);

            // modifiy database by adding an extra event
            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                var bankje = rawContext.Banks.Include(b => b.Events).Single(b => b.Id == 7);
                bankje.Version = 2;
                bankje.Events.Add(new Raw.BankEvent
                {
                    BankId    = 7,
                    Version   = 2,
                    EventType = "DDD.Core.Application.Test.AccountOpened",
                    EventData = "{\r\n  \"AccountNumber\": 6,\r\n  \"Owner\": \"Evert 't Reve\"\r\n}",
                });
                rawContext.SaveChanges();
            }

            Func <Task> act = () =>
            {
                return(target.SaveAsync(bank));
            };

            var ex = await Assert.ThrowsExceptionAsync <DbUpdateConcurrencyException>(act);

            Assert.IsTrue(ex.Message.StartsWith("Database operation expected to affect 1 row(s) but actually affected 0 row(s)."));
        }
Exemplo n.º 6
0
        public async Task SaveAggregate_AlreadyInStoreWithEvents()
        {
            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                rawContext.Database.EnsureCreated();

                var events = new List <Raw.BankEvent> {
                    new Raw.BankEvent
                    {
                        BankId    = 7, Version = 2,
                        EventType = "DDD.Core.Application.Test.AccountOpened",
                        EventData = "{\r\n  \"AccountNumber\": 5,\r\n  \"Owner\": \"Karina van Irak\"\r\n}",
                    },
                    new Raw.BankEvent
                    {
                        BankId    = 7, Version = 2,
                        EventType = "DDD.Core.Application.Test.AccountOpened",
                        EventData = "{\r\n  \"AccountNumber\": 6,\r\n  \"Owner\": \"Evert 't Reve\"\r\n}",
                    },
                };
                var bankje = new Raw.Bank {
                    Id = 7, Version = 2, Events = events
                };
                rawContext.Banks.Add(bankje);
                rawContext.SaveChanges();
            }

            var  target = new BankRepository(_options);
            Bank bank   = await target.FindAsync(7);

            await target.SaveAsync(bank);

            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                var bankFromDb = rawContext.Banks.Include(b => b.Events).FirstOrDefault();

                Assert.IsNotNull(bankFromDb);
                Assert.AreEqual(7, bankFromDb.Id);
                Assert.AreEqual(2, bankFromDb.Version);
                Assert.AreEqual(2, bankFromDb.Events.Count());
            }
        }
        public async Task LoadAggregateRootWithOnyEventsFromOtherAggregates()
        {
            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                rawContext.Database.EnsureCreated();

                var events = new List <Raw.BankEvent> {
                    new Raw.BankEvent
                    {
                        Id        = 1, BankId = 7, Version = 2,
                        EventType = "DDD.Core.Application.Test.AccountOpened",
                        EventData = "{ 'AccountNumber':5, 'Owner':'Karina van Irak' }",
                    },
                    new Raw.BankEvent
                    {
                        Id        = 2, BankId = 7, Version = 2,
                        EventType = "DDD.Core.Application.Test.AccountOpened",
                        EventData = "{ 'AccountNumber':6, 'Owner':'Evert \\'t Reve' }",
                    },
                };
                rawContext.Banks.Add(new Raw.Bank {
                    Id = 7, Version = 12, Events = events
                });
                rawContext.Banks.Add(new Raw.Bank {
                    Id = 5, Version = 12, Events = new List <Raw.BankEvent>()
                });
                rawContext.Banks.Add(new Raw.Bank {
                    Id = 3, Version = 12, Events = null
                });
                rawContext.SaveChanges();
            }

            var target = new BankRepository(_options);

            Bank bank = await target.FindAsync(5);

            Assert.AreEqual(5, bank.Id);
            Assert.AreEqual(0, bank.Version);
            Assert.AreEqual(0, bank.HandleAccountOpenedCallCount);
        }
        public async Task LoadAggregateRootWithZeroEvents()
        {
            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                rawContext.Database.EnsureCreated();

                var bankje = new Raw.Bank {
                    Id = 7, Version = 12,
                };
                rawContext.Banks.Add(bankje);
                rawContext.SaveChanges();
            }

            var target = new BankRepository(_options);

            Bank bank = await target.FindAsync(7);

            Assert.AreEqual(7, bank.Id);
            Assert.AreEqual(0, bank.Version);
            // the 'Version' should reflect the number of events that have been handled, which is 0, in this test.
            // this should be equal the the 'Version' in the database, but in this test the latter was manually set to 12.
        }