Skip to content

Commit 9d4d2c9

Browse files
bahusoidhazzik
authored andcommitted
Support Future for collection filters
Fixes #722
1 parent e313b07 commit 9d4d2c9

File tree

10 files changed

+317
-95
lines changed

10 files changed

+317
-95
lines changed

src/NHibernate.Test/Async/Futures/FutureQueryFixture.cs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
using System.Linq;
1212
using NHibernate.Driver;
13+
using NHibernate.Linq;
1314
using NUnit.Framework;
1415

1516
namespace NHibernate.Test.Futures
@@ -18,9 +19,21 @@ namespace NHibernate.Test.Futures
1819
[TestFixture]
1920
public class FutureQueryFixtureAsync : FutureFixture
2021
{
22+
protected override void OnTearDown()
23+
{
24+
using (var session = OpenSession())
25+
using (var transaction = session.BeginTransaction())
26+
{
27+
session.Delete("from Person");
28+
transaction.Commit();
29+
}
30+
}
31+
2132
[Test]
2233
public async Task DefaultReadOnlyTestAsync()
2334
{
35+
CreatePersons();
36+
2437
//NH-3575
2538
using (var s = Sfi.OpenSession())
2639
{
@@ -125,23 +138,23 @@ public async Task CanExecuteMultipleQueryWithSameParameterNameAsync()
125138
using (var s = Sfi.OpenSession())
126139
{
127140
IgnoreThisTestIfMultipleQueriesArentSupportedByDriver();
128-
141+
129142
var meContainer = s.CreateQuery("from Person p where p.Id = :personId")
130143
.SetParameter("personId", 1)
131144
.FutureValue<Person>();
132-
145+
133146
var possiblefriends = s.CreateQuery("from Person p where p.Id != :personId")
134147
.SetParameter("personId", 2)
135148
.Future<Person>();
136149

137150
using (var logSpy = new SqlLogSpy())
138151
{
139152
var me = await (meContainer.GetValueAsync());
140-
153+
141154
foreach (var person in await (possiblefriends.GetEnumerableAsync()))
142155
{
143156
}
144-
157+
145158
var events = logSpy.Appender.GetEvents();
146159
Assert.AreEqual(1, events.Length);
147160
var wholeLog = logSpy.GetWholeLog();
@@ -172,5 +185,24 @@ public async Task FutureExecutedOnGetEnumerableAsync()
172185
Sfi.Statistics.IsStatisticsEnabled = false;
173186
}
174187
}
188+
189+
//NH-1953 - Future<> doesn't work on CreateFilter
190+
[Test]
191+
public async Task FutureOnFilterAsync()
192+
{
193+
CreatePersons();
194+
195+
using (var s = Sfi.OpenSession())
196+
{
197+
var person = await (s.Query<Person>().Where(n => n.Name == "ParentTwoChildren").FirstOrDefaultAsync());
198+
199+
var f1 = (await (s.CreateFilterAsync(person.Children, "where Age > 30"))).Future<Person>();
200+
var f2 = (await (s.CreateFilterAsync(person.Children, "where Age > 5"))).Future<Person>();
201+
202+
Assert.That(person.Children.Count, Is.EqualTo(2), "invalid test set up");
203+
Assert.That((await (f1.GetEnumerableAsync())).ToList().Count, Is.EqualTo(0), "Invalid filtered results");
204+
Assert.That((await (f2.GetEnumerableAsync())).ToList().Count, Is.EqualTo(1), "Invalid filtered results");
205+
}
206+
}
175207
}
176208
}

src/NHibernate.Test/Async/Futures/LinqFutureFixture.cs

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,21 @@ namespace NHibernate.Test.Futures
2020
[TestFixture]
2121
public class LinqFutureFixtureAsync : FutureFixture
2222
{
23+
protected override void OnTearDown()
24+
{
25+
using (var session = OpenSession())
26+
using (var transaction = session.BeginTransaction())
27+
{
28+
session.Delete("from Person");
29+
transaction.Commit();
30+
}
31+
}
32+
2333
[Test]
2434
public async Task DefaultReadOnlyTestAsync()
2535
{
36+
CreatePersons();
37+
2638
//NH-3575
2739
using (var s = Sfi.OpenSession())
2840
{
@@ -56,13 +68,6 @@ public async Task CoalesceShouldWorkForFuturesAsync()
5668
var person = s.Query<Person>().Where(p => (p.Name ?? "e") == "e").ToFutureValue();
5769
Assert.AreEqual(personId, (await (person.GetValueAsync())).Id);
5870
}
59-
60-
using (ISession s = OpenSession())
61-
using (ITransaction tx = s.BeginTransaction())
62-
{
63-
await (s.DeleteAsync("from Person"));
64-
await (tx.CommitAsync());
65-
}
6671
}
6772

6873
[Test]
@@ -137,13 +142,6 @@ public async Task CanUseSkipAndFetchManyWithToFutureAsync()
137142
Assert.AreEqual(1, events.Length);
138143
}
139144
}
140-
141-
using (ISession s = OpenSession())
142-
using (ITransaction tx = s.BeginTransaction())
143-
{
144-
await (s.DeleteAsync("from Person"));
145-
await (tx.CommitAsync());
146-
}
147145
}
148146

149147
[Test]
@@ -270,13 +268,6 @@ public async Task CanUseFutureFetchQueryAsync()
270268
Assert.AreEqual(1, events.Length);
271269
}
272270
}
273-
274-
using (var s = OpenSession())
275-
using (var tx = s.BeginTransaction())
276-
{
277-
await (s.DeleteAsync("from Person"));
278-
await (tx.CommitAsync());
279-
}
280271
}
281272

282273
[Test]
@@ -360,13 +351,6 @@ public async Task CanCombineSingleFutureValueWithFetchManyAsync()
360351

361352
Assert.AreEqual(personId, (await (meContainer.GetValueAsync())).Id);
362353
}
363-
364-
using (var s = OpenSession())
365-
using (var tx = s.BeginTransaction())
366-
{
367-
await (s.DeleteAsync("from Person"));
368-
await (tx.CommitAsync());
369-
}
370354
}
371355

372356
[Test]
@@ -435,12 +419,6 @@ public async Task UsingManyParametersAndQueries_DoesNotCauseParameterNameCollisi
435419
Assert.That(result.Count,Is.EqualTo(1));
436420
}
437421
}
438-
using (var s = OpenSession())
439-
using (var tx = s.BeginTransaction())
440-
{
441-
await (s.DeleteAsync("from Person"));
442-
await (tx.CommitAsync());
443-
}
444422
}
445423

446424
[Test]
@@ -526,13 +504,6 @@ public async Task FutureCombineCachedAndNonCachedQueriesAsync()
526504

527505
Assert.That(Sfi.Statistics.PrepareStatementCount , Is.EqualTo(0), "Future queries must be retrieved from cache");
528506
}
529-
530-
using (var s = OpenSession())
531-
using (var tx = s.BeginTransaction())
532-
{
533-
await (s.DeleteAsync("from Person"));
534-
await (tx.CommitAsync());
535-
}
536507
}
537508

538509
[Test]
@@ -557,5 +528,22 @@ public async Task FutureAutoFlushAsync()
557528
Assert.That(count, Is.EqualTo(0), "Session wasn't auto flushed.");
558529
}
559530
}
531+
532+
[Test]
533+
public async Task FutureOnQueryableFilterAsync()
534+
{
535+
CreatePersons();
536+
537+
using (var s = Sfi.OpenSession())
538+
{
539+
var person = await (s.Query<Person>().Where(n => n.Name == "ParentTwoChildren").FirstOrDefaultAsync());
540+
var f1 = person.Children.AsQueryable().Where(p => p.Age > 30).ToFuture();
541+
var f2 = person.Children.AsQueryable().Where(p => p.Age > 5).ToFuture();
542+
543+
Assert.That(person.Children.Count, Is.EqualTo(2), "invalid test set up");
544+
Assert.That((await (f1.GetEnumerableAsync())).ToList().Count, Is.EqualTo(0), "Invalid filtered results");
545+
Assert.That((await (f2.GetEnumerableAsync())).ToList().Count, Is.EqualTo(1), "Invalid filtered results");
546+
}
547+
}
560548
}
561549
}

src/NHibernate.Test/Futures/FutureFixture.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,23 @@ protected void IgnoreThisTestIfMultipleQueriesArentSupportedByDriver()
2222
if (driver.SupportsMultipleQueries == false)
2323
Assert.Ignore("Driver {0} does not support multi-queries", driver.GetType().FullName);
2424
}
25+
26+
protected void CreatePersons()
27+
{
28+
using (var session = OpenSession())
29+
using (var transaction = session.BeginTransaction())
30+
{
31+
var p = new Person { Name = "ParentTwoChildren", Age = 40 };
32+
var c1 = new Person { Parent = p, Name = "Child1", Age = 7 };
33+
var c2 = new Person { Parent = p, Name = "Child2", Age = 3 };
34+
p.Children.Add(c1);
35+
p.Children.Add(c2);
36+
37+
session.Save(p);
38+
session.Save(c1);
39+
session.Save(c2);
40+
transaction.Commit();
41+
}
42+
}
2543
}
26-
}
44+
}

src/NHibernate.Test/Futures/FutureQueryFixture.cs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
using System.Linq;
22
using NHibernate.Driver;
3+
using NHibernate.Linq;
34
using NUnit.Framework;
45

56
namespace NHibernate.Test.Futures
67
{
78
[TestFixture]
89
public class FutureQueryFixture : FutureFixture
910
{
11+
protected override void OnTearDown()
12+
{
13+
using (var session = OpenSession())
14+
using (var transaction = session.BeginTransaction())
15+
{
16+
session.Delete("from Person");
17+
transaction.Commit();
18+
}
19+
}
20+
1021
[Test]
1122
public void DefaultReadOnlyTest()
1223
{
24+
CreatePersons();
25+
1326
//NH-3575
1427
using (var s = Sfi.OpenSession())
1528
{
@@ -114,23 +127,23 @@ public void CanExecuteMultipleQueryWithSameParameterName()
114127
using (var s = Sfi.OpenSession())
115128
{
116129
IgnoreThisTestIfMultipleQueriesArentSupportedByDriver();
117-
130+
118131
var meContainer = s.CreateQuery("from Person p where p.Id = :personId")
119132
.SetParameter("personId", 1)
120133
.FutureValue<Person>();
121-
134+
122135
var possiblefriends = s.CreateQuery("from Person p where p.Id != :personId")
123136
.SetParameter("personId", 2)
124137
.Future<Person>();
125138

126139
using (var logSpy = new SqlLogSpy())
127140
{
128141
var me = meContainer.Value;
129-
142+
130143
foreach (var person in possiblefriends.GetEnumerable())
131144
{
132145
}
133-
146+
134147
var events = logSpy.Appender.GetEvents();
135148
Assert.AreEqual(1, events.Length);
136149
var wholeLog = logSpy.GetWholeLog();
@@ -161,5 +174,24 @@ public void FutureExecutedOnGetEnumerable()
161174
Sfi.Statistics.IsStatisticsEnabled = false;
162175
}
163176
}
177+
178+
//NH-1953 - Future<> doesn't work on CreateFilter
179+
[Test]
180+
public void FutureOnFilter()
181+
{
182+
CreatePersons();
183+
184+
using (var s = Sfi.OpenSession())
185+
{
186+
var person = s.Query<Person>().Where(n => n.Name == "ParentTwoChildren").FirstOrDefault();
187+
188+
var f1 = s.CreateFilter(person.Children, "where Age > 30").Future<Person>();
189+
var f2 = s.CreateFilter(person.Children, "where Age > 5").Future<Person>();
190+
191+
Assert.That(person.Children.Count, Is.EqualTo(2), "invalid test set up");
192+
Assert.That(f1.GetEnumerable().ToList().Count, Is.EqualTo(0), "Invalid filtered results");
193+
Assert.That(f2.GetEnumerable().ToList().Count, Is.EqualTo(1), "Invalid filtered results");
194+
}
195+
}
164196
}
165197
}

0 commit comments

Comments
 (0)