LINQ Where method translator.
Inheritance: IMethodTranslator
Example #1
0
        public void UseWhereOnDouble()
        {
            // Arrange
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => p.Area < 350000);
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("Area lt 350000.0", translation.TableQuery.FilterString);
        }
        public void UseCompareOrdinalInWhereOnRowKeyTest()
        {
            // Arrange
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => String.CompareOrdinal(p.Name, "F") >= 0 && String.CompareOrdinal(p.Name, "G") <= 0);
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("RowKey ge 'F' and RowKey le 'G'", translation.TableQuery.FilterString);
        }
Example #3
0
        public void UseWhereOnPartitionKey()
        {
            // Arrange
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => p.Continent == Europe);
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("PartitionKey eq 'Europe'", translation.TableQuery.FilterString);
        }
Example #4
0
        public void UseEnumValueInWhereOnRowKey()
        {
            // Arrange
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => p.Name == Countries.Germany.ToString());
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("RowKey eq 'Germany'", translation.TableQuery.FilterString);
        }
        public void UseContainsAndEqualityInWhereOnRowKeyTest()
        {
            // Arrange
            var list = new List<String>
                {
                    "Latvia",
                    "Germany"
                };
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => list.Contains(p.Name) && p.Continent == "Africa");
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("(RowKey eq 'Latvia' or RowKey eq 'Germany') and PartitionKey eq 'Africa'", translation.TableQuery.FilterString);
        }
        public void UseNotContainsInWhereOnRowKeyTest()
        {
            // Arrange
            var list = new List<String>
                {
                    "Latvia",
                    "Germany"
                };
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => !list.Contains(p.Name));
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("RowKey ne 'Latvia' or RowKey ne 'Germany'", translation.TableQuery.FilterString);
        }
Example #7
0
        public void WhereWithNullableDateTime()
        {
            // Arrange
            var translator = new WhereTranslator(_nameChanges);
            var queryable = new EnumerableQuery<EntityWithFields>(new List<EntityWithFields>());
            var value = new DateTime(1980, 1, 1);
            Expression<Func<IQueryable<EntityWithFields>>> query = () => queryable.Where(p => p.NullableDateTime < value);
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression)query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.Equal("NullableDateTime lt datetime'1980-01-01T00:00:00'", translation.TableQuery.FilterString);
        }
Example #8
0
        public void WhereWithInvalidMethod()
        {
            // Arrange
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Take(2);
            var translation = new TranslationResult();

            // Act
            Assert.Throws<ArgumentOutOfRangeException>(() => translator.Translate((MethodCallExpression) query.Body, translation));

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.Null(translation.TableQuery.FilterString);
        }
Example #9
0
        public void BinaryExpressionWithPrivateMethodCall()
        {
            // Arrange
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => p.Name == GetName());
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("RowKey eq 'new name'", translation.TableQuery.FilterString);
        }
Example #10
0
        public void BinaryExpressionWithComplexExpression()
        {
            // Arrange
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => Math.Abs(p.Area - Convert.ToDouble(555)) < 0.2);
            var translation = new TranslationResult();

            // Act
            Assert.Throws<NotSupportedException>(() => translator.Translate((MethodCallExpression) query.Body, translation));

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.Null(translation.TableQuery.FilterString);
        }
Example #11
0
        public void UseGuidContainsInWhereOnRowKeyTest()
        {
            // Arrange
            var list = new List<Guid>
                {
                    new Guid("48ed2917-f7d4-4383-aa29-4062f1296bbc"),
                    new Guid("8a024e77-4f06-49d9-9d46-9a0e59d74fcd")
                };
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => list.Contains(p.Id));
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("Id eq guid'48ed2917-f7d4-4383-aa29-4062f1296bbc' or Id eq guid'8a024e77-4f06-49d9-9d46-9a0e59d74fcd'", translation.TableQuery.FilterString);
        }
Example #12
0
        public void BinaryQueryWithNotEqualBooleanValue()
        {
            // Arrange
            const bool value = false;
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => p.IsExists != value);
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("IsExists ne false", translation.TableQuery.FilterString);
        }
Example #13
0
        public void UseWhereOnGuid()
        {
            // Arrange
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => p.Id == new Guid(Id));
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("Id eq guid'829ea8b2-3bd5-45a4-8b54-533c69e608d7'", translation.TableQuery.FilterString);
        }
Example #14
0
        public void UseWhereOnDateTime()
        {
            // Arrange
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => p.Formed < new DateTime(1800, 1, 1));
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("Formed lt datetime'1800-01-01T00:00:00'", translation.TableQuery.FilterString);
        }
Example #15
0
        public void UseByteContainsInWhereOnRowKeyTest()
        {
            // Arrange
            var list = new List<byte[]>
                {
                    new byte[] {0x11, 0x22, 0x33},
                    new byte[] {0x44, 0x55, 0x66}
                };
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => list.Contains(p.TopSecretKey));
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("TopSecretKey eq X'112233' or TopSecretKey eq X'445566'", translation.TableQuery.FilterString);
        }
Example #16
0
        public void UseDateTimeContainsInWhereOnRowKeyTest()
        {
            // Arrange
            var list = new List<DateTime>
                {
                    DateTime.FromBinary(555),
                    DateTime.FromBinary(666)
                };
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => list.Contains(p.Formed));
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("Formed eq datetime'0001-01-01T00:00:00.0000555' or Formed eq datetime'0001-01-01T00:00:00.0000666'", translation.TableQuery.FilterString);
        }
Example #17
0
        public void WhereWithNullableDouble()
        {
            // Arrange
            var translator = new WhereTranslator(_nameChanges);
            var queryable = new EnumerableQuery<EntityWithFields>(new List<EntityWithFields>());
            const double value = .3;
            Expression<Func<IQueryable<EntityWithFields>>> query = () => queryable.Where(p => p.NullableDouble > value);
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression)query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.Equal("NullableDouble gt .3", translation.TableQuery.FilterString);
        }
Example #18
0
        public void UseWhereOnBytes()
        {
            // Arrange
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => p.TopSecretKey == new byte[] {0xff, 0xee, 0xdd});
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("TopSecretKey eq X'ffeedd'", translation.TableQuery.FilterString);
        }
Example #19
0
        public void WhereWithEnumartionConstantValue()
        {
            // Arrange
            var translator = new WhereTranslator(_nameChanges);
            var queryable = new EnumerableQuery<EntityWithFields>(new List<EntityWithFields>());

            const Countries value = Countries.Germany;

            Expression<Func<IQueryable<EntityWithFields>>> query = () => queryable.Where(p => p.String == value.ToString());
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression)query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.Equal(string.Format("String eq '{0}'", value), translation.TableQuery.FilterString);
        }
Example #20
0
        public void UseInt64ContainsInWhereOnRowKeyTest()
        {
            // Arrange
            var list = new List<Int64>
                {
                    0,
                    1
                };
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => list.Contains(p.Population));
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("Population eq 0L or Population eq 1L", translation.TableQuery.FilterString);
        }
Example #21
0
        public void UnaryQueryWithInversedBooleanValue()
        {
            // Arrange
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => !p.IsExists);
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("not IsExists", translation.TableQuery.FilterString);
        }
Example #22
0
        public void UseDoubleContainsInWhereOnRowKeyTest()
        {
            // Arrange
            var list = new List<double>
                {
                    111.11,
                    222.22
                };
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => list.Contains(p.Area));
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("Area eq 111.11 or Area eq 222.22", translation.TableQuery.FilterString);
        }
Example #23
0
        public void BinaryExpressionWithExternalMethodCall()
        {
            // Arrange
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => p.Area == Convert.ToDouble(555));
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("Area eq 555.0", translation.TableQuery.FilterString);
        }
Example #24
0
        public void UseBooleanContainsInWhereOnRowKeyTest()
        {
            // Arrange
            var list = new List<bool>
                {
                    true,
                    false
                };
            var translator = new WhereTranslator(_nameChanges);
            Expression<Func<IQueryable<Country>>> query = () => _countries.Where(p => list.Contains(p.IsExists));
            var translation = new TranslationResult();

            // Act
            translator.Translate((MethodCallExpression) query.Body, translation);

            // Assert
            Assert.NotNull(translation.TableQuery);
            Assert.NotNull(translation.TableQuery.FilterString);
            Assert.Equal("IsExists eq true or IsExists eq false", translation.TableQuery.FilterString);
        }