-
Notifications
You must be signed in to change notification settings - Fork 930
DateOnly and TimeOnly support #3712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gliljas
wants to merge
3
commits into
nhibernate:master
Choose a base branch
from
gliljas:DateOnly
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,007
−4
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/NHibernate.Test/TypesTest/AbstractTimeOnlyTypeFixture.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #if NET6_0_OR_GREATER | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq.Expressions; | ||
| using NHibernate.Type; | ||
|
|
||
| namespace NHibernate.Test.TypesTest | ||
| { | ||
| public class AbstractTimeOnlyTypeFixture<TType> : GenericTypeFixtureBase<TimeOnly, TType> where TType : IType | ||
| { | ||
| protected override IReadOnlyList<TimeOnly> TestValues => [new(12, 13, 14), new(23, 59, 59), new(0, 0, 0)]; | ||
| protected override IEnumerable<Expression<Func<TimeOnly, object>>> PropertiesToTestWithLinq | ||
| { | ||
| get | ||
| { | ||
| yield return (TimeOnly x) => x.Hour; | ||
| yield return (TimeOnly x) => x.Minute; | ||
| yield return (TimeOnly x) => x.Second; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| #endif |
49 changes: 49 additions & 0 deletions
49
src/NHibernate.Test/TypesTest/AbstractTimeOnlyTypeWithScaleFixture.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #if NET6_0_OR_GREATER | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| using NHibernate.Mapping.ByCode; | ||
| using NHibernate.Type; | ||
|
|
||
| namespace NHibernate.Test.TypesTest | ||
| { | ||
| public abstract class AbstractTimeOnlyTypeWithScaleFixture<TType> : AbstractTimeOnlyTypeFixture<TType> where TType : IType | ||
| { | ||
| private readonly bool _setMaxScale; | ||
| protected AbstractTimeOnlyTypeWithScaleFixture(bool setMaxScale) | ||
| { | ||
| _setMaxScale = setMaxScale; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The resolution used when setMaxScale is true | ||
| /// </summary> | ||
| protected virtual long MaxTimestampResolutionInTicks => Dialect.TimestampResolutionInTicks; | ||
|
|
||
| /// <summary> | ||
| /// Add fractional seconds to the test values when setMaxScale is true | ||
| /// </summary> | ||
| protected override IReadOnlyList<TimeOnly> TestValues => [.. base.TestValues.Select(x => _setMaxScale ? AdjustTestValueWithFractionalSeconds(x) : x)]; | ||
|
|
||
| private TimeOnly AdjustTestValueWithFractionalSeconds(TimeOnly value) | ||
| { | ||
| value = new TimeOnly(value.Hour,value.Minute,value.Second); | ||
| var ticks = value.Ticks + MaxTimestampResolutionInTicks; | ||
| if (ticks + MaxTimestampResolutionInTicks > TimeOnly.MaxValue.Ticks) | ||
| { | ||
| ticks = value.Ticks - MaxTimestampResolutionInTicks; | ||
| } | ||
| return new TimeOnly(ticks); | ||
| } | ||
|
|
||
| protected override void ConfigurePropertyMapping<TPersistentType>(IPropertyMapper propertyMapper) | ||
| { | ||
| if (_setMaxScale) | ||
| { | ||
| propertyMapper.Scale((short) Math.Floor(Math.Log10(TimeSpan.TicksPerSecond / MaxTimestampResolutionInTicks))); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| #endif |
26 changes: 26 additions & 0 deletions
26
src/NHibernate.Test/TypesTest/DateOnlyAsDateTypeFixture.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #if NET6_0_OR_GREATER | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq.Expressions; | ||
| using NHibernate.Type; | ||
|
|
||
| namespace NHibernate.Test.TypesTest | ||
| { | ||
| public class DateOnlyAsDateTypeFixture : GenericTypeFixtureBase<DateOnly, DateOnlyAsDateType> | ||
| { | ||
| protected override IReadOnlyList<DateOnly> TestValues => | ||
| [ | ||
| DateOnly.FromDateTime(Sfi.ConnectionProvider.Driver.MinDate.AddDays(1)), | ||
| DateOnly.FromDateTime(DateTime.Now), | ||
| DateOnly.MaxValue.AddDays(-1) | ||
| ]; | ||
|
|
||
| protected override IList<Expression<Func<DateOnly, object>>> PropertiesToTestWithLinq => | ||
| [ | ||
| (DateOnly x) => x.Year, | ||
| (DateOnly x) => x.Month, | ||
| (DateOnly x) => x.Day | ||
| ]; | ||
| } | ||
| } | ||
| #endif |
279 changes: 279 additions & 0 deletions
279
src/NHibernate.Test/TypesTest/GenericTypeFixtureBase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,279 @@ | ||||||||||||
| using System; | ||||||||||||
| using System.Collections.Generic; | ||||||||||||
| using System.Linq; | ||||||||||||
| using System.Linq.Expressions; | ||||||||||||
| using NHibernate.Cfg; | ||||||||||||
| using NHibernate.Linq; | ||||||||||||
| using NHibernate.Linq.Visitors; | ||||||||||||
| using NHibernate.Mapping.ByCode; | ||||||||||||
| using NHibernate.Type; | ||||||||||||
| using NUnit.Framework; | ||||||||||||
|
|
||||||||||||
| namespace NHibernate.Test.TypesTest | ||||||||||||
| { | ||||||||||||
| /// <summary> | ||||||||||||
| /// Base class for fixtures testing individual types, created to avoid | ||||||||||||
| /// code duplication in derived classes. | ||||||||||||
| /// </summary> | ||||||||||||
| public abstract class GenericTypeFixtureBase<TProperty, TType> : TestCase where TType : IType | ||||||||||||
| { | ||||||||||||
|
|
||||||||||||
| protected override string MappingsAssembly | ||||||||||||
| { | ||||||||||||
| get { return "NHibernate.Test"; } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| protected override string[] Mappings | ||||||||||||
| { | ||||||||||||
| get | ||||||||||||
| { | ||||||||||||
| return new string[] { }; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /// <summary> | ||||||||||||
| /// Creates a set of test values of type <typeparamref name="TProperty"/>. | ||||||||||||
| /// </summary> | ||||||||||||
| /// <returns>The test values</returns> | ||||||||||||
| /// <remarks>If the type is IComparable, make sure that the first value | ||||||||||||
| /// doesn't become invalid when incremented by <see cref="IncrementValue(TProperty)"/></remarks> | ||||||||||||
| protected abstract IReadOnlyList<TProperty> TestValues { get; } | ||||||||||||
|
|
||||||||||||
| /// <summary> | ||||||||||||
| /// Override in order to adjust the value of a property before comparisons. | ||||||||||||
| /// May be necessary when dealing with expected precision losses | ||||||||||||
| /// </summary> | ||||||||||||
| /// <param name="value">The value to adjust</param> | ||||||||||||
| /// <returns>The adjusted value</returns> | ||||||||||||
| protected virtual TProperty AdjustValue(TProperty value) => value; | ||||||||||||
|
|
||||||||||||
| /// <summary> | ||||||||||||
| /// Sub properties of <see cref="TType"/> which should be queryable using LINQ | ||||||||||||
| /// </summary> | ||||||||||||
| protected virtual IEnumerable<Expression<Func<TProperty, object>>> PropertiesToTestWithLinq => Enumerable.Empty<Expression<Func<TProperty, object>>>(); | ||||||||||||
|
|
||||||||||||
| /// <summary> | ||||||||||||
| /// Methods of <see cref="TType"/> which should be queryable using LINQ | ||||||||||||
| /// </summary> | ||||||||||||
| protected virtual IEnumerable<Expression<Func<TProperty, object>>> MethodsToTestWithLinq => Enumerable.Empty<Expression<Func<TProperty, object>>>(); | ||||||||||||
|
|
||||||||||||
| [Test] | ||||||||||||
| public virtual void CanPersist() | ||||||||||||
| { | ||||||||||||
| var testValues = GetAllTestValues(); | ||||||||||||
|
|
||||||||||||
| Dictionary<Guid, TProperty> expectedValues = []; | ||||||||||||
| using (var session = OpenSession()) | ||||||||||||
| using (var trans = session.BeginTransaction()) | ||||||||||||
| { | ||||||||||||
| foreach (var testValue in testValues) | ||||||||||||
| { | ||||||||||||
| var entity = new TestEntity { TestProperty = testValue }; | ||||||||||||
| session.Save(entity); | ||||||||||||
| expectedValues[entity.Id] = testValue; | ||||||||||||
| } | ||||||||||||
| trans.Commit(); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| using (var session = OpenSession()) | ||||||||||||
| using (var trans = session.BeginTransaction()) | ||||||||||||
| { | ||||||||||||
| foreach (var expectedValue in expectedValues) | ||||||||||||
| { | ||||||||||||
| var entity = session.Get<TestEntity>(expectedValue.Key); | ||||||||||||
| Assert.That(entity, Is.Not.Null); | ||||||||||||
| Assert.That(AdjustValue(entity.TestProperty), Is.EqualTo(AdjustValue(expectedValue.Value))); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| [Test] | ||||||||||||
| public void CanQuery() | ||||||||||||
| { | ||||||||||||
| var testValue = GetFirstTestValue(); | ||||||||||||
| Guid id; | ||||||||||||
| using (var session = OpenSession()) | ||||||||||||
| using (var trans = session.BeginTransaction()) | ||||||||||||
| { | ||||||||||||
| var entity = new TestEntity { TestProperty = testValue }; | ||||||||||||
| session.Save(entity); | ||||||||||||
| id = entity.Id; | ||||||||||||
|
|
||||||||||||
| trans.Commit(); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| using (var session = OpenSession()) | ||||||||||||
| using (var trans = session.BeginTransaction()) | ||||||||||||
| { | ||||||||||||
| var param = Expression.Parameter(typeof(TestEntity)); | ||||||||||||
| var prop = Expression.Property(param, nameof(TestEntity.TestProperty)); | ||||||||||||
| var value = Expression.Constant(testValue); | ||||||||||||
| var where = Expression.Lambda<Func<TestEntity, bool>>(Expression.Equal(prop, value), param); | ||||||||||||
| var entity = session.Query<TestEntity>().Single(where); | ||||||||||||
| Assert.That(entity, Is.Not.Null); | ||||||||||||
| Assert.That(entity.Id, Is.EqualTo(id)); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| private TProperty GetFirstTestValue() | ||||||||||||
| { | ||||||||||||
| var testValues = TestValues; | ||||||||||||
| if (testValues.Count == 0) | ||||||||||||
| { | ||||||||||||
| Assert.Ignore("No test values provided"); | ||||||||||||
| } | ||||||||||||
| return testValues[0]; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| private IReadOnlyList<TProperty> GetAllTestValues() | ||||||||||||
| { | ||||||||||||
| var testValues = TestValues; | ||||||||||||
| if (TestValues.Count == 0) | ||||||||||||
| { | ||||||||||||
| Assert.Ignore("No test values provided"); | ||||||||||||
| } | ||||||||||||
| return testValues; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| [Test] | ||||||||||||
| public virtual void CanCompare() | ||||||||||||
| { | ||||||||||||
| if (!typeof(IComparable).IsAssignableFrom(typeof(TProperty))) | ||||||||||||
| { | ||||||||||||
| Assert.Ignore("Not IComparable"); | ||||||||||||
| } | ||||||||||||
| var testValues = GetAllTestValues(); | ||||||||||||
|
|
||||||||||||
| if (testValues.Count < 2) | ||||||||||||
| { | ||||||||||||
| Assert.Fail("At least 2 test values required to test comparison"); | ||||||||||||
| } | ||||||||||||
| var testValue = testValues[0]; | ||||||||||||
| var biggerTestValue = testValues[1]; | ||||||||||||
| if (((IComparable) biggerTestValue).CompareTo(testValue) <= 0) | ||||||||||||
| { | ||||||||||||
| Assert.Fail("The second test value must be greater than the first"); | ||||||||||||
| return; | ||||||||||||
| } | ||||||||||||
| Guid id; | ||||||||||||
| using (var session = OpenSession()) | ||||||||||||
| using (var trans = session.BeginTransaction()) | ||||||||||||
| { | ||||||||||||
| var entity = new TestEntity { TestProperty = testValue }; | ||||||||||||
| session.Save(entity); | ||||||||||||
| id = entity.Id; | ||||||||||||
| entity = new TestEntity { TestProperty = biggerTestValue }; | ||||||||||||
| session.Save(entity); | ||||||||||||
| trans.Commit(); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| using (var session = OpenSession()) | ||||||||||||
| using (var trans = session.BeginTransaction()) | ||||||||||||
| { | ||||||||||||
| var param = Expression.Parameter(typeof(TestEntity)); | ||||||||||||
| var prop = Expression.Property(param, nameof(TestEntity.TestProperty)); | ||||||||||||
| var smallerValue = Expression.Constant(testValue, typeof(TProperty)); | ||||||||||||
| var biggerValue = Expression.Constant(biggerTestValue, typeof(TProperty)); | ||||||||||||
| var smallerWhere = Expression.Lambda<Func<TestEntity, bool>>(Expression.LessThan(prop, biggerValue), param); | ||||||||||||
| var biggerWhere = Expression.Lambda<Func<TestEntity, bool>>(Expression.GreaterThan(prop, smallerValue), param); | ||||||||||||
| var smaller = session.Query<TestEntity>().Single(smallerWhere); | ||||||||||||
| var bigger = session.Query<TestEntity>().Single(biggerWhere); | ||||||||||||
| Assert.That(smaller, Is.Not.Null); | ||||||||||||
| Assert.That(smaller.Id, Is.EqualTo(id)); | ||||||||||||
| Assert.That(bigger, Is.Not.Null); | ||||||||||||
| Assert.That(bigger.Id, Is.Not.EqualTo(id)); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| [Test] | ||||||||||||
| public virtual void CanQueryProperties() | ||||||||||||
| { | ||||||||||||
| if (PropertiesToTestWithLinq?.Any() != true) | ||||||||||||
| { | ||||||||||||
| Assert.Ignore(); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| var testValue = GetFirstTestValue(); | ||||||||||||
| Guid id; | ||||||||||||
| using (var session = OpenSession()) | ||||||||||||
| using (var trans = session.BeginTransaction()) | ||||||||||||
| { | ||||||||||||
| var entity = new TestEntity { TestProperty = testValue }; | ||||||||||||
| session.Save(entity); | ||||||||||||
| id = entity.Id; | ||||||||||||
| trans.Commit(); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| using (var session = OpenSession()) | ||||||||||||
| using (var trans = session.BeginTransaction()) | ||||||||||||
| { | ||||||||||||
| foreach (var property in PropertiesToTestWithLinq) | ||||||||||||
| { | ||||||||||||
| var param = Expression.Parameter(typeof(TestEntity)); | ||||||||||||
| var body = property.Body; | ||||||||||||
| if (body is UnaryExpression unaryExpression) | ||||||||||||
| { | ||||||||||||
| body = unaryExpression.Operand; | ||||||||||||
| } | ||||||||||||
| var member = body as MemberExpression; | ||||||||||||
| if (member is null) | ||||||||||||
| { | ||||||||||||
| Assert.Fail(body + " did not expose a member"); | ||||||||||||
| } | ||||||||||||
| var prop = Expression.Property(param, nameof(TestEntity.TestProperty)); | ||||||||||||
| var value = property.Compile()(testValue); | ||||||||||||
| var where = Expression.Lambda<Func<TestEntity, bool>>(Expression.Equal(body.Replace(property.Parameters[0], prop), Expression.Constant(value)), param); | ||||||||||||
| TestEntity entity = null; | ||||||||||||
| Assert.DoesNotThrow(() => entity = session.Query<TestEntity>().FirstOrDefault(where), "Unable to query property " + member.Member.Name); | ||||||||||||
gliljas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
| Assert.That(entity, Is.Not.Null, "Unable to query property " + member.Member.Name); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| protected override void AddMappings(Configuration configuration) | ||||||||||||
| { | ||||||||||||
| var mapper = new ModelMapper(); | ||||||||||||
|
|
||||||||||||
| mapper.Class<TestEntity>(m => | ||||||||||||
| { | ||||||||||||
| m.Table("TestEntity"); | ||||||||||||
| m.EntityName("TestEntity"); | ||||||||||||
| m.Id(p => p.Id, p => p.Generator(Generators.Guid)); | ||||||||||||
| m.Property(p => p.TestProperty, | ||||||||||||
| p => | ||||||||||||
| { | ||||||||||||
| p.Type<TType>(); | ||||||||||||
| ConfigurePropertyMapping<TType>(p); | ||||||||||||
| } | ||||||||||||
| ); | ||||||||||||
| }); | ||||||||||||
|
|
||||||||||||
| var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities(); | ||||||||||||
| configuration.AddMapping(mapping); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| protected virtual void ConfigurePropertyMapping<TPersistentType>(IPropertyMapper propertyMapper) { } | ||||||||||||
|
|
||||||||||||
| protected override void OnTearDown() | ||||||||||||
| { | ||||||||||||
| base.OnTearDown(); | ||||||||||||
|
|
||||||||||||
| using var s = OpenSession(); | ||||||||||||
| using var t = s.BeginTransaction(); | ||||||||||||
| s.Query<TestEntity>().Delete(); | ||||||||||||
| t.Commit(); | ||||||||||||
| } | ||||||||||||
| public class TestEntity | ||||||||||||
|
Comment on lines
+266
to
+267
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: missing blank line.
Suggested change
|
||||||||||||
| { | ||||||||||||
| public virtual Guid Id { get; set; } | ||||||||||||
| public virtual TProperty TestProperty { get; set; } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| public class TypeConfiguration | ||||||||||||
| { | ||||||||||||
| public object Parameters { get; set; } | ||||||||||||
| public short? Scale { get; set; } | ||||||||||||
| public short? Precision { get; set; } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: undue blank line.