Skip to content

Commit 472bad0

Browse files
fredericDelaportehazzik
authored andcommitted
NH-3919 - Cease cutting milliseconds.
* NHibernate DateTime type does not cuts milliseconds anymore * Add new types which cut milliseconds * Refactor everything together
1 parent 1464f48 commit 472bad0

40 files changed

+921
-799
lines changed

src/NHibernate.Test/Async/Legacy/CriteriaTest.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ public async Task SimpleSelectTestAsync()
4040
long simple1Key = 15;
4141
Simple simple1 = new Simple();
4242
simple1.Address = "Street 12";
43-
simple1.Date = DateTime.Now;
43+
simple1.Date = RoundForDialect(DateTime.Now);
4444
simple1.Name = "For Criteria Test";
4545
simple1.Count = 16;
4646

4747
long notSimple1Key = 17;
4848
Simple notSimple1 = new Simple();
4949
notSimple1.Address = "Street 123";
50-
notSimple1.Date = DateTime.Now;
50+
notSimple1.Date = RoundForDialect(DateTime.Now);
5151
notSimple1.Name = "Don't be found";
5252
notSimple1.Count = 18;
5353

@@ -62,19 +62,19 @@ public async Task SimpleSelectTestAsync()
6262
using (ISession s2 = OpenSession())
6363
using (ITransaction t2 = s2.BeginTransaction())
6464
{
65-
IList results2 = await (s2.CreateCriteria(typeof(Simple))
66-
.Add(Expression.Eq("Address", "Street 12"))
67-
.ListAsync());
65+
var results2 = await (s2.CreateCriteria<Simple>()
66+
.Add(Restrictions.Eq("Address", "Street 12"))
67+
.ListAsync<Simple>());
6868

69-
Assert.AreEqual(1, results2.Count);
69+
Assert.That(results2.Count, Is.EqualTo(1), "Unexpected result count");
7070

71-
Simple simple2 = (Simple) results2[0];
71+
var simple2 = results2[0];
7272

73-
Assert.IsNotNull(simple2, "Unable to load object");
74-
Assert.AreEqual(simple1.Count, simple2.Count, "Load failed");
75-
Assert.AreEqual(simple1.Name, simple2.Name, "Load failed");
76-
Assert.AreEqual(simple1.Address, simple2.Address, "Load failed");
77-
Assert.AreEqual(simple1.Date.ToString(), simple2.Date.ToString(), "Load failed");
73+
Assert.That(simple2, Is.Not.Null, "Unable to load object");
74+
Assert.That(simple2.Count, Is.EqualTo(simple1.Count), "Unexpected Count property value");
75+
Assert.That(simple2.Name, Is.EqualTo(simple1.Name), "Unexpected name");
76+
Assert.That(simple2.Address, Is.EqualTo(simple1.Address), "Unexpected address");
77+
Assert.That(simple2.Date, Is.EqualTo(simple1.Date), "Unexpected date");
7878

7979
await (s2.DeleteAsync("from Simple"));
8080

@@ -181,4 +181,4 @@ public async Task CriteriaLeftOuterJoinAsync()
181181
}
182182

183183
}
184-
}
184+
}

src/NHibernate.Test/Async/NHSpecificTest/NH3818/Fixture.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,19 @@
1010

1111
using System;
1212
using System.Linq;
13-
using System.Linq.Dynamic;
14-
using NHibernate.Linq;
1513
using NUnit.Framework;
14+
using NHibernate.Linq;
1615

1716
namespace NHibernate.Test.NHSpecificTest.NH3818
1817
{
19-
using System.Threading.Tasks;
20-
[TestFixture]
21-
public class FixtureAsync : BugTestCase
22-
{
23-
public override string BugNumber
24-
{
25-
get { return "NH3818"; }
26-
}
27-
18+
using System.Threading.Tasks;
19+
[TestFixture]
20+
public class FixtureAsync : BugTestCase
21+
{
2822
[Test]
2923
public async Task SelectConditionalValuesTestAsync()
3024
{
25+
var now = RoundForDialect(DateTime.Now);
3126
using (var spy = new SqlLogSpy())
3227
using (var session = OpenSession())
3328
using (session.BeginTransaction())
@@ -37,7 +32,7 @@ public async Task SelectConditionalValuesTestAsync()
3732
var cat = new MyLovelyCat
3833
{
3934
GUID = Guid.NewGuid(),
40-
Birthdate = DateTime.Now.AddDays(-days),
35+
Birthdate = now.AddDays(-days),
4136
Color = "Black",
4237
Name = "Kitty",
4338
Price = 0
@@ -51,31 +46,33 @@ public async Task SelectConditionalValuesTestAsync()
5146
.Select(o => new
5247
{
5348
o.Color,
54-
AliveDays = (int)(DateTime.Now - o.Birthdate).TotalDays,
49+
AliveDays = (now - o.Birthdate).TotalDays,
5550
o.Name,
5651
o.Price,
5752
})
5853
.SingleAsync());
5954

6055
//Console.WriteLine(spy.ToString());
61-
Assert.That(catInfo.AliveDays == days);
56+
// We need a tolerance: we are diffing dates as a timespan instead of counting days boundaries,
57+
// yielding a float. TimeSpan.Days yould always truncate a resulting partial day, so do not use it.
58+
Assert.That(catInfo.AliveDays, Is.EqualTo(days).Within(0.1));
6259

6360
var catInfo2 =
6461
await (session.Query<MyLovelyCat>()
6562
.Select(o => new
6663
{
6764
o.Color,
68-
AliveDays = o.Price > 0 ? (DateTime.Now - o.Birthdate).TotalDays : 0,
65+
AliveDays = o.Price > 0 ? (now - o.Birthdate).TotalDays : 0,
6966
o.Name,
7067
o.Price,
7168
})
7269
.SingleAsync());
7370

7471
//Console.WriteLine(spy.ToString());
75-
Assert.That(catInfo2.AliveDays == 0);
72+
Assert.That(catInfo2.AliveDays, Is.EqualTo(0));
7673

7774
}
7875
}
7976

8077
}
81-
}
78+
}

src/NHibernate.Test/Async/Operations/MergeFixture.cs

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//------------------------------------------------------------------------------
99

1010

11+
using System;
1112
using System.Collections.Generic;
1213
using NHibernate.Criterion;
1314
using NUnit.Framework;
@@ -189,9 +190,21 @@ public async Task MergeDeepTreeAsync()
189190
{
190191
ClearCounts();
191192

192-
var root = new Node {Name = "root"};
193-
var child = new Node {Name = "child"};
194-
var grandchild = new Node {Name = "grandchild"};
193+
var root = new Node
194+
{
195+
Created = RoundForDialect(DateTime.Now),
196+
Name = "root"
197+
};
198+
var child = new Node
199+
{
200+
Created = RoundForDialect(DateTime.Now),
201+
Name = "child"
202+
};
203+
var grandchild = new Node
204+
{
205+
Created = RoundForDialect(DateTime.Now),
206+
Name = "grandchild"
207+
};
195208

196209
using (ISession s = OpenSession())
197210
using (ITransaction tx = s.BeginTransaction())
@@ -207,7 +220,11 @@ public async Task MergeDeepTreeAsync()
207220
ClearCounts();
208221

209222
grandchild.Description = "the grand child";
210-
var grandchild2 = new Node {Name = "grandchild2"};
223+
var grandchild2 = new Node
224+
{
225+
Created = RoundForDialect(DateTime.Now),
226+
Name = "grandchild2"
227+
};
211228
child.AddChild(grandchild2);
212229

213230
using (var s = OpenSession())
@@ -221,8 +238,16 @@ public async Task MergeDeepTreeAsync()
221238
AssertUpdateCount(1);
222239
ClearCounts();
223240

224-
var child2 = new Node {Name = "child2"};
225-
var grandchild3 = new Node {Name = "grandchild3"};
241+
var child2 = new Node
242+
{
243+
Created = RoundForDialect(DateTime.Now),
244+
Name = "child2"
245+
};
246+
var grandchild3 = new Node
247+
{
248+
Created = RoundForDialect(DateTime.Now),
249+
Name = "grandchild3"
250+
};
226251
child2.AddChild(grandchild3);
227252
root.AddChild(child2);
228253

@@ -261,9 +286,9 @@ public async Task MergeDeepTreeWithGeneratedIdAsync()
261286
using (ISession s = OpenSession())
262287
{
263288
ITransaction tx = s.BeginTransaction();
264-
root = new NumberedNode("root");
265-
child = new NumberedNode("child");
266-
grandchild = new NumberedNode("grandchild");
289+
root = new NumberedNode("root", RoundForDialect(DateTime.Now));
290+
child = new NumberedNode("child", RoundForDialect(DateTime.Now));
291+
grandchild = new NumberedNode("grandchild", RoundForDialect(DateTime.Now));
267292
root.AddChild(child);
268293
child.AddChild(grandchild);
269294
root = (NumberedNode) await (s.MergeAsync(root));
@@ -281,7 +306,7 @@ public async Task MergeDeepTreeWithGeneratedIdAsync()
281306
cit.MoveNext();
282307
grandchild = cit.Current;
283308
grandchild.Description = "the grand child";
284-
var grandchild2 = new NumberedNode("grandchild2");
309+
var grandchild2 = new NumberedNode("grandchild2", RoundForDialect(DateTime.Now));
285310
child.AddChild(grandchild2);
286311

287312
using (ISession s = OpenSession())
@@ -297,8 +322,8 @@ public async Task MergeDeepTreeWithGeneratedIdAsync()
297322

298323
await (Sfi.EvictAsync(typeof (NumberedNode)));
299324

300-
var child2 = new NumberedNode("child2");
301-
var grandchild3 = new NumberedNode("grandchild3");
325+
var child2 = new NumberedNode("child2", RoundForDialect(DateTime.Now));
326+
var grandchild3 = new NumberedNode("grandchild3", RoundForDialect(DateTime.Now));
302327
child2.AddChild(grandchild3);
303328
root.AddChild(child2);
304329

@@ -331,7 +356,7 @@ public async Task MergeManagedAsync()
331356
NumberedNode root;
332357
using (ITransaction tx = s.BeginTransaction())
333358
{
334-
root = new NumberedNode("root");
359+
root = new NumberedNode("root", RoundForDialect(DateTime.Now));
335360
await (s.PersistAsync(root));
336361
await (tx.CommitAsync());
337362
}
@@ -340,7 +365,7 @@ public async Task MergeManagedAsync()
340365
NumberedNode mergedChild;
341366
using (var tx = s.BeginTransaction())
342367
{
343-
var child = new NumberedNode("child");
368+
var child = new NumberedNode("child", RoundForDialect(DateTime.Now));
344369
root.AddChild(child);
345370
Assert.That(await (s.MergeAsync(root)), Is.SameAs(root));
346371
IEnumerator<NumberedNode> rit = root.Children.GetEnumerator();
@@ -470,8 +495,16 @@ public async Task MergeTreeAsync()
470495
{
471496
ClearCounts();
472497

473-
var root = new Node {Name = "root"};
474-
var child = new Node {Name = "child"};
498+
var root = new Node
499+
{
500+
Created = RoundForDialect(DateTime.Now),
501+
Name = "root"
502+
};
503+
var child = new Node
504+
{
505+
Created = RoundForDialect(DateTime.Now),
506+
Name = "child"
507+
};
475508
using(ISession s = OpenSession())
476509
using (ITransaction tx = s.BeginTransaction())
477510
{
@@ -486,7 +519,11 @@ public async Task MergeTreeAsync()
486519
root.Description = "The root node";
487520
child.Description = "The child node";
488521

489-
var secondChild = new Node {Name = "second child"};
522+
var secondChild = new Node
523+
{
524+
Created = RoundForDialect(DateTime.Now),
525+
Name = "second child"
526+
};
490527

491528
root.AddChild(secondChild);
492529

@@ -506,8 +543,8 @@ public async Task MergeTreeWithGeneratedIdAsync()
506543
{
507544
ClearCounts();
508545

509-
var root = new NumberedNode("root");
510-
var child = new NumberedNode("child");
546+
var root = new NumberedNode("root", RoundForDialect(DateTime.Now));
547+
var child = new NumberedNode("child", RoundForDialect(DateTime.Now));
511548
using(ISession s = OpenSession())
512549
using (ITransaction tx = s.BeginTransaction())
513550
{
@@ -522,7 +559,7 @@ public async Task MergeTreeWithGeneratedIdAsync()
522559
root.Description = "The root node";
523560
child.Description = "The child node";
524561

525-
var secondChild = new NumberedNode("second child");
562+
var secondChild = new NumberedNode("second child", RoundForDialect(DateTime.Now));
526563

527564
root.AddChild(secondChild);
528565

@@ -540,7 +577,11 @@ public async Task MergeTreeWithGeneratedIdAsync()
540577
[Test]
541578
public async Task NoExtraUpdatesOnMergeAsync()
542579
{
543-
var node = new Node {Name = "test"};
580+
var node = new Node
581+
{
582+
Created = RoundForDialect(DateTime.Now),
583+
Name = "test"
584+
};
544585
using(ISession s = OpenSession())
545586
using (s.BeginTransaction())
546587
{
@@ -673,11 +714,19 @@ public async Task NoExtraUpdatesOnMergeVersionedWithCollectionAsync()
673714
[Test]
674715
public async Task NoExtraUpdatesOnMergeWithCollectionAsync()
675716
{
676-
var parent = new Node {Name = "parent"};
717+
var parent = new Node
718+
{
719+
Created = RoundForDialect(DateTime.Now),
720+
Name = "parent"
721+
};
677722
using(ISession s = OpenSession())
678723
using (s.BeginTransaction())
679724
{
680-
var child = new Node {Name = "child"};
725+
var child = new Node
726+
{
727+
Created = RoundForDialect(DateTime.Now),
728+
Name = "child"
729+
};
681730
parent.Children.Add(child);
682731
child.Parent = parent;
683732
await (s.PersistAsync(parent));
@@ -704,7 +753,12 @@ public async Task NoExtraUpdatesOnMergeWithCollectionAsync()
704753
IEnumerator<Node> it = parent.Children.GetEnumerator();
705754
it.MoveNext();
706755
it.Current.Description = "child's new description";
707-
parent.Children.Add(new Node {Name = "second child"});
756+
parent.Children.Add(
757+
new Node
758+
{
759+
Created = RoundForDialect(DateTime.Now),
760+
Name = "second child"
761+
});
708762
using(var s = OpenSession())
709763
using (s.BeginTransaction())
710764
{

src/NHibernate.Test/Async/TypesTest/DateTime2TypeFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class DateTime2TypeFixtureAsync
2626
[Test]
2727
public async Task NextAsync()
2828
{
29-
DateTimeType type = NHibernateUtil.DateTime2;
29+
var type = NHibernateUtil.DateTime2;
3030
object current = DateTime.Now.AddMilliseconds(-1);
3131
object next = await (type.NextAsync(current, null, CancellationToken.None));
3232

@@ -36,7 +36,7 @@ public async Task NextAsync()
3636
[Test]
3737
public async Task SeedAsync()
3838
{
39-
DateTimeType type = NHibernateUtil.DateTime;
39+
var type = NHibernateUtil.DateTime2;
4040
Assert.IsTrue(await (type.SeedAsync(null, CancellationToken.None)) is DateTime, "seed should be DateTime");
4141
}
4242
}

0 commit comments

Comments
 (0)