public void RemoveProductFromPurchaseOrder_POStatusIsPaid_ReturnsCannotRemoveProductsFromPaidPurchaseOrderResult() { /// Arrange // Get a command to remove a product from a purchase order. var command = new RemoveProductFromPurchaseOrder( purchaseOrderId: Guid.NewGuid(), productId: Guid.NewGuid() ); // Get a purchase order aggregate to use // when removing a product from a purchase order. var purchaseOrder = new PurchaseOrderForRemoveProductTask( purchaseOrderId: command.PurchaseOrderId, status: PurchaseOrderStatus.Paid, productIds: Enumerable.Empty <Guid>() // doesn't matter for this test ); // Get a function that returns the purchase order aggregate // to use when removing a product from a purchase order. Func <Guid, PurchaseOrderForRemoveProductTask> getPurchaseOrder = _ => purchaseOrder; // Get the event we expect the domain to return. var expected = new CannotRemoveProductsFromPaidPurchaseOrder( purchaseOrderId: command.PurchaseOrderId); /// Act var actual = JC.RemoveProductFromPurchaseOrder(getPurchaseOrder, 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 Save_ResultIsCannotRemoveProductsToPaidPurchaseOrder_PersistsExpectedDocument() { /// Arrange // Get an instance of the event. var @event = new CannotRemoveProductsFromPaidPurchaseOrder( purchaseOrderId: Guid.NewGuid()); // 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(CannotRemoveProductsFromPaidPurchaseOrder)))) .Add(new BsonElement(nameof(CannotRemoveProductsFromPaidPurchaseOrder.PurchaseOrderId), new BsonString(@event.PurchaseOrderId.ToString()))); // 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); }