Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to #19994 - Query/Test: figure out how to represent shadow properties in expected result queries #23579

Merged
merged 1 commit into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,85 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.TestModels.GearsOfWarModel;
using Microsoft.EntityFrameworkCore.TestUtilities;

namespace Microsoft.EntityFrameworkCore.Query
{
public abstract class GearsOfWarQueryRelationalFixture : GearsOfWarQueryFixtureBase
{
public override Dictionary<(Type, string), Func<object, object>> GetShadowPropertyMappings()
{
var discriminatorMapping = new Dictionary<(Type, string), Func<object, object>>
{
{
(typeof(Gear), "Discriminator"),
e =>
{
switch (((Gear)e)?.Nickname)
{
case "Baird":
case "Marcus":
return "Officer";

case "Cole Train":
case "Dom":
case "Paduk":
return "Gear";

default:
return null;
}
}
},
{
(typeof(Faction), "Discriminator"),
e =>
{
switch (((Faction)e)?.Id)
{
case 1:
case 2:
return "LocustHorde";

default:
return null;
}
}
},
{
(typeof(LocustLeader), "Discriminator"),
e =>
{
switch (((LocustLeader)e)?.Name)
{
case "General Karn":
case "General RAAM":
case "High Priest Skorge":
case "The Speaker":
return "LocustLeader";

case "Queen Myrrah":
case "Unknown":
return "LocustCommander";

default:
return null;
}
}
},
};

foreach (var shadowPropertyMappingElement in base.GetShadowPropertyMappings())
{
discriminatorMapping.Add(shadowPropertyMappingElement.Key, shadowPropertyMappingElement.Value);
}

return discriminatorMapping;
}

public new RelationalTestStore TestStore
=> (RelationalTestStore)base.TestStore;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,41 @@ public override Task Where_coalesce_with_anonymous_types(bool async)
return AssertTranslationFailed(() => base.Where_coalesce_with_anonymous_types(async));
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Project_discriminator_columns(bool async)
{
await AssertQuery(
async,
ss => ss.Set<Gear>().Select(g => new { g.Nickname, Discriminator = EF.Property<string>(g, "Discriminator") }),
elementSorter: e => e.Nickname);

await AssertQuery(
async,
ss => ss.Set<Gear>().OfType<Officer>().Select(g => new { g.Nickname, Discriminator = EF.Property<string>(g, "Discriminator") }),
elementSorter: e => e.Nickname);

await AssertQuery(
async,
ss => ss.Set<Faction>().Select(f => new { f.Id, Discriminator = EF.Property<string>(f, "Discriminator") }),
elementSorter: e => e.Id);

await AssertQuery(
async,
ss => ss.Set<Faction>().OfType<LocustHorde>().Select(lh => new { lh.Id, Discriminator = EF.Property<string>(lh, "Discriminator") }),
elementSorter: e => e.Id);

await AssertQuery(
async,
ss => ss.Set<LocustLeader>().Select(ll => new { ll.Name, Discriminator = EF.Property<string>(ll, "Discriminator") }),
elementSorter: e => e.Name);

await AssertQuery(
async,
ss => ss.Set<LocustLeader>().OfType<LocustCommander>().Select(ll => new { ll.Name, Discriminator = EF.Property<string>(ll, "Discriminator") }),
elementSorter: e => e.Name);
}

protected virtual bool CanExecuteQueryString
=> false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ public override async Task Cast_to_derived_followed_by_include_and_FirstOrDefaul
{
await base.Cast_to_derived_followed_by_include_and_FirstOrDefault(async);
}

public override Task Project_discriminator_columns(bool async)
=> Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,207 @@ namespace Microsoft.EntityFrameworkCore.Query
{
public abstract class ComplexNavigationsQueryFixtureBase : SharedStoreFixtureBase<ComplexNavigationsContext>, IQueryFixtureBase
{
private ComplexNavigationsDefaultData _expectedData;

protected override string StoreName { get; } = "ComplexNavigations";

public Func<DbContext> GetContextCreator()
=> () => CreateContext();

public virtual ISetSource GetExpectedData()
=> new ComplexNavigationsDefaultData();
{
if (_expectedData == null)
{
_expectedData = new ComplexNavigationsDefaultData();
}

return _expectedData;
}

public virtual Dictionary<(Type, string), Func<object, object>> GetShadowPropertyMappings()
{
var l1s = GetExpectedData().Set<Level1>().ToList();
var l2s = GetExpectedData().Set<Level2>().ToList();
var l3s = GetExpectedData().Set<Level3>().ToList();
var l4s = GetExpectedData().Set<Level4>().ToList();

var ib1s = GetExpectedData().Set<InheritanceBase1>().ToList();
var ib2s = GetExpectedData().Set<InheritanceBase2>().ToList();
var il1s = GetExpectedData().Set<InheritanceLeaf1>().ToList();
var il2s = GetExpectedData().Set<InheritanceLeaf2>().ToList();

return new Dictionary<(Type, string), Func<object, object>>
{
{
(typeof(Level1), "OneToOne_Optional_Self1Id"),
e => l1s.SingleOrDefault(l => l.Id == ((Level1)e)?.Id)?.OneToOne_Optional_Self1?.Id
},
{
(typeof(Level1), "OneToMany_Required_Self_Inverse1Id"),
e => l1s.SingleOrDefault(l => l.Id == ((Level1)e)?.Id)?.OneToMany_Required_Self_Inverse1?.Id
},
{
(typeof(Level1), "OneToMany_Optional_Self_Inverse1Id"),
e => l1s.SingleOrDefault(l => l.Id == ((Level1)e)?.Id)?.OneToMany_Optional_Self_Inverse1?.Id
},

{
(typeof(Level2), "OneToOne_Optional_PK_Inverse2Id"),
e => l2s.SingleOrDefault(l => l.Id == ((Level2)e)?.Id)?.OneToOne_Optional_PK_Inverse2?.Id
},
{
(typeof(Level2), "OneToMany_Required_Inverse2Id"),
e => l2s.SingleOrDefault(l => l.Id == ((Level2)e)?.Id)?.OneToMany_Required_Inverse2?.Id
},
{
(typeof(Level2), "OneToMany_Optional_Inverse2Id"),
e => l2s.SingleOrDefault(l => l.Id == ((Level2)e)?.Id)?.OneToMany_Optional_Inverse2?.Id
},
{
(typeof(Level2), "OneToOne_Optional_Self2Id"),
e => l2s.SingleOrDefault(l => l.Id == ((Level2)e)?.Id)?.OneToOne_Optional_Self2?.Id
},
{
(typeof(Level2), "OneToMany_Required_Self_Inverse2Id"),
e => l2s.SingleOrDefault(l => l.Id == ((Level2)e)?.Id)?.OneToMany_Required_Self_Inverse2?.Id
},
{
(typeof(Level2), "OneToMany_Optional_Self_Inverse2Id"),
e => l2s.SingleOrDefault(l => l.Id == ((Level2)e)?.Id)?.OneToMany_Optional_Self_Inverse2?.Id
},

{
(typeof(Level3), "OneToOne_Optional_PK_Inverse3Id"),
e => l3s.SingleOrDefault(l => l.Id == ((Level3)e)?.Id)?.OneToOne_Optional_PK_Inverse3?.Id
},
{
(typeof(Level3), "OneToMany_Required_Inverse3Id"),
e => l3s.SingleOrDefault(l => l.Id == ((Level3)e)?.Id)?.OneToMany_Required_Inverse3?.Id
},
{
(typeof(Level3), "OneToMany_Optional_Inverse3Id"),
e => l3s.SingleOrDefault(l => l.Id == ((Level3)e)?.Id)?.OneToMany_Optional_Inverse3?.Id
},
{
(typeof(Level3), "OneToOne_Optional_Self3Id"),
e => l3s.SingleOrDefault(l => l.Id == ((Level3)e)?.Id)?.OneToOne_Optional_Self3?.Id
},
{
(typeof(Level3), "OneToMany_Required_Self_Inverse3Id"),
e => l3s.SingleOrDefault(l => l.Id == ((Level3)e)?.Id)?.OneToMany_Required_Self_Inverse3?.Id
},
{
(typeof(Level3), "OneToMany_Optional_Self_Inverse3Id"),
e => l3s.SingleOrDefault(l => l.Id == ((Level3)e)?.Id)?.OneToMany_Optional_Self_Inverse3?.Id
},

{
(typeof(Level4), "OneToOne_Optional_PK_Inverse4Id"),
e => l4s.SingleOrDefault(l => l.Id == ((Level4)e)?.Id)?.OneToOne_Optional_PK_Inverse4?.Id
},
{
(typeof(Level4), "OneToMany_Required_Inverse4Id"),
e => l4s.SingleOrDefault(l => l.Id == ((Level4)e)?.Id)?.OneToMany_Required_Inverse4?.Id
},
{
(typeof(Level4), "OneToMany_Optional_Inverse4Id"),
e => l4s.SingleOrDefault(l => l.Id == ((Level4)e)?.Id)?.OneToMany_Optional_Inverse4?.Id
},
{
(typeof(Level4), "OneToOne_Optional_Self4Id"),
e => l4s.SingleOrDefault(l => l.Id == ((Level4)e)?.Id)?.OneToOne_Optional_Self4?.Id
},
{
(typeof(Level4), "OneToMany_Required_Self_Inverse4Id"),
e => l4s.SingleOrDefault(l => l.Id == ((Level4)e)?.Id)?.OneToMany_Required_Self_Inverse4?.Id
},
{
(typeof(Level4), "OneToMany_Optional_Self_Inverse4Id"),
e => l4s.SingleOrDefault(l => l.Id == ((Level4)e)?.Id)?.OneToMany_Optional_Self_Inverse4?.Id
},

{
(typeof(InheritanceBase1), "InheritanceBase2Id"),
e => ((InheritanceBase1)e)?.Id == 1 ? 1 : null
},
{
(typeof(InheritanceBase1), "InheritanceBase2Id1"),
e => ((InheritanceBase1)e)?.Id == 1 ? null : 1
},

{
(typeof(InheritanceBase2), "InheritanceLeaf2Id"),
e => ((InheritanceBase2)e)?.Id == 1 ? 1 : null
},

{
(typeof(InheritanceLeaf1), "DifferentTypeReference_InheritanceDerived1Id"),
e =>
{
switch (((InheritanceLeaf1)e)?.Id)
{
case 1: return 1;
case 2: return 2;
default: return null;
}
}
},
{
(typeof(InheritanceLeaf1), "InheritanceDerived1Id"),
e =>
{
switch (((InheritanceLeaf1)e)?.Id)
{
case 1: return 1;
case 2: return 2;
case 3: return 2;
default: return null;
}
}
},
{
(typeof(InheritanceLeaf1), "InheritanceDerived1Id1"),
e => ((InheritanceLeaf1)e)?.Id == 1 ? 1 : null
},
{
(typeof(InheritanceLeaf1), "InheritanceDerived2Id"),
e =>
{
switch (((InheritanceLeaf1)e)?.Id)
{
case 2: return 3;
case 3: return 3;
default: return null;
}
}
},
{
(typeof(InheritanceLeaf1), "SameTypeReference_InheritanceDerived1Id"),
e =>
{
switch (((InheritanceLeaf1)e)?.Id)
{
case 1: return 1;
case 2: return 2;
default: return null;
}
}
},
{
(typeof(InheritanceLeaf1), "SameTypeReference_InheritanceDerived2Id"),
e => ((InheritanceLeaf1)e)?.Id == 3 ? 3 : null
},

{
(typeof(InheritanceLeaf2), "DifferentTypeReference_InheritanceDerived2Id"),
e => ((InheritanceLeaf2)e)?.Id == 1 ? 3 : null
},
{
(typeof(InheritanceLeaf2), "InheritanceDerived2Id"),
e => ((InheritanceLeaf2)e)?.Id == 1 ? 3 : null
},
};
}
public IReadOnlyDictionary<Type, object> GetEntitySorters()
=> new Dictionary<Type, Func<object, object>>
{
Expand Down Expand Up @@ -356,6 +549,16 @@ public override IQueryable<TEntity> Set<TEntity>()
return (IQueryable<TEntity>)InheritanceBaseTwos.AsQueryable();
}

if (typeof(TEntity) == typeof(InheritanceLeaf1))
{
return (IQueryable<TEntity>)InheritanceLeafOnes.AsQueryable();
}

if (typeof(TEntity) == typeof(InheritanceLeaf2))
{
return (IQueryable<TEntity>)InheritanceLeafTwos.AsQueryable();
}

throw new InvalidOperationException("Invalid entity type: " + typeof(TEntity));
}
}
Expand Down
Loading