Exemple #1
0
        public void FromBsonDocument_should_throw_when_locale_element_is_missing()
        {
            var document = BsonDocument.Parse("{ }");

            var exception = Record.Exception(() => Collation.FromBsonDocument(document));

            exception.Should().BeOfType <ArgumentException>();
        }
Exemple #2
0
        public void FromBsonDocument_should_throw_when_invalid_element_is_present()
        {
            var document = BsonDocument.Parse("{ invalid : 1 }");

            var exception = Record.Exception(() => Collation.FromBsonDocument(document));

            exception.Should().BeOfType <ArgumentException>();
        }
Exemple #3
0
        public void FromBsonDocument_should_return_expected_result()
        {
            var document = BsonDocument.Parse("{ locale : 'en_US' }");

            var result = Collation.FromBsonDocument(document);

            result.Alternate.Should().BeNull();
            result.Backwards.Should().NotHaveValue();
            result.CaseFirst.Should().BeNull();
            result.CaseLevel.Should().NotHaveValue();
            result.Locale.Should().Be("en_US");
            result.MaxVariable.Should().BeNull();
            result.Normalization.Should().NotHaveValue();
            result.NumericOrdering.Should().NotHaveValue();
            result.Strength.Should().BeNull();
        }
Exemple #4
0
        public void FromBsonDocument_should_return_expected_result_when_backwards_element_is_present(
            [Values(false, true)]
            bool backwards)
        {
            var document = new BsonDocument
            {
                { "locale", "en_US" },
                { "backwards", backwards }
            };

            var result = Collation.FromBsonDocument(document);

            result.Alternate.Should().BeNull();
            result.Backwards.Should().Be(backwards);
            result.CaseFirst.Should().BeNull();
            result.CaseLevel.Should().NotHaveValue();
            result.Locale.Should().Be("en_US");
            result.MaxVariable.Should().BeNull();
            result.Normalization.Should().NotHaveValue();
            result.NumericOrdering.Should().NotHaveValue();
            result.Strength.Should().BeNull();
        }
Exemple #5
0
        public void FromBsonDocument_should_return_expected_result_when_alternate_element_is_present(
            [Values("non-ignorable", "shifted")]
            string alternateString)
        {
            var document = new BsonDocument
            {
                { "locale", "en_US" },
                { "alternate", alternateString }
            };

            var result = Collation.FromBsonDocument(document);

            result.Alternate.Should().Be(Collation.ToCollationAlternate(alternateString));
            result.Backwards.Should().NotHaveValue();
            result.CaseFirst.Should().BeNull();
            result.CaseLevel.Should().NotHaveValue();
            result.Locale.Should().Be("en_US");
            result.MaxVariable.Should().BeNull();
            result.Normalization.Should().NotHaveValue();
            result.NumericOrdering.Should().NotHaveValue();
            result.Strength.Should().BeNull();
        }
Exemple #6
0
        public void FromBsonDocument_should_return_expected_result_when_strength_element_is_present(
            [Values(1, 2, 3, 4, 5)]
            int strengthInteger)
        {
            var document = new BsonDocument
            {
                { "locale", "en_US" },
                { "strength", strengthInteger }
            };

            var result = Collation.FromBsonDocument(document);

            result.Alternate.Should().BeNull();
            result.Backwards.Should().NotHaveValue();
            result.CaseFirst.Should().BeNull();
            result.CaseLevel.Should().NotHaveValue();
            result.Locale.Should().Be("en_US");
            result.MaxVariable.Should().BeNull();
            result.Normalization.Should().NotHaveValue();
            result.NumericOrdering.Should().NotHaveValue();
            result.Strength.Should().Be(Collation.ToCollationStrength(strengthInteger));
        }
Exemple #7
0
        public void FromBsonDocument_should_return_expected_result_when_caseFirst_element_is_present(
            [Values("lower", "off", "upper")]
            string caseFirstString)
        {
            var document = new BsonDocument
            {
                { "locale", "en_US" },
                { "caseFirst", caseFirstString }
            };

            var result = Collation.FromBsonDocument(document);

            result.Alternate.Should().BeNull();
            result.Backwards.Should().NotHaveValue();
            result.CaseFirst.Should().Be(Collation.ToCollationCaseFirst(caseFirstString));
            result.CaseLevel.Should().NotHaveValue();
            result.Locale.Should().Be("en_US");
            result.MaxVariable.Should().BeNull();
            result.Normalization.Should().NotHaveValue();
            result.NumericOrdering.Should().NotHaveValue();
            result.Strength.Should().BeNull();
        }
        private CommandResult CreateView(IClientSessionHandle session, string viewName, string viewOn, IEnumerable <BsonDocument> pipeline, IMongoCreateViewOptions options)
        {
            if (viewName == null)
            {
                throw new ArgumentNullException(nameof(viewName));
            }
            if (viewOn == null)
            {
                throw new ArgumentNullException(nameof(viewOn));
            }
            if (pipeline == null)
            {
                throw new ArgumentNullException(nameof(pipeline));
            }

            Collation collation = null;

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

                BsonValue value;
                if (optionsDocument.TryGetValue("collation", out value))
                {
                    collation = Collation.FromBsonDocument(value.AsBsonDocument);
                }
            }

            var operation = new CreateViewOperation(_namespace, viewName, viewOn, pipeline, GetMessageEncoderSettings())
            {
                Collation    = collation,
                WriteConcern = _settings.WriteConcern
            };

            var response = ExecuteWriteOperation(session, operation);

            return(new CommandResult(response));
        }
        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));
        }