-
Notifications
You must be signed in to change notification settings - Fork 337
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
Add DateTime ranged relay #1162
Open
Tarig0
wants to merge
9
commits into
AutoFixture:master
Choose a base branch
from
Tarig0:patch-1
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
76535d0
Add a handler for ranged DateTimes
Tarig0 1b226a4
Add DateTimeRangeRequestRelay to default behavior
Tarig0 490aced
Add unit tests
Tarig0 c0ee47a
fix name space
Tarig0 274d4dc
Add header comment
Tarig0 f452653
Move header comment
Tarig0 8185b8f
Add Usage test
Tarig0 664ece6
Update FixtureTest.cs
Tarig0 5f01ac0
Fix commas
Tarig0 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
65 changes: 65 additions & 0 deletions
65
Src/AutoFixture/DataAnnotations/DateTimeRangedRequestRelay.cs
This file contains 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,65 @@ | ||
using System; | ||
using System.Globalization; | ||
using AutoFixture.Kernel; | ||
|
||
namespace AutoFixture.DataAnnotations | ||
{ | ||
/// <summary> | ||
/// Handles <see cref="RangedRequest"/> of DateTime type by forwarding requests | ||
/// to the <see cref="RangedNumberRequest"/> with min and max DateTime as ticks values. | ||
/// </summary> | ||
public class DateTimeRangedRequestRelay : ISpecimenBuilder | ||
{ | ||
/// <inheritdoc /> | ||
public object Create(object request, ISpecimenContext context) | ||
{ | ||
if (context == null) throw new ArgumentNullException(nameof(context)); | ||
|
||
var rangedRequest = request as RangedRequest; | ||
|
||
if (rangedRequest == null) | ||
return new NoSpecimen(); | ||
|
||
if (rangedRequest.MemberType != typeof(DateTime)) | ||
return new NoSpecimen(); | ||
|
||
return CreateDateTimeSpecimen(rangedRequest, context); | ||
} | ||
|
||
private static object CreateDateTimeSpecimen(RangedRequest rangedRequest, ISpecimenContext context) | ||
{ | ||
if (!(rangedRequest.Minimum is string) || !(rangedRequest.Maximum is string)) | ||
return new NoSpecimen(); | ||
|
||
var range = ParseTimeSpanRange(rangedRequest); | ||
return RandomizeDateTimeInRange(range, context); | ||
} | ||
|
||
private static DateTimeRange ParseTimeSpanRange(RangedRequest rangedRequest) | ||
{ | ||
return new DateTimeRange | ||
{ | ||
Min = DateTime.Parse((string)rangedRequest.Minimum, CultureInfo.CurrentCulture), | ||
Max = DateTime.Parse((string)rangedRequest.Maximum, CultureInfo.CurrentCulture) | ||
}; | ||
} | ||
|
||
private static object RandomizeDateTimeInRange(DateTimeRange range, ISpecimenContext context) | ||
{ | ||
var ticksInRange = context.Resolve( | ||
new RangedNumberRequest(typeof(long), range.Min.Ticks, range.Max.Ticks)); | ||
|
||
if (ticksInRange is NoSpecimen) | ||
return new NoSpecimen(); | ||
|
||
return new DateTime((long)ticksInRange); | ||
} | ||
|
||
private struct DateTimeRange | ||
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: We could also make it a 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. Will the TimeSpanRange also need to be updated? |
||
{ | ||
public DateTime Min { get; set; } | ||
|
||
public DateTime Max { get; set; } | ||
} | ||
} | ||
} |
This file contains 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
118 changes: 118 additions & 0 deletions
118
Src/AutoFixtureUnitTest/DataAnnotations/DateTimeRangedRequestRelayTest.cs
This file contains 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,118 @@ | ||
using System; | ||
using AutoFixture.DataAnnotations; | ||
using AutoFixture.Kernel; | ||
using AutoFixtureUnitTest.Kernel; | ||
using Xunit; | ||
|
||
namespace AutoFixtureUnitTest.DataAnnotations | ||
{ | ||
public class DateTimeRangedRequestRelayTest | ||
{ | ||
[Fact] | ||
public void SutShouldBeASpecimenBuilder() | ||
{ | ||
// Arrange | ||
var sut = new DateTimeRangedRequestRelay(); | ||
|
||
// Act & Assert | ||
Assert.IsAssignableFrom<ISpecimenBuilder>(sut); | ||
} | ||
|
||
[Fact] | ||
public void ShouldFailForNullContext() | ||
{ | ||
// Arrange | ||
var sut = new DateTimeRangedRequestRelay(); | ||
var request = new object(); | ||
|
||
// Act & Assert | ||
Assert.ThrowsAny<ArgumentNullException>(() => | ||
sut.Create(request, null)); | ||
} | ||
|
||
[Fact] | ||
public void ShouldNotHandleRequestIfMemberTypeIsNotDateTime() | ||
{ | ||
// Arrange | ||
var sut = new DateTimeRangedRequestRelay(); | ||
var request = | ||
new RangedRequest(memberType: typeof(object), operandType: typeof(object), minimum: 0, maximum: 1); | ||
var context = new DelegatingSpecimenContext(); | ||
|
||
// Act | ||
var result = sut.Create(request, context); | ||
|
||
// Assert | ||
Assert.Equal(new NoSpecimen(), result); | ||
} | ||
|
||
[Fact] | ||
public void ShouldHandleRequestOfDateTimeType() | ||
{ | ||
// Arrange | ||
var sut = new DateTimeRangedRequestRelay(); | ||
var request = | ||
new RangedRequest(memberType: typeof(DateTime), operandType: typeof(DateTime), minimum: "2020-01-01 00:00:00", | ||
maximum: "2020-12-31 23:59:59"); | ||
|
||
var context = new DelegatingSpecimenContext | ||
{ | ||
OnResolve = _ => new DateTime(2020, 06, 15, 12, 30, 30).Ticks | ||
}; | ||
|
||
// Act | ||
var actualResult = sut.Create(request, context); | ||
|
||
// Assert | ||
Assert.Equal(new DateTime(2020, 06, 15, 12, 30, 30), actualResult); | ||
} | ||
|
||
[Fact] | ||
public void ShouldReturnNoSpecimenIfUnableToSatisfyRangedRequest() | ||
{ | ||
// Arrange | ||
var sut = new DateTimeRangedRequestRelay(); | ||
var request = | ||
new RangedRequest(memberType: typeof(DateTime), operandType: typeof(DateTime), minimum: "2020-01-01 00:00:00", | ||
maximum: "2020-12-31 23:59:59"); | ||
|
||
var context = new DelegatingSpecimenContext | ||
{ | ||
OnResolve = _ => new NoSpecimen() | ||
}; | ||
|
||
// Act | ||
var actualResult = sut.Create(request, context); | ||
|
||
// Assert | ||
Assert.Equal(new NoSpecimen(), actualResult); | ||
} | ||
|
||
[Fact] | ||
public void ShouldCorrectPassMinimumAndMaximumAsTicks() | ||
{ | ||
// Arrange | ||
var sut = new DateTimeRangedRequestRelay(); | ||
|
||
var request = new RangedRequest(typeof(DateTime), typeof(DateTime), "2020-01-01 00:00:00", "2020-12-31 23:59:59"); | ||
RangedNumberRequest capturedNumericRequest = null; | ||
|
||
var context = new DelegatingSpecimenContext | ||
{ | ||
OnResolve = r => | ||
{ | ||
capturedNumericRequest = (RangedNumberRequest)r; | ||
return new NoSpecimen(); | ||
} | ||
}; | ||
|
||
// Act | ||
sut.Create(request, context); | ||
|
||
// Assert | ||
Assert.NotNull(capturedNumericRequest); | ||
Assert.Equal(new DateTime(2020, 1, 1, 0, 0, 0).Ticks, capturedNumericRequest.Minimum); | ||
Assert.Equal(new DateTime(2020, 12, 31, 23, 59, 59).Ticks, capturedNumericRequest.Maximum); | ||
} | ||
} | ||
} |
This file contains 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
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.
ParseDateTimeRange, right?