-
Notifications
You must be signed in to change notification settings - Fork 256
Translate simple range operators + compatibility support #616
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,23 +16,10 @@ namespace Npgsql.EntityFrameworkCore.PostgreSQL.Query | |
| /// </summary> | ||
| public class RangeQueryNpgsqlTest : IClassFixture<RangeQueryNpgsqlTest.RangeQueryNpgsqlFixture> | ||
| { | ||
| /// <summary> | ||
| /// Provides resources for unit tests. | ||
| /// </summary> | ||
| RangeQueryNpgsqlFixture Fixture { get; } | ||
|
|
||
| /// <summary> | ||
| /// Initializes resources for unit tests. | ||
| /// </summary> | ||
| /// <param name="fixture">The fixture of resources for testing.</param> | ||
| public RangeQueryNpgsqlTest(RangeQueryNpgsqlFixture fixture) | ||
| { | ||
| Fixture = fixture; | ||
| Fixture.TestSqlLoggerFactory.Clear(); | ||
| } | ||
|
|
||
| #region Tests | ||
|
|
||
| #region Operators | ||
|
|
||
| /// <summary> | ||
| /// Tests translation for <see cref="NpgsqlRangeExtensions.Contains{T}(NpgsqlRange{T},NpgsqlRange{T})"/>. | ||
| /// </summary> | ||
|
|
@@ -490,6 +477,10 @@ public void RangeExceptRange(NpgsqlRange<int> range) | |
| } | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region User-defined ranges | ||
|
|
||
| [Fact] | ||
| public void UserDefined() | ||
| { | ||
|
|
@@ -501,7 +492,120 @@ public void UserDefined() | |
| } | ||
| } | ||
|
|
||
| #endregion Tests | ||
| #endregion | ||
|
|
||
| #region Functions | ||
|
|
||
| [Fact] | ||
| public void RangeLowerBound() | ||
| { | ||
| using (RangeContext context = Fixture.CreateContext()) | ||
| { | ||
| try | ||
| { | ||
| var _ = context.RangeTestEntities.Select(x => x.Range.LowerBound).ToArray(); | ||
|
Contributor
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. Out of curiosity, does such variables make sense? As I know, there is no reason to make them since the compiler elides them.
Contributor
Author
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. It keeps Rider/ReSharper from complaining about pure functions going unused, and IIRC So yes, functionally they make no difference, but I would prefer to keep them for testing/debugging purposes.
Contributor
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. Okay, let's leave it for Rider.
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.
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. Actually scratch that, your way is better :) |
||
| } | ||
| catch | ||
| { | ||
| AssertContainsSql("SELECT COALESCE(lower(x.\"Range\"), 0)"); | ||
| } | ||
|
|
||
| AssertContainsSql("SELECT COALESCE(lower(x.\"Range\"), 0)"); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RangeUpperBound() | ||
| { | ||
| using (RangeContext context = Fixture.CreateContext()) | ||
| { | ||
| var _ = context.RangeTestEntities.Select(x => x.Range.UpperBound).ToArray(); | ||
| AssertContainsSql("SELECT COALESCE(upper(x.\"Range\"), 0)"); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RangeIsEmpty() | ||
| { | ||
| using (RangeContext context = Fixture.CreateContext()) | ||
| { | ||
| var _ = context.RangeTestEntities.Select(x => x.Range.IsEmpty).ToArray(); | ||
| AssertContainsSql("SELECT isempty(x.\"Range\")"); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RangeLowerBoundIsInclusive() | ||
| { | ||
| using (RangeContext context = Fixture.CreateContext()) | ||
| { | ||
| var _ = context.RangeTestEntities.Select(x => x.Range.LowerBoundIsInclusive).ToArray(); | ||
| AssertContainsSql("SELECT lower_inc(x.\"Range\")"); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RangeUpperBoundIsInclusive() | ||
| { | ||
| using (RangeContext context = Fixture.CreateContext()) | ||
| { | ||
| var _ = context.RangeTestEntities.Select(x => x.Range.UpperBoundIsInclusive).ToArray(); | ||
| AssertContainsSql("SELECT upper_inc(x.\"Range\")"); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RangeLowerBoundInfinite() | ||
| { | ||
| using (RangeContext context = Fixture.CreateContext()) | ||
| { | ||
| var _ = context.RangeTestEntities.Select(x => x.Range.LowerBoundInfinite).ToArray(); | ||
| AssertContainsSql("SELECT lower_inf(x.\"Range\")"); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RangeUpperBoundInfinite() | ||
| { | ||
| using (RangeContext context = Fixture.CreateContext()) | ||
| { | ||
| var _ = context.RangeTestEntities.Select(x => x.Range.UpperBoundInfinite).ToArray(); | ||
| AssertContainsSql("SELECT upper_inf(x.\"Range\")"); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RangeMergeRange() | ||
| { | ||
| using (RangeContext context = Fixture.CreateContext()) | ||
| { | ||
| var _ = context.RangeTestEntities.Select(x => x.Range.Merge(x.Range)).ToArray(); | ||
| AssertContainsSql("SELECT range_merge(x.\"Range\", x.\"Range\")"); | ||
| } | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #endregion | ||
|
|
||
| #region Setup | ||
|
|
||
| /// <summary> | ||
| /// Provides resources for unit tests. | ||
| /// </summary> | ||
| RangeQueryNpgsqlFixture Fixture { get; } | ||
|
|
||
| /// <summary> | ||
| /// Initializes resources for unit tests. | ||
| /// </summary> | ||
| /// <param name="fixture">The fixture of resources for testing.</param> | ||
| public RangeQueryNpgsqlTest(RangeQueryNpgsqlFixture fixture) | ||
| { | ||
| Fixture = fixture; | ||
| Fixture.TestSqlLoggerFactory.Clear(); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region TheoryData | ||
|
|
||
|
|
@@ -674,7 +778,7 @@ public class RangeContext : DbContext | |
| /// <param name="options"> | ||
| /// The options to be used for configuration. | ||
| /// </param> | ||
| public RangeContext(DbContextOptions options) : base(options) { } | ||
| public RangeContext(DbContextOptions options) : base(options) {} | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override void OnModelCreating(ModelBuilder builder) | ||
|
|
||
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.
What is this required for exactly?
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.
Passing back a
DefaultExpressionthrows if we don't override. The expression itself is introduced becauselowerandupperreturnnullon empty or infinite bounds. But since our range types may have non-nullable bounds (e.g.NpgsqlRange<int>.LowerBoundisintnotint?) we need to coalesce the databasenullto what the CLR bound ought to be (currently, the default value).Needing to do this might be another symptom of
NpgsqlRange<T>needing some more work... but changes there would be breaking, so my thought was that mitigating the incompatibility here would be reasonable for now.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.
The rest of this PR is totally fine, this is the only part of this PR that I want to understand a bit better (and have no time today), will look again tomorrow.
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.
@roji Did you still want to take a look at this?