public void CreatePurchaseOrder_VendorExists_ReturnsPurchaseOrderCreatedEvent() { /// Arrange // Get a create purchase order command with the vendor. // (By the time the command gets to the domain it's already been structurally validated.) var command = new CreatePurchaseOrder( vendorId: Guid.NewGuid(), lines: new List <CreatePurchaseOrderLine> { new CreatePurchaseOrderLine(productId: Guid.NewGuid(), quantity: 354M, measure: "EA", pricePerUnit: 4.75M), new CreatePurchaseOrderLine(productId: Guid.NewGuid(), quantity: 10M, measure: "EA", pricePerUnit: 24.99M) } ); // Assume the vendor exists. Func <Guid, bool> getVendorExists = _ => true; // Get some id as the next id. var nextId = Guid.NewGuid(); // Get the event we expect the domain to return. var expected = new PurchaseOrderCreated( purchaseOrderId: nextId, status: PurchaseOrderStatus.Unpaid, vendorId: command.VendorId, lines: command.Lines.Select(l => new PurchaseOrderLine(l.ProductId, l.Quantity, l.Measure, l.PricePerUnit)) ); /// Act var actual = JC.CreatePurchaseOrder(getVendorExists, nextId, command); /// Assert CustomAssert.CoreValuesAreEqual(expected, actual); }
public static void Save(IMongoClient client, Guid nextId, object @event) { var bson = (@event switch { PurchaseOrderCreated e => Map(e), VendorDoesNotExist e => Map(e), ProductAddedToPurchaseOrder e => Map(e), CannotAddProductsToPaidPurchaseOrder e => Map(e), ProductRemovedFromPurchaseOrder e => Map(e), CannotRemoveProductsFromPaidPurchaseOrder e => Map(e), _ => throw new NotImplementedException() });
public void On(PurchaseOrderCreated @event) { }
public void Save_ResultIsPurchaseOrderCreated_PersistsExpectedDocument(PurchaseOrderStatus status) { /// Arrange // Get a purchase order created event. var @event = new PurchaseOrderCreated( purchaseOrderId: Guid.NewGuid(), status: status, vendorId: Guid.NewGuid(), lines: Enumerable.Empty <PurchaseOrderLine>() .Append(new PurchaseOrderLine( productId: Guid.NewGuid(), quantity: 14.25M, measure: "EA", pricePerUnit: 2.24M)) .Append(new PurchaseOrderLine( productId: Guid.NewGuid(), quantity: 5.5M, measure: "FT", pricePerUnit: 0.75M))); // Get some next id. var nextId = Guid.NewGuid(); // Get the expected document dictionary. var expected = new BsonDocument() .Add(new BsonElement("_id", BsonString.Create(nextId.ToString()))) .Add(new BsonElement("_type", BsonString.Create(nameof(PurchaseOrderCreated)))) .Add(new BsonElement(nameof(PurchaseOrderCreated.PurchaseOrderId), new BsonString(@event.PurchaseOrderId.ToString()))) .Add(new BsonElement(nameof(PurchaseOrderCreated.Status), new BsonString(Enum.GetName <PurchaseOrderStatus>(@event.Status)))) .Add(new BsonElement(nameof(PurchaseOrderCreated.VendorId), new BsonString(@event.VendorId.ToString()))) .Add(new BsonElement(nameof(PurchaseOrderCreated.Lines), new BsonArray() .Add(new BsonDocument() .Add(new BsonElement(nameof(PurchaseOrderLine.ProductId), BsonString.Create(@event.Lines.ElementAt(0).ProductId.ToString()))) .Add(new BsonElement(nameof(PurchaseOrderLine.Quantity), BsonDecimal128.Create(@event.Lines.ElementAt(0).Quantity))) .Add(new BsonElement(nameof(PurchaseOrderLine.Measure), BsonString.Create(@event.Lines.ElementAt(0).Measure))) .Add(new BsonElement(nameof(PurchaseOrderLine.PricePerUnit), BsonDecimal128.Create(@event.Lines.ElementAt(0).PricePerUnit)))) .Add(new BsonDocument() .Add(new BsonElement(nameof(PurchaseOrderLine.ProductId), BsonString.Create(@event.Lines.ElementAt(1).ProductId.ToString()))) .Add(new BsonElement(nameof(PurchaseOrderLine.Quantity), BsonDecimal128.Create(@event.Lines.ElementAt(1).Quantity))) .Add(new BsonElement(nameof(PurchaseOrderLine.Measure), BsonString.Create(@event.Lines.ElementAt(1).Measure))) .Add(new BsonElement(nameof(PurchaseOrderLine.PricePerUnit), BsonDecimal128.Create(@event.Lines.ElementAt(1).PricePerUnit)))))); // Mock up a mongo client for the mongo db event store. BsonDocument actual = null; var collection = Mock.Create <IMongoCollection <BsonDocument> >(); Mock.Arrange(() => collection.InsertOne(Arg.IsAny <BsonDocument>(), Arg.IsAny <InsertOneOptions>(), Arg.IsAny <CancellationToken>())) .DoInstead((BsonDocument document) => actual = document); var database = Mock.Create <IMongoDatabase>(); Mock.Arrange(() => database.GetCollection <BsonDocument>(Arg.AnyString, Arg.IsAny <MongoCollectionSettings>())) .Returns(collection); var client = Mock.Create <IMongoClient>(); Mock.Arrange(() => client.GetDatabase(Arg.AnyString, Arg.IsAny <MongoDatabaseSettings>())) .Returns(database); /// Act // Save the event. MongoDBEventStore.Save(client, nextId, @event); /// Assert // Verify that the actual document matches the expected document. Assert.Equal(expected, actual); }
//we decide which event type necessitates the creation of //a new record in the read model and pass in a new instance //of the view model to represent that record with a unique Id //in this case the OrderId is being used public object Any(PurchaseOrderCreated @event) => writer.Add(new OrderViewModel(@event.Id));