Example #1
0
        public void ValidationLevel_get_and_set_should_work(
            [Values(null, DocumentValidationLevel.Moderate, DocumentValidationLevel.Off)]
            DocumentValidationLevel?value)
        {
            var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings);

            subject.ValidationLevel = value;
            var result = subject.ValidationLevel;

            result.Should().Be(value);
        }
Example #2
0
        public void CreateCommand_should_return_expected_result_when_ValidationLevel_is_set(
            [Values(null, DocumentValidationLevel.Moderate, DocumentValidationLevel.Off)]
            DocumentValidationLevel?validationLevel)
        {
            var subject = new CreateCollectionOperation(_collectionNamespace, _messageEncoderSettings)
            {
                ValidationLevel = validationLevel
            };

            var result = subject.CreateCommand(null);

            var expectedResult = new BsonDocument
            {
                { "create", _collectionNamespace.CollectionName },
                { "validationLevel", () => validationLevel.ToString().ToLowerInvariant(), validationLevel != null }
            };

            result.Should().Be(expectedResult);
        }
        private CommandResult CreateCollection(IClientSessionHandle session, string collectionName, IMongoCollectionOptions options)
        {
            if (collectionName == null)
            {
                throw new ArgumentNullException("collectionName");
            }

            var                      collectionNamespace    = new CollectionNamespace(_namespace, collectionName);
            var                      messageEncoderSettings = GetMessageEncoderSettings();
            bool?                    autoIndexId            = null;
            bool?                    capped              = null;
            Collation                collation           = null;
            BsonDocument             indexOptionDefaults = null;
            int?                     maxDocuments        = null;
            long?                    maxSize             = null;
            bool?                    noPadding           = null;
            BsonDocument             storageEngine       = null;
            bool?                    usePowerOf2Sizes    = null;
            DocumentValidationAction?validationAction    = null;
            DocumentValidationLevel? validationLevel     = null;
            BsonDocument             validator           = null;

            if (options != null)
            {
                var optionsDocument = options.ToBsonDocument();

                BsonValue value;
                if (optionsDocument.TryGetValue("autoIndexId", out value))
                {
                    autoIndexId = value.ToBoolean();
                }
                if (optionsDocument.TryGetValue("capped", out value))
                {
                    capped = value.ToBoolean();
                }
                if (optionsDocument.TryGetValue("collation", out value))
                {
                    collation = Collation.FromBsonDocument(value.AsBsonDocument);
                }
                if (optionsDocument.TryGetValue("indexOptionDefaults", out value))
                {
                    indexOptionDefaults = value.AsBsonDocument;
                }
                if (optionsDocument.TryGetValue("max", out value))
                {
                    maxDocuments = value.ToInt32();
                }
                if (optionsDocument.TryGetValue("flags", out value))
                {
                    noPadding = ((CollectionUserFlags)value.ToInt32() & CollectionUserFlags.NoPadding) != 0;
                }
                if (optionsDocument.TryGetValue("size", out value))
                {
                    maxSize = value.ToInt64();
                }
                if (optionsDocument.TryGetValue("storageEngine", out value))
                {
                    storageEngine = value.AsBsonDocument;
                }
                if (optionsDocument.TryGetValue("flags", out value))
                {
                    usePowerOf2Sizes = ((CollectionUserFlags)value.ToInt32() & CollectionUserFlags.UsePowerOf2Sizes) != 0;
                }
                if (optionsDocument.TryGetValue("validationAction", out value))
                {
                    validationAction = (DocumentValidationAction)Enum.Parse(typeof(DocumentValidationAction), value.AsString, ignoreCase: true);
                }
                if (optionsDocument.TryGetValue("validationLevel", out value))
                {
                    validationLevel = (DocumentValidationLevel)Enum.Parse(typeof(DocumentValidationLevel), value.AsString, ignoreCase: true);
                }
                if (optionsDocument.TryGetValue("validator", out value))
                {
                    validator = value.AsBsonDocument;
                }
            }

            var operation = new CreateCollectionOperation(collectionNamespace, messageEncoderSettings)
            {
                AutoIndexId         = autoIndexId,
                Capped              = capped,
                Collation           = collation,
                IndexOptionDefaults = indexOptionDefaults,
                MaxDocuments        = maxDocuments,
                MaxSize             = maxSize,
                NoPadding           = noPadding,
                StorageEngine       = storageEngine,
                UsePowerOf2Sizes    = usePowerOf2Sizes,
                ValidationAction    = validationAction,
                ValidationLevel     = validationLevel,
                Validator           = validator,
                WriteConcern        = _settings.WriteConcern
            };

            var response = ExecuteWriteOperation(session, operation);

            return(new CommandResult(response));
        }
Example #4
0
        /// <summary>
        /// Creates a collection. MongoDB creates collections automatically when they are first used, so
        /// you only need to call this method if you want to provide non-default options.
        /// </summary>
        /// <param name="collectionName">The name of the collection.</param>
        /// <param name="options">Options for creating this collection (usually a CollectionOptionsDocument or constructed using the CollectionOptions builder).</param>
        /// <returns>A CommandResult.</returns>
        public virtual CommandResult CreateCollection(string collectionName, IMongoCollectionOptions options)
        {
            if (collectionName == null)
            {
                throw new ArgumentNullException(nameof(collectionName));
            }

            var                      collectionNamespace    = new CollectionNamespace(_namespace, collectionName);
            var                      messageEncoderSettings = GetMessageEncoderSettings();
            bool?                    autoIndexId            = null;
            bool?                    capped = null;
            BsonDocument             indexOptionDefaults = null;
            int?                     maxDocuments        = null;
            long?                    maxSize             = null;
            BsonDocument             storageEngine       = null;
            bool?                    usePowerOf2Sizes    = null;
            DocumentValidationAction?validationAction    = null;
            DocumentValidationLevel? validationLevel     = null;
            BsonDocument             validator           = null;

            if (options != null)
            {
                var optionsDocument = options.ToBsonDocument();

                BsonValue value;
                if (optionsDocument.TryGetValue("autoIndexId", out value))
                {
                    autoIndexId = value.ToBoolean();
                }
                if (optionsDocument.TryGetValue("capped", out value))
                {
                    capped = value.ToBoolean();
                }
                if (optionsDocument.TryGetValue("indexOptionDefaults", out value))
                {
                    indexOptionDefaults = value.AsBsonDocument;
                }
                if (optionsDocument.TryGetValue("max", out value))
                {
                    maxDocuments = value.ToInt32();
                }
                if (optionsDocument.TryGetValue("size", out value))
                {
                    maxSize = value.ToInt64();
                }
                if (optionsDocument.TryGetValue("storageEngine", out value))
                {
                    storageEngine = value.AsBsonDocument;
                }
                if (optionsDocument.TryGetValue("flags", out value))
                {
                    usePowerOf2Sizes = value.ToInt32() == 1;
                }
                if (optionsDocument.TryGetValue("validationAction", out value))
                {
                    validationAction = (DocumentValidationAction)Enum.Parse(typeof(DocumentValidationAction), value.AsString, ignoreCase: true);
                }
                if (optionsDocument.TryGetValue("validationLevel", out value))
                {
                    validationLevel = (DocumentValidationLevel)Enum.Parse(typeof(DocumentValidationLevel), value.AsString, ignoreCase: true);
                }
                if (optionsDocument.TryGetValue("validator", out value))
                {
                    validator = value.AsBsonDocument;
                }
            }

            var operation = new CreateCollectionOperation(collectionNamespace, messageEncoderSettings)
            {
                AutoIndexId         = autoIndexId,
                Capped              = capped,
                IndexOptionDefaults = indexOptionDefaults,
                MaxDocuments        = maxDocuments,
                MaxSize             = maxSize,
                StorageEngine       = storageEngine,
                UsePowerOf2Sizes    = usePowerOf2Sizes,
                ValidationAction    = validationAction,
                ValidationLevel     = validationLevel,
                Validator           = validator
            };

            var response = ExecuteWriteOperation(operation);

            return(new CommandResult(response));
        }