Пример #1
0
        public void CanCreateFromState()
        {
            // arrange
            var factory     = AggregateFactory.For <Counter>();
            var aggregateId = "some id";
            var state       = new CounterState();

            for (var i = 0; i < 10; ++i)
            {
                state.Mutate(new AggregateEvent <CounterIncremented>(
                                 aggregateId, i, DateTime.UtcNow, new CounterIncremented()));
            }

            // act
            var aggregate = factory.CreateFromState(aggregateId, state);

            // assert
            Assert.NotNull(aggregate);
            Assert.AreEqual(aggregateId, aggregate.Id);
            Assert.AreEqual(state.Version, aggregate.OriginalVersion);
            Assert.AreEqual(0, aggregate.Changes.Count());

            Assert.NotNull(aggregate.State);
            // no changes, so state version is the same
            Assert.AreEqual(state.Version, aggregate.State.Version);
        }
Пример #2
0
        public void CanCreateFromEvents()
        {
            // arrange
            var factory     = AggregateFactory.For <Counter>();
            var aggregateId = "some id";
            var events      = new IAggregateEvent[] {
                new AggregateEvent <CounterIncremented>(
                    aggregateId, 1, DateTime.UtcNow, new CounterIncremented {
                    Increment = 1
                }),
                new AggregateEvent <CounterMultiplied>(
                    aggregateId, 2, DateTime.UtcNow, new CounterMultiplied {
                    Factor = 2
                }),
                new AggregateEvent <CounterIncremented>(
                    aggregateId, 3, DateTime.UtcNow, new CounterIncremented {
                    Increment = 5
                }),
            };

            // act
            var aggregate = factory.CreateFromEvents(aggregateId, events);

            // assert
            Assert.NotNull(aggregate);
            Assert.AreEqual(aggregateId, aggregate.Id);
            Assert.AreEqual(events.Last().SequenceId, aggregate.OriginalVersion);
            Assert.AreEqual(0, aggregate.Changes.Count());

            Assert.NotNull(aggregate.State);
            // no changes, so state version is the same
            Assert.AreEqual(events.Last().SequenceId, aggregate.State.Version);
        }
Пример #3
0
        public void WhenNewEvents_StateChanges()
        {
            // arrange
            var factory     = AggregateFactory.For <Counter>();
            var aggregateId = "some id";
            var events      = new IAggregateEvent[] {
                new AggregateEvent <CounterIncremented>(aggregateId, 1, DateTime.UtcNow, new CounterIncremented {
                    Increment = 1
                }),
                new AggregateEvent <CounterMultiplied>(aggregateId, 2, DateTime.UtcNow, new CounterMultiplied {
                    Factor = 2
                }),
            };
            var counter = factory.CreateFromEvents(aggregateId, events);

            // act
            counter.Increment(5);

            // assert
            Assert.AreEqual(2, counter.OriginalVersion);
            Assert.AreEqual(3, counter.State.Version);
            Assert.AreEqual(1, counter.Changes.Count());
            Assert.AreEqual(5, counter.Changes
                            .OfType <IAggregateEvent <CounterIncremented> >()
                            .First().Payload.Increment);
        }
        public void FactoriesAreCached()
        {
            // arrange & act
            var f1 = AggregateFactory.For <Race>();
            var f2 = AggregateFactory.For <Race>();

            // assert
            Assert.AreSame(f1, f2);
        }
Пример #5
0
        public static Sale CreateSale(int?saleStock = null)
        {
            var stock = saleStock ?? 10;

            var sale = new Sale("test-sale-id", new SaleState());

            sale.Create("test-seller-id",
                        new Values.Item {
                Id = "item-id", Name = "item-name"
            },
                        price: 10,
                        stock: stock);

            // create a new aggregate from the state, to ensure the aggregate doesn't come with changes
            return(AggregateFactory.For <Sale>().CreateFromState(sale.Id, sale.State));
        }
Пример #6
0
        public void CanCreateNewAggregate()
        {
            // arrange
            var factory     = AggregateFactory.For <Counter>();
            var aggregateId = "some id";

            // act
            var aggregate = factory.CreateNew(aggregateId);

            // assert
            Assert.NotNull(aggregate);
            Assert.AreEqual(aggregateId, aggregate.Id);
            Assert.AreEqual(0, aggregate.OriginalVersion);
            Assert.AreEqual(0, aggregate.Changes.Count());

            Assert.NotNull(aggregate.State);
            Assert.AreEqual(0, aggregate.State.Version);
        }
Пример #7
0
        public static Auction CreateTestAuction(decimal?minPrice = null,
                                                Window window    = null)
        {
            var auction = new Auction("test-auction-id", new AuctionState());

            var price = minPrice ?? 10;

            window = window ?? new Window {
                Start = DateTime.UtcNow.AddDays(-1), End = DateTime.UtcNow.AddDays(1)
            };

            auction.Create(
                "test-seller-id",
                new Item {
                Id = "test-item-id", Name = "item name"
            },
                price,
                window);

            // create a new aggregate from the state, to ensure the aggregate doesn't come with changes
            return(AggregateFactory.For <Auction>().CreateFromState(auction.Id, auction.State));
        }
Пример #8
0
        public void CannotCreateFromUnknownEvents()
        {
            // arrange
            var factory     = AggregateFactory.For <Counter>();
            var aggregateId = "some id";
            var events      = new IAggregateEvent[] {
                new AggregateEvent <CounterIncremented>(aggregateId, 1, DateTime.UtcNow, new CounterIncremented {
                    Increment = 1
                }),
                new AggregateEvent <EventThatNoAggregateKnows>(aggregateId, 2, DateTime.UtcNow, new EventThatNoAggregateKnows()),
            };

            // act and assert
            try
            {
                // TODO: should throw a specific known exception for this scenario
                factory.CreateFromEvents(aggregateId, events);
                Assert.Fail("should not have been possible to create the aggregate using an unknown event");
            }
            catch (Exception)
            {
                Assert.Pass();
            }
        }
 public void CanCreateFactory()
 {
     Assert.DoesNotThrow(() => AggregateFactory.For <Race>());
 }