diff --git a/AggregateTests.cs b/AggregateTests.cs new file mode 100644 index 0000000..5c0173e --- /dev/null +++ b/AggregateTests.cs @@ -0,0 +1,50 @@ +using System; +using System.Linq; +using Shouldly; + +namespace EventSourcing.Demo +{ + public abstract class AggregateTests where TAggregate : AggregateRoot + { + private readonly TAggregate _aggregateRoot; + + protected AggregateTests(TAggregate aggregateRoot) + { + _aggregateRoot = aggregateRoot; + } + + protected void Given(params IEvent[] events) + { + if (events != null) + { + _aggregateRoot.Load(events); + } + } + + protected void When(Action command) + { + command(_aggregateRoot); + } + + protected void Then(params Action[] conditions) + { + var events = _aggregateRoot.GetUncommittedEvents(); + events.Count.ShouldBe(1); + var evnt = events.First(); + evnt.ShouldBeOfType(); + if (conditions != null) + { + ((TEvent)evnt).ShouldSatisfyAllConditions(conditions); + } + } + + protected void Throws(Action command, params Action[] conditions) where TException : Exception + { + var ex = Should.Throw(() => command(_aggregateRoot)); + if (conditions != null) + { + ex.ShouldSatisfyAllConditions(conditions); + } + } + } +} \ No newline at end of file diff --git a/AggregateTests.cs b/AggregateTests.cs new file mode 100644 index 0000000..5c0173e --- /dev/null +++ b/AggregateTests.cs @@ -0,0 +1,50 @@ +using System; +using System.Linq; +using Shouldly; + +namespace EventSourcing.Demo +{ + public abstract class AggregateTests where TAggregate : AggregateRoot + { + private readonly TAggregate _aggregateRoot; + + protected AggregateTests(TAggregate aggregateRoot) + { + _aggregateRoot = aggregateRoot; + } + + protected void Given(params IEvent[] events) + { + if (events != null) + { + _aggregateRoot.Load(events); + } + } + + protected void When(Action command) + { + command(_aggregateRoot); + } + + protected void Then(params Action[] conditions) + { + var events = _aggregateRoot.GetUncommittedEvents(); + events.Count.ShouldBe(1); + var evnt = events.First(); + evnt.ShouldBeOfType(); + if (conditions != null) + { + ((TEvent)evnt).ShouldSatisfyAllConditions(conditions); + } + } + + protected void Throws(Action command, params Action[] conditions) where TException : Exception + { + var ex = Should.Throw(() => command(_aggregateRoot)); + if (conditions != null) + { + ex.ShouldSatisfyAllConditions(conditions); + } + } + } +} \ No newline at end of file diff --git a/Test.cs b/Test.cs deleted file mode 100644 index 3998f5f..0000000 --- a/Test.cs +++ /dev/null @@ -1,226 +0,0 @@ -using System; -using System.Linq; -using AutoFixture; -using Shouldly; -using Xunit; - -namespace EventSourcing.Demo -{ - public abstract class AggregateTests where TAggregate : AggregateRoot - { - private readonly TAggregate _aggregateRoot; - - protected AggregateTests(TAggregate aggregateRoot) - { - _aggregateRoot = aggregateRoot; - } - - protected void Given(params IEvent[] events) - { - if (events != null) - { - _aggregateRoot.Load(events); - } - } - - protected void When(Action command) - { - command(_aggregateRoot); - } - - protected void Then(params Action[] conditions) - { - var events = _aggregateRoot.GetUncommittedEvents(); - events.Count.ShouldBe(1); - var evnt = events.First(); - evnt.ShouldBeOfType(); - if (conditions != null) - { - ((TEvent)evnt).ShouldSatisfyAllConditions(conditions); - } - } - - protected void Throws(Action command, params Action[] conditions) where TException : Exception - { - var ex = Should.Throw(() => command(_aggregateRoot)); - if (conditions != null) - { - ex.ShouldSatisfyAllConditions(conditions); - } - } - } - - public class GivenWhenThenTests : AggregateTests - { - private readonly Fixture _fixture; - private readonly string _sku = "abc123"; - private readonly int _initialQuantity; - - public GivenWhenThenTests() : base(new WarehouseProduct("abc123")) - { - _fixture = new Fixture(); - _fixture.Customizations.Add(new Int32SequenceGenerator()); - _initialQuantity = (int)_fixture.Create(); - } - - [Fact] - public void ShipProductShouldRaiseProductShipped() - { - Given(new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow)); - - var quantityToShip = _fixture.Create(); - When(x => x.ShipProduct(quantityToShip)); - - Then( - x => x.Quantity.ShouldBe(quantityToShip), - x => x.Sku.ShouldBe(_sku), - x => x.EventType.ShouldBe("ProductShipped")); - } - - [Fact] - public void ShipProductShouldThrowIfNoQuantityOnHand() - { - Given(); - - Throws( - x => x.ShipProduct(1), - x => x.Message.ShouldBe("Cannot Ship to a negative Quantity on Hand.")); - } - - [Fact] - public void ReceiveProductShouldRaiseProductReceived() - { - Given(new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow)); - - var quantityToReceive = _fixture.Create(); - When(x => x.ReceiveProduct(quantityToReceive)); - - Then( - x => x.Quantity.ShouldBe(quantityToReceive), - x => x.Sku.ShouldBe(_sku), - x => x.EventType.ShouldBe("ProductReceived")); - } - - [Fact] - public void AdjustInventoryShouldRaiseProductAdjusted() - { - Given(new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow)); - - var quantityToAdjust = _fixture.Create(); - var reason = _fixture.Create(); - - When(x => x.AdjustInventory(quantityToAdjust, reason)); - - Then( - x => x.Quantity.ShouldBe(quantityToAdjust), - x => x.Sku.ShouldBe(_sku), - x => x.Reason.ShouldBe(reason), - x => x.EventType.ShouldBe("InventoryAdjusted")); - } - - [Fact] - public void AdjustInventoryShouldThrowIfNoQuantityOnHand() - { - Given(); - - var reason = _fixture.Create(); - - Throws( - x => x.AdjustInventory(-1, reason), - x => x.Message.ShouldBe("Cannot adjust to a negative Quantity on Hand.")); - } - } - - public class ProductTests - { - private readonly string _sku; - private readonly int _initialQuantity; - private readonly WarehouseProduct _sut; - private readonly Fixture _fixture; - - public ProductTests() - { - _fixture = new Fixture(); - _fixture.Customizations.Add(new Int32SequenceGenerator()); - _sku = _fixture.Create(); - _initialQuantity = (int)_fixture.Create(); - - _sut = WarehouseProduct.Load(_sku, new [] { - new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow) - }); - } - - [Fact] - public void ShipProductShouldRaiseProductShipped() - { - var quantityToShip = _fixture.Create(); - _sut.ShipProduct(quantityToShip); - - var outEvents = _sut.GetUncommittedEvents(); - outEvents.Count.ShouldBe(1); - var outEvent = outEvents.Single(); - outEvent.ShouldBeOfType(); - - var productShipped = (ProductShipped)outEvent; - productShipped.ShouldSatisfyAllConditions( - x => x.Quantity.ShouldBe(quantityToShip), - x => x.Sku.ShouldBe(_sku), - x => x.EventType.ShouldBe("ProductShipped") - ); - } - - [Fact] - public void ShipProductShouldThrowIfNoQuantityOnHand() - { - var ex = Should.Throw(() => _sut.ShipProduct(_initialQuantity + 1)); - ex.Message.ShouldBe("Cannot Ship to a negative Quantity on Hand."); - } - - [Fact] - public void ReceiveProductShouldRaiseProductReceived() - { - var quantityToReceive = _fixture.Create(); - _sut.ReceiveProduct(quantityToReceive); - - var outEvents = _sut.GetUncommittedEvents(); - outEvents.Count.ShouldBe(1); - var outEvent = outEvents.Single(); - outEvent.ShouldBeOfType(); - - var productReceived = (ProductReceived)outEvent; - productReceived.ShouldSatisfyAllConditions( - x => x.Quantity.ShouldBe(quantityToReceive), - x => x.Sku.ShouldBe(_sku), - x => x.EventType.ShouldBe("ProductReceived") - ); - } - - [Fact] - public void AdjustInventoryShouldRaiseProductAdjusted() - { - var quantityToAdjust = _fixture.Create(); - var reason = _fixture.Create(); - _sut.AdjustInventory(quantityToAdjust, reason); - - var outEvents = _sut.GetUncommittedEvents(); - outEvents.Count.ShouldBe(1); - var outEvent = outEvents.Single(); - outEvent.ShouldBeOfType(); - - var productShipped = (InventoryAdjusted)outEvent; - productShipped.ShouldSatisfyAllConditions( - x => x.Quantity.ShouldBe(quantityToAdjust), - x => x.Sku.ShouldBe(_sku), - x => x.Reason.ShouldBe(reason), - x => x.EventType.ShouldBe("InventoryAdjusted") - ); - } - - [Fact] - public void AdjustInventoryShouldThrowIfNoQuantityOnHand() - { - var ex = Should.Throw(() => _sut.AdjustInventory((_initialQuantity + 1) * -1, string.Empty)); - ex.Message.ShouldBe("Cannot adjust to a negative Quantity on Hand."); - } - } -} \ No newline at end of file diff --git a/AggregateTests.cs b/AggregateTests.cs new file mode 100644 index 0000000..5c0173e --- /dev/null +++ b/AggregateTests.cs @@ -0,0 +1,50 @@ +using System; +using System.Linq; +using Shouldly; + +namespace EventSourcing.Demo +{ + public abstract class AggregateTests where TAggregate : AggregateRoot + { + private readonly TAggregate _aggregateRoot; + + protected AggregateTests(TAggregate aggregateRoot) + { + _aggregateRoot = aggregateRoot; + } + + protected void Given(params IEvent[] events) + { + if (events != null) + { + _aggregateRoot.Load(events); + } + } + + protected void When(Action command) + { + command(_aggregateRoot); + } + + protected void Then(params Action[] conditions) + { + var events = _aggregateRoot.GetUncommittedEvents(); + events.Count.ShouldBe(1); + var evnt = events.First(); + evnt.ShouldBeOfType(); + if (conditions != null) + { + ((TEvent)evnt).ShouldSatisfyAllConditions(conditions); + } + } + + protected void Throws(Action command, params Action[] conditions) where TException : Exception + { + var ex = Should.Throw(() => command(_aggregateRoot)); + if (conditions != null) + { + ex.ShouldSatisfyAllConditions(conditions); + } + } + } +} \ No newline at end of file diff --git a/Test.cs b/Test.cs deleted file mode 100644 index 3998f5f..0000000 --- a/Test.cs +++ /dev/null @@ -1,226 +0,0 @@ -using System; -using System.Linq; -using AutoFixture; -using Shouldly; -using Xunit; - -namespace EventSourcing.Demo -{ - public abstract class AggregateTests where TAggregate : AggregateRoot - { - private readonly TAggregate _aggregateRoot; - - protected AggregateTests(TAggregate aggregateRoot) - { - _aggregateRoot = aggregateRoot; - } - - protected void Given(params IEvent[] events) - { - if (events != null) - { - _aggregateRoot.Load(events); - } - } - - protected void When(Action command) - { - command(_aggregateRoot); - } - - protected void Then(params Action[] conditions) - { - var events = _aggregateRoot.GetUncommittedEvents(); - events.Count.ShouldBe(1); - var evnt = events.First(); - evnt.ShouldBeOfType(); - if (conditions != null) - { - ((TEvent)evnt).ShouldSatisfyAllConditions(conditions); - } - } - - protected void Throws(Action command, params Action[] conditions) where TException : Exception - { - var ex = Should.Throw(() => command(_aggregateRoot)); - if (conditions != null) - { - ex.ShouldSatisfyAllConditions(conditions); - } - } - } - - public class GivenWhenThenTests : AggregateTests - { - private readonly Fixture _fixture; - private readonly string _sku = "abc123"; - private readonly int _initialQuantity; - - public GivenWhenThenTests() : base(new WarehouseProduct("abc123")) - { - _fixture = new Fixture(); - _fixture.Customizations.Add(new Int32SequenceGenerator()); - _initialQuantity = (int)_fixture.Create(); - } - - [Fact] - public void ShipProductShouldRaiseProductShipped() - { - Given(new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow)); - - var quantityToShip = _fixture.Create(); - When(x => x.ShipProduct(quantityToShip)); - - Then( - x => x.Quantity.ShouldBe(quantityToShip), - x => x.Sku.ShouldBe(_sku), - x => x.EventType.ShouldBe("ProductShipped")); - } - - [Fact] - public void ShipProductShouldThrowIfNoQuantityOnHand() - { - Given(); - - Throws( - x => x.ShipProduct(1), - x => x.Message.ShouldBe("Cannot Ship to a negative Quantity on Hand.")); - } - - [Fact] - public void ReceiveProductShouldRaiseProductReceived() - { - Given(new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow)); - - var quantityToReceive = _fixture.Create(); - When(x => x.ReceiveProduct(quantityToReceive)); - - Then( - x => x.Quantity.ShouldBe(quantityToReceive), - x => x.Sku.ShouldBe(_sku), - x => x.EventType.ShouldBe("ProductReceived")); - } - - [Fact] - public void AdjustInventoryShouldRaiseProductAdjusted() - { - Given(new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow)); - - var quantityToAdjust = _fixture.Create(); - var reason = _fixture.Create(); - - When(x => x.AdjustInventory(quantityToAdjust, reason)); - - Then( - x => x.Quantity.ShouldBe(quantityToAdjust), - x => x.Sku.ShouldBe(_sku), - x => x.Reason.ShouldBe(reason), - x => x.EventType.ShouldBe("InventoryAdjusted")); - } - - [Fact] - public void AdjustInventoryShouldThrowIfNoQuantityOnHand() - { - Given(); - - var reason = _fixture.Create(); - - Throws( - x => x.AdjustInventory(-1, reason), - x => x.Message.ShouldBe("Cannot adjust to a negative Quantity on Hand.")); - } - } - - public class ProductTests - { - private readonly string _sku; - private readonly int _initialQuantity; - private readonly WarehouseProduct _sut; - private readonly Fixture _fixture; - - public ProductTests() - { - _fixture = new Fixture(); - _fixture.Customizations.Add(new Int32SequenceGenerator()); - _sku = _fixture.Create(); - _initialQuantity = (int)_fixture.Create(); - - _sut = WarehouseProduct.Load(_sku, new [] { - new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow) - }); - } - - [Fact] - public void ShipProductShouldRaiseProductShipped() - { - var quantityToShip = _fixture.Create(); - _sut.ShipProduct(quantityToShip); - - var outEvents = _sut.GetUncommittedEvents(); - outEvents.Count.ShouldBe(1); - var outEvent = outEvents.Single(); - outEvent.ShouldBeOfType(); - - var productShipped = (ProductShipped)outEvent; - productShipped.ShouldSatisfyAllConditions( - x => x.Quantity.ShouldBe(quantityToShip), - x => x.Sku.ShouldBe(_sku), - x => x.EventType.ShouldBe("ProductShipped") - ); - } - - [Fact] - public void ShipProductShouldThrowIfNoQuantityOnHand() - { - var ex = Should.Throw(() => _sut.ShipProduct(_initialQuantity + 1)); - ex.Message.ShouldBe("Cannot Ship to a negative Quantity on Hand."); - } - - [Fact] - public void ReceiveProductShouldRaiseProductReceived() - { - var quantityToReceive = _fixture.Create(); - _sut.ReceiveProduct(quantityToReceive); - - var outEvents = _sut.GetUncommittedEvents(); - outEvents.Count.ShouldBe(1); - var outEvent = outEvents.Single(); - outEvent.ShouldBeOfType(); - - var productReceived = (ProductReceived)outEvent; - productReceived.ShouldSatisfyAllConditions( - x => x.Quantity.ShouldBe(quantityToReceive), - x => x.Sku.ShouldBe(_sku), - x => x.EventType.ShouldBe("ProductReceived") - ); - } - - [Fact] - public void AdjustInventoryShouldRaiseProductAdjusted() - { - var quantityToAdjust = _fixture.Create(); - var reason = _fixture.Create(); - _sut.AdjustInventory(quantityToAdjust, reason); - - var outEvents = _sut.GetUncommittedEvents(); - outEvents.Count.ShouldBe(1); - var outEvent = outEvents.Single(); - outEvent.ShouldBeOfType(); - - var productShipped = (InventoryAdjusted)outEvent; - productShipped.ShouldSatisfyAllConditions( - x => x.Quantity.ShouldBe(quantityToAdjust), - x => x.Sku.ShouldBe(_sku), - x => x.Reason.ShouldBe(reason), - x => x.EventType.ShouldBe("InventoryAdjusted") - ); - } - - [Fact] - public void AdjustInventoryShouldThrowIfNoQuantityOnHand() - { - var ex = Should.Throw(() => _sut.AdjustInventory((_initialQuantity + 1) * -1, string.Empty)); - ex.Message.ShouldBe("Cannot adjust to a negative Quantity on Hand."); - } - } -} \ No newline at end of file diff --git a/WarehouseProductAggregateRootTests.cs b/WarehouseProductAggregateRootTests.cs new file mode 100644 index 0000000..d51df0f --- /dev/null +++ b/WarehouseProductAggregateRootTests.cs @@ -0,0 +1,88 @@ +using System; +using AutoFixture; +using Shouldly; +using Xunit; + +namespace EventSourcing.Demo +{ + public class WarehouseProductAggregateRootTests : AggregateTests + { + private readonly Fixture _fixture; + private readonly string _sku = "abc123"; + private readonly int _initialQuantity; + + public WarehouseProductAggregateRootTests() : base(new WarehouseProduct("abc123")) + { + _fixture = new Fixture(); + _fixture.Customizations.Add(new Int32SequenceGenerator()); + _initialQuantity = (int)_fixture.Create(); + } + + [Fact] + public void ShipProductShouldRaiseProductShipped() + { + Given(new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow)); + + var quantityToShip = _fixture.Create(); + When(x => x.ShipProduct(quantityToShip)); + + Then( + x => x.Quantity.ShouldBe(quantityToShip), + x => x.Sku.ShouldBe(_sku), + x => x.EventType.ShouldBe("ProductShipped")); + } + + [Fact] + public void ShipProductShouldThrowIfNoQuantityOnHand() + { + Given(); + + Throws( + x => x.ShipProduct(1), + x => x.Message.ShouldBe("Cannot Ship to a negative Quantity on Hand.")); + } + + [Fact] + public void ReceiveProductShouldRaiseProductReceived() + { + Given(new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow)); + + var quantityToReceive = _fixture.Create(); + When(x => x.ReceiveProduct(quantityToReceive)); + + Then( + x => x.Quantity.ShouldBe(quantityToReceive), + x => x.Sku.ShouldBe(_sku), + x => x.EventType.ShouldBe("ProductReceived")); + } + + [Fact] + public void AdjustInventoryShouldRaiseProductAdjusted() + { + Given(new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow)); + + var quantityToAdjust = _fixture.Create(); + var reason = _fixture.Create(); + + When(x => x.AdjustInventory(quantityToAdjust, reason)); + + Then( + x => x.Quantity.ShouldBe(quantityToAdjust), + x => x.Sku.ShouldBe(_sku), + x => x.Reason.ShouldBe(reason), + x => x.EventType.ShouldBe("InventoryAdjusted")); + } + + [Fact] + public void AdjustInventoryShouldThrowIfNoQuantityOnHand() + { + Given(); + + var reason = _fixture.Create(); + + Throws( + x => x.AdjustInventory(-1, reason), + x => x.Message.ShouldBe("Cannot adjust to a negative Quantity on Hand.")); + } + } +} \ No newline at end of file diff --git a/AggregateTests.cs b/AggregateTests.cs new file mode 100644 index 0000000..5c0173e --- /dev/null +++ b/AggregateTests.cs @@ -0,0 +1,50 @@ +using System; +using System.Linq; +using Shouldly; + +namespace EventSourcing.Demo +{ + public abstract class AggregateTests where TAggregate : AggregateRoot + { + private readonly TAggregate _aggregateRoot; + + protected AggregateTests(TAggregate aggregateRoot) + { + _aggregateRoot = aggregateRoot; + } + + protected void Given(params IEvent[] events) + { + if (events != null) + { + _aggregateRoot.Load(events); + } + } + + protected void When(Action command) + { + command(_aggregateRoot); + } + + protected void Then(params Action[] conditions) + { + var events = _aggregateRoot.GetUncommittedEvents(); + events.Count.ShouldBe(1); + var evnt = events.First(); + evnt.ShouldBeOfType(); + if (conditions != null) + { + ((TEvent)evnt).ShouldSatisfyAllConditions(conditions); + } + } + + protected void Throws(Action command, params Action[] conditions) where TException : Exception + { + var ex = Should.Throw(() => command(_aggregateRoot)); + if (conditions != null) + { + ex.ShouldSatisfyAllConditions(conditions); + } + } + } +} \ No newline at end of file diff --git a/Test.cs b/Test.cs deleted file mode 100644 index 3998f5f..0000000 --- a/Test.cs +++ /dev/null @@ -1,226 +0,0 @@ -using System; -using System.Linq; -using AutoFixture; -using Shouldly; -using Xunit; - -namespace EventSourcing.Demo -{ - public abstract class AggregateTests where TAggregate : AggregateRoot - { - private readonly TAggregate _aggregateRoot; - - protected AggregateTests(TAggregate aggregateRoot) - { - _aggregateRoot = aggregateRoot; - } - - protected void Given(params IEvent[] events) - { - if (events != null) - { - _aggregateRoot.Load(events); - } - } - - protected void When(Action command) - { - command(_aggregateRoot); - } - - protected void Then(params Action[] conditions) - { - var events = _aggregateRoot.GetUncommittedEvents(); - events.Count.ShouldBe(1); - var evnt = events.First(); - evnt.ShouldBeOfType(); - if (conditions != null) - { - ((TEvent)evnt).ShouldSatisfyAllConditions(conditions); - } - } - - protected void Throws(Action command, params Action[] conditions) where TException : Exception - { - var ex = Should.Throw(() => command(_aggregateRoot)); - if (conditions != null) - { - ex.ShouldSatisfyAllConditions(conditions); - } - } - } - - public class GivenWhenThenTests : AggregateTests - { - private readonly Fixture _fixture; - private readonly string _sku = "abc123"; - private readonly int _initialQuantity; - - public GivenWhenThenTests() : base(new WarehouseProduct("abc123")) - { - _fixture = new Fixture(); - _fixture.Customizations.Add(new Int32SequenceGenerator()); - _initialQuantity = (int)_fixture.Create(); - } - - [Fact] - public void ShipProductShouldRaiseProductShipped() - { - Given(new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow)); - - var quantityToShip = _fixture.Create(); - When(x => x.ShipProduct(quantityToShip)); - - Then( - x => x.Quantity.ShouldBe(quantityToShip), - x => x.Sku.ShouldBe(_sku), - x => x.EventType.ShouldBe("ProductShipped")); - } - - [Fact] - public void ShipProductShouldThrowIfNoQuantityOnHand() - { - Given(); - - Throws( - x => x.ShipProduct(1), - x => x.Message.ShouldBe("Cannot Ship to a negative Quantity on Hand.")); - } - - [Fact] - public void ReceiveProductShouldRaiseProductReceived() - { - Given(new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow)); - - var quantityToReceive = _fixture.Create(); - When(x => x.ReceiveProduct(quantityToReceive)); - - Then( - x => x.Quantity.ShouldBe(quantityToReceive), - x => x.Sku.ShouldBe(_sku), - x => x.EventType.ShouldBe("ProductReceived")); - } - - [Fact] - public void AdjustInventoryShouldRaiseProductAdjusted() - { - Given(new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow)); - - var quantityToAdjust = _fixture.Create(); - var reason = _fixture.Create(); - - When(x => x.AdjustInventory(quantityToAdjust, reason)); - - Then( - x => x.Quantity.ShouldBe(quantityToAdjust), - x => x.Sku.ShouldBe(_sku), - x => x.Reason.ShouldBe(reason), - x => x.EventType.ShouldBe("InventoryAdjusted")); - } - - [Fact] - public void AdjustInventoryShouldThrowIfNoQuantityOnHand() - { - Given(); - - var reason = _fixture.Create(); - - Throws( - x => x.AdjustInventory(-1, reason), - x => x.Message.ShouldBe("Cannot adjust to a negative Quantity on Hand.")); - } - } - - public class ProductTests - { - private readonly string _sku; - private readonly int _initialQuantity; - private readonly WarehouseProduct _sut; - private readonly Fixture _fixture; - - public ProductTests() - { - _fixture = new Fixture(); - _fixture.Customizations.Add(new Int32SequenceGenerator()); - _sku = _fixture.Create(); - _initialQuantity = (int)_fixture.Create(); - - _sut = WarehouseProduct.Load(_sku, new [] { - new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow) - }); - } - - [Fact] - public void ShipProductShouldRaiseProductShipped() - { - var quantityToShip = _fixture.Create(); - _sut.ShipProduct(quantityToShip); - - var outEvents = _sut.GetUncommittedEvents(); - outEvents.Count.ShouldBe(1); - var outEvent = outEvents.Single(); - outEvent.ShouldBeOfType(); - - var productShipped = (ProductShipped)outEvent; - productShipped.ShouldSatisfyAllConditions( - x => x.Quantity.ShouldBe(quantityToShip), - x => x.Sku.ShouldBe(_sku), - x => x.EventType.ShouldBe("ProductShipped") - ); - } - - [Fact] - public void ShipProductShouldThrowIfNoQuantityOnHand() - { - var ex = Should.Throw(() => _sut.ShipProduct(_initialQuantity + 1)); - ex.Message.ShouldBe("Cannot Ship to a negative Quantity on Hand."); - } - - [Fact] - public void ReceiveProductShouldRaiseProductReceived() - { - var quantityToReceive = _fixture.Create(); - _sut.ReceiveProduct(quantityToReceive); - - var outEvents = _sut.GetUncommittedEvents(); - outEvents.Count.ShouldBe(1); - var outEvent = outEvents.Single(); - outEvent.ShouldBeOfType(); - - var productReceived = (ProductReceived)outEvent; - productReceived.ShouldSatisfyAllConditions( - x => x.Quantity.ShouldBe(quantityToReceive), - x => x.Sku.ShouldBe(_sku), - x => x.EventType.ShouldBe("ProductReceived") - ); - } - - [Fact] - public void AdjustInventoryShouldRaiseProductAdjusted() - { - var quantityToAdjust = _fixture.Create(); - var reason = _fixture.Create(); - _sut.AdjustInventory(quantityToAdjust, reason); - - var outEvents = _sut.GetUncommittedEvents(); - outEvents.Count.ShouldBe(1); - var outEvent = outEvents.Single(); - outEvent.ShouldBeOfType(); - - var productShipped = (InventoryAdjusted)outEvent; - productShipped.ShouldSatisfyAllConditions( - x => x.Quantity.ShouldBe(quantityToAdjust), - x => x.Sku.ShouldBe(_sku), - x => x.Reason.ShouldBe(reason), - x => x.EventType.ShouldBe("InventoryAdjusted") - ); - } - - [Fact] - public void AdjustInventoryShouldThrowIfNoQuantityOnHand() - { - var ex = Should.Throw(() => _sut.AdjustInventory((_initialQuantity + 1) * -1, string.Empty)); - ex.Message.ShouldBe("Cannot adjust to a negative Quantity on Hand."); - } - } -} \ No newline at end of file diff --git a/WarehouseProductAggregateRootTests.cs b/WarehouseProductAggregateRootTests.cs new file mode 100644 index 0000000..d51df0f --- /dev/null +++ b/WarehouseProductAggregateRootTests.cs @@ -0,0 +1,88 @@ +using System; +using AutoFixture; +using Shouldly; +using Xunit; + +namespace EventSourcing.Demo +{ + public class WarehouseProductAggregateRootTests : AggregateTests + { + private readonly Fixture _fixture; + private readonly string _sku = "abc123"; + private readonly int _initialQuantity; + + public WarehouseProductAggregateRootTests() : base(new WarehouseProduct("abc123")) + { + _fixture = new Fixture(); + _fixture.Customizations.Add(new Int32SequenceGenerator()); + _initialQuantity = (int)_fixture.Create(); + } + + [Fact] + public void ShipProductShouldRaiseProductShipped() + { + Given(new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow)); + + var quantityToShip = _fixture.Create(); + When(x => x.ShipProduct(quantityToShip)); + + Then( + x => x.Quantity.ShouldBe(quantityToShip), + x => x.Sku.ShouldBe(_sku), + x => x.EventType.ShouldBe("ProductShipped")); + } + + [Fact] + public void ShipProductShouldThrowIfNoQuantityOnHand() + { + Given(); + + Throws( + x => x.ShipProduct(1), + x => x.Message.ShouldBe("Cannot Ship to a negative Quantity on Hand.")); + } + + [Fact] + public void ReceiveProductShouldRaiseProductReceived() + { + Given(new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow)); + + var quantityToReceive = _fixture.Create(); + When(x => x.ReceiveProduct(quantityToReceive)); + + Then( + x => x.Quantity.ShouldBe(quantityToReceive), + x => x.Sku.ShouldBe(_sku), + x => x.EventType.ShouldBe("ProductReceived")); + } + + [Fact] + public void AdjustInventoryShouldRaiseProductAdjusted() + { + Given(new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow)); + + var quantityToAdjust = _fixture.Create(); + var reason = _fixture.Create(); + + When(x => x.AdjustInventory(quantityToAdjust, reason)); + + Then( + x => x.Quantity.ShouldBe(quantityToAdjust), + x => x.Sku.ShouldBe(_sku), + x => x.Reason.ShouldBe(reason), + x => x.EventType.ShouldBe("InventoryAdjusted")); + } + + [Fact] + public void AdjustInventoryShouldThrowIfNoQuantityOnHand() + { + Given(); + + var reason = _fixture.Create(); + + Throws( + x => x.AdjustInventory(-1, reason), + x => x.Message.ShouldBe("Cannot adjust to a negative Quantity on Hand.")); + } + } +} \ No newline at end of file diff --git a/WarehouseProductTests.cs b/WarehouseProductTests.cs new file mode 100644 index 0000000..ec4be3f --- /dev/null +++ b/WarehouseProductTests.cs @@ -0,0 +1,101 @@ +using System; +using System.Linq; +using AutoFixture; +using Shouldly; +using Xunit; + +namespace EventSourcing.Demo +{ + public class WarehouseProductTests + { + private readonly string _sku; + private readonly int _initialQuantity; + private readonly WarehouseProduct _sut; + private readonly Fixture _fixture; + + public WarehouseProductTests() + { + _fixture = new Fixture(); + _fixture.Customizations.Add(new Int32SequenceGenerator()); + _sku = _fixture.Create(); + _initialQuantity = (int)_fixture.Create(); + + _sut = WarehouseProduct.Load(_sku, new [] { + new ProductReceived(_sku, _initialQuantity, DateTime.UtcNow) + }); + } + + [Fact] + public void ShipProductShouldRaiseProductShipped() + { + var quantityToShip = _fixture.Create(); + _sut.ShipProduct(quantityToShip); + + var outEvents = _sut.GetUncommittedEvents(); + outEvents.Count.ShouldBe(1); + var outEvent = outEvents.Single(); + outEvent.ShouldBeOfType(); + + var productShipped = (ProductShipped)outEvent; + productShipped.ShouldSatisfyAllConditions( + x => x.Quantity.ShouldBe(quantityToShip), + x => x.Sku.ShouldBe(_sku), + x => x.EventType.ShouldBe("ProductShipped") + ); + } + + [Fact] + public void ShipProductShouldThrowIfNoQuantityOnHand() + { + var ex = Should.Throw(() => _sut.ShipProduct(_initialQuantity + 1)); + ex.Message.ShouldBe("Cannot Ship to a negative Quantity on Hand."); + } + + [Fact] + public void ReceiveProductShouldRaiseProductReceived() + { + var quantityToReceive = _fixture.Create(); + _sut.ReceiveProduct(quantityToReceive); + + var outEvents = _sut.GetUncommittedEvents(); + outEvents.Count.ShouldBe(1); + var outEvent = outEvents.Single(); + outEvent.ShouldBeOfType(); + + var productReceived = (ProductReceived)outEvent; + productReceived.ShouldSatisfyAllConditions( + x => x.Quantity.ShouldBe(quantityToReceive), + x => x.Sku.ShouldBe(_sku), + x => x.EventType.ShouldBe("ProductReceived") + ); + } + + [Fact] + public void AdjustInventoryShouldRaiseProductAdjusted() + { + var quantityToAdjust = _fixture.Create(); + var reason = _fixture.Create(); + _sut.AdjustInventory(quantityToAdjust, reason); + + var outEvents = _sut.GetUncommittedEvents(); + outEvents.Count.ShouldBe(1); + var outEvent = outEvents.Single(); + outEvent.ShouldBeOfType(); + + var productShipped = (InventoryAdjusted)outEvent; + productShipped.ShouldSatisfyAllConditions( + x => x.Quantity.ShouldBe(quantityToAdjust), + x => x.Sku.ShouldBe(_sku), + x => x.Reason.ShouldBe(reason), + x => x.EventType.ShouldBe("InventoryAdjusted") + ); + } + + [Fact] + public void AdjustInventoryShouldThrowIfNoQuantityOnHand() + { + var ex = Should.Throw(() => _sut.AdjustInventory((_initialQuantity + 1) * -1, string.Empty)); + ex.Message.ShouldBe("Cannot adjust to a negative Quantity on Hand."); + } + } +} \ No newline at end of file