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()));
 }
 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()));
 }
 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 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 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 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 ShouldThrowNotSupportedOnConstants()
 {
     Expression<Func<TestClass, string>> expression = x => "some nonsense";
     var expressionVisitor = new ExpressionVisitor();
     expressionVisitor.GetMemberNameFromExpression(expression);
 }
 public void ShouldGetMemberNameFromDeeplyNestedLambdaExpression()
 {
     Expression<Func<TestClass, string>> expression = x => x.EmbeddedData.DeeplyEmbeddedData.EvenMoreData;
     var expressionVisitor = new ExpressionVisitor();
     var memberName = expressionVisitor.GetMemberNameFromExpression(expression);
     Assert.That(memberName, Is.EqualTo("EmbeddedData.DeeplyEmbeddedData.EvenMoreData"));
 }
 public void ShouldModifyIdNameFromLambdaExpression()
 {
     Expression<Func<TestClass, string>> expression = x => x.Id;
     var expressionVisitor = new ExpressionVisitor();
     var memberName = expressionVisitor.GetMemberNameFromExpression(expression);
     Assert.That(memberName, Is.EqualTo("_id"));
 }
        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 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 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 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 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 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 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 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 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 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 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()));
 }
 public void ShouldThrowNotSupportedOnNonMembers()
 {
     var myClass = new TestClass();
     Expression<Func<TestClass, string>> expression = x => myClass.SomeData;
     var expressionVisitor = new ExpressionVisitor();
     expressionVisitor.GetMemberNameFromExpression(expression);
 }
 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 ShouldThrowNotSupportedOnUnsupportedTypes()
 {
     var expressionVisitor = new ExpressionVisitor();
     expressionVisitor.Visit(null, Expression.ListInit(Expression.New(typeof(List<TestClass>)), Expression.Constant(new TestClass())), null);
 }
 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 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()));
 }
        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()));
        }