Skip to content

Commit

Permalink
Fix Queryable.Contains method for Linq provider (#2674)
Browse files Browse the repository at this point in the history
Fixes #2544
  • Loading branch information
maca88 authored Feb 15, 2021
1 parent 5ea209d commit 7a77430
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 6 deletions.
14 changes: 13 additions & 1 deletion src/NHibernate.Test/Async/Linq/MethodCallTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public async Task CanExecuteAnyWithArgumentsAsync()
Assert.IsFalse(result);
}

[Test]
public async Task CanExecuteContainsAsync()
{
var user = await (db.Users.FirstOrDefaultAsync());
var result = db.Users.Contains(user);
Assert.That(result, Is.True);

user = new User("test", DateTime.Now);
result = db.Users.Contains(user);
Assert.That(result, Is.False);
}

[Test]
public async Task CanExecuteCountWithOrderByArgumentsAsync()
{
Expand Down Expand Up @@ -163,4 +175,4 @@ public async Task CanSelectPropertiesIntoNestedObjectArraysAsync()
Assert.That(nestedNestedObjectArray[1], Is.EqualTo("ayende"));
}
}
}
}
44 changes: 44 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/NH3850/MainFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ public async Task AnyBBaseWithNameAsync()
}
}

[Test]
public async Task ContainsBBaseAsync()
{
using (var session = OpenSession())
{
var item = await (session.Query<DomainClassBExtendedByA>().FirstAsync(dc => dc.Name == SearchName1));
var result = session.Query<DomainClassBExtendedByA>().Contains(item);
Assert.That(result, Is.True);
}
}

// Non-reg case
[Test]
public async Task AnyCBaseAsync()
Expand All @@ -213,6 +224,17 @@ public async Task AnyCBaseWithNameAsync()
}
}

[Test]
public async Task ContainsCBaseAsync()
{
using (var session = OpenSession())
{
var item = await (session.Query<DomainClassCExtendedByD>().FirstAsync(dc => dc.Name == SearchName1));
var result = session.Query<DomainClassCExtendedByD>().Contains(item);
Assert.That(result, Is.True);
}
}

// Non-reg case
[Test]
public async Task AnyEAsync()
Expand All @@ -239,6 +261,17 @@ public async Task AnyEWithNameAsync()
}
}

[Test]
public async Task ContainsEAsync()
{
using (var session = OpenSession())
{
var item = await (session.Query<DomainClassE>().FirstAsync(dc => dc.Name == SearchName1));
var result = session.Query<DomainClassE>().Contains(item);
Assert.That(result, Is.True);
}
}

// Non-reg case
[Test]
public async Task AnyFAsync()
Expand Down Expand Up @@ -291,6 +324,17 @@ public async Task AnyGBaseWithNameAsync()
}
}

[Test]
public async Task ContainsGBaseAsync()
{
using (var session = OpenSession())
{
var item = await (session.Query<DomainClassGExtendedByH>().FirstAsync(dc => dc.Name == SearchName1));
var result = session.Query<DomainClassGExtendedByH>().Contains(item);
Assert.That(result, Is.True);
}
}

// Failing case till NH-3850 is fixed
[Test]
public async Task AnyObjectAsync()
Expand Down
14 changes: 13 additions & 1 deletion src/NHibernate.Test/Linq/MethodCallTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public void CanExecuteAnyWithArguments()
Assert.IsFalse(result);
}

[Test]
public void CanExecuteContains()
{
var user = db.Users.FirstOrDefault();
var result = db.Users.Contains(user);
Assert.That(result, Is.True);

user = new User("test", DateTime.Now);
result = db.Users.Contains(user);
Assert.That(result, Is.False);
}

[Test]
public void CanExecuteCountWithOrderByArguments()
{
Expand Down Expand Up @@ -151,4 +163,4 @@ public void CanSelectPropertiesIntoNestedObjectArrays()
Assert.That(nestedNestedObjectArray[1], Is.EqualTo("ayende"));
}
}
}
}
55 changes: 55 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/NH3850/MainFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ public void AnyBBaseWithName()
}
}

[Test]
public void ContainsBBase()
{
using (var session = OpenSession())
{
var item = session.Query<DomainClassBExtendedByA>().First(dc => dc.Name == SearchName1);
var result = session.Query<DomainClassBExtendedByA>().Contains(item);
Assert.That(result, Is.True);
}
}

// Non-reg case
[Test]
public void AnyCBase()
Expand All @@ -220,6 +231,17 @@ public void AnyCBaseWithName()
}
}

[Test]
public void ContainsCBase()
{
using (var session = OpenSession())
{
var item = session.Query<DomainClassCExtendedByD>().First(dc => dc.Name == SearchName1);
var result = session.Query<DomainClassCExtendedByD>().Contains(item);
Assert.That(result, Is.True);
}
}

// Non-reg case
[Test]
public void AnyE()
Expand All @@ -246,6 +268,17 @@ public void AnyEWithName()
}
}

[Test]
public void ContainsE()
{
using (var session = OpenSession())
{
var item = session.Query<DomainClassE>().First(dc => dc.Name == SearchName1);
var result = session.Query<DomainClassE>().Contains(item);
Assert.That(result, Is.True);
}
}

// Non-reg case
[Test]
public void AnyF()
Expand All @@ -272,6 +305,17 @@ public void AnyFWithName()
}
}

[Test]
public void ContainsF()
{
using (var session = OpenSession())
{
var item = new DomainClassF() {Id = -1};
var result = session.Query<DomainClassF>().Contains(item);
Assert.That(result, Is.False);
}
}

// Non-reg case
[Test]
public void AnyGBase()
Expand All @@ -298,6 +342,17 @@ public void AnyGBaseWithName()
}
}

[Test]
public void ContainsGBase()
{
using (var session = OpenSession())
{
var item = session.Query<DomainClassGExtendedByH>().First(dc => dc.Name == SearchName1);
var result = session.Query<DomainClassGExtendedByH>().Contains(item);
Assert.That(result, Is.True);
}
}

// Failing case till NH-3850 is fixed
[Test]
public void AnyObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ namespace NHibernate.Linq.Visitors.ResultOperatorProcessors
public class ProcessAny : IResultOperatorProcessor<AnyResultOperator>
{
public void Process(AnyResultOperator anyOperator, QueryModelVisitor queryModelVisitor, IntermediateHqlTree tree)
{
Process(tree);
}

internal static void Process(IntermediateHqlTree tree)
{
if (tree.IsRoot)
{
Expand All @@ -24,8 +29,8 @@ public void Process(AnyResultOperator anyOperator, QueryModelVisitor queryModelV
}
else
{
tree.SetRoot(tree.TreeBuilder.Exists((HqlQuery)tree.Root));
tree.SetRoot(tree.TreeBuilder.Exists((HqlQuery) tree.Root));
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using NHibernate.Hql.Ast;
using Remotion.Linq.Clauses.ResultOperators;

Expand Down Expand Up @@ -37,7 +40,7 @@ public void Process(ContainsResultOperator resultOperator, QueryModelVisitor que
tree.AddWhereClause(tree.TreeBuilder.Equality(
tree.TreeBuilder.Ident(GetFromAlias(tree.Root).AstNode.Text),
itemExpression));
tree.SetRoot(tree.TreeBuilder.Exists((HqlQuery)tree.Root));
ProcessAny.Process(tree);
}
else
{
Expand Down

0 comments on commit 7a77430

Please sign in to comment.