Skip to content

Commit

Permalink
Add tests for Navigation issues that no longer reproduce
Browse files Browse the repository at this point in the history
Add test for #3804 - Select Navigation following Skip or Take
Add test for #3674 - GroupBy with Where on Navigation Property
Add test for #3676 - GroupBy Let on Navigation Property (disabled)
Add test for #3913 - Sum + Count inside Select
  • Loading branch information
mikary committed May 17, 2016
1 parent 2dc6007 commit de7c938
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ from dynamic l2oItem in l2oItems
entryCount: 1);
}

[ConditionalFact]
public virtual void Take_Select_Navigation()
{
AssertQuery<Customer>(
cs => cs.Take(2)
.Select(c => c.Orders.FirstOrDefault()));
}

[ConditionalFact]
public virtual void Skip_Select_Navigation()
{
AssertQuery<Customer>(
cs => cs.OrderBy(c => c.CustomerID)
.Skip(20)
.Select(c => c.Orders
.OrderBy(o => o.OrderID)
.FirstOrDefault()));
}

[ConditionalFact]
public virtual void Select_Where_Navigation_Null()
{
Expand Down Expand Up @@ -180,6 +199,15 @@ public virtual void Select_Where_Navigation_Included()
entryCount: 15);
}

[ConditionalFact]
public virtual void Select_count_plus_sum()
{
AssertQuery<Order>(os => os.Select(o => new
{
Total = o.OrderDetails.Sum(od => od.Quantity) + o.OrderDetails.Count()
}));
}

[ConditionalFact]
public virtual void Singleton_Navigation_With_Member_Access()
{
Expand Down Expand Up @@ -644,6 +672,51 @@ join efItem in efItems on l2oItem.Key equals efItem.Key
});
}

[ConditionalFact]
public virtual void Where_nav_prop_group_by()
{
AssertQuery<OrderDetail, IGrouping<short, OrderDetail>>(
ods => from od in ods
where od.Order.CustomerID == "ALFKI"
group od by od.Quantity,
asserter: (l2oItems, efItems) =>
{
foreach (var pair in
from l2oItem in l2oItems
join efItem in efItems on l2oItem.Key equals efItem.Key
select new { l2oItem, efItem })
{
Assert.Equal(
pair.l2oItem.Select(i => i.OrderID).OrderBy(i => i),
pair.efItem.Select(i => i.OrderID).OrderBy(i => i));
}
});
}

// issue #3676
//// [ConditionalFact]
public virtual void Let_group_by_nav_prop()
{
AssertQuery<OrderDetail, IGrouping<string, OrderDetail>>(
ods => from od in ods
let customer = od.Order.CustomerID
group od by customer
into odg
select odg,
asserter: (l2oItems, efItems) =>
{
foreach (var pair in
from l2oItem in l2oItems
join efItem in efItems on l2oItem.Key equals efItem.Key
select new { l2oItem, efItem })
{
Assert.Equal(
pair.l2oItem.Select(i => i.OrderID).OrderBy(i => i),
pair.efItem.Select(i => i.OrderID).OrderBy(i => i));
}
});
}

protected QueryNavigationsTestBase(TFixture fixture)
{
Fixture = fixture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,34 @@ static NorthwindData()
_orders = CreateOrders();
_orderDetails = CreateOrderDetails();

foreach (var customer in _customers)
{
customer.Orders = new List<Order>();
}

foreach (var product in _products)
{
product.OrderDetails = new List<OrderDetail>();
}

foreach (var order in _orders)
{
order.OrderDetails = new List<OrderDetail>();

var customer = _customers.Where(c => c.CustomerID == order.CustomerID).First();
order.Customer = customer;

if (customer.Orders == null)
{
customer.Orders = new List<Order>();
}

customer.Orders.Add(order);
}

foreach (var orderDetail in _orderDetails)
{
var order = _orders.Where(o => o.OrderID == orderDetail.OrderID).First();
var product = _products.Where(p => p.ProductID == orderDetail.ProductID).First();
orderDetail.Order = order;
orderDetail.Product = product;

if (order.OrderDetails == null)
{
order.OrderDetails = new List<OrderDetail>();
}

order.OrderDetails.Add(orderDetail);

if (product.OrderDetails == null)
{
product.OrderDetails = new List<OrderDetail>();
}

var product = _products.Where(p => p.ProductID == orderDetail.ProductID).First();
orderDetail.Product = product;
product.OrderDetails.Add(orderDetail);

}

foreach (var employee in _employees)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,44 @@ FROM [Order Details] AS [od]
Sql);
}

public override void Take_Select_Navigation()
{
base.Take_Select_Navigation();

Assert.StartsWith(
@"@__p_0: 2
SELECT [t].[CustomerID]
FROM (
SELECT TOP(@__p_0) [c0].*
FROM [Customers] AS [c0]
) AS [t]
SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Orders] AS [o]
",
Sql);
}

public override void Skip_Select_Navigation()
{
base.Skip_Select_Navigation();

Assert.StartsWith(
@"@__p_0: 20
SELECT [c].[CustomerID]
FROM [Customers] AS [c]
ORDER BY [c].[CustomerID]
OFFSET @__p_0 ROWS
SELECT [o].[OrderID], [o].[CustomerID], [o].[EmployeeID], [o].[OrderDate]
FROM [Orders] AS [o]
ORDER BY [o].[OrderID]
",
Sql);
}

public override void Select_Where_Navigation_Included()
{
base.Select_Where_Navigation_Included();
Expand Down Expand Up @@ -121,6 +159,24 @@ FROM [Orders] AS [o]
Sql);
}

public override void Select_count_plus_sum()
{
base.Select_count_plus_sum();

Assert.StartsWith(
@"SELECT (
SELECT SUM([od0].[Quantity])
FROM [Order Details] AS [od0]
WHERE [o].[OrderID] = [od0].[OrderID]
) + (
SELECT COUNT(*)
FROM [Order Details] AS [o1]
WHERE [o].[OrderID] = [o1].[OrderID]
)
FROM [Orders] AS [o]",
Sql);
}

public override void Singleton_Navigation_With_Member_Access()
{
base.Singleton_Navigation_With_Member_Access();
Expand Down Expand Up @@ -641,6 +697,28 @@ FROM [Orders] AS [o]
Sql);
}

public override void Where_nav_prop_group_by()
{
base.Where_nav_prop_group_by();

Assert.Equal(
@"SELECT [od].[OrderID], [od].[ProductID], [od].[Discount], [od].[Quantity], [od].[UnitPrice]
FROM [Order Details] AS [od]
INNER JOIN [Orders] AS [od.Order] ON [od].[OrderID] = [od.Order].[OrderID]
WHERE [od.Order].[CustomerID] = N'ALFKI'
ORDER BY [od].[Quantity]",
Sql);
}

public override void Let_group_by_nav_prop()
{
base.Let_group_by_nav_prop();

Assert.Equal(
@"",
Sql);
}

protected override void ClearLog() => TestSqlLoggerFactory.Reset();

private static string Sql => TestSqlLoggerFactory.Sql;
Expand Down

0 comments on commit de7c938

Please sign in to comment.