Пример #1
0
        public void WhereWithCollectionContainsMethod()
        {
            var products = new ProductTest[]
            {
                new ProductTest(Guid.NewGuid(), "Product A", 99),
                new ProductTest(Guid.NewGuid(), "Product B", 150),
                new ProductTest(Guid.NewGuid(), "Product C", 200),
                new ProductTest(Guid.NewGuid(), "Product D", 300)
            };

            var productList = GenerateData(1000, products);

            AddData(productList.ToArray());

            var client = ObterCliente();

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            var productsIds = products.Select(s => s.ProductId).ToList();

            query = query.Where(w => productsIds.Contains(w.ProductId));

            var result = query.ToList();

            Assert.NotEmpty(result);
            Assert.True(result.All(a => products.Any(p => p.ProductId == a.ProductId)));
        }
Пример #2
0
        public void AggregacaoSum()
        {
            var produto1 = new ProductTest(Guid.NewGuid(), "ProductTest", 9.9M);
            var produto2 = new ProductTest(Guid.NewGuid(), "ProductTest 2", 5M);

            AddData(produto1, produto2);

            var client = ObterCliente();

            var query      = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);
            var precoTotal = query.Sum(s => s.Price);

            Assert.Equal(produto1.Price + produto2.Price, precoTotal);
        }
Пример #3
0
        public void LessThanInvalid()
        {
            var produto = new ProductTest(Guid.NewGuid(), "ProductTest", 9.9M);

            AddData(produto);

            var client = ObterCliente();

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            query = query.Where(w => w.Price < 1);

            var result = query.ToList();

            Assert.Empty(result);
        }
Пример #4
0
        public void LessThanValid()
        {
            var produto = new ProductTest(Guid.NewGuid(), "ProductTest", 9.9M);

            AddData(produto);

            var client = ObterCliente();

            var pId = produto.ProductId;

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            query = query.Where(w => w.Price < 50);

            var result = query.ToList();

            Assert.NotEmpty(result);
            Assert.Contains(result, f => f.ProductId == pId);
        }
Пример #5
0
        public void EqualsInvalid()
        {
            var produto = new ProductTest(Guid.NewGuid(), "ProductTest", 9.9M);

            AddData(produto);

            var client = ObterCliente();

            var pId   = produto.ProductId;
            var pNome = produto.Name;

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            query = query.Where(w => w.ProductId == Guid.Empty);

            var result = query.ToList();

            Assert.Empty(result);
        }
Пример #6
0
        public void OrderByDescendingAndWhere()
        {
            var product = new ProductTest(Guid.NewGuid(), "Product A", 150);

            var productList = GenerateData(1000, product);

            AddData(productList.ToArray());
            var client = ObterCliente();


            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            query = query.Where(w => w.Price == 150).OrderByDescending(o => o.Name).ThenByDescending(o => o.Price);

            var result = query.ToList();

            Assert.Contains(result, f => f.ProductId == product.ProductId);
            Assert.Equal(product.Price, result.First().Price);
        }
Пример #7
0
        public void StartsWithInvalid()
        {
            var produto = new ProductTest(Guid.NewGuid(), "ProductTest", 9.9M);

            var productList = GenerateData(1000, produto);

            AddData(productList.ToArray());

            var client = ObterCliente();

            var pId   = produto.ProductId;
            var pNome = produto.Name;

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            query = query.Where(w => w.Name.StartsWith("zzzzzzzzzzz"));

            var result = query.ToList();

            Assert.Empty(result);
        }
Пример #8
0
        public void LinqSum()
        {
            var produto1 = new ProductTest(Guid.NewGuid(), "ProductTest", 9.9M);
            var produto2 = new ProductTest(Guid.NewGuid(), "ProductTest 2", 5M);

            AddData(produto1, produto2);

            var client = ObterCliente();

            var query  = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);
            var result = (from o in query
                          group o by o.Name into g
                          select new
            {
                Produto = g.Key,
                Total = g.Sum(o => o.Price),
                Min = g.Min(o => o.Price),
                Avg = g.Average(o => o.Price)
            }).ToList();

            Assert.Equal(produto1.Price + produto2.Price, result.Sum(s => s.Total));
        }