Skip to content

Commit

Permalink
Adding tests for previously fixed issues
Browse files Browse the repository at this point in the history
Resolves #12274
Resolves #11835
Resolves #12597
Resolves #12379
Resolves #12717
Resolves #17775
Resolves #18962
  • Loading branch information
maumar committed Sep 22, 2020
1 parent a9871fe commit e49d518
Show file tree
Hide file tree
Showing 16 changed files with 526 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.TestModels.Northwind;
using Microsoft.EntityFrameworkCore.TestUtilities;
Expand Down Expand Up @@ -742,6 +743,15 @@ FROM root c
WHERE (c[""Discriminator""] = ""Order"")");
}

[ConditionalTheory(Skip = "Issue#16146")]
public override async Task Average_on_nav_subquery_in_projection(bool isAsync)
{
await base.Average_on_nav_subquery_in_projection(isAsync);

AssertSql(
@"");
}

[ConditionalTheory(Skip = "Issue#17246")]
public override async Task OrderBy_client_Take(bool async)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -29,7 +30,7 @@ public override void Average_no_data_subquery()
using var context = CreateContext();

Assert.Equal(
"Sequence contains no elements",
CoreStrings.SequenceContainsNoElements,
Assert.Throws<InvalidOperationException>(
() => context.Customers.Select(c => c.Orders.Where(o => o.OrderID == -1).Average(o => o.OrderID)).ToList()).Message);
}
Expand All @@ -39,7 +40,7 @@ public override void Max_no_data_subquery()
using var context = CreateContext();

Assert.Equal(
"Sequence contains no elements",
CoreStrings.SequenceContainsNoElements,
Assert.Throws<InvalidOperationException>(
() => context.Customers.Select(c => c.Orders.Where(o => o.OrderID == -1).Max(o => o.OrderID)).ToList()).Message);
}
Expand All @@ -49,11 +50,19 @@ public override void Min_no_data_subquery()
using var context = CreateContext();

Assert.Equal(
"Sequence contains no elements",
CoreStrings.SequenceContainsNoElements,
Assert.Throws<InvalidOperationException>(
() => context.Customers.Select(c => c.Orders.Where(o => o.OrderID == -1).Min(o => o.OrderID)).ToList()).Message);
}

public override async Task Average_on_nav_subquery_in_projection(bool isAsync)
{
Assert.Equal(
CoreStrings.SequenceContainsNoElements,
(await Assert.ThrowsAsync<InvalidOperationException>(
() => base.Average_on_nav_subquery_in_projection(isAsync))).Message);
}

public override Task Collection_Last_member_access_in_projection_translated(bool async)
{
return Assert.ThrowsAsync<InvalidOperationException>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ public NorthwindAsyncSimpleQueryInMemoryTest(NorthwindQueryInMemoryFixture<NoopM
{
}

// InMemory can throw server side exception
public override Task Average_on_nav_subquery_in_projection()
{
return Assert.ThrowsAsync<InvalidOperationException>(() => base.Average_on_nav_subquery_in_projection());
}

// mapping to view not supported on InMemory
public override Task Query_backed_by_database_view()
=> Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.TestModels.Northwind;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;
Expand Down Expand Up @@ -118,7 +119,7 @@ public override async Task Max_on_empty_sequence_throws(bool async)
? (await Assert.ThrowsAsync<InvalidOperationException>(() => query.ToListAsync())).Message
: Assert.Throws<InvalidOperationException>(() => query.ToList()).Message;

Assert.Equal("Sequence contains no elements", message);
Assert.Equal(CoreStrings.SequenceContainsNoElements, message);
}
}
}
38 changes: 38 additions & 0 deletions test/EFCore.Specification.Tests/Query/GearsOfWarQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7860,6 +7860,44 @@ public virtual Task FirstOrDefault_over_int_compared_to_zero(bool async)
elementSorter: e => e);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Correlated_collection_with_inner_collection_references_element_two_levels_up(bool async)
{
return AssertQuery(
async,
ss => from o in ss.Set<Gear>().OfType<Officer>()
select new
{
o.FullName,
Collection = (from r in o.Reports
select new
{
ReportName = r.FullName,
OfficerName = o.FullName
}).ToList()
},
elementSorter: e => e.FullName,
elementAsserter: (e, a) =>
{
Assert.Equal(e.FullName, a.FullName);
AssertCollection(e.Collection, a.Collection, elementSorter: ee => (ee.OfficerName, ee.ReportName));
});
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Accessing_derived_property_using_hard_and_soft_cast(bool async)
{
await AssertQuery(
async,
ss => ss.Set<LocustLeader>().Where(ll => ll is LocustCommander && ((LocustCommander)ll).HighCommandId != 0));

await AssertQuery(
async,
ss => ss.Set<LocustLeader>().Where(ll => ll is LocustCommander && (ll as LocustCommander).HighCommandId != 0));
}

protected GearsOfWarContext CreateContext()
=> Fixture.CreateContext();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1986,5 +1986,28 @@ public virtual Task Min_after_default_if_empty_does_not_throw(bool isAsync)
isAsync,
ss => ss.Set<Order>().Where(o => o.OrderID == 10243).Select(o => o.OrderID).DefaultIfEmpty());
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Average_on_nav_subquery_in_projection(bool isAsync)
{
return AssertQuery(
isAsync,
ss => ss.Set<Customer>()
.OrderBy(c => c.CustomerID)
.Select(c => new { Ave = (double?)c.Orders.Average(o => o.OrderID) }),
ss => ss.Set<Customer>()
.OrderBy(c => c.CustomerID)
.Select(c => new { Ave = c.Orders != null && c.Orders.Count() > 0 ? (double?)c.Orders.Average(o => o.OrderID) : null }),
assertOrder: true,
elementAsserter: (e, a) =>
{
Assert.Equal(e.Ave.HasValue, a.Ave.HasValue);
if (e.Ave.HasValue)
{
Assert.InRange(e.Ave.Value - a.Ave.Value, -0.1D, 0.1D);
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,6 @@ var results
Assert.Equal(830, results.SelectMany(a => a.Orders).ToList().Count);
}

[ConditionalFact(Skip = "Issue #17775")]
public virtual async Task Average_on_nav_subquery_in_projection()
{
using var context = CreateContext();
var results
= await context.Customers.Select(
c => new { Ave = c.Orders.Average(o => o.OrderID) })
.ToListAsync();

Assert.Equal(91, results.ToList().Count);
}

[ConditionalFact]
public virtual async Task ToListAsync_can_be_canceled()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6326,5 +6326,15 @@ public virtual Task First_on_collection_in_projection(bool async)
Assert.Equal(e.OrderDate, a.OrderDate);
});
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task SkipWhile_throws_meaningful_exception(bool async)
{
return AssertTranslationFailed(
() => AssertQuery(
async,
ss => ss.Set<Customer>().OrderBy(c => c.CustomerID).SkipWhile(c => c.CustomerID != "Foo").Skip(1)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1845,5 +1845,20 @@ public virtual Task Custom_projection_reference_navigation_PK_to_FK_optimization
AssertEqual(e.Customer, a.Customer);
});
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Projecting_Length_of_a_string_property_after_FirstOrDefault_on_correlated_collection(bool async)
{
return AssertQueryScalar(
async,
ss => ss.Set<Customer>()
.OrderBy(c => c.CustomerID)
.Select(c => (int?)c.Orders.OrderBy(o => o.OrderID).Select(o => o.CustomerID).FirstOrDefault().Length),
ss => ss.Set<Customer>()
.OrderBy(c => c.CustomerID)
.Select(c => c.Orders.OrderBy(o => o.OrderID).Select(o => o.CustomerID).FirstOrDefault().MaybeScalar(x => x.Length)),
assertOrder: true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7159,6 +7159,37 @@ FROM [Gears] AS [g]
WHERE ([s].[Id] = [g].[SquadId]) AND ([g].[HasSoulPatch] = CAST(1 AS bit))), 0) <> 0)");
}

public override async Task Correlated_collection_with_inner_collection_references_element_two_levels_up(bool async)
{
await base.Correlated_collection_with_inner_collection_references_element_two_levels_up(async);

AssertSql(
@"SELECT [g].[FullName], [g].[Nickname], [g].[SquadId], [t].[ReportName], [t].[OfficerName], [t].[Nickname], [t].[SquadId]
FROM [Gears] AS [g]
LEFT JOIN [Officers] AS [o] ON ([g].[Nickname] = [o].[Nickname]) AND ([g].[SquadId] = [o].[SquadId])
OUTER APPLY (
SELECT [g0].[FullName] AS [ReportName], [g].[FullName] AS [OfficerName], [g0].[Nickname], [g0].[SquadId]
FROM [Gears] AS [g0]
WHERE ([g].[Nickname] = [g0].[LeaderNickname]) AND ([g].[SquadId] = [g0].[LeaderSquadId])
) AS [t]
WHERE [o].[Nickname] IS NOT NULL
ORDER BY [g].[Nickname], [g].[SquadId], [t].[Nickname], [t].[SquadId]");
}

public override async Task Accessing_derived_property_using_hard_and_soft_cast(bool async)
{
await base.Accessing_derived_property_using_hard_and_soft_cast(async);

AssertSql(
@"SELECT [l].[Name], [l].[Discriminator], [l].[LocustHordeId], [l].[ThreatLevel], [l].[ThreatLevelByte], [l].[ThreatLevelNullableByte], [l].[DefeatedByNickname], [l].[DefeatedBySquadId], [l].[HighCommandId]
FROM [LocustLeaders] AS [l]
WHERE ([l].[Discriminator] = N'LocustCommander') AND (([l].[HighCommandId] <> 0) OR [l].[HighCommandId] IS NULL)",
//
@"SELECT [l].[Name], [l].[Discriminator], [l].[LocustHordeId], [l].[ThreatLevel], [l].[ThreatLevelByte], [l].[ThreatLevelNullableByte], [l].[DefeatedByNickname], [l].[DefeatedBySquadId], [l].[HighCommandId]
FROM [LocustLeaders] AS [l]
WHERE ([l].[Discriminator] = N'LocustCommander') AND (([l].[HighCommandId] <> 0) OR [l].[HighCommandId] IS NULL)");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,19 @@ public override async Task Count_on_projection_with_client_eval(bool async)
FROM [Orders] AS [o]");
}

public override async Task Average_on_nav_subquery_in_projection(bool isAsync)
{
await base.Average_on_nav_subquery_in_projection(isAsync);

AssertSql(
@"SELECT (
SELECT AVG(CAST([o].[OrderID] AS float))
FROM [Orders] AS [o]
WHERE [c].[CustomerID] = [o].[CustomerID]) AS [Ave]
FROM [Customers] AS [c]
ORDER BY [c].[CustomerID]");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,20 @@ FROM [Orders] AS [o]
LEFT JOIN [Customers] AS [c] ON [o].[CustomerID] = [c].[CustomerID]");
}

public override async Task Projecting_Length_of_a_string_property_after_FirstOrDefault_on_correlated_collection(bool async)
{
await base.Projecting_Length_of_a_string_property_after_FirstOrDefault_on_correlated_collection(async);

AssertSql(
@"SELECT (
SELECT TOP(1) CAST(LEN([o].[CustomerID]) AS int)
FROM [Orders] AS [o]
WHERE [c].[CustomerID] = [o].[CustomerID]
ORDER BY [o].[OrderID])
FROM [Customers] AS [c]
ORDER BY [c].[CustomerID]");
}

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

Expand Down
Loading

0 comments on commit e49d518

Please sign in to comment.