Skip to content

Reproduce many-to-many filter not working issue #3680

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
@@ -0,0 +1,104 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using NUnit.Framework;
using System.Linq;

namespace NHibernate.Test.NHSpecificTest.ManyToManyWithFilter
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
private Department _department1;
private Department _department2;
private Employee _employee1;
private Employee _employee2;

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
_department1 = new Department();
_department2 = new Department();

_employee1 = new Employee();
_employee2 = new Employee();

_employee1.Departments.Add(_department1);
_employee2.Departments.Add(_department1);
_employee2.Departments.Add(_department2);

session.Save(_department1);
session.Save(_department2);
session.Save(_employee1);
session.Save(_employee2);

tx.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
session.Delete(_employee1);
session.Delete(_employee2);
session.Delete(_department1);
session.Delete(_department2);

tx.Commit();
}
}

[Theory]
public async Task Querying_Employees_Departments_ManyToMany_With_FilterAsync(bool enableFilter)
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
_department1.DeletedAt = DateTime.UtcNow;
_department2.DeletedAt = DateTime.UtcNow;

await (session.UpdateAsync(_department1));
await (session.UpdateAsync(_department2));

await (tx.CommitAsync());
}

using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
if (enableFilter)
session.EnableFilter("NotDeletedFilter");

var departments = session.Query<Department>();
var employee2 = await (session.GetAsync<Employee>(_employee2.Id));

if (enableFilter)
{
Assert.That(departments, Is.Empty);
Assert.That(employee2.Departments, Is.Empty);
}
else
{
Assert.That(departments.Count, Is.EqualTo(2));
Assert.That(employee2.Departments, Has.Count.EqualTo(2));
}


await (tx.CommitAsync());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace NHibernate.Test.NHSpecificTest.ManyToManyWithFilter
{
public class BaseClass
{
public virtual Guid Id { get; set; }

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.ManyToManyWithFilter
{
public class Department : BaseClass
{
public virtual ISet<Employee> Employees { get; set; } = new HashSet<Employee>();

public virtual DateTime? DeletedAt { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.ManyToManyWithFilter
{
public class Employee : BaseClass
{
public virtual ISet<Department> Departments { get; set; } = new HashSet<Department>();
}
}
93 changes: 93 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/ManyToManyWithFilter/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using NUnit.Framework;
using System.Linq;

namespace NHibernate.Test.NHSpecificTest.ManyToManyWithFilter
{
[TestFixture]
public class Fixture : BugTestCase
{
private Department _department1;
private Department _department2;
private Employee _employee1;
private Employee _employee2;

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
_department1 = new Department();
_department2 = new Department();

_employee1 = new Employee();
_employee2 = new Employee();

_employee1.Departments.Add(_department1);
_employee2.Departments.Add(_department1);
_employee2.Departments.Add(_department2);

session.Save(_department1);
session.Save(_department2);
session.Save(_employee1);
session.Save(_employee2);

tx.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
session.Delete(_employee1);
session.Delete(_employee2);
session.Delete(_department1);
session.Delete(_department2);

tx.Commit();
}
}

[Theory]
public void Querying_Employees_Departments_ManyToMany_With_Filter(bool enableFilter)
{
using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
_department1.DeletedAt = DateTime.UtcNow;
_department2.DeletedAt = DateTime.UtcNow;

session.Update(_department1);
session.Update(_department2);

tx.Commit();
}

using (var session = OpenSession())
using (var tx = session.BeginTransaction())
{
if (enableFilter)
session.EnableFilter("NotDeletedFilter");

var departments = session.Query<Department>();
var employee2 = session.Get<Employee>(_employee2.Id);

if (enableFilter)
{
Assert.That(departments, Is.Empty);
Assert.That(employee2.Departments, Is.Empty);
}
else
{
Assert.That(departments.Count, Is.EqualTo(2));
Assert.That(employee2.Departments, Has.Count.EqualTo(2));
}


tx.Commit();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="NHibernate.Test.NHSpecificTest.ManyToManyWithFilter"
assembly="NHibernate.Test">

<class name="BaseClass">
<id name="Id">
<generator class="guid.comb" />
</id>
<discriminator column="discriminator" type="String" />

<subclass name="Employee" discriminator-value="Emp">
<set name="Departments" table="EmployeeDepartment" cascade="all-delete-orphan" inverse="false">
<key column="EmployeeId"/>
<many-to-many class="Department" column="DepartmentId"/>
</set>
</subclass>

<subclass name="Department" discriminator-value="Dep">
<property name="DeletedAt" column="DeletedAt" type="DateTime"/>
<set name="Employees" table="EmployeeDepartment" cascade="none" inverse="true">
<key column="DepartmentId"/>
<many-to-many class="Employee" column="EmployeeId"/>
</set>
<filter name="NotDeletedFilter" condition="DeletedAt IS NULL"/>
</subclass>
</class>
<filter-def name="NotDeletedFilter"/>

</hibernate-mapping>