Skip to content

Commit 8ee63c6

Browse files
authored
Reproduce many-to-many filter not working issue
1 parent c8e1129 commit 8ee63c6

File tree

6 files changed

+259
-0
lines changed

6 files changed

+259
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System;
12+
using NUnit.Framework;
13+
using System.Linq;
14+
15+
namespace NHibernate.Test.NHSpecificTest.ManyToManyWithFilter
16+
{
17+
using System.Threading.Tasks;
18+
[TestFixture]
19+
public class FixtureAsync : BugTestCase
20+
{
21+
private Department _department1;
22+
private Department _department2;
23+
private Employee _employee1;
24+
private Employee _employee2;
25+
26+
protected override void OnSetUp()
27+
{
28+
using (var session = OpenSession())
29+
using (var tx = session.BeginTransaction())
30+
{
31+
_department1 = new Department();
32+
_department2 = new Department();
33+
34+
_employee1 = new Employee();
35+
_employee2 = new Employee();
36+
37+
_employee1.Departments.Add(_department1);
38+
_employee2.Departments.Add(_department1);
39+
_employee2.Departments.Add(_department2);
40+
41+
session.Save(_department1);
42+
session.Save(_department2);
43+
session.Save(_employee1);
44+
session.Save(_employee2);
45+
46+
tx.Commit();
47+
}
48+
}
49+
50+
protected override void OnTearDown()
51+
{
52+
using (var session = OpenSession())
53+
using (var tx = session.BeginTransaction())
54+
{
55+
session.Delete(_employee1);
56+
session.Delete(_employee2);
57+
session.Delete(_department1);
58+
session.Delete(_department2);
59+
60+
tx.Commit();
61+
}
62+
}
63+
64+
[Theory]
65+
public async Task Querying_Employees_Departments_ManyToMany_With_FilterAsync(bool enableFilter)
66+
{
67+
using (var session = OpenSession())
68+
using (var tx = session.BeginTransaction())
69+
{
70+
_department1.DeletedAt = DateTime.UtcNow;
71+
_department2.DeletedAt = DateTime.UtcNow;
72+
73+
await (session.UpdateAsync(_department1));
74+
await (session.UpdateAsync(_department2));
75+
76+
await (tx.CommitAsync());
77+
}
78+
79+
using (var session = OpenSession())
80+
using (var tx = session.BeginTransaction())
81+
{
82+
if (enableFilter)
83+
session.EnableFilter("NotDeletedFilter");
84+
85+
var departments = session.Query<Department>();
86+
var employee2 = await (session.GetAsync<Employee>(_employee2.Id));
87+
88+
if (enableFilter)
89+
{
90+
Assert.That(departments, Is.Empty);
91+
Assert.That(employee2.Departments, Is.Empty);
92+
}
93+
else
94+
{
95+
Assert.That(departments.Count, Is.EqualTo(2));
96+
Assert.That(employee2.Departments, Has.Count.EqualTo(2));
97+
}
98+
99+
100+
await (tx.CommitAsync());
101+
}
102+
}
103+
}
104+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.ManyToManyWithFilter
4+
{
5+
public class BaseClass
6+
{
7+
public virtual Guid Id { get; set; }
8+
9+
}
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.ManyToManyWithFilter
5+
{
6+
public class Department : BaseClass
7+
{
8+
public virtual ISet<Employee> Employees { get; set; } = new HashSet<Employee>();
9+
10+
public virtual DateTime? DeletedAt { get; set; }
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Test.NHSpecificTest.ManyToManyWithFilter
4+
{
5+
public class Employee : BaseClass
6+
{
7+
public virtual ISet<Department> Departments { get; set; } = new HashSet<Department>();
8+
}
9+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using NUnit.Framework;
3+
using System.Linq;
4+
5+
namespace NHibernate.Test.NHSpecificTest.ManyToManyWithFilter
6+
{
7+
[TestFixture]
8+
public class Fixture : BugTestCase
9+
{
10+
private Department _department1;
11+
private Department _department2;
12+
private Employee _employee1;
13+
private Employee _employee2;
14+
15+
protected override void OnSetUp()
16+
{
17+
using (var session = OpenSession())
18+
using (var tx = session.BeginTransaction())
19+
{
20+
_department1 = new Department();
21+
_department2 = new Department();
22+
23+
_employee1 = new Employee();
24+
_employee2 = new Employee();
25+
26+
_employee1.Departments.Add(_department1);
27+
_employee2.Departments.Add(_department1);
28+
_employee2.Departments.Add(_department2);
29+
30+
session.Save(_department1);
31+
session.Save(_department2);
32+
session.Save(_employee1);
33+
session.Save(_employee2);
34+
35+
tx.Commit();
36+
}
37+
}
38+
39+
protected override void OnTearDown()
40+
{
41+
using (var session = OpenSession())
42+
using (var tx = session.BeginTransaction())
43+
{
44+
session.Delete(_employee1);
45+
session.Delete(_employee2);
46+
session.Delete(_department1);
47+
session.Delete(_department2);
48+
49+
tx.Commit();
50+
}
51+
}
52+
53+
[Theory]
54+
public void Querying_Employees_Departments_ManyToMany_With_Filter(bool enableFilter)
55+
{
56+
using (var session = OpenSession())
57+
using (var tx = session.BeginTransaction())
58+
{
59+
_department1.DeletedAt = DateTime.UtcNow;
60+
_department2.DeletedAt = DateTime.UtcNow;
61+
62+
session.Update(_department1);
63+
session.Update(_department2);
64+
65+
tx.Commit();
66+
}
67+
68+
using (var session = OpenSession())
69+
using (var tx = session.BeginTransaction())
70+
{
71+
if (enableFilter)
72+
session.EnableFilter("NotDeletedFilter");
73+
74+
var departments = session.Query<Department>();
75+
var employee2 = session.Get<Employee>(_employee2.Id);
76+
77+
if (enableFilter)
78+
{
79+
Assert.That(departments, Is.Empty);
80+
Assert.That(employee2.Departments, Is.Empty);
81+
}
82+
else
83+
{
84+
Assert.That(departments.Count, Is.EqualTo(2));
85+
Assert.That(employee2.Departments, Has.Count.EqualTo(2));
86+
}
87+
88+
89+
tx.Commit();
90+
}
91+
}
92+
}
93+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
4+
namespace="NHibernate.Test.NHSpecificTest.ManyToManyWithFilter"
5+
assembly="NHibernate.Test">
6+
7+
<class name="BaseClass">
8+
<id name="Id">
9+
<generator class="guid.comb" />
10+
</id>
11+
<discriminator column="discriminator" type="String" />
12+
13+
<subclass name="Employee" discriminator-value="Emp">
14+
<set name="Departments" table="EmployeeDepartment" cascade="all-delete-orphan" inverse="false">
15+
<key column="EmployeeId"/>
16+
<many-to-many class="Department" column="DepartmentId"/>
17+
</set>
18+
</subclass>
19+
20+
<subclass name="Department" discriminator-value="Dep">
21+
<property name="DeletedAt" column="DeletedAt" type="DateTime"/>
22+
<set name="Employees" table="EmployeeDepartment" cascade="none" inverse="true">
23+
<key column="DepartmentId"/>
24+
<many-to-many class="Employee" column="EmployeeId"/>
25+
</set>
26+
<filter name="NotDeletedFilter" condition="DeletedAt IS NULL"/>
27+
</subclass>
28+
</class>
29+
<filter-def name="NotDeletedFilter"/>
30+
31+
</hibernate-mapping>

0 commit comments

Comments
 (0)