Skip to content

Commit

Permalink
Fix #11803 - QueryFilters: query filters are not applied recursively
Browse files Browse the repository at this point in the history
- Visit generated subqueries/predicates to ensure nested filters get applied.
  • Loading branch information
anpete committed May 3, 2018
1 parent 77006d1 commit 4a35f9d
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public virtual void DbContext_property_parameter_does_not_clash_with_closure_par
}

[Fact]
public virtual void DbContext_field_is_parametrized()
public virtual void DbContext_field_is_parameterized()
{
using (var context = CreateContext())
{
Expand All @@ -47,7 +47,7 @@ public virtual void DbContext_field_is_parametrized()
}

[Fact]
public virtual void DbContext_property_is_parametrized()
public virtual void DbContext_property_is_parameterized()
{
using (var context = CreateContext())
{
Expand All @@ -62,7 +62,7 @@ public virtual void DbContext_property_is_parametrized()
}

[Fact]
public virtual void DbContext_method_call_is_parametrized()
public virtual void DbContext_method_call_is_parameterized()
{
using (var context = CreateContext())
{
Expand All @@ -72,7 +72,7 @@ public virtual void DbContext_method_call_is_parametrized()
}

[Fact]
public virtual void DbContext_list_is_parametrized()
public virtual void DbContext_list_is_parameterized()
{
using (var context = CreateContext())
{
Expand All @@ -95,7 +95,7 @@ public virtual void DbContext_list_is_parametrized()
}

[Fact]
public virtual void DbContext_property_chain_is_parametrized()
public virtual void DbContext_property_chain_is_parameterized()
{
using (var context = CreateContext())
{
Expand All @@ -113,7 +113,7 @@ public virtual void DbContext_property_chain_is_parametrized()
}

[Fact]
public virtual void DbContext_property_method_call_is_parametrized()
public virtual void DbContext_property_method_call_is_parameterized()
{
using (var context = CreateContext())
{
Expand All @@ -127,7 +127,7 @@ public virtual void DbContext_property_method_call_is_parametrized()
}

[Fact]
public virtual void DbContext_method_call_chain_is_parametrized()
public virtual void DbContext_method_call_chain_is_parameterized()
{
using (var context = CreateContext())
{
Expand All @@ -137,7 +137,7 @@ public virtual void DbContext_method_call_chain_is_parametrized()
}

[Fact]
public virtual void DbContext_complex_expression_is_parametrized()
public virtual void DbContext_complex_expression_is_parameterized()
{
using (var context = CreateContext())
{
Expand Down Expand Up @@ -172,7 +172,7 @@ public virtual void DbContext_property_based_filter_does_not_short_circuit()
}

[Fact]
public virtual void EntityTypeConfiguration_DbContext_field_is_parametrized()
public virtual void EntityTypeConfiguration_DbContext_field_is_parameterized()
{
using (var context = CreateContext())
{
Expand All @@ -187,7 +187,7 @@ public virtual void EntityTypeConfiguration_DbContext_field_is_parametrized()
}

[Fact]
public virtual void EntityTypeConfiguration_DbContext_property_is_parametrized()
public virtual void EntityTypeConfiguration_DbContext_property_is_parameterized()
{
using (var context = CreateContext())
{
Expand All @@ -202,7 +202,7 @@ public virtual void EntityTypeConfiguration_DbContext_property_is_parametrized()
}

[Fact]
public virtual void EntityTypeConfiguration_DbContext_method_call_is_parametrized()
public virtual void EntityTypeConfiguration_DbContext_method_call_is_parameterized()
{
using (var context = CreateContext())
{
Expand All @@ -212,7 +212,7 @@ public virtual void EntityTypeConfiguration_DbContext_method_call_is_parametrize
}

[Fact]
public virtual void EntityTypeConfiguration_DbContext_property_chain_is_parametrized()
public virtual void EntityTypeConfiguration_DbContext_property_chain_is_parameterized()
{
using (var context = CreateContext())
{
Expand All @@ -230,7 +230,7 @@ public virtual void EntityTypeConfiguration_DbContext_property_chain_is_parametr
}

[Fact]
public virtual void Local_method_DbContext_field_is_parametrized()
public virtual void Local_method_DbContext_field_is_parameterized()
{
using (var context = CreateContext())
{
Expand All @@ -245,7 +245,7 @@ public virtual void Local_method_DbContext_field_is_parametrized()
}

[Fact]
public virtual void Local_static_method_DbContext_property_is_parametrized()
public virtual void Local_static_method_DbContext_property_is_parameterized()
{
using (var context = CreateContext())
{
Expand All @@ -260,7 +260,7 @@ public virtual void Local_static_method_DbContext_property_is_parametrized()
}

[Fact]
public virtual void Remote_method_DbContext_property_method_call_is_parametrized()
public virtual void Remote_method_DbContext_property_method_call_is_parameterized()
{
using (var context = CreateContext())
{
Expand All @@ -274,7 +274,7 @@ public virtual void Remote_method_DbContext_property_method_call_is_parametrized
}

[Fact]
public virtual void Extension_method_DbContext_field_is_parametrized()
public virtual void Extension_method_DbContext_field_is_parameterized()
{
using (var context = CreateContext())
{
Expand All @@ -289,7 +289,7 @@ public virtual void Extension_method_DbContext_field_is_parametrized()
}

[Fact]
public virtual void Extension_method_DbContext_property_chain_is_parametrized()
public virtual void Extension_method_DbContext_property_chain_is_parameterized()
{
using (var context = CreateContext())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var parameterizedQuery
parameterize: false,
generateContextAccessors: true);

var subQueryModel = _queryModelGenerator.ParseQuery(parameterizedQuery);
var subQueryModel = _queryModelGenerator.ParseQuery(Visit(parameterizedQuery));

newExpression = new SubQueryExpression(subQueryModel);
}
Expand All @@ -143,7 +143,7 @@ var predicateExpression
.Replace(
oldParameterExpression,
newParameterExpression,
parameterizedFilter.Body);
Visit(parameterizedFilter.Body));

var whereExpression
= Expression.Call(
Expand Down
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 Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.EntityFrameworkCore.Query
Expand All @@ -15,6 +16,18 @@ public QueryFilterFuncletizationInMemoryTest(
{
}

[Fact(Skip = "#11879")]
public override void Using_DbSet_in_filter_works()
{
base.Using_DbSet_in_filter_works();
}

[Fact(Skip = "#11879")]
public override void Using_Context_set_method_in_filter_works()
{
base.Using_Context_set_method_in_filter_works();
}

public class QueryFilterFuncletizationInMemoryFixture : QueryFilterFuncletizationFixtureBase
{
protected override ITestStoreFactory TestStoreFactory => InMemoryTestStoreFactory.Instance;
Expand Down
82 changes: 82 additions & 0 deletions test/EFCore.SqlServer.FunctionalTests/Query/QueryBugsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3440,6 +3440,88 @@ public class MaumarEntity11818

#endregion

#region Bug11803

[Fact]
public virtual void Query_filter_with_db_set_should_not_block_other_filters()
{
using (CreateDatabase11803())
{
using (var context = new MyContext11803(_options))
{
context.Factions.ToList();

AssertSql(
@"SELECT [f].[Id], [f].[Name]
FROM [Factions] AS [f]
WHERE EXISTS (
SELECT 1
FROM [Leaders] AS [l]
WHERE ([l].[Name] LIKE N'Bran' + N'%' AND (LEFT([l].[Name], LEN(N'Bran')) = N'Bran')) AND ([l].[Name] = N'Crach an Craite'))");
}
}
}

private SqlServerTestStore CreateDatabase11803()
{
return CreateTestStore(
() => new MyContext11803(_options),
context =>
{
var f1 = new Faction { Name = "Skeliege" };
var f2 = new Faction { Name = "Monsters" };
var f3 = new Faction { Name = "Nilfgaard" };
var f4 = new Faction { Name = "Northern Realms" };
var f5 = new Faction { Name = "Scioia'tael" };

var l11 = new Leader { Faction = f1, Name = "Bran Tuirseach" };
var l12 = new Leader { Faction = f1, Name = "Crach an Craite" };
var l13 = new Leader { Faction = f1, Name = "Eist Tuirseach" };
var l14 = new Leader { Faction = f1, Name = "Harald the Cripple" };

context.Factions.AddRange(f1, f2, f3, f4, f5);
context.Leaders.AddRange(l11, l12, l13, l14);

context.SaveChanges();

ClearLog();
});
}

public class MyContext11803 : DbContext
{
public DbSet<Faction> Factions { get; set; }
public DbSet<Leader> Leaders { get; set; }

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

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Leader>().HasQueryFilter(l => l.Name.StartsWith("Bran")); // this one is ignored
modelBuilder.Entity<Faction>().HasQueryFilter(f => Leaders.Any(l => l.Name == "Crach an Craite"));
}
}

public class Faction
{
public int Id { get; set; }
public string Name { get; set; }

public List<Leader> Leaders { get; set; }
}

public class Leader
{
public int Id { get; set; }
public string Name { get; set; }
public Faction Faction { get; set; }
}

#endregion

private DbContextOptions _options;

private SqlServerTestStore CreateTestStore<TContext>(
Expand Down
Loading

0 comments on commit 4a35f9d

Please sign in to comment.