public void RemoveProductFromPurchaseOrder_PurchaseOrderStatusIsUnPaidAndPurchaseOrderHasProduct_ReturnsProductRemovedFromPurchaseOrderResult()
        {
            /// 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,
            // and make sure that it has the product.
            var purchaseOrder = new PurchaseOrderForRemoveProductTask(
                purchaseOrderId: command.PurchaseOrderId,
                status: PurchaseOrderStatus.Unpaid,
                productIds: Enumerable.Empty <Guid>()
                .Append(command.ProductId)
                );

            // 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 ProductRemovedFromPurchaseOrder(
                purchaseOrderId: command.PurchaseOrderId,
                productId: command.ProductId);

            /// Act
            var actual = JC.RemoveProductFromPurchaseOrder(getPurchaseOrder, command);

            /// Assert
            CustomAssert.CoreValuesAreEqual(expected, actual);
        }
Exemple #2
0
 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()
     });
Exemple #3
0
        public void Save_ResultIsProductRemovedFromPurchaseOrder_PersistsExpectedDocument()
        {
            /// Arrange
            // Get a product Removed to purchase order event.
            var @event = new ProductRemovedFromPurchaseOrder(
                purchaseOrderId: Guid.NewGuid(),
                productId: 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(ProductRemovedFromPurchaseOrder))))
                           .Add(new BsonElement(nameof(ProductRemovedFromPurchaseOrder.PurchaseOrderId), new BsonString(@event.PurchaseOrderId.ToString())))
                           .Add(new BsonElement(nameof(ProductRemovedFromPurchaseOrder.ProductId), new BsonString(@event.ProductId.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);
        }
Exemple #4
0
        public void loadtest()
        {
            // Get a MongoClient.
            var client = new MongoClient("mongodb://localhost:27017");

            Func <Guid, bool> getVendorExists = _ => true;
            Func <Guid, PurchaseOrderForAddProductTask>    getPurchaseOrderForAddProductTask    = purchaseOrderId => MongoDBEventStore.GetPurchaseOrderForAddProductTask(client, purchaseOrderId);
            Func <Guid, PurchaseOrderForRemoveProductTask> getPurchaseOrderForRemoveProductTask = purchaseOrderId => MongoDBEventStore.GetPurchaseOrderForRemoveProductTask(client, purchaseOrderId);
            Func <Guid> nextId = Guid.NewGuid;

            var create = new CreatePurchaseOrder(
                vendorId: nextId(),
                lines: new List <CreatePurchaseOrderLine>
            {
                new CreatePurchaseOrderLine(
                    productId: nextId(),
                    quantity: 14.5M,
                    measure: "FT",
                    pricePerUnit: 2.8M),
                new CreatePurchaseOrderLine(
                    productId: nextId(),
                    quantity: 1.5M,
                    measure: "YD",
                    pricePerUnit: 235.82M)
            });


            Guid purchaseOrderId = nextId();

            var createResult = JC.CreatePurchaseOrder(
                getVendorExists: getVendorExists,
                nextId: purchaseOrderId,
                command: create);

            MongoDBEventStore.Save(client, nextId(), createResult);

            var expected = getPurchaseOrderForAddProductTask(purchaseOrderId);

            var productId = Guid.NewGuid();

            // Put 10000 events in the store for this purchase order.
            for (int i = 0; i < 5000; i++)
            {
                var addResult = new ProductAddedToPurchaseOrder(
                    purchaseOrderId: purchaseOrderId,
                    productId: productId,
                    measure: "EA",
                    quantity: i);

                MongoDBEventStore.Save(client, nextId(), addResult);

                var removeResult = new ProductRemovedFromPurchaseOrder(
                    purchaseOrderId: purchaseOrderId,
                    productId: productId);

                MongoDBEventStore.Save(client, nextId(), removeResult);
            }

            // Now for the time trial
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            var actual = getPurchaseOrderForAddProductTask(purchaseOrderId);

            stopwatch.Stop();

            CustomAssert.CoreValuesAreEqual(expected, actual);
            Assert.Equal(0, stopwatch.ElapsedMilliseconds);
        }