Skip to content

Commit

Permalink
Adding some regression tests for issues that have been fixed previously
Browse files Browse the repository at this point in the history
Resolves #14028
Resolves #18346
  • Loading branch information
maumar committed Jul 10, 2020
1 parent 321d16c commit 19314ab
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 18 deletions.
50 changes: 32 additions & 18 deletions test/EFCore.Specification.Tests/Query/OwnedQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -664,46 +664,46 @@ public virtual Task Can_OrderBy_owened_indexer_properties_converted(bool async)

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Can_group_by_indexer_property(bool isAsync)
public virtual Task Can_group_by_indexer_property(bool async)
{
return AssertQueryScalar(
isAsync,
async,
ss => ss.Set<OwnedPerson>().GroupBy(c => c["Name"]).Select(g => g.Count()));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Can_group_by_converted_indexer_property(bool isAsync)
public virtual Task Can_group_by_converted_indexer_property(bool async)
{
return AssertQueryScalar(
isAsync,
async,
ss => ss.Set<OwnedPerson>().GroupBy(c => (string)c["Name"]).Select(g => g.Count()));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Can_group_by_owned_indexer_property(bool isAsync)
public virtual Task Can_group_by_owned_indexer_property(bool async)
{
return AssertQueryScalar(
isAsync,
async,
ss => ss.Set<OwnedPerson>().GroupBy(c => c.PersonAddress["ZipCode"]).Select(g => g.Count()));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Can_group_by_converted_owned_indexer_property(bool isAsync)
public virtual Task Can_group_by_converted_owned_indexer_property(bool async)
{
return AssertQueryScalar(
isAsync,
async,
ss => ss.Set<OwnedPerson>().GroupBy(c => (int)c.PersonAddress["ZipCode"]).Select(g => g.Count()));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Can_join_on_indexer_property_on_query(bool isAsync)
public virtual Task Can_join_on_indexer_property_on_query(bool async)
{
return AssertQuery(
isAsync,
async,
ss =>
(from c1 in ss.Set<OwnedPerson>()
join c2 in ss.Set<OwnedPerson>()
Expand All @@ -713,41 +713,41 @@ on c1.PersonAddress["ZipCode"] equals c2.PersonAddress["ZipCode"]

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Projecting_indexer_property_ignores_include(bool isAsync)
public virtual Task Projecting_indexer_property_ignores_include(bool async)
{
return AssertQuery(
isAsync,
async,
ss => from c in ss.Set<OwnedPerson>().AsTracking()
select new { Nation = c.PersonAddress["ZipCode"] });
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Projecting_indexer_property_ignores_include_converted(bool isAsync)
public virtual Task Projecting_indexer_property_ignores_include_converted(bool async)
{
return AssertQuery(
isAsync,
async,
ss => from c in ss.Set<OwnedPerson>().AsTracking()
select new { Nation = (int)c.PersonAddress["ZipCode"] });
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Indexer_property_is_pushdown_into_subquery(bool isAsync)
public virtual Task Indexer_property_is_pushdown_into_subquery(bool async)
{
return AssertQuery(
isAsync,
async,
ss => ss.Set<OwnedPerson>()
.Where(g => (string)ss.Set<OwnedPerson>().Where(c => c.Id == g.Id).FirstOrDefault()["Name"] == "Mona Cy")
.Select(c => (string)c["Name"]));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Can_query_indexer_property_on_owned_collection(bool isAsync)
public virtual Task Can_query_indexer_property_on_owned_collection(bool async)
{
return AssertQuery(
isAsync,
async,
ss => ss.Set<OwnedPerson>().Where(ow => ow.Orders.Where(o => ((DateTime)o["OrderDate"]).Year == 2018).Count() == 1)
.Select(c => (string)c["Name"]));
}
Expand Down Expand Up @@ -796,6 +796,20 @@ public virtual Task Trying_to_access_non_existent_indexer_property_throws_meanin
CoreStrings.QueryUnableToTranslateMember("Foo", nameof(OwnedPerson)));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task GroupBy_with_multiple_aggregates_on_owned_navigation_properties(bool async)
{
return AssertQuery(
async,
ss => ss.Set<OwnedPerson>().GroupBy(e => 1, x => x.PersonAddress.Country.Planet.Star).Select(e => new
{
p1 = e.Average(x => x.Id),
p2 = e.Sum(x => x.Id),
p3 = e.Max(x => x.Name.Length),
}));
}

protected virtual DbContext CreateContext() => Fixture.CreateContext();

public abstract class OwnedQueryFixtureBase : SharedStoreFixtureBase<PoolableDbContext>, IQueryFixtureBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,17 @@ FROM [OwnedPerson] AS [o]
ORDER BY [o].[Id]");
}

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

AssertSql(
@"SELECT AVG(CAST([s].[Id] AS float)) AS [p1], COALESCE(SUM([s].[Id]), 0) AS [p2], MAX(CAST(LEN([s].[Name]) AS int)) AS [p3]
FROM [OwnedPerson] AS [o]
LEFT JOIN [Planet] AS [p] ON [o].[PersonAddress_Country_PlanetId] = [p].[Id]
LEFT JOIN [Star] AS [s] ON [p].[StarId] = [s].[Id]");
}

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

Expand Down
74 changes: 74 additions & 0 deletions test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7761,6 +7761,80 @@ private SqlServerTestStore CreateDatabase21540()

#endregion

#region Issue18346

[ConditionalFact]
public virtual void Can_query_hierarchy_with_non_nullable_property_on_derived()
{
using (CreateDatabase18346())
{
using var context = new MyContext18346(_options);
var query = context.Businesses.ToList();
Assert.Equal(3, query.Count);

AssertSql(
@"SELECT [b].[Id], [b].[Name], [b].[Type], [b].[IsOnline]
FROM [Businesses] AS [b]");
}
}

private abstract class Business18346
{
public int Id { get; set; }
public string Name { get; set; }
public BusinessType18346 Type { get; set; }
}

private class Shop18346 : Business18346
{
public bool IsOnline { get; set; }
}

private class Brand18346 : Business18346
{
}

private enum BusinessType18346
{
Shop,
Brand,
}

private class MyContext18346 : DbContext
{
public DbSet<Business18346> Businesses { get; set; }

public MyContext18346(DbContextOptions options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Business18346>()
.HasDiscriminator(x => x.Type)
.HasValue<Shop18346>(BusinessType18346.Shop)
.HasValue<Brand18346>(BusinessType18346.Brand);
}
}

private SqlServerTestStore CreateDatabase18346()
=> CreateTestStore(
() => new MyContext18346(_options),
context =>
{
var shop1 = new Shop18346 { IsOnline = true, Name = "Amzn" };
var shop2 = new Shop18346 { IsOnline = false, Name = "Mom and Pop's Shoppe" };
var brand = new Brand18346 { Name = "Tsla" };
context.Businesses.AddRange(shop1, shop2, brand);
context.SaveChanges();
ClearLog();
});

#endregion


private DbContextOptions _options;

private SqlServerTestStore CreateTestStore<TContext>(
Expand Down

0 comments on commit 19314ab

Please sign in to comment.