public void FilterStringsAreParsed()
        {
            //Arrange
            var output  = new Output();
            var strings = new[]
            {
                "var eq 1",
                "var eq 1 and var2 eq 'thing'",
                "(var eq 1)",
                "(var eq 1 or var2 eq 'thing') and var3 gt 56",
                "var eq guid'9E37E338-27B2-4F32-AFFA-DC74F017AF1D' or (var2 ge 45L and var3 ne 35.4)",
                "eq eq eq",
                "and",
                "and and and",
                "(a eq 1",
                "a eq 1)",
                "PartitionKey eq '2017-07-30T21:15:09' and (Alpha eq 'Delta' and (Delta eq 'Alpha' or Delta eq 'Beta'))"
            };

            //Act
            foreach (var filter in strings)
            {
                var result = FilterStringParser.Parse(filter);

                output.WrapLine($@"Filter string: {filter}");
                output.WrapLine(result.ToString());
                output.WriteLine();
                output.WriteLine();
            }

            //Assert
            Approvals.Verify(output.Report);
        }
Пример #2
0
        private IEnumerable <IDictionary <string, EntityProperty> > InternalQuery(string filterString, IList <string> selectColumns)
        {
            Debug.WriteLine($"Q: {filterString}");

            var parseResult = FilterStringParser.Parse(filterString);

            if (!parseResult.Success)
            {
                throw new InvalidFilterString(parseResult.Error);
            }

            foreach (var row in _tableStore.GetAllRows(_table))
            {
                if (!parseResult.Root.Execute(row, out string error))
                {
                    if (error != null)
                    {
                        throw new QueryFailed(error);
                    }
                }
                else
                {
                    if (selectColumns != null)
                    {
                        yield return(row.Where(p => selectColumns.Contains(p.Key)).ToDictionary(p => p.Key, p => p.Value));
                    }
                    else
                    {
                        yield return(row);
                    }
                }
            }
        }
        public void FilterStringsAreParsed()
        {
            //Arrange
            var buffer = new OutputBuffer();

            buffer.BufferWidth = 120;
            var output = new Output(buffer);

            var strings = new[]
            {
                "var eq 1",
                "var eq 1 and var2 eq 'thing'",
                "(var eq 1)",
                "(var eq 1 or var2 eq 'thing') and var3 gt 56",
                "varg eq guid'9E37E338-27B2-4F32-AFFA-DC74F017AF1D' or (varL ge 45L and var3 ne 35.4)",
                "eq eq eq",
                "(PartitionKey eq '2017-07-30T21:15:09') and((Alpha eq 'Delta') and((Delta eq 'Alpha') or(Delta eq 'Beta')))",
                "(PartitionKey eq '2017-07-30T21:15:09') and((Alpha eq 'Delta') and((Delta eq 'Alpha') or(Delta eq 'Gamma')))",
                "PartitionKey eq '2017-07-30T21:15:09' and Alpha eq 'Delta' and (Delta eq 'Alpha' or Delta eq 'Beta')",
                "PartitionKey eq '2017-07-30T21:15:09' and Alpha eq 'Delta' and (Delta eq 'Alpha' or Delta eq 'Gamma')",
                "PartitionKey eq '2017-07-30T21:15:09' and (Alpha eq 'Delta' and Delta eq 'Alpha' or Delta eq 'Beta')",
                "PartitionKey eq '2017-07-30T21:15:09' and (Alpha eq 'Delta' and Delta eq 'Alpha' or Delta eq 'Gamma')",
            };

            var data = new Dictionary <string, EntityProperty>
            {
                { "var", new EntityProperty(1) },
                { "var2", new EntityProperty("thing") },
                { "var3", new EntityProperty(99.5) },
                { "eq", new EntityProperty("equals") },
                { "varg", new EntityProperty(Guid.Parse("9E37E338-27B2-4F32-AFFA-DC74F017AF1D")) },
                { "varL", new EntityProperty(long.MaxValue) },
                { "PartitionKey", new EntityProperty("2017-07-30T21:15:09") },
                { "Alpha", new EntityProperty("Delta") },
                { "Delta", new EntityProperty("Beta") },
            };

            output.FormatObject(data.Select(d => new { VariableName = d.Key, Value = d.Value.PropertyAsObject.ToString() }));

            //Act
            foreach (var filter in strings)
            {
                var parseResult = FilterStringParser.Parse(filter);

                output.WrapLine($@"Filter string: {filter}");
                output.WrapLine(parseResult.ToString());
                output.WriteLine();
                var result = parseResult.Root.Execute(data, out string error);
                output.WrapLine($"Result = {error ?? result.ToString()}");
                output.WriteLine();
                output.WriteLine();
            }

            //Assert
            Approvals.Verify(output.Report);
        }
Пример #4
0
        public void FilterStringParser_returns_every_distinct_value_as_a_separate_filter()
        {
            var sut = new FilterStringParser();

            var result = sut.Parse("tomato, cucumber,oregano");

            Assert.AreEqual(result.Include.Count, 3);
            Assert.AreEqual(result.Exclude.Count, 0);
            Assert.Contains("tomato", result.Include);
            Assert.Contains("cucumber", result.Include);
            Assert.Contains("oregano", result.Include);
        }
Пример #5
0
        public void FilterStringParser_understands_minus_as_add_to_exclude_list()
        {
            var sut = new FilterStringParser();

            var result = sut.Parse("tomato, cucumber,-oregano");

            Assert.AreEqual(2, result.Include.Count);
            Assert.AreEqual(1, result.Exclude.Count);
            Assert.Contains("tomato", result.Include);
            Assert.Contains("cucumber", result.Include);
            Assert.Contains("oregano", result.Exclude);
        }
        public virtual async Task <Items <T> > GetAll([FromQuery] LoadParams loadParams, [FromServices] IMediator mediator, [FromServices] IMapper mapper)
        {
            var filters      = FilterStringParser.Parse(loadParams.Filter);
            var result       = new Items <T>();
            var countRequest = new GetCountQuery <T>
            {
                Filters = filters
            };

            result.Count = await mediator.Send(countRequest);

            if (result.Count > 0)
            {
                var dataRequest = new GetItemsQuery <T>
                {
                    Paging  = mapper.Map <LoadParams, PagingData>(loadParams),
                    Sort    = CreateSort(loadParams),
                    Filters = filters
                };
                result.Data = await mediator.Send(dataRequest);
            }
            return(result);
        }