コード例 #1
0
        public void Visit_NonSimplifableMethodCall_ThrowsException()
        {
            // Arrange

            Expression <Func <Document, string> > expression =
                p => p.StringField.ToUpper();

            // Act/Assert

            Assert.Throws <NotSupportedException>(() => SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression));
        }
コード例 #2
0
        public void Visit_ParameterAsArrayIndex_ThrowsException()
        {
            // Arrange

            Expression <Func <Document, string> > expression =
                p => p.StringDictionary[p.StringProperty];

            // Act/Assert

            Assert.Throws <NotSupportedException>(() => SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression));
        }
コード例 #3
0
        private static string ParsePath <TDocument, TContent>(ITypeSerializerProvider typeSerializerProvider, Expression <Func <TDocument, TContent> > path)
        {
            var generatedSerializer = typeSerializerProvider != null ? typeSerializerProvider.Serializer as IExtendedTypeSerializer : null;

            if (generatedSerializer == null)
            {
                throw new NotSupportedException("Serializer must be IExtendedTypeSerializer to support subdocument paths.");
            }

            return(SubDocumentPathExpressionVisitor.GetPath(generatedSerializer, path));
        }
        public void Visit_DoesntUseParameter_ThrowsException()
        {
            // Arrange


            Expression <Func <Document, string> > expression =
                p => "key";

            // Act/Assert

            Assert.Throws <NotSupportedException>(() => SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression));
        }
コード例 #5
0
        public void Visit_NullSerializer_ThrowsException()
        {
            // Arrange


            Expression <Func <Document, string> > expression =
                p => "key";

            // Act/Assert

            var result = Assert.Throws <ArgumentNullException>(() => SubDocumentPathExpressionVisitor.GetPath(null, expression));

            Assert.AreEqual("serializer", result.ParamName);
        }
        public void Visit_ListCalculatedIndex_ReturnsPath()
        {
            // Arrange

            Expression <Func <Document, string> > expression =
                p => p.StringIList[2 + 1];

            // Act

            var result = SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression);

            // Assert

            Assert.Equal("`stringIList`[3]", result);
        }
        public void Visit_Field_ReturnsPath()
        {
            // Arrange

            Expression <Func <Document, string> > expression =
                p => p.StringField;

            // Act

            var result = SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression);

            // Assert

            Assert.Equal("`stringField`", result);
        }
        public void Visit_DictionaryCalculatedIndex_ReturnsPath()
        {
            // Arrange

            Expression <Func <Document, string> > expression =
                p => p.StringIDictionary["key" + "part"];

            // Act

            var result = SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression);

            // Assert

            Assert.Equal("`stringIDictionary`.`keypart`", result);
        }
        public void Visit_Dynamic_ReturnsPath()
        {
            // Arrange

            Expression <Func <Document, dynamic> > expression =
                p => p.DynamicProperty;

            // Act

            var result = SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression);

            // Assert

            Assert.Equal("`dynamicProperty`", result);
        }
        public void Visit_SubDocument_ReturnsPath()
        {
            // Arrange

            Expression <Func <Document, Document> > expression =
                p => p.SubDocument;

            // Act

            var result = SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression);

            // Assert

            Assert.Equal("`subDocument`", result);
        }
        public void Visit_NullablePropertyWithValue_ReturnsPath()
        {
            // Arrange

            Expression <Func <Document, int> > expression =
                p => p.NullableProperty.Value;

            // Act

            var result = SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression);

            // Assert

            Assert.Equal("`nullableProperty`", result);
        }
        public void Visit_SubDocumentInListProperty_ReturnsPath()
        {
            // Arrange

            Expression <Func <Document, string> > expression =
                p => p.SubDocumentList[1].StringProperty;

            // Act

            var result = SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression);

            // Assert

            Assert.Equal("`subDocumentList`[1].`stringProperty`", result);
        }
        public void Visit_ListAsMainDocumentSubProperty_ReturnsPath()
        {
            // Arrange

            Expression <Func <List <Document>, string> > expression =
                p => p[2].StringProperty;

            // Act

            var result = SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression);

            // Assert

            Assert.Equal("[2].`stringProperty`", result);
        }
        public void Visit_DictionaryAsMainDocumentSubProperty_ReturnsPath()
        {
            // Arrange

            Expression <Func <Dictionary <string, Document>, string> > expression =
                p => p["key"].StringProperty;

            // Act

            var result = SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression);

            // Assert

            Assert.Equal("`key`.`stringProperty`", result);
        }
        public void Visit_ListAsMainDocument_ReturnsPath()
        {
            // Arrange

            Expression <Func <List <Document>, Document> > expression =
                p => p[2];

            // Act

            var result = SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression);

            // Assert

            Assert.Equal("[2]", result);
        }
コード例 #16
0
        public static LookupInSpecBuilder <TDocument> Count <TDocument, TContent>(this LookupInSpecBuilder <TDocument> builder,
                                                                                  Expression <Func <TDocument, TContent> > path)
        {
            // ReSharper disable ConditionIsAlwaysTrueOrFalse
            if (builder == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(builder));
            }
            if (path == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(path));
            }
            // ReSharper restore ConditionIsAlwaysTrueOrFalse

            return((LookupInSpecBuilder <TDocument>)builder.Count(SubDocumentPathExpressionVisitor.GetPath(builder, path)));
        }
        public void Visit_DictionaryExternalVariableAndCalculatedIndex_ReturnsPath()
        {
            // Arrange

            var key = "key";
            Expression <Func <Document, string> > expression =
                p => p.StringIDictionary[key + "path"];

            // Act

            var result = SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression);

            // Assert

            Assert.Equal("`stringIDictionary`.`keypath`", result);
        }
コード例 #18
0
        public static MutateInSpecBuilder <TDocument> Replace <TDocument, TContent>(this MutateInSpecBuilder <TDocument> builder,
                                                                                    Expression <Func <TDocument, TContent> > path, TContent value)
        {
            // ReSharper disable ConditionIsAlwaysTrueOrFalse
            if (builder == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(builder));
            }
            if (path == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(path));
            }
            // ReSharper restore ConditionIsAlwaysTrueOrFalse

            return((MutateInSpecBuilder <TDocument>)
                   builder.Replace(SubDocumentPathExpressionVisitor.GetPath(builder, path), value));
        }
コード例 #19
0
        public static MutateInSpecBuilder <TDocument> Decrement <TDocument, TContent>(this MutateInSpecBuilder <TDocument> builder,
                                                                                      Expression <Func <TDocument, TContent> > path, long delta, bool createPath = false)
        {
            // ReSharper disable ConditionIsAlwaysTrueOrFalse
            if (builder == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(builder));
            }
            if (path == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(path));
            }
            // ReSharper restore ConditionIsAlwaysTrueOrFalse

            return((MutateInSpecBuilder <TDocument>)
                   builder.Decrement(SubDocumentPathExpressionVisitor.GetPath(builder, path), delta, createPath));
        }
        public void Visit_ListNegatedExternalVariableIndex_ReturnsPath()
        {
            // Arrange

            var index = 5;

            Expression <Func <Document, string> > expression =
                p => p.StringIList[-index];

            // Act

            var result = SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression);

            // Assert

            Assert.Equal("`stringIList`[-5]", result);
        }
        public void Visit_ListExternalVariableConditionalIndex_ReturnsPath()
        {
            // Arrange

            bool b = true;

            Expression <Func <Document, string> > expression =
                p => p.StringIList[b ? 1 : -1];

            // Act

            var result = SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression);

            // Assert

            Assert.Equal("`stringIList`[1]", result);
        }
コード例 #22
0
        public static MutateInSpecBuilder <TDocument> ArrayAddUnique <TDocument, TContent, TElement>(this MutateInSpecBuilder <TDocument> builder,
                                                                                                     Expression <Func <TDocument, TContent> > path, TElement value, bool createPath = false)
            where TContent : ICollection <TElement>
        {
            // ReSharper disable ConditionIsAlwaysTrueOrFalse
            if (builder == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(builder));
            }
            if (path == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(path));
            }
            // ReSharper restore ConditionIsAlwaysTrueOrFalse

            return((MutateInSpecBuilder <TDocument>)
                   builder.ArrayAddUnique(SubDocumentPathExpressionVisitor.GetPath(builder, path), value, createPath));
        }
        public void Visit_ListAsMainDocumentNoIndexer_ReturnsEmptyPath()
        {
            // Note: used for mutation subdocument commands such as array insert
            // where the top-level document is a JSON array

            // Arrange

            Expression <Func <List <Document>, List <Document> > > expression =
                p => p;

            // Act

            var result = SubDocumentPathExpressionVisitor.GetPath(new DefaultSerializer(), expression);

            // Assert

            Assert.Equal("", result);
        }
コード例 #24
0
        public static bool Exists <TDocument, TContent>(this ILookupInResult <TDocument> result, Expression <Func <TDocument, TContent> > path)
        {
            // ReSharper disable ConditionIsAlwaysTrueOrFalse
            if (result == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(result));
            }
            if (path == null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(path));
            }
            // ReSharper restore ConditionIsAlwaysTrueOrFalse

            var pathString = SubDocumentPathExpressionVisitor.GetPath(result, path);
            var index      = result.IndexOf(pathString);

            if (index < 0)
            {
                ThrowHelper.ThrowArgumentException(nameof(path), $"Path '{pathString}' is not found.");
            }

            return(result.Exists(index));
        }