Skip to content

Commit 4a21b51

Browse files
author
mika
committed
TestCase added which fails due to a wrongly created filter condition for Collection-Filters
1 parent 86d2930 commit 4a21b51

File tree

5 files changed

+199
-85
lines changed

5 files changed

+199
-85
lines changed

src/NHibernate.Test/Async/SubclassFilterTest/JoinedSubclassFilterTest.cs

Lines changed: 80 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
//------------------------------------------------------------------------------
99

1010

11-
using System;
1211
using System.Collections;
1312
using NUnit.Framework;
1413
using System.Linq;
14+
using NHibernate.Linq;
1515

1616
namespace NHibernate.Test.SubclassFilterTest
1717
{
@@ -25,10 +25,7 @@ protected override string[] Mappings
2525
get { return new string[] {"SubclassFilterTest.joined-subclass.hbm.xml"}; }
2626
}
2727

28-
protected override string MappingsAssembly
29-
{
30-
get { return "NHibernate.Test"; }
31-
}
28+
protected override string MappingsAssembly => "NHibernate.Test";
3229

3330
[Test]
3431
public async Task FiltersWithSubclassAsync()
@@ -103,48 +100,92 @@ public async Task FiltersWithSubclassAsync()
103100
await (s.DeleteAsync("from Customer c where c.ContactOwner is not null"));
104101
await (s.DeleteAsync("from Employee e where e.Manager is not null"));
105102
await (s.DeleteAsync("from Person"));
103+
await (s.DeleteAsync("from Car"));
106104
await (t.CommitAsync());
107105
s.Close();
108106
}
109-
110-
private static async Task PrepareTestDataAsync(ISession s, CancellationToken cancellationToken = default(CancellationToken))
107+
108+
109+
[Test(Description = "Tests the joined subclass collection filter of a single table with a collection mapping " +
110+
"on the parent class.")]
111+
public async Task FilterCollectionJoinedSubclassAsync()
111112
{
112-
Employee john = new Employee("John Doe");
113-
john.Company = ("JBoss");
114-
john.Department = ("hr");
115-
john.Title = ("hr guru");
116-
john.Region = ("US");
117-
118-
Employee polli = new Employee("Polli Wog");
119-
polli.Company = ("JBoss");
120-
polli.Department = ("hr");
121-
polli.Title = ("hr novice");
122-
polli.Region = ("US");
123-
polli.Manager = (john);
124-
john.Minions.Add(polli);
113+
using(ISession session = OpenSession())
114+
using(ITransaction t = session.BeginTransaction())
115+
{
116+
await (PrepareTestDataAsync(session));
125117

126-
Employee suzie = new Employee("Suzie Q");
127-
suzie.Company = ("JBoss");
128-
suzie.Department = ("hr");
129-
suzie.Title = ("hr novice");
130-
suzie.Region = ("EMEA");
131-
suzie.Manager = (john);
132-
john.Minions.Add(suzie);
118+
// sets the filter
119+
session.EnableFilter("region").SetParameter("userRegion", "US");
133120

134-
Customer cust = new Customer("John Q Public");
135-
cust.Company = ("Acme");
136-
cust.Region = ("US");
137-
cust.ContactOwner = (john);
121+
var result = await (session.Query<Car>()
122+
.Where(c => c.Drivers.Any())
123+
.ToListAsync());
124+
125+
Assert.AreEqual(1, result.Count);
138126

139-
Person ups = new Person("UPS guy");
140-
ups.Company = ("UPS");
141-
ups.Region = ("US");
127+
await (t.RollbackAsync());
128+
session.Close();
129+
}
130+
}
131+
132+
133+
private static async Task PrepareTestDataAsync(ISession session, CancellationToken cancellationToken = default(CancellationToken))
134+
{
135+
Car sharedCar1 = new Car { LicensePlate = "1234" };
136+
Car sharedCar2 = new Car { LicensePlate = "5678" };
137+
138+
Employee john = new Employee("John Doe")
139+
{
140+
Company = "JBoss",
141+
Department = "hr",
142+
Title = "hr guru",
143+
Region = "US",
144+
SharedCar = sharedCar1
145+
};
146+
147+
Employee polli = new Employee("Polli Wog")
148+
{
149+
Company = "JBoss",
150+
Department = "hr",
151+
Title = "hr novice",
152+
Region = "US",
153+
Manager = john,
154+
SharedCar = sharedCar1
155+
};
156+
john.Minions.Add(polli);
142157

143-
await (s.SaveAsync(john, cancellationToken));
144-
await (s.SaveAsync(cust, cancellationToken));
145-
await (s.SaveAsync(ups, cancellationToken));
158+
Employee suzie = new Employee("Suzie Q")
159+
{
160+
Company = "JBoss",
161+
Department = "hr",
162+
Title = "hr novice",
163+
Region = "EMEA",
164+
Manager = john,
165+
SharedCar = sharedCar2
166+
};
167+
john.Minions.Add(suzie);
146168

147-
await (s.FlushAsync(cancellationToken));
169+
Customer cust = new Customer("John Q Public")
170+
{
171+
Company = "Acme",
172+
Region = "US",
173+
ContactOwner = john
174+
};
175+
176+
Person ups = new Person("UPS guy")
177+
{
178+
Company = "UPS",
179+
Region = "US"
180+
};
181+
182+
await (session.SaveAsync(sharedCar1, cancellationToken));
183+
await (session.SaveAsync(sharedCar2, cancellationToken));
184+
await (session.SaveAsync(john, cancellationToken));
185+
await (session.SaveAsync(cust, cancellationToken));
186+
await (session.SaveAsync(ups, cancellationToken));
187+
188+
await (session.FlushAsync(cancellationToken));
148189
}
149190
}
150-
}
191+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Test.SubclassFilterTest
4+
{
5+
public class Car
6+
{
7+
public virtual int Id { get; set; }
8+
9+
public virtual string LicensePlate { get; set; }
10+
11+
public virtual IList<Employee> Drivers { get; set; }
12+
}
13+
}

src/NHibernate.Test/SubclassFilterTest/Employee.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32

43
namespace NHibernate.Test.SubclassFilterTest
@@ -41,5 +40,7 @@ public virtual ISet<Employee> Minions
4140
get { return minions; }
4241
set { minions = value; }
4342
}
43+
44+
public virtual Car SharedCar { get; set; }
4445
}
45-
}
46+
}

src/NHibernate.Test/SubclassFilterTest/JoinedSubclassFilterTest.cs

Lines changed: 79 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections;
32
using NUnit.Framework;
43
using System.Linq;
@@ -13,10 +12,7 @@ protected override string[] Mappings
1312
get { return new string[] {"SubclassFilterTest.joined-subclass.hbm.xml"}; }
1413
}
1514

16-
protected override string MappingsAssembly
17-
{
18-
get { return "NHibernate.Test"; }
19-
}
15+
protected override string MappingsAssembly => "NHibernate.Test";
2016

2117
[Test]
2218
public void FiltersWithSubclass()
@@ -91,48 +87,92 @@ public void FiltersWithSubclass()
9187
s.Delete("from Customer c where c.ContactOwner is not null");
9288
s.Delete("from Employee e where e.Manager is not null");
9389
s.Delete("from Person");
90+
s.Delete("from Car");
9491
t.Commit();
9592
s.Close();
9693
}
97-
98-
private static void PrepareTestData(ISession s)
94+
95+
96+
[Test(Description = "Tests the joined subclass collection filter of a single table with a collection mapping " +
97+
"on the parent class.")]
98+
public void FilterCollectionJoinedSubclass()
9999
{
100-
Employee john = new Employee("John Doe");
101-
john.Company = ("JBoss");
102-
john.Department = ("hr");
103-
john.Title = ("hr guru");
104-
john.Region = ("US");
105-
106-
Employee polli = new Employee("Polli Wog");
107-
polli.Company = ("JBoss");
108-
polli.Department = ("hr");
109-
polli.Title = ("hr novice");
110-
polli.Region = ("US");
111-
polli.Manager = (john);
112-
john.Minions.Add(polli);
100+
using(ISession session = OpenSession())
101+
using(ITransaction t = session.BeginTransaction())
102+
{
103+
PrepareTestData(session);
113104

114-
Employee suzie = new Employee("Suzie Q");
115-
suzie.Company = ("JBoss");
116-
suzie.Department = ("hr");
117-
suzie.Title = ("hr novice");
118-
suzie.Region = ("EMEA");
119-
suzie.Manager = (john);
120-
john.Minions.Add(suzie);
105+
// sets the filter
106+
session.EnableFilter("region").SetParameter("userRegion", "US");
121107

122-
Customer cust = new Customer("John Q Public");
123-
cust.Company = ("Acme");
124-
cust.Region = ("US");
125-
cust.ContactOwner = (john);
108+
var result = session.Query<Car>()
109+
.Where(c => c.Drivers.Any())
110+
.ToList();
111+
112+
Assert.AreEqual(1, result.Count);
126113

127-
Person ups = new Person("UPS guy");
128-
ups.Company = ("UPS");
129-
ups.Region = ("US");
114+
t.Rollback();
115+
session.Close();
116+
}
117+
}
118+
119+
120+
private static void PrepareTestData(ISession session)
121+
{
122+
Car sharedCar1 = new Car { LicensePlate = "1234" };
123+
Car sharedCar2 = new Car { LicensePlate = "5678" };
124+
125+
Employee john = new Employee("John Doe")
126+
{
127+
Company = "JBoss",
128+
Department = "hr",
129+
Title = "hr guru",
130+
Region = "US",
131+
SharedCar = sharedCar1
132+
};
133+
134+
Employee polli = new Employee("Polli Wog")
135+
{
136+
Company = "JBoss",
137+
Department = "hr",
138+
Title = "hr novice",
139+
Region = "US",
140+
Manager = john,
141+
SharedCar = sharedCar1
142+
};
143+
john.Minions.Add(polli);
130144

131-
s.Save(john);
132-
s.Save(cust);
133-
s.Save(ups);
145+
Employee suzie = new Employee("Suzie Q")
146+
{
147+
Company = "JBoss",
148+
Department = "hr",
149+
Title = "hr novice",
150+
Region = "EMEA",
151+
Manager = john,
152+
SharedCar = sharedCar2
153+
};
154+
john.Minions.Add(suzie);
134155

135-
s.Flush();
156+
Customer cust = new Customer("John Q Public")
157+
{
158+
Company = "Acme",
159+
Region = "US",
160+
ContactOwner = john
161+
};
162+
163+
Person ups = new Person("UPS guy")
164+
{
165+
Company = "UPS",
166+
Region = "US"
167+
};
168+
169+
session.Save(sharedCar1);
170+
session.Save(sharedCar2);
171+
session.Save(john);
172+
session.Save(cust);
173+
session.Save(ups);
174+
175+
session.Flush();
136176
}
137177
}
138-
}
178+
}

src/NHibernate.Test/SubclassFilterTest/joined-subclass.hbm.xml

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?xml version='1.0' encoding='utf-8'?>
2-
<hibernate-mapping
3-
assembly='NHibernate.Test'
4-
namespace='NHibernate.Test.SubclassFilterTest'
5-
xmlns='urn:nhibernate-mapping-2.2'>
2+
<hibernate-mapping assembly='NHibernate.Test'
3+
namespace='NHibernate.Test.SubclassFilterTest'
4+
xmlns='urn:nhibernate-mapping-2.2'>
65
<class name="Person" table="JPerson">
76

87
<id name="Id" column="person_id">
@@ -18,6 +17,7 @@
1817
<property name="Title"/>
1918
<property name="Department" column="dept"/>
2019
<many-to-one name="Manager" column="mgr_id" class="Employee" cascade="none"/>
20+
<many-to-one name="SharedCar"/>
2121
<set name="Minions" inverse="true" lazy="true" cascade="all">
2222
<key column="mgr_id"/>
2323
<one-to-many class="Employee"/>
@@ -33,7 +33,26 @@
3333

3434
</class>
3535

36+
<class name="Car">
37+
38+
<id name="Id" column="car_id" >
39+
<generator class="native"/>
40+
</id>
41+
42+
<property name="LicensePlate"/>
43+
44+
<bag cascade="all-delete-orphan" inverse="true" name="Drivers">
45+
<key>
46+
<column name="SharedCar" />
47+
</key>
48+
<one-to-many class="Employee" />
49+
<filter name="region" condition="Region = :userRegion"/>
50+
</bag>
51+
52+
</class>
53+
3654
<filter-def name="region">
3755
<filter-param name="userRegion" type="string"/>
3856
</filter-def>
39-
</hibernate-mapping>
57+
58+
</hibernate-mapping>

0 commit comments

Comments
 (0)