Exemplo n.º 1
0
        public void FacetShouldWorkWithNullableExpressions()
        {
            var expected1 = RangeFacet <Person2> .Parse(x => x.Age1 < 15);

            var expected2 = RangeFacet <Person2> .Parse(x => x.Age1 >= 15 && x.Age1 < 25);

            var actual1 = RangeFacet <Person> .Parse(x => x.Age1 < 15);

            var actual2 = RangeFacet <Person> .Parse(x => x.Age1 >= 15 && x.Age1 < 25);

            Assert.Equal(expected1, actual1);
            Assert.Equal(expected2, actual2);

            expected1 = RangeFacet <Person2> .Parse(x => x.Age2 < 15);

            expected2 = RangeFacet <Person2> .Parse(x => x.Age2 >= 15 && x.Age2 < 25);

            actual1 = RangeFacet <Person> .Parse(x => x.Age2 < 15);

            actual2 = RangeFacet <Person> .Parse(x => x.Age2 >= 15 && x.Age2 < 25);

            Assert.Equal(expected1, actual1);
            Assert.Equal(expected2, actual2);

            expected1 = RangeFacet <Person2> .Parse(x => x.Age3 < 15);

            expected2 = RangeFacet <Person2> .Parse(x => x.Age3 >= 15 && x.Age3 < 25);

            actual1 = RangeFacet <Person> .Parse(x => x.Age3 < 15);

            actual2 = RangeFacet <Person> .Parse(x => x.Age3 >= 15 && x.Age3 < 25);

            Assert.Equal(expected1, actual1);
            Assert.Equal(expected2, actual2);
        }
Exemplo n.º 2
0
        public void IdeallyTheVariableWouldNotNeedToBeANullable()
        {
            DateTimeOffset now    = new DateTimeOffset(2017, 1, 2, 0, 0, 0, TimeSpan.Zero);
            DateTimeOffset min    = DateTimeOffset.MinValue;
            var            actual = RangeFacet <ClassWithDateTimeOffset> .Parse(c => c.NullableDateTimeOffset > min && c.NullableDateTimeOffset < now);

            Assert.Equal("NullableDateTimeOffset > '0001-01-01T00:00:00.0000000Z' and NullableDateTimeOffset < '2017-01-02T00:00:00.0000000Z'", actual);
        }
Exemplo n.º 3
0
        public void FacetShouldWorkWithNullableDateTimeOffset()
        {
            DateTimeOffset?now    = new DateTimeOffset(2017, 1, 2, 0, 0, 0, TimeSpan.Zero);
            DateTimeOffset?min    = DateTimeOffset.MinValue;
            var            actual = RangeFacet <ClassWithDateTimeOffset> .Parse(c => c.NullableDateTimeOffset > min && c.NullableDateTimeOffset < now);

            Assert.Equal("NullableDateTimeOffset > '0001-01-01T00:00:00.0000000Z' and NullableDateTimeOffset < '2017-01-02T00:00:00.0000000Z'", actual);
        }
Exemplo n.º 4
0
        public void FacetShouldWorkWithDateOffset()
        {
            var now    = new DateTime(2017, 1, 2);
            var min    = DateTime.MinValue;
            var actual = RangeFacet <ClassWithDateTimeOffset> .Parse(c => c.Date > min && c.Date < now);

            Assert.Equal("Date > '0001-01-01T00:00:00.0000000' and Date < '2017-01-02T00:00:00.0000000'", actual);
        }
Exemplo n.º 5
0
        public static FacetToken Create <T>(RangeFacet <T> facet, Func <object, string> addQueryParameter)
        {
            var optionsParameterName = GetOptionsParameterName(facet, addQueryParameter);

            var ranges = new List <string>();

            foreach (var expression in facet.Ranges)
            {
                ranges.Add(RangeFacet <T> .Parse(null, expression, addQueryParameter));
            }

            var token = new FacetToken(null, QueryFieldUtil.EscapeIfNecessary(facet.DisplayFieldName), ranges, optionsParameterName);

            ApplyAggregations(facet, token);

            return(token);
        }
Exemplo n.º 6
0
        public void CanReturnDescriptiveParsingErrors()
        {
            var invalidOperatorEx = Assert.Throws <InvalidOperationException>(() => RangeFacet <Employee> .Parse(x => x.Salary == 5));

            Assert.Equal("Cannot use Equal as facet range. Allowed operators: <, <=, >, >=.", invalidOperatorEx.Message);

            var invalidChaningEx = Assert.Throws <InvalidOperationException>(() => RangeFacet <Employee> .Parse(x => x.Salary < 5 || x.Salary > 8));

            Assert.Equal("Range can be only specified using: '&&'. Cannot use: 'OrElse'", invalidChaningEx.Message);

            var rangeFieldsEx = Assert.Throws <InvalidOperationException>(() => RangeFacet <Employee> .Parse(x => x.Salary < 5 && x.Salary > 8 && x.Salary > 60));

            Assert.Equal("Expressions on both sides of '&&' must point to range field. E.g. x => x.Age > 18 && x.Age < 99", rangeFieldsEx.Message);

            var differentFieldsEx = Assert.Throws <InvalidOperationException>(() => RangeFacet <Employee> .Parse(x => x.Salary < 5 && x.Age > 15));

            Assert.Equal("Different range fields were detected: 'Salary' and 'Age'", differentFieldsEx.Message);

            var invalidOperatorsInChainEx = Assert.Throws <InvalidOperationException>(() => RangeFacet <Employee> .Parse(x => x.Salary == 5 && x.Salary == 9));

            Assert.Equal("Members in sub-expression(s) are not the correct types (expected '<', '<=', '>' or '>=')", invalidOperatorsInChainEx.Message);

            var invalidRange1Ex = Assert.Throws <InvalidOperationException>(() => RangeFacet <Employee> .Parse(x => x.Salary < 5 && x.Salary >= 15));

            Assert.Equal("Invalid range: 15..5", invalidRange1Ex.Message);

            var parsedRange1 = RangeFacet <Employee> .Parse(x => x.Salary >= 5 && x.Salary <= 15);

            Assert.Equal("Salary BETWEEN 5 AND 15", parsedRange1);

            var invalidRange2Ex = Assert.Throws <InvalidOperationException>(() => RangeFacet <Employee> .Parse(x => x.Salary >= 15 && x.Salary <= 5));

            Assert.Equal("Invalid range: 15..5", invalidRange2Ex.Message);

            var parsedRange2 = RangeFacet <Employee> .Parse(x => x.Salary <= 15 && x.Salary >= 5);

            Assert.Equal("Salary BETWEEN 5 AND 15", parsedRange2);

            var parsedRange3 = RangeFacet <Employee> .Parse(x => x.Salary >= 5.1m && x.Salary <= 5.1m);

            Assert.Equal("Salary BETWEEN 5.1 AND 5.1", parsedRange3);
        }
Exemplo n.º 7
0
        public void IdeallyIWouldNotNeedAVariable()
        {
            var actual = RangeFacet <ClassWithDateTimeOffset> .Parse(c => c.DateTimeOffset > DateTimeOffset.MinValue && c.DateTimeOffset < new DateTimeOffset(2017, 1, 2, 0, 0, 0, TimeSpan.Zero));

            Assert.Equal("DateTimeOffset > '0001-01-01T00:00:00.0000000Z' and DateTimeOffset < '2017-01-02T00:00:00.0000000Z'", actual);
        }