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

Adding tests for #15832 - Non-constant property name in EF.Property (and indexer property) #24846

Merged
merged 1 commit into from
Aug 5, 2021
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
Expand Up @@ -2178,6 +2178,26 @@ public override Task Where_equals_method_string_with_ignore_case(bool async)
return AssertTranslationFailed(() => base.Where_equals_method_string_with_ignore_case(async));
}

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

AssertSql(
maumar marked this conversation as resolved.
Show resolved Hide resolved
@"SELECT c
FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND (c[""CustomerID""] = ""ALFKI""))");
}

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

AssertSql(
@"SELECT c
FROM root c
WHERE ((c[""Discriminator""] = ""Customer"") AND (c[""CustomerID""] = ""ALFKI""))");
}

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

Expand Down
20 changes: 20 additions & 0 deletions test/EFCore.Cosmos.FunctionalTests/Query/OwnedQueryCosmosTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,26 @@ public override async Task Projecting_collection_correlated_with_keyless_entity_
AssertSql(" ");
}

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

AssertSql(
@"SELECT c
FROM root c
WHERE (c[""Discriminator""] IN (""OwnedPerson"", ""Branch"", ""LeafB"", ""LeafA"") AND (c[""PersonAddress""][""ZipCode""] = 38654))");
}

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

AssertSql(
@"SELECT c
FROM root c
WHERE (c[""Discriminator""] IN (""OwnedPerson"", ""Branch"", ""LeafB"", ""LeafA"") AND (c[""PersonAddress""][""ZipCode""] = 38654))");
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2487,5 +2487,30 @@ public virtual Task Filter_with_property_compared_to_null_wrapped_in_explicit_co
ss => ss.Set<Customer>().Where(c => (object)c.Region == null),
entryCount: 60);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Filter_with_EF_Property_using_closure_for_property_name(bool async)
{
var id = "CustomerID";

return AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => EF.Property<string>(c, id) == "ALFKI"),
entryCount: 1);
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Filter_with_EF_Property_using_function_for_property_name(bool async)
{
return AssertQuery(
async,
ss => ss.Set<Customer>().Where(c => EF.Property<string>(c, StringMethod("CustomerID")) == "ALFKI"),
entryCount: 1);
}

private string StringMethod(string arg)
=> arg;
}
}
31 changes: 31 additions & 0 deletions test/EFCore.Specification.Tests/Query/OwnedQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,37 @@ public virtual Task Projecting_collection_correlated_with_keyless_entity_after_n
});
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task Filter_on_indexer_using_closure(bool async)
{
var zipCode = "ZipCode";

return AssertQuery(
async,
ss => ss.Set<OwnedPerson>().Where(p => (int)p.PersonAddress[zipCode] == 38654));
maumar marked this conversation as resolved.
Show resolved Hide resolved
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Filter_on_indexer_using_function_argument(bool async)
{
var zipCode = "ZipCode";

Func<bool, string, Task> myFunc = async (b, n) =>
{
using var ctx = CreateContext();

var query = async
? await ctx.Set<OwnedPerson>().Where(p => (int)p.PersonAddress[n] == 38654).ToListAsync()
: ctx.Set<OwnedPerson>().Where(p => (int)p.PersonAddress[n] == 38654).ToList();

Assert.Single(query);
};

await myFunc(async, zipCode);
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2248,6 +2248,26 @@ FROM [Customers] AS [c]
WHERE ([c].[Region] <> N'WA') AND [c].[Region] IS NOT NULL");
}

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

AssertSql(
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE [c].[CustomerID] = N'ALFKI'");
}

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

AssertSql(
@"SELECT [c].[CustomerID], [c].[Address], [c].[City], [c].[CompanyName], [c].[ContactName], [c].[ContactTitle], [c].[Country], [c].[Fax], [c].[Phone], [c].[PostalCode], [c].[Region]
FROM [Customers] AS [c]
WHERE [c].[CustomerID] = N'ALFKI'");
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,38 @@ LEFT JOIN [Planet] AS [p] ON ([b].[Throned_Value] <> [p].[Id]) OR [b].[Throned_V
ORDER BY [f].[Id], [b].[Id], [p].[Id]");
}

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

AssertSql(
@"SELECT [o].[Id], [o].[Discriminator], [o].[Name], [t].[ClientId], [t].[Id], [t].[OrderDate], [t].[OrderClientId], [t].[OrderId], [t].[Id0], [t].[Detail], [o].[PersonAddress_AddressLine], [o].[PersonAddress_PlaceType], [o].[PersonAddress_ZipCode], [o].[PersonAddress_Country_Name], [o].[PersonAddress_Country_PlanetId], [o].[BranchAddress_BranchName], [o].[BranchAddress_PlaceType], [o].[BranchAddress_Country_Name], [o].[BranchAddress_Country_PlanetId], [o].[LeafBAddress_LeafBType], [o].[LeafBAddress_PlaceType], [o].[LeafBAddress_Country_Name], [o].[LeafBAddress_Country_PlanetId], [o].[LeafAAddress_LeafType], [o].[LeafAAddress_PlaceType], [o].[LeafAAddress_Country_Name], [o].[LeafAAddress_Country_PlanetId]
FROM [OwnedPerson] AS [o]
LEFT JOIN (
SELECT [o0].[ClientId], [o0].[Id], [o0].[OrderDate], [o1].[OrderClientId], [o1].[OrderId], [o1].[Id] AS [Id0], [o1].[Detail]
FROM [Order] AS [o0]
LEFT JOIN [OrderDetail] AS [o1] ON ([o0].[ClientId] = [o1].[OrderClientId]) AND ([o0].[Id] = [o1].[OrderId])
) AS [t] ON [o].[Id] = [t].[ClientId]
WHERE [o].[PersonAddress_ZipCode] = 38654
ORDER BY [o].[Id], [t].[ClientId], [t].[Id], [t].[OrderClientId], [t].[OrderId], [t].[Id0]");
}

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

AssertSql(
@"SELECT [o].[Id], [o].[Discriminator], [o].[Name], [t].[ClientId], [t].[Id], [t].[OrderDate], [t].[OrderClientId], [t].[OrderId], [t].[Id0], [t].[Detail], [o].[PersonAddress_AddressLine], [o].[PersonAddress_PlaceType], [o].[PersonAddress_ZipCode], [o].[PersonAddress_Country_Name], [o].[PersonAddress_Country_PlanetId], [o].[BranchAddress_BranchName], [o].[BranchAddress_PlaceType], [o].[BranchAddress_Country_Name], [o].[BranchAddress_Country_PlanetId], [o].[LeafBAddress_LeafBType], [o].[LeafBAddress_PlaceType], [o].[LeafBAddress_Country_Name], [o].[LeafBAddress_Country_PlanetId], [o].[LeafAAddress_LeafType], [o].[LeafAAddress_PlaceType], [o].[LeafAAddress_Country_Name], [o].[LeafAAddress_Country_PlanetId]
FROM [OwnedPerson] AS [o]
LEFT JOIN (
SELECT [o0].[ClientId], [o0].[Id], [o0].[OrderDate], [o1].[OrderClientId], [o1].[OrderId], [o1].[Id] AS [Id0], [o1].[Detail]
FROM [Order] AS [o0]
LEFT JOIN [OrderDetail] AS [o1] ON ([o0].[ClientId] = [o1].[OrderClientId]) AND ([o0].[Id] = [o1].[OrderId])
) AS [t] ON [o].[Id] = [t].[ClientId]
WHERE [o].[PersonAddress_ZipCode] = 38654
ORDER BY [o].[Id], [t].[ClientId], [t].[Id], [t].[OrderClientId], [t].[OrderId], [t].[Id0]");
}

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

Expand Down