public MongoDBEventStoreIntegrationTests()
        {
            CreateConnection();

            _eventPublisher    = new Mock <IEventPublisher>();
            _mongoDBEventStore = new MongoDBEventStore(_eventPublisher.Object, _database);
        }
Ejemplo n.º 2
0
        public MongoDBEventStoreTests()
        {
            _publisher       = new Mock <IEventPublisher>();
            _mongoDb         = new Mock <IMongoDatabase>();
            _mongoCollection = new Mock <IMongoCollection <BaseEvent> >();

            _mongoDb.Setup(db => db.GetCollection <BaseEvent>("events", null))
            .Returns(_mongoCollection.Object);

            _mongoDBEventStore = new MongoDBEventStore(_publisher.Object, _mongoDb.Object);
        }
        public void Save_ResultIsProductAddedToPurchaseOrder_PersistsExpectedDocument()
        {
            /// Arrange
            // Get a product added to purchase order event.
            var @event = new ProductAddedToPurchaseOrder(
                purchaseOrderId: Guid.NewGuid(),
                productId: Guid.NewGuid(),
                measure: "EA",
                quantity: 14.25M);

            // 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(ProductAddedToPurchaseOrder))))
                           .Add(new BsonElement(nameof(ProductAddedToPurchaseOrder.PurchaseOrderId), new BsonString(@event.PurchaseOrderId.ToString())))
                           .Add(new BsonElement(nameof(ProductAddedToPurchaseOrder.ProductId), new BsonString(@event.ProductId.ToString())))
                           .Add(new BsonElement(nameof(ProductAddedToPurchaseOrder.Measure), BsonString.Create(@event.Measure)))
                           .Add(new BsonElement(nameof(ProductAddedToPurchaseOrder.Quantity), BsonDecimal128.Create(@event.Quantity)));

            // 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);
        }
        public void Save_ResultIsVendorDoesNotExist_PersistsExpectedDocument()
        {
            /// Arrange
            // Get a purchase order created event.
            var @event = new VendorDoesNotExist(
                vendorId: 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(VendorDoesNotExist))))
                           .Add(new BsonElement(nameof(VendorDoesNotExist.VendorId), new BsonString(@event.VendorId.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);
        }
        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);
        }
Ejemplo n.º 6
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);
        }