示例#1
0
        public void SieveProvider__FromAssembly__WithIncorrectProperty__ShouldThrowException()
        {
            var attributeProvider = new SieveConfiguratorProvider(typeof(SieveConfiguratorProviderTests).Assembly);
            var sieveProvider     = new SieveProvider();

            attributeProvider.Configure(sieveProvider);

            var query = new[]
            {
                new AttributedTestModel {
                    Boolean = true
                },
                new AttributedTestModel {
                    Boolean = false
                },
                new AttributedTestModel {
                    Boolean = false
                },
            }.AsQueryable();

            Assert.Throws <ArgumentException>(() =>
            {
                sieveProvider.Apply(query, new[]
                {
                    new FilterPredicate <AttributedTestModel>(new SimpleFilterPipeline <AttributedTestModel, bool>(
                                                                  model => model.Boolean,
                                                                  SimpleFilterOperation.Equal, true))
                });
            });
        }
        public void Sort__WithDisallowedProperty__ShouldThrowException()
        {
            var provider = new SieveProvider()
                           .AddModel <SimpleTestModel>(_ => { });
            var query = new[]
            {
                new SimpleTestModel {
                    Number = 2
                },
                new SimpleTestModel {
                    Number = 3
                },
                new SimpleTestModel {
                    Number = 1
                }
            }.AsQueryable();
            var sortPredicate = new SortPredicate <SimpleTestModel>(
                new SortPipeline <SimpleTestModel, int>(model => model.Number, SortDirection.Descending)
                );

            Assert.Throws <ArgumentException>(() =>
            {
                provider.Apply(query, new[] { sortPredicate });
            });
        }
示例#3
0
        public void SieveProvider__FromAssembly__ShouldConstructCorrectly()
        {
            var attributeProvider = new SieveConfiguratorProvider(typeof(SieveConfiguratorProviderTests).Assembly);
            var sieveProvider     = new SieveProvider();

            attributeProvider.Configure(sieveProvider);

            var query = new[]
            {
                new AttributedTestModel {
                    Number = 1, String = "a"
                },
                new AttributedTestModel {
                    Number = 2, String = "b"
                },
                new AttributedTestModel {
                    Number = 3, String = "c"
                },
            }.AsQueryable();
            var result = sieveProvider.Apply(query, new[]
            {
                new FilterPredicate <AttributedTestModel>(
                    new SimpleFilterPipeline <AttributedTestModel, string>(model => model.String,
                                                                           SimpleFilterOperation.Equal, "a"))
            }).ToList();

            Assert.Collection(result, r => Assert.Equal("a", r.String));
        }
        public void Sort__WithAllowedProperty__ShouldBeOk()
        {
            var provider = new SieveProvider()
                           .AddModel <SimpleTestModel>(builder =>
            {
                builder.CanSort(model => model.Number);
            });
            var query = new[]
            {
                new SimpleTestModel {
                    Number = 2
                },
                new SimpleTestModel {
                    Number = 3
                },
                new SimpleTestModel {
                    Number = 1
                }
            }.AsQueryable();
            var sortPredicate = new SortPredicate <SimpleTestModel>(
                new SortPipeline <SimpleTestModel, int>(model => model.Number, SortDirection.Descending)
                );

            var result = provider.Apply(query, new[] { sortPredicate }).ToList();

            Assert.Collection(result,
                              r => Assert.Equal(3, r.Number),
                              r => Assert.Equal(2, r.Number),
                              r => Assert.Equal(1, r.Number));
        }
        public void Sort__WithMultipleProperties__ShouldBeOk()
        {
            var provider = new SieveProvider()
                           .AddModel <SimpleTestModel>(builder =>
            {
                builder.CanSort(x => x.Boolean);
                builder.CanSort(x => x.Number);
            });
            var query = new[]
            {
                new SimpleTestModel {
                    Number = 1, Boolean = true
                },
                new SimpleTestModel {
                    Number = 3, Boolean = false
                },
                new SimpleTestModel {
                    Number = 1, Boolean = false
                },
                new SimpleTestModel {
                    Number = 2, Boolean = true
                },
            }.AsQueryable();
            var sortPredicate = new SortPredicate <SimpleTestModel>(
                new SortPipeline <SimpleTestModel, int>(model => model.Number, SortDirection.Ascending),
                new SortPipeline <SimpleTestModel, bool>(model => model.Boolean, SortDirection.Descending));

            var result = provider.Apply(query, new[] { sortPredicate }).ToArray();

            Assert.Collection(result,
                              r =>
            {
                Assert.Equal(1, r.Number);
                Assert.True(r.Boolean);
            },
                              r =>
            {
                Assert.Equal(1, r.Number);
                Assert.False(r.Boolean);
            },
                              r =>
            {
                Assert.Equal(2, r.Number);
                Assert.True(r.Boolean);
            },
                              r =>
            {
                Assert.Equal(3, r.Number);
                Assert.False(r.Boolean);
            });
        }
示例#6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="services"></param>
        /// <param name="invoker">What assemblies contain Entity/View models marked with <see cref="CanFilterAttribute"/> or
        /// <see cref="CanSortAttribute"/></param>
        /// <returns></returns>
        public static IServiceCollection AddSieveProvider(this IServiceCollection services, Action <SieveConfiguration> invoker = null)
        {
            var config = new SieveConfiguration();

            invoker?.Invoke(config);

            var configurator = config.Assemblies.Length > 0
                ? new SieveConfiguratorProvider(config.Assemblies)
                : new SieveConfiguratorProvider();

            var provider = new SieveProvider();

            configurator.Configure(provider);
            config.Configurator?.Invoke(provider);

            services.AddSingleton <ISieveProvider>(provider);
            services.AddSingleton <IParser>(config.Parser);

            return(services);
        }
        public void ComplexFilter__WithAllowedProperty__ShouldBeOk()
        {
            var provider = new SieveProvider()
                           .AddModel <SimpleTestModel>(builder =>
            {
                builder.CanFilter(model => model.Boolean);
                builder.CanFilter(model => model.Number);
            });
            var query = new[]
            {
                new SimpleTestModel {
                    Boolean = false, Number = 1
                },
                new SimpleTestModel {
                    Boolean = true, Number = 2
                },
                new SimpleTestModel {
                    Boolean = true, Number = 3
                }
            }.AsQueryable();
            var filterPredicate = new FilterPredicate <SimpleTestModel>(new ComplexFilterPipeline <SimpleTestModel>(
                                                                            ComplexFilterOperation.Or, new IFilterPipeline <SimpleTestModel>[]
            {
                new ComplexFilterPipeline <SimpleTestModel>(ComplexFilterOperation.And,
                                                            new IFilterPipeline <SimpleTestModel>[]
                {
                    new SimpleFilterPipeline <SimpleTestModel, bool>(model => model.Boolean,
                                                                     SimpleFilterOperation.Equal, true),
                    new SimpleFilterPipeline <SimpleTestModel, int>(model => model.Number,
                                                                    SimpleFilterOperation.Equal, 3),
                }),
                new SimpleFilterPipeline <SimpleTestModel, bool>(model => model.Boolean, SimpleFilterOperation.Equal,
                                                                 false)
            }));

            var result = provider.Apply(query, new[] { filterPredicate }).ToList();

            Assert.Equal(2, result.Count);
            Assert.Single(result, model => model.Boolean == true && model.Number == 3);
            Assert.Single(result, model => model.Boolean == false && model.Number == 1);
        }
        public void SimpleFilter__WithDisallowedProperty__ShouldThrowException()
        {
            var provider = new SieveProvider()
                           .AddModel <SimpleTestModel>(_ => { });
            var query = new[]
            {
                new SimpleTestModel {
                    Boolean = false
                },
                new SimpleTestModel {
                    Boolean = true
                }
            }.AsQueryable();
            var filterPredicate = new FilterPredicate <SimpleTestModel>(
                new SimpleFilterPipeline <SimpleTestModel, bool>(model => model.Boolean, SimpleFilterOperation.Equal, true)
                );

            Assert.Throws <ArgumentException>(() =>
            {
                provider.Apply(query, new[] { filterPredicate });
            });
        }
        public void Sieve__WithUnregisteredModel__ShouldThrowException()
        {
            var provider = new SieveProvider();
            var query    = new[]
            {
                new SimpleTestModel {
                    Number = 2, Boolean = true
                },
                new SimpleTestModel {
                    Number = 3, Boolean = false
                },
                new SimpleTestModel {
                    Number = 1, Boolean = true
                }
            }.AsQueryable();
            var sieve = new Sieve <SimpleTestModel>
            {
                Filter = new SimpleFilterPipeline <SimpleTestModel, bool>(model => model.Boolean, SimpleFilterOperation.Equal, true)
            };

            Assert.Throws <ArgumentException>(() => provider.Apply(query, sieve));
        }
        public void EmptyComplexFilter__ShouldBeOk()
        {
            var provider = new SieveProvider()
                           .AddModel <SimpleTestModel>(builder =>
            {
                builder.CanFilter(model => model.Boolean);
            });
            var query = new[]
            {
                new SimpleTestModel {
                    Boolean = false
                },
                new SimpleTestModel {
                    Boolean = true
                }
            }.AsQueryable();
            var filterPredicate = new FilterPredicate <SimpleTestModel>(
                new ComplexFilterPipeline <SimpleTestModel>(ComplexFilterOperation.And, new IFilterPipeline <SimpleTestModel> [0])
                );

            var result = provider.Apply(query, new[] { filterPredicate }).ToList();

            Assert.Equal(2, result.Count);
        }
        public void Sieve__WithAllowedProperties__ShouldBeOk()
        {
            var provider = new SieveProvider()
                           .AddModel <SimpleTestModel>(builder =>
            {
                builder.CanFilter(model => model.Boolean);
                builder.CanSort(model => model.Number);
            });
            var query = new[]
            {
                new SimpleTestModel {
                    Number = 2, Boolean = true
                },
                new SimpleTestModel {
                    Number = 3, Boolean = false
                },
                new SimpleTestModel {
                    Number = 1, Boolean = true
                }
            }.AsQueryable();
            var sieve = new Sieve <SimpleTestModel>
            {
                Filter = new SimpleFilterPipeline <SimpleTestModel, bool>(model => model.Boolean, SimpleFilterOperation.Equal, true)
                ,
                Sort = new List <ISortPipeline <SimpleTestModel> >
                {
                    new SortPipeline <SimpleTestModel, int>(model => model.Number, SortDirection.Ascending)
                }
            };

            var result = provider.Apply(query, sieve).ToList();

            Assert.Collection(result,
                              r => Assert.Equal(1, r.Number),
                              r => Assert.Equal(2, r.Number));
        }
        public void SimpleFilter__WithAllowedProperty__ShouldBeOk()
        {
            var provider = new SieveProvider()
                           .AddModel <SimpleTestModel>(builder =>
            {
                builder.CanFilter(model => model.Boolean);
            });
            var query = new[]
            {
                new SimpleTestModel {
                    Boolean = false
                },
                new SimpleTestModel {
                    Boolean = true
                }
            }.AsQueryable();
            var filterPredicate = new FilterPredicate <SimpleTestModel>(
                new SimpleFilterPipeline <SimpleTestModel, bool>(model => model.Boolean, SimpleFilterOperation.Equal, true)
                );

            var result = provider.Apply(query, new[] { filterPredicate }).ToList();

            Assert.All(result, r => Assert.True(r.Boolean));
        }