public void ShouldCreateEqualityQueryOnCollectionContainsMethod()
 {
     Expression<Func<TestClass, bool>> expression = x => x.CollectionData.Contains("5");
     var expressionVisitor = new ExpressionVisitor();
     var repositoryQuery = new RepositoryQuery();
     var queryContainer = new QueryContainer(repositoryQuery);
     expressionVisitor.Visit(null, expression, queryContainer);
     var query = queryContainer.Query;
     Assert.That(query, Is.Not.Null);
     Assert.That(query.Operand, Is.EqualTo(MongoOperand.Equals));
     Assert.That(query.PropertyName, Is.EqualTo("CollectionData"));
     Assert.That(query.Value, Is.EqualTo(BsonValue.Create("5")));
     Assert.That(query.Inverted, Is.False);
     Assert.That(query.MongoQuery.ToString(), Is.EqualTo(Query.EQ("CollectionData", "5").ToString()));
 }
        private string FindDeepParameter(Expression expression)
        {
            if (expression.NodeType == ExpressionType.MemberAccess)
            {
                var memberExpression = expression as MemberExpression;
                if (memberExpression != null)
                {
                    var name = memberExpression.Member.Name;
                    var subName = FindDeepParameter(memberExpression.Expression);
                    if (!string.IsNullOrEmpty(subName))
                    {
                        return subName + "." + name;
                    }
                    return name;
                }
            }

            if (expression.NodeType == ExpressionType.Call)
            {
                var methodCallExpression = expression as MethodCallExpression;
                if (methodCallExpression != null &&
                    methodCallExpression.Method.Name == "get_Item" &&
                    methodCallExpression.Object != null &&
                    methodCallExpression.Object.Type.GetGenericTypeDefinition() == typeof(Dictionary<,>))
                {
                    var container = new QueryContainer(new RepositoryQuery());
                    Visit(null, methodCallExpression.Object, container);
                    var dictionaryKey = GetValueFromExpression(methodCallExpression.Arguments[0]) as string;
                    var name = container.Query.PropertyName + "." + dictionaryKey;
                    return name;
                }
            }
            return string.Empty;
        }
        public void ShouldSupportToStringOnMemberAccessExpression()
        {
            const int someConstant = 5;
            Expression<Func<TestClass, bool>> expression = x => x.SomeData == someConstant.ToString();
            var expressionVisitor = new ExpressionVisitor();
            var repositoryQuery = new RepositoryQuery();
            var queryContainer = new QueryContainer(repositoryQuery);

            expressionVisitor.Visit(null, expression, queryContainer);

            var query = queryContainer.Query;
            Assert.That(query, Is.Not.Null);
            Assert.That(query.Inverted, Is.False);
            Assert.That(query.Operand, Is.EqualTo(MongoOperand.Equals));
            Assert.That(query.Value, Is.EqualTo(BsonValue.Create("5")));
            Assert.That(query.MongoQuery.ToString(), Is.EqualTo(Query.EQ("SomeData", "5").ToString()));
        }
 public void ShouldThrowNotSupportedExceptionOnToStringMethodOnCollectionValue()
 {
     Expression<Func<TestClass, bool>> expression = x => x.IntData.ToString() == "5";
     var expressionVisitor = new ExpressionVisitor();
     var repositoryQuery = new RepositoryQuery();
     var queryContainer = new QueryContainer(repositoryQuery);
     expressionVisitor.Visit(null, expression, queryContainer);
 }
 public void ShouldSupportQueriesWithMultipleAnds()
 {
     var expressionVisitor = new ExpressionVisitor();
     Expression<Func<TestClass, bool>> expression = x => x.Id == "1" && x.IntData == 2 && x.SomeData == "3";
     var repositoryQuery = new RepositoryQuery();
     var queryContainer = new QueryContainer(repositoryQuery);
     expressionVisitor.Visit(null, expression, queryContainer);
     var query = queryContainer.Query;
     var andQuery = Query.And(Query.EQ("_id", "1"), Query.EQ("IntData", 2), Query.EQ("SomeData", "3"));
     Assert.That(query.MongoQuery.ToString(), Is.EqualTo(andQuery.ToString()));
 }
 public void ShouldSupportQueriesWithOrOperatorAndConstants()
 {
     var expressionVisitor = new ExpressionVisitor();
     Expression<Func<TestClass, bool>> expression = x => x.Id == "5" || x.SomeData == "6";
     var repositoryQuery = new RepositoryQuery();
     var queryContainer = new QueryContainer(repositoryQuery);
     expressionVisitor.Visit(null, expression, queryContainer);
     var query = queryContainer.Query;
     var andQuery = Query.Or(Query.EQ("_id", "5"), Query.EQ("SomeData", "6"));
     Assert.That(query.MongoQuery.ToString(), Is.EqualTo(andQuery.ToString()));
 }
 private void VisitUnary(ParameterExpression[] parameters, UnaryExpression unaryExpression, QueryContainer container)
 {
     Visit(parameters, unaryExpression.Operand, container);
 }
 public void ShouldSupportQueriesWithAndOperatorAndConstantVariableExpression()
 {
     var expressionVisitor = new ExpressionVisitor();
     const string idValue = "5";
     const string someDataValue = "6";
     Expression<Func<TestClass, bool>> expression = x => x.Id == idValue && x.SomeData == someDataValue;
     var repositoryQuery = new RepositoryQuery();
     var queryContainer = new QueryContainer(repositoryQuery);
     expressionVisitor.Visit(null, expression, queryContainer);
     var query = queryContainer.Query;
     var andQuery = Query.And(Query.EQ("_id", idValue), Query.EQ("SomeData", someDataValue));
     Assert.That(query.MongoQuery.ToString(), Is.EqualTo(andQuery.ToString()));
 }
        private void VisitMethodCall(ParameterExpression[] parameters, MethodCallExpression expression, QueryContainer container)
        {
            var declaringType = expression.Method.DeclaringType;
            var enumerableType = (declaringType.GetInterfaces()
                                      .Where(x => x.IsGenericType)
                                      .Where(x => x.GetGenericTypeDefinition() == typeof(IEnumerable<>))
                                      .Select(x => x.GetGenericArguments()[0])
                                      .FirstOrDefault() != null);
            if (enumerableType && expression.Method.Name == "Contains")
            {
                var memberExpression = expression.Object as MemberExpression;
                if (memberExpression != null)
                {
                    var constantExpression = memberExpression.Expression as ConstantExpression;
                    var prop = memberExpression.Member;
                    var fieldName = prop.Name;
                    if (constantExpression != null)
                    {
                        var value = constantExpression.Value;
                        var fieldType = value.GetType();
                        var fieldInfo = fieldType.GetField(fieldName);
                        var field = fieldInfo.GetValue(value);
                        var bsonValue = BsonArray.Create(field);
                        var inverted = container.Query.Inverted;
                        container.Query = new InRepositoryQuery { Value = bsonValue, Inverted = inverted };
                    }
                }
                Visit(parameters, expression.Arguments[0], container);
            }
            else if ((declaringType == typeof(IEnumerable) || declaringType == typeof(Enumerable) && declaringType != typeof(string) && declaringType.IsPrimitive == false) && expression.Method.Name == "Contains")
            {

                var inverted = container.Query.Inverted;
                var memberExpression = expression.Arguments[0] as MemberExpression;

                if (memberExpression != null && memberExpression.Expression is ParameterExpression)
                {
                    container.Query.Operand = MongoOperand.Equals;
                }
                else
                {
                    container.Query = new InRepositoryQuery { Inverted = inverted };
                }
                Visit(parameters, expression.Arguments[0], container);
                Visit(parameters, expression.Arguments[1], container);
            }
            else if (expression.Method.Name == "get_Item" &&
                     expression.Object != null &&
                     expression.Object.Type.GetGenericTypeDefinition() == typeof(Dictionary<,>))
            {
                Visit(parameters, expression.Object, container);
                var dictionaryKey = GetValueFromExpression(expression.Arguments[0]) as string;
                container.Query.PropertyName += "." + dictionaryKey; //Append the dictionary key
            }
            else if(expression.Method.Name == "IsNullOrEmpty")
            {
                var orRepositoryQuery = new OrRepositoryQuery
                                            {
                                                LeftContainer = new QueryContainer(new RepositoryQuery())
                                                                    {
                                                                        Query =
                                                                            {
                                                                                Value = "",
                                                                                Operand = MongoOperand.Equals,
                                                                                Inverted = container.Query.Inverted
                                                                            }
                                                                    }
                                            };
                Visit(null, expression.Arguments[0], orRepositoryQuery.LeftContainer);
                orRepositoryQuery.RightContainer = new QueryContainer(new RepositoryQuery())
                                                       {
                                                           Query =
                                                               {
                                                                   Value = null,
                                                                   Operand = MongoOperand.Equals,
                                                                   Inverted = container.Query.Inverted
                                                               }
                                                       };

                Visit(null, expression.Arguments[0], orRepositoryQuery.RightContainer);
                container.Query = orRepositoryQuery;
            }
            else if(expression.Method.Name == "IsNotNullOrEmpty")
            {
                var orRepositoryQuery = new OrRepositoryQuery
                                            {
                                                LeftContainer = new QueryContainer(new RepositoryQuery())
                                                                    {
                                                                        Query =
                                                                            {
                                                                                Value = "",
                                                                                Operand = MongoOperand.NotEquals
                                                                            }
                                                                    }
                                            };
                Visit(null, expression.Arguments[0], orRepositoryQuery.LeftContainer);
                orRepositoryQuery.RightContainer = new QueryContainer(new RepositoryQuery())
                                                       {
                                                           Query =
                                                               {
                                                                   Value = null,
                                                                   Operand = MongoOperand.NotEquals
                                                               }
                                                       };

                Visit(null, expression.Arguments[0], orRepositoryQuery.RightContainer);
                container.Query = orRepositoryQuery;
            }
            else if (expression.Method.Name == "Parse")
            {
                var memberExpression = expression.Arguments[0] as MemberExpression;
                if (expression.Arguments[0] is ConstantExpression || (memberExpression != null && memberExpression.Expression is ConstantExpression))
                {
                    var value = GetValueFromExpression(expression);
                    container.Query.Value = BsonValue.Create(value);
                }
                else
                {
                    throw new NotSupportedException("Parse on non-Constant Expressions is not supported");
                }
            }
            else if (expression.Method.Name == "ToString")
            {
                var memberExpression = expression.Object as MemberExpression;
                if (expression.Object is ConstantExpression || (memberExpression != null && memberExpression.Expression is ConstantExpression))
                {
                    var value = GetValueFromExpression(expression);
                    container.Query.Value = BsonValue.Create(value);
                }
                else
                {
                    throw new NotSupportedException("ToString on non-Constant Expressions is not supported");
                }
            }
            else
            {
                throw new NotSupportedException(string.Format("Method {0} is not supported", expression.Method.Name));
            }
        }
        public void Visit(ParameterExpression[] parameters, Expression expression, QueryContainer container)
        {
            if (expression == null)
            {
                return;
            }

            switch (expression.NodeType)
            {

                case ExpressionType.Not:
                    container.Query.Inverted = true;
                    VisitUnary(parameters, (UnaryExpression)expression, container);
                    break;
                case ExpressionType.Negate:
                case ExpressionType.NegateChecked:
                case ExpressionType.Convert:
                case ExpressionType.ConvertChecked:
                case ExpressionType.ArrayLength:
                case ExpressionType.Quote:
                case ExpressionType.TypeAs:
                    VisitUnary(parameters, (UnaryExpression)expression, container);
                    break;
                case ExpressionType.And:
                case ExpressionType.AndAlso:
                    VisitBinary(parameters, expression as BinaryExpression, container);
                    break;
                case ExpressionType.Add:
                case ExpressionType.AddChecked:
                case ExpressionType.Subtract:
                case ExpressionType.SubtractChecked:
                case ExpressionType.Multiply:
                case ExpressionType.MultiplyChecked:
                case ExpressionType.Divide:
                case ExpressionType.Modulo:
                case ExpressionType.Or:
                case ExpressionType.OrElse:
                case ExpressionType.LessThan:
                case ExpressionType.LessThanOrEqual:
                case ExpressionType.GreaterThan:
                case ExpressionType.GreaterThanOrEqual:
                case ExpressionType.Equal:
                case ExpressionType.NotEqual:
                case ExpressionType.Coalesce:
                case ExpressionType.ArrayIndex:
                case ExpressionType.RightShift:
                case ExpressionType.LeftShift:
                case ExpressionType.ExclusiveOr:
                    VisitBinary(parameters, (BinaryExpression)expression, container);
                    break;
                case ExpressionType.TypeIs:
                case ExpressionType.Conditional:
                case ExpressionType.Constant:
                    VisitConstant(expression as ConstantExpression, container);
                    break;
                case ExpressionType.MemberAccess:
                    VisitMember(parameters, expression, container);
                    break;
                case ExpressionType.Lambda:
                    VisitLambda((LambdaExpression)expression, container);
                    break;
                case ExpressionType.Call:
                    VisitMethodCall(parameters, expression as MethodCallExpression, container);
                    break;
                default:
                    throw new NotSupportedException(string.Format("Unhandled expression type: '{0}'", expression.NodeType));
            }
        }
        private void VisitMember(ParameterExpression[] parameters, Expression expression, QueryContainer container)
        {
            var memberExpression = (MemberExpression)expression;
            var expressionName = expression.ToString();
            var subStrings = expressionName.Split('.');
            var isDeepParameter = false;
            if (subStrings.Length > 0 && parameters != null && parameters.Length > 0)
            {
                isDeepParameter = parameters[0].ToString() == subStrings[0];
            }

            if (memberExpression.Expression is ParameterExpression || isDeepParameter)
            {
                string name;
                if (isDeepParameter)
                {
                    name = FindDeepParameter(memberExpression);
                }
                else
                {
                    name = memberExpression.Member.Name;
                }
                if (name.ToLower() == "id")
                {
                    name = "_id";
                }
                container.Query.PropertyName = name;
                if (memberExpression.Type == typeof(bool) && container.Query.Operand == MongoOperand.NotSet)
                {
                    container.Query.Operand = MongoOperand.Equals;
                    container.Query.Value = true;
                }
            }
            else
            {
                var value = GetValueFromExpression(memberExpression);
                container.Query.Value = BsonValue.Create(value);
            }
        }
 private void VisitLambda(LambdaExpression lambdaExpression, QueryContainer container)
 {
     var newLambdaParam = lambdaExpression.Parameters.ToArray();
     Visit(newLambdaParam, lambdaExpression.Body, container);
 }
        private void VisitBinary(ParameterExpression[] parameters, BinaryExpression binaryExpression, QueryContainer container)
        {
            QueryContainer queryLeft;
            QueryContainer queryRight;
            switch (binaryExpression.NodeType)
            {
                case ExpressionType.AndAlso:
                case ExpressionType.And:
                    var andQuery = new AndRepositoryQuery();
                    queryLeft = new QueryContainer(new RepositoryQuery());
                    queryRight = new QueryContainer(new RepositoryQuery());
                    andQuery.LeftContainer = queryLeft;
                    andQuery.RightContainer = queryRight;
                    container.Query = andQuery;
                    break;
                case ExpressionType.Or:
                case ExpressionType.OrElse:
                    var orQuery = new OrRepositoryQuery();
                    queryLeft = new QueryContainer(new RepositoryQuery());
                    queryRight = new QueryContainer(new RepositoryQuery());
                    orQuery.LeftContainer = queryLeft;
                    orQuery.RightContainer = queryRight;
                    container.Query = orQuery;
                    break;
                default:
                    container.Query.Operand = FindMongoOperand(binaryExpression.NodeType);
                    queryLeft = queryRight = container;
                    break;

            }
            Visit(parameters, binaryExpression.Left, queryLeft);
            Visit(parameters, binaryExpression.Right, queryRight);
        }
 public void ShouldSupportImplicitCastWithEnums()
 {
     var expressionVisitor = new ExpressionVisitor();
     Expression<Func<TestClass, bool>> expression = x => x.EnumValue == TestEnum.EnumValue1;
     var repositoryQuery = new RepositoryQuery();
     var queryContainer = new QueryContainer(repositoryQuery);
     expressionVisitor.Visit(null, expression, queryContainer);
     var query = queryContainer.Query;
     Assert.That(query, Is.Not.Null);
     Assert.That(query.Operand, Is.EqualTo(MongoOperand.Equals));
     Assert.That(query.Inverted, Is.False);
     Assert.That(query.PropertyName, Is.EqualTo("EnumValue"));
     Assert.That(query.Value, Is.EqualTo(BsonValue.Create(TestEnum.EnumValue1)));
     Assert.That(query.MongoQuery.ToString(), Is.EqualTo(Query.EQ("EnumValue", TestEnum.EnumValue1).ToString()));
 }
        public void ShouldCreateEqualsQueryOnInvertedSimpleDictionaryData()
        {
            Expression<Func<DictionaryTestClass, bool>> expression = x => !(x.SimpleDictionaryData["myKey"] != "myValue");//I know resharper wants you to refactor this, please don't
            var expressionVisitor = new ExpressionVisitor();
            var repositoryQuery = new RepositoryQuery();
            var queryContainer = new QueryContainer(repositoryQuery);
            expressionVisitor.Visit(null, expression, queryContainer);

            var query = queryContainer.Query;
            Assert.That(query, Is.Not.Null);
            Assert.That(query.Operand, Is.EqualTo(MongoOperand.NotEquals));
            Assert.That(query.Inverted, Is.True);
            Assert.That(query.PropertyName, Is.EqualTo("SimpleDictionaryData.myKey"));
            Assert.That(query.Value, Is.EqualTo(BsonValue.Create("myValue")));
            Assert.That(query.MongoQuery.ToString(), Is.EqualTo(Query.EQ("SimpleDictionaryData.myKey", "myValue").ToString()));
        }
        public void ShouldSupportParseMethodOnConstantExpression()
        {
            Expression<Func<TestClass, bool>> expression = x => x.IntData == int.Parse("5");
            var expressionVisitor = new ExpressionVisitor();
            var repositoryQuery = new RepositoryQuery();
            var queryContainer = new QueryContainer(repositoryQuery);

            expressionVisitor.Visit(null, expression, queryContainer);

            var query = queryContainer.Query;
            Assert.That(query, Is.Not.Null);
            Assert.That(query.Inverted, Is.False);
            Assert.That(query.Operand, Is.EqualTo(MongoOperand.Equals));
            Assert.That(query.Value, Is.EqualTo(BsonValue.Create(5)));
            Assert.That(query.MongoQuery.ToString(), Is.EqualTo(Query.EQ("IntData", 5).ToString()));
        }
        public void ShouldCreateEqualsQueryOnReallyComplexDictionaryData()
        {
            Expression<Func<DictionaryTestClass, bool>> expression = x => x.ComplexDictionaryData["myKey"].DeeplyEmbeddedData.EvenMoreData == "myValue";
            var expressionVisitor = new ExpressionVisitor();
            var repositoryQuery = new RepositoryQuery();
            var queryContainer = new QueryContainer(repositoryQuery);
            expressionVisitor.Visit(null, expression, queryContainer);

            var query = queryContainer.Query;
            Assert.That(query, Is.Not.Null);
            Assert.That(query.Operand, Is.EqualTo(MongoOperand.Equals));
            Assert.That(query.Inverted, Is.False);
            Assert.That(query.PropertyName, Is.EqualTo("ComplexDictionaryData.myKey.DeeplyEmbeddedData.EvenMoreData"));
            Assert.That(query.Value, Is.EqualTo(BsonValue.Create("myValue")));
            Assert.That(query.MongoQuery.ToString(), Is.EqualTo(Query.EQ("ComplexDictionaryData.myKey.DeeplyEmbeddedData.EvenMoreData", "myValue").ToString()));
        }
 public void ShouldSupportQueriesWithAndOperatorAndMemberExpression()
 {
     var expressionVisitor = new ExpressionVisitor();
     // ReSharper disable ConvertToConstant.Local
     var idValue = "5"; //these should not be changed to const! That's a different code path
     var someDataValue = "6"; //these should not be changed to const! That's a different code path
     // ReSharper restore ConvertToConstant.Local
     Expression<Func<TestClass, bool>> expression = x => x.Id == idValue && x.SomeData == someDataValue;
     var repositoryQuery = new RepositoryQuery();
     var queryContainer = new QueryContainer(repositoryQuery);
     expressionVisitor.Visit(null, expression, queryContainer);
     var query = queryContainer.Query;
     var andQuery = Query.And(Query.EQ("_id", idValue), Query.EQ("SomeData", someDataValue));
     Assert.That(query.MongoQuery.ToString(), Is.EqualTo(andQuery.ToString()));
 }
        public void ShouldCreateInvertedNotEqualsQuery()
        {
            var expressionVisitor = new ExpressionVisitor();
            Expression<Func<TestClass, bool>> expression = x => !(x.Id != "5");//I know resharper wants you to refactor this, please don't
            var repositoryQuery = new RepositoryQuery();
            var queryContainer = new QueryContainer(repositoryQuery);

            expressionVisitor.Visit(null, expression, queryContainer);

            var query = queryContainer.Query;
            Assert.That(query.Value, Is.EqualTo(BsonValue.Create("5")));
            Assert.That(query.Operand, Is.EqualTo(MongoOperand.NotEquals));
            Assert.That(query.PropertyName, Is.EqualTo("_id"));
            Assert.That(query.Inverted, Is.True);
            Assert.That(query.MongoQuery.ToString(),Is.EqualTo(Query.EQ("_id", "5").ToString()));
        }
 public void ShouldSupportQueriesWithMultipleOrs()
 {
     var expressionVisitor = new ExpressionVisitor();
     Expression<Func<TestClass, bool>> expression = x => x.Id == "1" || x.Id == "2" || x.Id == "3";
     var repositoryQuery = new RepositoryQuery();
     var queryContainer = new QueryContainer(repositoryQuery);
     expressionVisitor.Visit(null, expression, queryContainer);
     var query = queryContainer.Query;
     var orQuery = Query.Or(Query.EQ("_id", "1"), Query.EQ("_id", "2"), Query.EQ("_id", "3"));
     Assert.That(query.MongoQuery.ToString(), Is.EqualTo(orQuery.ToString()));
 }
 public void ShouldCreateLessThanQuery()
 {
     var expressionVisitor = new ExpressionVisitor();
     Expression<Func<TestClass, bool>> expression = x => x.IntData < 5;
     var repositoryQuery = new RepositoryQuery();
     var queryContainer = new QueryContainer(repositoryQuery);
     expressionVisitor.Visit(null, expression, queryContainer);
     var query = queryContainer.Query;
     Assert.That(query.Value, Is.EqualTo(BsonValue.Create(5)));
     Assert.That(query.Operand, Is.EqualTo(MongoOperand.LessThan));
     Assert.That(query.PropertyName, Is.EqualTo("IntData"));
     Assert.That(query.Inverted, Is.False);
     Assert.That(query.MongoQuery.ToString(), Is.EqualTo(Query.LT("IntData", 5).ToString()));
 }
 public void ShouldSupportStringIsNullOrEmpty()
 {
     Expression<Func<TestClass, bool>> expression = x => string.IsNullOrEmpty(x.SomeData);
     var expressionVisitor = new ExpressionVisitor();
     var repositoryQuery = new RepositoryQuery();
     var queryContainer = new QueryContainer(repositoryQuery);
     expressionVisitor.Visit(null, expression, queryContainer);
     var query = queryContainer.Query;
     Assert.That(query, Is.Not.Null);
     Assert.That(query.Inverted, Is.False);
     Assert.That(query is OrRepositoryQuery);
     var queryComplete = Query.Or(Query.EQ("SomeData", ""), Query.EQ("SomeData", BsonNull.Value));
     Assert.That(query.MongoQuery.ToString(), Is.EqualTo(queryComplete.ToString()));
 }
        public void ShouldCreateNotEqualsQueryOnInvertedBooleanValue()
        {
            Expression<Func<TestClass, bool>> expression = x => !x.Enabled;
            var expressionVisitor = new ExpressionVisitor();
            var repositoryQuery = new RepositoryQuery();
            var queryContainer = new QueryContainer(repositoryQuery);
            expressionVisitor.Visit(null, expression, queryContainer);

            var query = queryContainer.Query;
            Assert.That(query, Is.Not.Null);
            Assert.That(query.Operand, Is.EqualTo(MongoOperand.Equals));
            Assert.That(query.Inverted, Is.True);
            Assert.That(query.PropertyName, Is.EqualTo("Enabled"));
            Assert.That(query.Value, Is.EqualTo(BsonValue.Create(true)));
            Assert.That(query.MongoQuery.ToString(), Is.EqualTo(Query.NE("Enabled", true).ToString()));
        }
 public void ShouldThrowNotSupportedExceptionOnParseMethodOnCollectionValue()
 {
     Expression<Func<TestClass, bool>> expression = x => long.Parse(x.Id) == 5;
     var expressionVisitor = new ExpressionVisitor();
     var repositoryQuery = new RepositoryQuery();
     var queryContainer = new QueryContainer(repositoryQuery);
     expressionVisitor.Visit(null, expression, queryContainer);
 }
        public void ShouldCreateNotEqualsQueryOnSimpleDictionaryData()
        {
            Expression<Func<DictionaryTestClass, bool>> expression = x => x.SimpleDictionaryData["myKey"] != "myValue";
            var expressionVisitor = new ExpressionVisitor();
            var repositoryQuery = new RepositoryQuery();
            var queryContainer = new QueryContainer(repositoryQuery);
            expressionVisitor.Visit(null, expression, queryContainer);

            var query = queryContainer.Query;
            Assert.That(query, Is.Not.Null);
            Assert.That(query.Operand, Is.EqualTo(MongoOperand.NotEquals));
            Assert.That(query.Inverted, Is.False);
            Assert.That(query.PropertyName, Is.EqualTo("SimpleDictionaryData.myKey"));
            Assert.That(query.Value, Is.EqualTo(BsonValue.Create("myValue")));
            Assert.That(query.MongoQuery.ToString(), Is.EqualTo(Query.NE("SimpleDictionaryData.myKey", "myValue").ToString()));
        }
 public void ShouldThrowNotSupportedOnUnknownOperand()
 {
     Expression<Func<TestClass, bool>> expression = x => x.SomeData + x.Id == "something";
     var expressionVisitor = new ExpressionVisitor();
     var repositoryQuery = new RepositoryQuery();
     var queryContainer = new QueryContainer(repositoryQuery);
     expressionVisitor.Visit(null, expression, queryContainer);
 }
 public void ShouldCreateNotInQueryOnInvertedParameterContainsMethodUsingList()
 {
     var dataStrings = new[] {"1", "2"}.ToList();
     Expression<Func<TestClass, bool>> expression = x => !dataStrings.Contains(x.Id);
     var expressionVisitor = new ExpressionVisitor();
     var repositoryQuery = new RepositoryQuery();
     var queryContainer = new QueryContainer(repositoryQuery);
     expressionVisitor.Visit(null, expression, queryContainer);
     var query = queryContainer.Query;
     Assert.That(query, Is.Not.Null);
     var inQuery = Query.NotIn("_id", BsonArray.Create(dataStrings));
     Assert.That(query.MongoQuery.ToString(), Is.EqualTo(inQuery.ToString()));
 }
 public void ShouldCreateEqualsQuery()
 {
     var expressionVisitor = new ExpressionVisitor();
     Expression<Func<TestClass, bool>> expression = x => x.Id == "5";
     var repositoryQuery = new RepositoryQuery();
     var queryContainer = new QueryContainer(repositoryQuery);
     expressionVisitor.Visit(null, expression, queryContainer);
     var query = queryContainer.Query;
     Assert.That(query.Value, Is.EqualTo(BsonValue.Create("5")));
     Assert.That(query.Operand, Is.EqualTo(MongoOperand.Equals));
     Assert.That(query.PropertyName, Is.EqualTo("_id"));
     Assert.That(query.Inverted, Is.False);
     Assert.That(query.MongoQuery.ToString(), Is.EqualTo(Query.EQ("_id", "5").ToString()));
 }
 private static void VisitConstant(ConstantExpression expression, QueryContainer container)
 {
     var value = GetValueFromExpression(expression);
     container.Query.Value = BsonValue.Create(value);
 }