Beispiel #1
0
        public void LessThanOrEqualFilter()
        {
            var builder = new WebFilterConverter <User>();

            var goodUser = new User {
                Id = 0
            };
            var goodUser2 = new User {
                Id = 1
            };
            var badUser = new User {
                Id = 3
            };

            var filter = new WebFilter <User>(PropertyExpression <User> .New(u => u.Id), WebFilterOperand.LessThanOrEqual, new List <int> {
                1, 2
            });
            var expression = builder.ToExpression(filter);
            var users = new List <User> {
                goodUser, goodUser2, badUser
            }.AsQueryable().Where(expression).ToList();

            Assert.Equal(2, users.Count);
            Assert.Equal(users[0], goodUser);
            Assert.Equal(users[1], goodUser2);
        }
Beispiel #2
0
        public void StartsFilter()
        {
            var builder  = new WebFilterConverter <User>();
            var goodUser = new User {
                Name = "AAAseed"
            };
            var goodUser2 = new User {
                Name = "bBbseed"
            };
            var badUser = new User {
                Name = "edgseed"
            };

            var filter = new WebFilter <User>(PropertyExpression <User> .New(u => u.Name), WebFilterOperand.Starts, new List <string> {
                "aAa", "bbb"
            });
            var expression = builder.ToExpression(filter);
            var users = new List <User> {
                goodUser, goodUser2, badUser
            }.AsQueryable().Where(expression).ToList();

            Assert.Equal(2, users.Count);
            Assert.Equal(users[0], goodUser);
            Assert.Equal(users[1], goodUser2);
        }
Beispiel #3
0
        public void NullAnniversary()
        {
            var builder = new WebFilterConverter <User>();

            var goodUser = new User {
                BirthDay = DateTime.Today
            };
            var badUser = new User {
                BirthDay = DateTime.Today.AddDays(1)
            };
            var nullUser = new User {
            };

            var filter = new WebFilter <User>(PropertyExpression <User> .New(u => u.BirthDay), WebFilterOperand.Anniversary, new List <DateTime?> {
                DateTime.Today, null
            });
            var expression = builder.ToExpression(filter);
            var users = new List <User> {
                goodUser, badUser, nullUser
            }.AsQueryable().Where(expression).ToList();

            Assert.Equal(2, users.Count);
            Assert.Equal(users[0], goodUser);
            Assert.Equal(users[1], nullUser);
        }
Beispiel #4
0
        public void LikeOnNullableString(string name, bool result)
        {
            var pattern = "abc";
            var builder = new WebFilterConverter <User>();
            var user    = new User {
                Name = name
            };

            var filter = new WebFilter <User>(PropertyExpression <User> .New(u => u.Name), WebFilterOperand.Like, new List <string> {
                pattern
            });
            var expression = builder.ToExpression(filter);
            var compiled   = expression.Compile();

            Assert.Equal(result, compiled.Invoke(user));
        }
Beispiel #5
0
        public void LikeOnNullableDate(string date, bool result)
        {
            var pattern = "2019";
            var user    = new User();

            if (DateTime.TryParse(date, out var parsedDate))
            {
                user.BirthDay = parsedDate;
            }
            var builder = new WebFilterConverter <User>();

            var filter = new WebFilter <User>(PropertyExpression <User> .New(u => u.BirthDay), WebFilterOperand.Like, new List <string> {
                pattern
            });
            var expression = builder.ToExpression(filter);
            var compiled   = expression.Compile();

            Assert.Equal(result, compiled.Invoke(user));
        }
Beispiel #6
0
        public void EqualsFilter(params int[] values)
        {
            var builder  = new WebFilterConverter <User>();
            var goodUser = new User {
                Id = 1
            };
            var badUser = new User {
                Id = 3
            };

            var filter = new WebFilter <User>(PropertyExpression <User> .New(u => u.Id), WebFilterOperand.Equals, new List <int>(values));
            var expression = builder.ToExpression(filter);
            var users = new List <User> {
                goodUser, badUser
            }.AsQueryable().Where(expression).ToList();

            Assert.Single(users);
            Assert.Equal(users[0], goodUser);
        }
Beispiel #7
0
        public void BetweenFilter()
        {
            var builder  = new WebFilterConverter <User>();
            var goodUser = new User {
                ContractStart = DateTime.Today.AddHours(12)
            };
            var badUser = new User {
                ContractStart = DateTime.Today.AddDays(2)
            };

            var filter = new WebFilter <User>(PropertyExpression <User> .New(u => u.ContractStart), WebFilterOperand.Between, new List <Period> {
                new Period(DateTime.Today, DateTime.Today.AddDays(1))
            });
            var expression = builder.ToExpression(filter);
            var users = new List <User> {
                goodUser, badUser
            }.AsQueryable().Where(expression).ToList();

            Assert.Single(users);
            Assert.Equal(users[0], goodUser);
        }
Beispiel #8
0
        public void GreaterThanFilter()
        {
            var builder  = new WebFilterConverter <User>();
            var goodUser = new User {
                Id = 3
            };
            var badUser = new User {
                Id = 1
            };

            var filter = new WebFilter <User>(PropertyExpression <User> .New(u => u.Id), WebFilterOperand.GreaterThan, new List <int> {
                1, 2
            });
            var expression = builder.ToExpression(filter);
            var users = new List <User> {
                goodUser, badUser
            }.AsQueryable().Where(expression).ToList();

            Assert.Single(users);
            Assert.Equal(users[0], goodUser);
        }
Beispiel #9
0
        public void AnniversaryNotNullable()
        {
            var builder = new WebFilterConverter <User>();

            var goodUser = new User {
                ContractStart = DateTime.Today
            };
            var badUser = new User {
                ContractStart = DateTime.Today.AddDays(1)
            };

            var filter = new WebFilter <User>(PropertyExpression <User> .New(u => u.ContractStart), WebFilterOperand.Anniversary, new List <DateTime?> {
                DateTime.Today
            });
            var expression = builder.ToExpression(filter);
            var users = new List <User> {
                goodUser, badUser
            }.AsQueryable().Where(expression).ToList();

            Assert.Single(users);
            Assert.Equal(users[0], goodUser);
        }
Beispiel #10
0
        public void ContainsAllFilter()
        {
            var builder = new WebFilterConverter <User>();

            var badUser = new User {
                Id = 3
            };
            var badUser2 = new User {
                Id = 2
            };

            var filter = new WebFilter <User>(PropertyExpression <User> .New(u => u.Id), WebFilterOperand.ContainsAll, new List <int> {
                1, 2
            });
            var expression = builder.ToExpression(filter);
            var users = new List <User> {
                badUser2, badUser
            }.AsQueryable().Where(expression).ToList();

            Assert.Empty(users);

            Assert.Throws <QueryBuilderException>(() => builder.ContainsAll(filter.Expression, Enumerable.Range(0, 10000).ToList()));
        }
Beispiel #11
0
        public void LikeOnGuidsShouldWork()
        {
            var goodGuid = Guid.NewGuid();
            var badGuid  = Guid.NewGuid();
            var builder  = new WebFilterConverter <User>();

            var goodUser = new User {
                PictureId = goodGuid
            };
            var badUser = new User {
                PictureId = badGuid
            };

            var filter = new WebFilter <User>(PropertyExpression <User> .New(u => u.PictureId), WebFilterOperand.Like, new List <Guid> {
                goodGuid
            });
            var expression = builder.ToExpression(filter);
            var compiled   = expression.Compile();

            Assert.True(compiled.Invoke(goodUser));
            Assert.False(compiled.Invoke(badUser));

            Assert.Throws <QueryBuilderException>(() => builder.Like(filter.Expression, Enumerable.Range(0, 100000).ToList()));
        }
Beispiel #12
0
        public async Task GenerateMultipleQueries()
        {
            var services = new ServiceCollection();

            services.AddEntityFrameworkInMemoryDatabase();
            services.AddDbContext <UselessDbContext>((service, options) => options.UseInMemoryDatabase("leaktest_3_0"));

            var provider = services.BuildServiceProvider();

            var dbContext = provider.GetRequiredService <UselessDbContext>();

            var builder  = new WebFilterConverter <UselessEntity>();
            var initExpr = PropertyExpression <UselessEntity> .New(u => u.Id);

            var expressions = Enumerable.Range(0, 1000).Select(i => builder.Equals(initExpr, new List <int> {
                i
            })).ToArray();

            var tmp = await dbContext.UselessEntities.Where(expressions[0]).ToListAsync();//Warmup

            for (int i = 0; i < 1000; i++)
            {
                tmp = await dbContext.UselessEntities.Where(expressions[i]).ToListAsync();

                Assert.Empty(tmp);
            }

#pragma warning disable EF1001 // Internal EF Core API usage.
            var efServiceProviders = ServiceProviderCache.Instance;

            var efConfigs            = typeof(ServiceProviderCache).GetField("_configurations", BindingFlags.NonPublic | BindingFlags.Instance);
            var localServiceProvider = efConfigs.GetValue(efServiceProviders) as ConcurrentDictionary <long, (IServiceProvider ServiceProvider, IDictionary <string, string> DebugInfo)>;

            var localCache    = localServiceProvider.Values.First().ServiceProvider.GetService <ICompiledQueryCache>() as CompiledQueryCache;
            var cacheProperty = typeof(CompiledQueryCache).GetField("_memoryCache", BindingFlags.NonPublic | BindingFlags.Instance);
            var cache         = cacheProperty.GetValue(localCache) as MemoryCache;
#pragma warning restore EF1001 // Internal EF Core API usage.

            int cachedQueryCount = 0;

            var entriesProperty = typeof(MemoryCache).GetProperty("EntriesCollection", BindingFlags.NonPublic | BindingFlags.Instance);
            var entries         = entriesProperty.GetValue(cache) as ICollection;// as ConcurrentDictionary<object, ICacheEntry>;
            var items           = new List <string>();
            if (entries != null)
            {
                foreach (var item in entries)
                {
                    var methodInfoVal = item.GetType().GetProperty("Value");
                    var val           = methodInfoVal.GetValue(item) as ICacheEntry;
                    if (val?.Value == null)
                    {
                        continue;
                    }
                    var contentType = val.Value.GetType();
                    var gens        = contentType.GenericTypeArguments;
                    if (gens.Length == 2 && gens[0] == typeof(QueryContext) && gens[1] == typeof(IAsyncEnumerable <UselessEntity>))
                    {
                        cachedQueryCount++;
                    }
                }
            }

            Assert.True(cachedQueryCount <= 1);// is either one or zero, but doesn't matter, that's not what we are testing.
        }