Skip to content

Commit c16868d

Browse files
NH-1752 - Cease silently nullifying dates before 1753
Obsolete base date parameterization and set its default value to MinDate
1 parent 6166cb5 commit c16868d

File tree

19 files changed

+181
-65
lines changed

19 files changed

+181
-65
lines changed

src/NHibernate.Test/Async/FilterTest/DynamicFilterTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,4 +773,4 @@ public void Dispose()
773773
}
774774
}
775775
}
776-
}
776+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.Collections.Generic;
12+
using NHibernate.Dialect;
13+
using NHibernate.Type;
14+
using NUnit.Framework;
15+
using System;
16+
17+
namespace NHibernate.Test.TypesTest
18+
{
19+
using System.Threading.Tasks;
20+
using System.Threading;
21+
22+
[TestFixture]
23+
public class DateTypeFixtureAsync : TypeFixtureBase
24+
{
25+
protected override string TypeName
26+
{
27+
get { return "Date"; }
28+
}
29+
30+
[Test]
31+
public Task ReadWriteNormalAsync()
32+
{
33+
try
34+
{
35+
var expected = DateTime.Today;
36+
37+
return ReadWriteAsync(expected);
38+
}
39+
catch (Exception ex)
40+
{
41+
return Task.FromException<object>(ex);
42+
}
43+
}
44+
45+
[Test]
46+
public Task ReadWriteMinAsync()
47+
{
48+
try
49+
{
50+
var expected = Sfi.ConnectionProvider.Driver.MinDate;
51+
52+
return ReadWriteAsync(expected);
53+
}
54+
catch (Exception ex)
55+
{
56+
return Task.FromException<object>(ex);
57+
}
58+
}
59+
60+
private async Task ReadWriteAsync(DateTime expected, CancellationToken cancellationToken = default(CancellationToken))
61+
{
62+
// Add an hour to check it is correctly ignored once read back from db.
63+
var basic = new DateClass { DateValue = expected.AddHours(1) };
64+
object savedId;
65+
using (var s = OpenSession())
66+
{
67+
savedId = await (s.SaveAsync(basic, cancellationToken));
68+
await (s.FlushAsync(cancellationToken));
69+
}
70+
using (var s = OpenSession())
71+
{
72+
basic = await (s.GetAsync<DateClass>(savedId, cancellationToken));
73+
Assert.That(basic.DateValue, Is.EqualTo(expected));
74+
await (s.DeleteAsync(basic, cancellationToken));
75+
await (s.FlushAsync(cancellationToken));
76+
}
77+
}
78+
}
79+
}

src/NHibernate.Test/FilterTest/Category.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public class Category
77
{
88
private long id;
99
private String name;
10-
private DateTime effectiveStartDate;
11-
private DateTime effectiveEndDate;
10+
private DateTime? effectiveStartDate;
11+
private DateTime? effectiveEndDate;
1212
private ISet<Product> products;
1313

1414
public Category()
@@ -39,13 +39,13 @@ public virtual string Name
3939
set { name = value; }
4040
}
4141

42-
public virtual DateTime EffectiveStartDate
42+
public virtual DateTime? EffectiveStartDate
4343
{
4444
get { return effectiveStartDate; }
4545
set { effectiveStartDate = value; }
4646
}
4747

48-
public virtual DateTime EffectiveEndDate
48+
public virtual DateTime? EffectiveEndDate
4949
{
5050
get { return effectiveEndDate; }
5151
set { effectiveEndDate = value; }
@@ -93,4 +93,4 @@ public override int GetHashCode()
9393
return result;
9494
}
9595
}
96-
}
96+
}

src/NHibernate.Test/FilterTest/DynamicFilterTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,4 +747,4 @@ public void Dispose()
747747
}
748748
}
749749
}
750-
}
750+
}

src/NHibernate.Test/FilterTest/Order.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public class Order
77
{
88
private long id;
99
private String region;
10-
private DateTime placementDate;
11-
private DateTime fulfillmentDate;
10+
private DateTime? placementDate;
11+
private DateTime? fulfillmentDate;
1212
private Salesperson salesperson;
1313
private String buyer;
1414
private IList<LineItem> lineItems = new List<LineItem>();
@@ -25,13 +25,13 @@ public virtual string Region
2525
set { region = value; }
2626
}
2727

28-
public virtual DateTime PlacementDate
28+
public virtual DateTime? PlacementDate
2929
{
3030
get { return placementDate; }
3131
set { placementDate = value; }
3232
}
3333

34-
public virtual DateTime FulfillmentDate
34+
public virtual DateTime? FulfillmentDate
3535
{
3636
get { return fulfillmentDate; }
3737
set { fulfillmentDate = value; }
@@ -70,4 +70,4 @@ public virtual void RemoveLineItem(int sequence)
7070
LineItems.RemoveAt(sequence);
7171
}
7272
}
73-
}
73+
}

src/NHibernate.Test/FilterTest/Product.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public class Product
88
private long id;
99
private string name;
1010
private int stockNumber; // int for ease of hashCode() impl
11-
private DateTime effectiveStartDate;
12-
private DateTime effectiveEndDate;
11+
private DateTime? effectiveStartDate;
12+
private DateTime? effectiveEndDate;
1313
private ISet<LineItem> orderLineItems;
1414
private ISet<Category> categories;
1515

@@ -51,13 +51,13 @@ public virtual int StockNumber
5151
set { stockNumber = value; }
5252
}
5353

54-
public virtual DateTime EffectiveStartDate
54+
public virtual DateTime? EffectiveStartDate
5555
{
5656
get { return effectiveStartDate; }
5757
set { effectiveStartDate = value; }
5858
}
5959

60-
public virtual DateTime EffectiveEndDate
60+
public virtual DateTime? EffectiveEndDate
6161
{
6262
get { return effectiveEndDate; }
6363
set { effectiveEndDate = value; }
@@ -85,4 +85,4 @@ public override bool Equals(object obj)
8585
return obj is Product && (((Product) obj).stockNumber == this.stockNumber);
8686
}
8787
}
88-
}
88+
}

src/NHibernate.Test/FilterTest/Salesperson.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class Salesperson
88
private long id;
99
private String name;
1010
private String region;
11-
private DateTime hireDate;
11+
private DateTime? hireDate;
1212
private Department department;
1313
private ISet<Order> orders = new HashSet<Order>();
1414

@@ -30,7 +30,7 @@ public virtual string Region
3030
set { region = value; }
3131
}
3232

33-
public virtual DateTime HireDate
33+
public virtual DateTime? HireDate
3434
{
3535
get { return hireDate; }
3636
set { hireDate = value; }
@@ -48,4 +48,4 @@ public virtual ISet<Order> Orders
4848
set { orders = value; }
4949
}
5050
}
51-
}
51+
}

src/NHibernate.Test/Hql/Ast/Mammal.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ namespace NHibernate.Test.Hql.Ast
55
public class Mammal: Animal
66
{
77
private bool pregnant;
8-
private DateTime birthdate;
8+
private DateTime? birthdate;
99

1010
public virtual bool Pregnant
1111
{
1212
get { return pregnant; }
1313
set { pregnant = value; }
1414
}
1515

16-
public virtual DateTime Birthdate
16+
public virtual DateTime? Birthdate
1717
{
1818
get { return birthdate; }
1919
set { birthdate = value; }
2020
}
2121
}
22-
}
22+
}

src/NHibernate.Test/LinqBulkManipulation/Domain/Mammal.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ namespace NHibernate.Test.LinqBulkManipulation.Domain
55
public class Mammal: Animal
66
{
77
private bool pregnant;
8-
private DateTime birthdate;
8+
private DateTime? birthdate;
99

1010
public virtual bool Pregnant
1111
{
1212
get { return pregnant; }
1313
set { pregnant = value; }
1414
}
1515

16-
public virtual DateTime Birthdate
16+
public virtual DateTime? Birthdate
1717
{
1818
get { return birthdate; }
1919
set { birthdate = value; }
2020
}
2121
}
22-
}
22+
}

src/NHibernate.Test/Stateless/Naturalness.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Human : Animal
1818
{
1919
public virtual string Name { get; set; }
2020
public virtual string NickName { get; set; }
21-
public virtual DateTime Birthdate { get; set; }
21+
public virtual DateTime? Birthdate { get; set; }
2222
}
2323

2424
public class Family<T> where T: Animal
@@ -34,4 +34,4 @@ public virtual ISet<T> Childs
3434
set { childs = value; }
3535
}
3636
}
37-
}
37+
}

src/NHibernate.Test/TypesTest/DateTypeTest.cs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,30 @@ namespace NHibernate.Test.TypesTest
99
[TestFixture]
1010
public class DateTypeTest
1111
{
12-
[Test]
12+
[Test, Obsolete("Testing obsolete SetParameterValues")]
1313
public void WhenNoParameterThenDefaultValueIsBaseDateValue()
1414
{
1515
var dateType = new DateType();
1616
Assert.That(dateType.DefaultValue, Is.EqualTo(DateType.BaseDateValue));
1717
}
1818

19-
[Test]
19+
[Test, Obsolete("Testing obsolete SetParameterValues")]
2020
public void WhenSetParameterThenDefaultValueIsParameterValue()
2121
{
2222
var dateType = new DateType();
23-
dateType.SetParameterValues(new Dictionary<string, string>{{DateType.BaseValueParameterName, "0001/01/01"}});
23+
dateType.SetParameterValues(new Dictionary<string, string> { { DateType.BaseValueParameterName, "0001/01/01" } });
2424
Assert.That(dateType.DefaultValue, Is.EqualTo(DateTime.MinValue));
2525
}
2626

27-
[Test]
27+
[Test, Obsolete("Testing obsolete SetParameterValues")]
2828
public void WhenSetParameterNullThenNotThrow()
2929
{
3030
var dateType = new DateType();
3131
Assert.That(() => dateType.SetParameterValues(null), Throws.Nothing);
3232
}
3333
}
3434

35+
[TestFixture]
3536
public class DateTypeFixture : TypeFixtureBase
3637
{
3738
protected override string TypeName
@@ -53,38 +54,33 @@ public void ShouldBeDateType()
5354
[Test]
5455
public void ReadWriteNormal()
5556
{
56-
var expected = DateTime.Today.Date;
57+
var expected = DateTime.Today;
5758

58-
var basic = new DateClass { DateValue = expected.AddHours(1) };
59-
object savedId;
60-
using (ISession s = OpenSession())
61-
{
62-
savedId = s.Save(basic);
63-
s.Flush();
64-
}
65-
using (ISession s = OpenSession())
66-
{
67-
basic = s.Get<DateClass>(savedId);
68-
Assert.That(basic.DateValue, Is.EqualTo(expected));
69-
s.Delete(basic);
70-
s.Flush();
71-
}
59+
ReadWrite(expected);
7260
}
7361

7462
[Test]
75-
public void ReadWriteBaseValue()
63+
public void ReadWriteMin()
64+
{
65+
var expected = Sfi.ConnectionProvider.Driver.MinDate;
66+
67+
ReadWrite(expected);
68+
}
69+
70+
private void ReadWrite(DateTime expected)
7671
{
77-
var basic = new DateClass { DateValue = new DateTime(1899,1,1) };
72+
// Add an hour to check it is correctly ignored once read back from db.
73+
var basic = new DateClass { DateValue = expected.AddHours(1) };
7874
object savedId;
79-
using (ISession s = OpenSession())
75+
using (var s = OpenSession())
8076
{
81-
savedId = s.Save(basic);
77+
savedId = s.Save(basic);
8278
s.Flush();
8379
}
84-
using (ISession s = OpenSession())
80+
using (var s = OpenSession())
8581
{
8682
basic = s.Get<DateClass>(savedId);
87-
Assert.That(basic.DateValue.HasValue, Is.False);
83+
Assert.That(basic.DateValue, Is.EqualTo(expected));
8884
s.Delete(basic);
8985
s.Flush();
9086
}

src/NHibernate/Driver/DriverBase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,5 +321,8 @@ public DbParameter GenerateOutputParameter(DbCommand command)
321321
public virtual bool SupportsEnlistmentWhenAutoEnlistmentIsDisabled => true;
322322

323323
public virtual bool HasDelayedDistributedTransactionCompletion => false;
324+
325+
/// <inheritdoc />
326+
public virtual DateTime MinDate => DateTime.MinValue;
324327
}
325-
}
328+
}

src/NHibernate/Driver/IDriver.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,10 @@ public interface IDriver
158158
/// crash due to this, because they re-use the connection in the second phase.
159159
/// </remarks>
160160
bool HasDelayedDistributedTransactionCompletion { get; }
161+
162+
/// <summary>
163+
/// The minimal date supplied as a <see cref="DateTime" /> supported by this driver.
164+
/// </summary>
165+
DateTime MinDate { get; }
161166
}
162-
}
167+
}

0 commit comments

Comments
 (0)