Beispiel #1
0
        public async Task Queryable_Should_Allow_Composition()
        {
            // Arrange
            var comparer  = new MyProductComparer();
            var expected1 = new MyProduct {
                Id = 1, Name = "Chai", Price = 18.00m, Category = "Beverages"
            };
            var expected2 = new MyProduct {
                Id = 2, Name = "Chang", Price = 19.00m, Category = "Beverages"
            };

            var repository = new Repository <Product>(_fixture.Context);

            // Act
            var query    = repository.Queryable();
            var products = await query
                           .Take(2)
                           .Include(p => p.Category)
                           .Where(p => p.UnitPrice > 15)
                           .Select(p => new MyProduct
            {
                Id       = p.ProductId,
                Name     = p.ProductName,
                Price    = p.UnitPrice,
                Category = p.Category.CategoryName
            })
                           .ToListAsync();

            // Assert
            Assert.Collection(products,
                              p => Assert.Equal(expected1, p, comparer),
                              p => Assert.Equal(expected2, p, comparer));
        }
        public async Task QueryableSql_Should_Allow_Composition()
        {
            // Arrange
            var comparer  = new MyProductComparer();
            var expected1 = new MyProduct {
                Id = 1, Name = "Chai", Price = 18m, Category = "Beverages"
            };
            var expected2 = new MyProduct {
                Id = 2, Name = "Chang", Price = 19m, Category = "Beverages"
            };
            var expected3 = new MyProduct {
                Id = 35, Name = "Steeleye Stout", Price = 18m, Category = "Beverages"
            };
            var repository = new Repository <Product>(_fixture.Context);

            // Act
            var query    = repository.QueryableSql("SELECT * FROM Products");
            var products = await query
                           .Include(p => p.Category)
                           .Where(p => (new int[] { 1, 2, 35 }).Contains(p.ProductId))
                           .Select(p => new MyProduct
            {
                Id       = p.ProductId,
                Name     = p.ProductName,
                Price    = p.UnitPrice,
                Category = p.Category.CategoryName
            })
                           .ToListAsync();

            // Assert
            Assert.Collection(products.Take(3),
                              p => Assert.Equal(expected1, p, comparer),
                              p => Assert.Equal(expected2, p, comparer),
                              p => Assert.Equal(expected3, p, comparer));
        }