Beispiel #1
0
        public void CreatePurchaseOrder_VendorDoesNotExist_ReturnsExpectedError()
        {
            /// 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 does not exist.
            Func <Guid, bool> getVendorExists = _ => false;

            // Get some id as the next id.
            var nextId = Guid.NewGuid();

            // Get the error we expect the domain to return.
            var expected = new VendorDoesNotExist(command.VendorId);

            /// Act
            var actual = JC.CreatePurchaseOrder(getVendorExists, nextId, command);

            /// Assert
            CustomAssert.CoreValuesAreEqual(expected, actual);
        }
Beispiel #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()
     });
        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);
        }